Closed
Bug 373635
Opened 19 years ago
Closed 19 years ago
getElementsByTagName() incompatible with createContextualFragment()
Categories
(Firefox :: Extension Compatibility, defect)
Tracking
()
RESOLVED
INVALID
People
(Reporter: moreau, Unassigned)
Details
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2
I implemented a "spider" in greasemonkey/JavaScript. The spider parses the current document searching for hyperlinks. Each hyperlink is fetched and its content parsed in search of yet further hyperlinks.
I use getElementsByTagName('a') to search for hyperlinks. This works fine on the original document:
document.getElementsByTagName('a');
but fails on the other documents:
createContextualFragment(XmlHttpRequest()).getElementsByTagName('a'));
(the code above is simplified for clarity)
I expected getElementsByTagName() to work irrespective of the origin of the document, i.e. whether it is the main document or whether it is HTML data I have fetched using XmlHTTPRequest() and parsed using createContextualFragment(). However, it appears the type of node returned by createContextualFragment() is incompatible with getElementsByTagName() (and similar functions).
Reproducible: Always
Steps to Reproduce:
responseDetails = XmlHttpRequest(...); // abbreviated for clarity
range = document.createRange();
range.selectNode(document.body);
parsedHTML = range.createContextualFragment(responseDetails.responseText);
// The following line fails because parsedHTML is a native object:
parsedHTML.getElementsByTagName('p');
// ... but the following line succeeds:
parsedHTML.childNodes[0];
Actual Results:
parsedHTML.getElementsByTagName(...) fails
Expected Results:
parsedHTML.getElementsByTagName(...) should behave as with any node from the current document
Comment 1•19 years ago
|
||
createContextualFragment returns an nsIDocumentFragment which doesn't have a getElementsByTagName method. getElementsByTagName is only defined for documents and elements, not all nodes.
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → INVALID
| Reporter | ||
Comment 2•19 years ago
|
||
From a JavaScript's programmer point of view, this sounds a little weird, and more like implementation details. Why should the navigation be different when I navigate the current document and a document that I have fetched using XmlHTTPRequest()? They are, after all, both HTML documents.
Comment 3•19 years ago
|
||
Not sure how this last comment pertains to this. createContextualFragment isn't related to XMLHttpRequest. XMLHttpRequest.responseXML returns an xml document. XMLHttpRequest.responseText returns plain text (not a document at all) XMLHttpRequest doesn't parse html, only xml.
createContextualFragment doesn't parses a string of html and returns a DocumentFragment. It can't return a document because a string such as "<head>...</head><body>...</body>" doesn't have a single root node.
Comment 4•19 years ago
|
||
(In reply to comment #2)
> From a JavaScript's programmer point of view, this sounds a little weird, and
> more like implementation details.
Like Enn said the problem is w3c doesn't define getElementsByTagName() for nsIDocumentFragment (see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-745549614 and http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-B63ED1A3). And here you should use DOM programmers point of view I guess :)
| Reporter | ||
Comment 5•19 years ago
|
||
My issue is: I want to annotate the current HTML document with information obtained from other HTML documents. I use XmlHTTPRequest() to obtain these other HTML documents. I then need to parse them and obtain an HTML DOM. The only supported/documented way I've found is createContextualFragment(). But it has the issue reported above (partial implementation of DOM navigation).
Googling more, I've seen now seen: http://blog.mozilla.com/rob-sayre/2007/03/09/using-domparser-to-parse-html/
From this post:
1. It looks like others also have an interest in this.
2. This bug could be a duplicate of Bug 102699.
3. The proposed fix is currently to DOMParser.parseStringFromString(), which is fine. I didn't use this method in the first place because it does not support "text/html".
Comment 6•19 years ago
|
||
Like you said DOMParser doens't support 'text/html'. Though if you sure you get XHTML document (not HTML) then you can use XMLHttpRequest.responseXML or use DOMParser with 'text/xml'. If you use nsIDOMDocumentFragment then you can run through its children and invoke for them getElementsByTagName I guess.
Btw, Enn. Do you know why nsIDOMDocumentFragment is inherited from Node? Why don't they want to use Element as a base? Possibly it would be fine to have methods like getElementsByTagName() on it.
Comment 7•19 years ago
|
||
Because a document fragment isn't an element.
Comment 8•19 years ago
|
||
(In reply to comment #7)
> Because a document fragment isn't an element.
>
But is it a node?
You need to log in
before you can comment on or make changes to this bug.
Description
•