Closed Bug 314980 Opened 18 years ago Closed 18 years ago

DOM element.getAttribute() method error (CAttributeToken::SanitizeKey())


(Core :: DOM: HTML Parser, defect, P4)






(Reporter: atec_post, Assigned: mrbkap)



(Keywords: verified1.8.0.10, verified1.8.1.2)


(3 files, 1 obsolete file)

User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050920 Firefox/1.0.7 SUSE/1.0.7-0.1
Build Identifier: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050920 Firefox/1.0.7 SUSE/1.0.7-0.1

The call to getAttribute() method in Javascript doesn't work properly.

Reproducible: Always

Steps to Reproduce:
1. Make an HTML page with an element's name attribute set to "main_" (i.e. <td main_="100"> )
2. Try to get the attribute with getAttribute("main_")

Actual Results:  
The call returns NULL

Expected Results:  
Following the example (steps to reproduce) the call should return "main_" 

This is a cut from a post on netscape.public.mozilla.dom news.
Message subject: DOM element.getAttribute() error?

The next code is a cut from an HTML page:

<tr parent_type="head_blind" id="tr_1012" main_="110">
<td class="folder_content"><img onclick="../" height="1" src="../skin/xp/img/pixel_empty.gif" width="10"><img style="cursor: pointer;" border="0" src="../skin/xp/img/folder_off.gif"><span type_action="folder">Chiquito</span></td><td class="folder_content" width="1"><img style="cursor: pointer;" border="0" src="../skin/xp/img/arrow_single_up.gif"></td>

I wanted to find all the "TR" nodes with the "main_" attribute with:

function getElmWithAtt(elm, a, v)
    var nList = f.contentDocument.getElementsByTagName(elm);
    var i=0;           
    for (; i<nList.length; i++)
        var m = nList[i];
        if (m.getAttribute(a) != null)
            if (m.getAttribute(a) == v)
    return nList[i];

getElmWithAtt("TR", "main_", 110);

But it doesn't work.
It worked with: getElmWithAtt("TR", "main", 110);
Assignee: nobody → mrbkap
Component: General → HTML: Parser
Product: Firefox → Core
QA Contact: general → parser
Version: unspecified → Trunk
Yeah, might as well sync up with IE. It might be worth it to figure out if IE doesn't allow various characters in attribute keys (note: this function was added for bug 7548).
Ever confirmed: true
OS: Linux → All
Priority: -- → P4
Hardware: PC → All
Target Milestone: --- → mozilla1.9alpha
IMHO we should do as little "fixup" as possible of the markup. Dropping ending '/'  seems like a good idea to ease xhtml migration. Other then that I think we should not strip any more characters then what IE does.
I did some testing; IE treats / as a terminator for attributes (which means that in cases like <foo f/g>, it treats foo as having 2 attributes, f and g). Do we want to emulate that?
Ugh, our current behavour is less then ideal. For <div f/b> we create an attribute named 'f/b'. IMHO we should never create attributes or elements with names containing illigal characters. Not sure what qualifies as legal characters in HTML/SGML, but in XML it's pretty clearly defined

Ideally we should drop other charaters, so parsing <div f/b> as <div f b> or something similar seems good to me.

Very imporantly, we should never put an ascii-null inside a element or attribute name. I suspect that that could cause security issues if we compare strings and atoms.
Attached patch Potential patch (obsolete) — Splinter Review
This needs to be pounded on, hard. I think it should do the right thing, though.
Attached file testcases
Running these testcases should be self-explanatory. Are there any cases that I'm missing?

Obsoleting attachment 201898 [details] [diff] [review] in preparation for a better patch.
Attachment #201898 - Attachment is obsolete: true
Attachment #202114 - Attachment is patch: false
Attachment #202114 - Attachment mime type: text/plain → text/html
Attached patch Fixed patchSplinter Review
This patch seems to provide the wanted compatibility.
Attachment #202115 - Flags: review?(bzbarsky)
Comment on attachment 202115 [details] [diff] [review]
Fixed patch

>Index: parser/htmlparser/src/nsHTMLTokens.cpp
>+  NS_ASSERTION(aChar == kApostrophe|| aChar == kQuote || aChar == kForwardSlash,

Space before first ||, please.

>@@ -2029,39 +1994,41 @@ nsresult CAttributeToken::Consume(PRUnic
>+                result = aScanner.SkipOver(aChar); //strip quote.

quote or slash

Attachment #202115 - Flags: review?(bzbarsky) → review+
Attachment #202115 - Flags: superreview?(jst)
Comment on attachment 202115 [details] [diff] [review]
Fixed patch

Attachment #202115 - Flags: superreview?(jst) → superreview+
Fix checked into trunk. I'll file a new bug on our potentially allowing NULs in attribute keys in a second.
Closed: 18 years ago
Resolution: --- → FIXED
Depends on: 315933
This patch is simply the result of CVS joins. It applies cleanly to the 1.8 and 1.8.0 branches.
Attachment #250215 - Flags: approval1.8.1.2?
Attachment #250215 - Flags: approval1.8.0.10?
Wanted for compatibility with other browser's parsing of attributes; being non-standard here is complicating web apps trying to filter content.
Flags: blocking1.8.1.2?
Flags: blocking1.8.0.10?
Flags: blocking1.8.1.2?
Flags: blocking1.8.1.2+
Flags: blocking1.8.0.10?
Flags: blocking1.8.0.10+
Comment on attachment 250215 [details] [diff] [review]
Patch for the 1.8 branches

Approved for both branches, a=jay for drivers.
Attachment #250215 - Flags: approval1.8.1.2?
Attachment #250215 - Flags: approval1.8.1.2+
Attachment #250215 - Flags: approval1.8.0.10?
Attachment #250215 - Flags: approval1.8.0.10+
Fixed on the 1.8 branches.
Using provided testcase confirmed existence of bug in

Verified for and  All PASS for testcase.

Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv: Gecko/2007011103 BonEcho/

Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv: Gecko/20070111 Firefox/
You need to log in before you can comment on or make changes to this bug.