Skip to content
Browse files

simple_selector: attributes with brackets in double quotes not recogn…

…ized

HTML::Selector#simple_selector can recognize attributes in single and
double quotes. But when the attribute value contains square brackets,
and the value is enclosed in double quotes, an exception
`ArgumentError: Invalid selector: "]` is thrown.

Related to issue #2412.
  • Loading branch information...
1 parent 7d43217 commit 988b6b4082a5975f7298e51e0e674251410e77a8 @Arsen7 committed Aug 4, 2011
View
2 actionpack/lib/action_controller/vendor/html-scanner/html/selector.rb
@@ -558,7 +558,7 @@ def simple_selector(statement, values, can_negate = true)
end
# Attribute value.
- next if statement.sub!(/^\[\s*([[:alpha:]][\w\-:]*)\s*((?:[~|^$*])?=)?\s*('[^']*'|"[^*]"|[^\]]*)\s*\]/) do |match|
+ next if statement.sub!(/^\[\s*([[:alpha:]][\w\-:]*)\s*((?:[~|^$*])?=)?\s*('[^']*'|"[^"]*"|[^\]]*)\s*\]/) do |match|
name, equality, value = $1, $2, $3
if value == "?"
value = values.shift
View
10 actionpack/test/controller/selector_test.rb
@@ -102,7 +102,7 @@ def test_attribute
def test_attribute_quoted
- parse(%Q{<div id="1" title="foo"></div><div id="2" title="bar"></div><div id="3" title=" bar "></div>})
+ parse(%Q{<div id="1" title="foo"></div><div id="2" title="bar"></div><div id="3" title=" bar "></div><div id="4" name="item[baz]"></div>})
# Match without quotes.
select("[title = bar]")
assert_equal 1, @matches.size
@@ -119,6 +119,14 @@ def test_attribute_quoted
select("[title = \" bar \" ]")
assert_equal 1, @matches.size
assert_equal "3", @matches[0].attributes["id"]
+ # Match brackets in single quotes.
+ select("[name = 'item[baz]' ]")
+ assert_equal 1, @matches.size
+ assert_equal "4", @matches[0].attributes["id"]
+ # Match brackets in double quotes.
+ select("[name = \"item[baz]\" ]")
+ assert_equal 1, @matches.size
+ assert_equal "4", @matches[0].attributes["id"]
end

0 comments on commit 988b6b4

Please sign in to comment.
Something went wrong with that request. Please try again.