Skip to content

Commit

Permalink
dom/element: fix #at_css and #at_xpath
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Feb 8, 2014
1 parent 75c7fe7 commit f0d2be5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
20 changes: 16 additions & 4 deletions opal/browser/dom/element.rb
Expand Up @@ -131,9 +131,15 @@ def at(path_or_selector)
#
# @return [Node?]
def at_css(*rules)
rules.find_value {|rule|
css(rule).first
result = nil

rules.each {|rule|
if result = css(rule).first
break
end
}

result
end

# Get the first node matching the given XPath.
Expand All @@ -142,9 +148,15 @@ def at_css(*rules)
#
# @return [Node?]
def at_xpath(*paths)
paths.find_value {|path|
xpath(path).first
result = nil

paths.each {|path|
if result = xpath(path).first
break
end
}

result
end

alias attr []
Expand Down
22 changes: 22 additions & 0 deletions spec/dom/element_spec.rb
@@ -1,6 +1,8 @@
require 'spec_helper'

describe Browser::DOM::Element do
DOM = Browser::DOM

describe '#id' do
html <<-HTML
<div id="lol"><div class="wut"></div></div>
Expand Down Expand Up @@ -156,4 +158,24 @@
expect($document[:lol][:for]).to eq(:baz)
end
end

describe '#at_css' do
html <<-HTML
<label id="lol" class="name" for="hue"></label>
HTML

it 'checks for all the selectors' do
expect($document.at_css('div', 'span', '#lol')).to be_a(DOM::Element)
end
end

describe '#at_xpath' do
html <<-HTML
<label id="lol" class="name" for="hue"></label>
HTML

it 'checks for all the paths' do
expect($document.at_xpath('//div', '//span', '//[@id="lol"]')).to be_a(DOM::Element)
end
end
end

0 comments on commit f0d2be5

Please sign in to comment.