CSS: Use createElementNS to support XSLT documents#5775
Conversation
In XSLT-transformed documents, document.createElement("div").style
is undefined because elements are created in the XML namespace rather
than XHTML. Using document.createElementNS with the XHTML namespace
ensures .style is always available.
Ref jquerygh-4730
mgol
left a comment
There was a problem hiding this comment.
Thanks for the PR. However, source changes are just a small part of what would be required to support XML documents, even on a basic level.
See my comment at #4730 (comment). Some infrastructure parts have changed, but you'd still need to modify mock.php & mockserver.js to support serving true XML documents and then define a few tests using them via testIframe. You can look at some tests already using testIframe to see how to use it; focus on the ones that pass mock.php?action=... as the URL.
Also, we have many more document.createElement usage, it seems we should replace them all? Or maybe create file ./src/var/createElement.js that would wrap the namespace logic? That'd also require measuring the size impact in both cases (running the build locally will tell you the sizes & the diff from a previous build).
Would you like to try that?
- Create src/var/createElement.js that wraps createElementNS with the XHTML namespace to support XSLT/XML documents where document.createElement creates elements without .style - Replace all document.createElement calls in src/ with the helper - Add mock server action (xmlCss) to serve XHTML as application/xml - Add testIframe test verifying jQuery CSS works in XML documents Size impact (minified): -170 bytes, gzipped: -6 bytes The repeated namespace URI string is now centralized in one place. Ref jquerygh-4730
|
Thanks for the review @mgol! I've pushed a follow-up commit addressing your feedback: Changes
Size impact
The minified size actually decreased because the repeated All CSS, ajax, and support tests pass on both Chrome and Firefox. |
|
The size impact table is comparing with the previous iteration, right? Against |
|
You're right, the size table was comparing against the previous commit which already had inline |
Address review feedback: - Remove redundant comment from xmlDocument.xhtml - Use single var declaration in test scripts - Wrap test code in try-catch, pass `threw` to startIframeTest - Add XML document test for ajax cross-domain detection (jquerygh-4730) Ref jquerygh-4730
|
Pushed a follow-up commit addressing the review feedback:
|
- Uninitialized vars first, initialized last per style guide - Drop empty line between assignments in CSS test - Rename xmlDocument.xhtml to xmlDocCss.xhtml and xmlDocCrossDomainDetection.xhtml for unique names - Update createElement.js comment to reference XML documents generally, not just XSLT Ref jquerygh-4730
|
Pushed another commit addressing the latest feedback:
|
mgol
left a comment
There was a problem hiding this comment.
Thanks! This LGTM. I would not backport it to the 3.x-stable line as it has enough additional support tests that may cause more issues.
I want to discuss it with the team before merging, though, so please wait a bit more.
BTW, did you use AI to help with making the changes? I don't mind either way as the changes are high-quality and all my feedback was addressed; in the past, we've had issues with AI-submitted contributions that didn't make any sense, so that'd be the first for me here.
|
Thanks for the review! Happy to wait for the team discussion. And yes, I used Claude Code (Opus 4.6) to assist with the changes. |
|
Landed, thanks! |
Summary
document.createElement("div").styleisundefinedbecause elements are created in the XML namespace rather than XHTMLdocument.createElementtodocument.createElementNS("http://www.w3.org/1999/xhtml", ...)insrc/css/finalPropName.jsandsrc/css/support.jsso that.styleis always availableFiles changed
src/css/finalPropName.js—emptyStyleelement creation now uses XHTML namespacesrc/css/support.js—table,col,tr,tdelement creation now uses XHTML namespace (thetable.styleguard on line 19 already existed, but the root cause is the same)How to reproduce the original bug
document.createElement("div").style→undefineddocument.createElementNS("http://www.w3.org/1999/xhtml", "div").style→CSSStyleDeclarationFixes gh-4730