squizlabs/HTML_CodeSniffer — pull request #10 by lwright-sq (2012-06-07 01:09:51+00:00)
Title: Same-doc fragment link test fails when pointing to an invalid ID

When a same-doc fragment link points to a fragment with a technically invalid ID (such as, containing colons, or having a digit as its first character), HTMLCS will bomb with an error such as "DOM Error 12" or "An invalid or illegal string was specified".

This is due to using querySelector() for finding the appropriate element, which defaults to throwing a DOM Exception when the ID is not legal.

Note that "legal" in this case means legal with respect to CSS identifiers, which is what querySelector is based upon. This generally aligns with the HTML 4/XHTML 1.0 definition of an ID, although HTML5 appears more permissive.

This initial solution replaces the querySelector() call with getElementById() on the owner document.

In this case, since the test should pass if the ID is found anywhere in the owner document, using this should be OK even if it is used on a node of a full document. The main concern is where the top element being passed is being used as a proxy for a full document (without being in an IFRAME somewhere), meaning any framing elements could accidentally match, resulting in a false passing.

Example page: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp - fails on some of the auto-generated heading IDs in the table of contents, since it does not urlencode the colons in the "Example:" headings.