diff --git a/lib/xpath/dsl.rb b/lib/xpath/dsl.rb index e4763f8..24bf560 100644 --- a/lib/xpath/dsl.rb +++ b/lib/xpath/dsl.rb @@ -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 diff --git a/lib/xpath/renderer.rb b/lib/xpath/renderer.rb index 5ed51bf..2ca37e4 100644 --- a/lib/xpath/renderer.rb +++ b/lib/xpath/renderer.rb @@ -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 diff --git a/spec/xpath_spec.rb b/spec/xpath_spec.rb index 9247fa4..8c9faca 100644 --- a/spec/xpath_spec.rb +++ b/spec/xpath_spec.rb @@ -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') }