Ticket #115 (closed defect: fixed)

Opened 11 months ago

Last modified 6 months ago

Attribute values with brackets in them cause problems

Reported by: ntalbott Owned by: why
Priority: critical Milestone:
Component: ext/hpricot_scan Version:
Keywords: Cc: nathaniel@…

Description

I've attached a patch which adds a test for the issue; to summarize:

  doc = Hpricot('<input name="vendor[porkpies]"/>')
  assert_equal 1, (doc/'input[@name^="vendor[porkpies]"]').length # PASSES
  assert_equal 1, (doc/'input[@name="vendor[porkpies]"]').length # FAILS
  assert_equal 0, (doc/'input[@name$="]]]]]"]').length # FAILS

Change History

Changed 11 months ago by ntalbott

Well, I was going to attach a patch... but that's giving me an internal server error at the moment. Please drop a comment here when it's fixed and I'll try again. Thanks!

Changed 11 months ago by ntalbott

  • cc nathaniel@… added

Changed 11 months ago by ntalbott

It gets worse... this also fails:

  doc = Hpricot('<input name="vendor[porkpies][meaty]"/>')
  assert_equal 1, (doc/'input[@name^="vendor[porkpies][meaty]"]').length # FAILS

Which makes it nigh on impossible to test Rails views with deeply nested hashes.

  :'-(

Changed 10 months ago by wrs

The reason turns out to be pretty obvious, but puzzling -- ATTR_RE in elements.rb is defined as

ATTR_RE = %r!\[ *(?:(@)([\w\(\)-]+)|([\w\(\)-]+\(\))) *([~\!\|\*$\^=]*) *'?"?([^\]'"]*)'?"? *\]!i

which explicitly forbids right brackets from the attribute value. But I don't know the intention -- perhaps to avoid conflict with BRACK_RE? The equivalent expression in JQuery is

/^\[ *(@)([\w-]+) *([!*$^=]*) *('?"?)(.*?)\4 *\]/

which allows anything inside the brackets.

Removing the restriction from ATTR_RE makes the tests (original and ntalbott's) pass. Monkey patch is:

module Hpricot
  class Elements
    ATTR_RE = %r!\[ *(?:(@)([\w\(\)-]+)|([\w\(\)-]+\(\))) *([~\!\|\*$\^=]*) *'?"?([^'"]*)'?"? *\]!i
  end
end

Changed 6 months ago by why

  • status changed from new to closed
  • resolution set to fixed

Thanks, ntalbott and wrs! This is addressed by [160].

Note: See TracTickets for help on using tickets.