Skip to content

Commit

Permalink
Merge pull request #39 from excepttheweasel/starts_with
Browse files Browse the repository at this point in the history
Add support for xpath starts-with function
  • Loading branch information
jnicklas committed Apr 1, 2012
2 parents a31816d + 98527d0 commit f1dbaaf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/xpath/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def contains(expression)
Expression.new(:contains, current, expression)
end

def starts_with(expression)
Expression.new(:starts_with, current, expression)
end

def text
Expression.new(:text, current)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/xpath/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def contains(current, value)
"contains(#{current}, #{value})"
end

def starts_with(current, value)
"starts-with(#{current}, #{value})"
end

def and(one, two)
"(#{one} and #{two})"
end
Expand Down
19 changes: 19 additions & 0 deletions spec/xpath_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ def xpath(&block)
end
end

describe '#starts_with' do
it "should find nodes that begin with the given string" do
@results = xpath do |x|
x.descendant(:*).where(x.attr(:id).starts_with('foo'))
end
@results.size.should == 2
@results[0][:id].should == "foo"
@results[1][:id].should == "fooDiv"
end

it "should find nodes that contain the given expression" do
@results = xpath do |x|
expression = x.anywhere(:div).where(x.attr(:title) == 'fooDiv').attr(:id)
x.descendant(:div).where(x.attr(:title).starts_with(expression))
end
@results[0][:id].should == "foo"
end
end

describe '#text' do
it "should select a node's text" do
@results = xpath { |x| x.descendant(:p).where(x.text == 'Bax') }
Expand Down

0 comments on commit f1dbaaf

Please sign in to comment.