Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/xpath/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def union(*expressions)
end
alias_method :+, :union

def group
Expression.new(:group, current)
end

def last
function(:last)
end
Expand Down
48 changes: 48 additions & 0 deletions lib/xpath/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def initialize(type)
end

def render(node)
return render_grouped_where(node) if grouped_where_clause?(node)

arguments = node.arguments.map { |argument| convert_argument(argument) }
send(node.expression, *arguments)
end
Expand Down Expand Up @@ -107,8 +109,54 @@ def function(name, *arguments)
"#{name}(#{arguments.join(', ')})"
end

def group(expression)
"(#{expression})"
end

private

def grouped_where_clause?(node)
node.expression == :where &&
node.arguments.length == 2 &&
node.arguments[0].is_a?(Expression) &&
node.arguments[0].expression == :group
end

def render_grouped_where(node)
group_content = render(node.arguments[0].arguments[0])
condition = convert_argument(node.arguments[1])
condition = unwrap_outer_parentheses(condition)

"(#{group_content})[#{condition}]"
end

def unwrap_outer_parentheses(condition)
return condition unless wrapped_in_parentheses?(condition)
return condition unless balanced_inner_parentheses?(condition)

condition[1..-2]
end

def wrapped_in_parentheses?(string)
string.start_with?('(') && string.end_with?(')')
end

def balanced_inner_parentheses?(string)
inner_content = string[1..-2]
parentheses_count = 0

inner_content.each_char do |char|
case char
when '(' then parentheses_count += 1
when ')' then parentheses_count -= 1
end

return false if parentheses_count.negative?
end

parentheses_count.zero?
end

def with_element_conditions(expression, element_names)
if element_names.length == 1
"#{expression}#{element_names.first}"
Expand Down
11 changes: 11 additions & 0 deletions spec/xpath_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,15 @@ def xpath(type = nil, &block)
expect(@results[2][:id]).to eq 'foo'
end
end

describe '#group' do
it 'wraps expressions in parentheses' do
expect(XPath.descendant(:div).group.to_xpath).to eq '(.//div)'
end

it 'allows predicates to apply to grouped expressions' do
grouped = XPath.descendant(:div).attr(:id).group[XPath.position == XPath.last]
expect(grouped.to_xpath).to eq '(.//div/@id)[position() = last()]'
end
end
end