-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parsing with type text/html
leads to unwanted default namespace
#377
Comments
Thank you for reporting this. I don't have a lot of experience regarding xpath, but this is what I found out: According to https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate the code you provided should find something, if it works the same way as the browser. I created the linked stackblitz to reproduce it and am able to verify that the xpath library is not able to find anything (output 4). https://stackblitz.com/edit/js-xmldom359-fd3yir?devtoolsheight=33&file=index.js import xmldom from '@xmldom/xmldom';
import xpath from 'xpath';
// See https://github.com/xmldom/xmldom/issues/377
const source = `<input value="abc">`;
const doc = new DOMParser().parseFromString(source, 'text/html');
console.log(
'1. The namespace of the input element in the browser API to parse source',
doc.getElementsByTagName('input')[0].namespaceURI
);
const xmldomDoc = new xmldom.DOMParser().parseFromString(source, 'text/html');
console.log(
'2. The namespace of the input element when using @xmldom/xmldom to parse source',
xmldomDoc.getElementsByTagName('input')[0].namespaceURI
);
let browserXPathResult = doc.evaluate('//input', doc, null, 0, null);
console.log(
'3. The xpath result when using the browser API',
browserXPathResult.iterateNext()
);
let xpathResult = xpath.evaluate('//input', doc, null, 0, null);
console.log(
'4. The xpath result when using the xpath lib',
xpathResult.iterateNext()
); I disagree that this is because of the namespace added by xmldom (output 2) though, since the namespace is also present when the browser parses the snippet you provide (output 1). Using the browser API to And I also found the related specs, that state that this is the expected behavior:
The DOM specification also has someplaces related to that:
So at the moment I don't see how this problem is related to or should be solved by changing how xmldom is adding the HTML namespace in html (and xhtml) documents. Just an idea: Is it possible that the xpath library is not searching in the default namespace? I will of course not dig into the code of the xpath library now. But if you have more information, that shows what xmldom is doing differently to cause this issue, feel free to reopen this github issue. PS: Please be aware that the xmldom library is now published under |
Thanks for your thorough answer! I agree on your analysis and that adding Also FYI I posted goto100/xpath#27 (comment) |
Just for later reference/linking: Somebody just helped me to understand that @xmldom/[email protected] breaks the "HTML mode" detection of xpath, which was previously assuming |
Hi!
When using
DOMParser
with mime typetext/html
, the parser adds the default namespacehttp://www.w3.org/1999/xhtml
, because of this line of code. It looks like it's expected, because there is even a test for it but in my opinion, it's quite surprising and make simple xpath queries fail, example using xpath library:I know I could use
local-name
but in that case, I don't control the xpath, so what's the rationale of adding this default namespace? Can we opt-out of that behaviour?As a current workaround, I did this to pass a
xmlns
object with a setter ignoring the update of''
key. It works, but it's obviously quite brittle and hacky.The text was updated successfully, but these errors were encountered: