-
Notifications
You must be signed in to change notification settings - Fork 50.4k
setInnerHTML for XHTML documents #2161
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
Changes from all commits
1b86c51
8e20b8d
9f33b6b
1274f2f
b901438
cadc94c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,21 @@ var setInnerHTML = function(node, html) { | |
| }; | ||
|
|
||
| if (ExecutionEnvironment.canUseDOM) { | ||
| if (document.contentType === "application/xhtml+xml") { | ||
| // XML mode: Turn HTML into valid XML before setting innerHTML | ||
| setInnerHTML = function(node, html) { | ||
| var dom = new DOMParser().parseFromString(html, 'text/html'); | ||
| node.innerHTML = new XMLSerializer().serializeToString(dom.body).replace(/^<body[^>]*>/, '').replace(/<\/body>$/, ''); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At a glance the whole thing seems pretty reasonable, but I haven't looked closely. The first things that jumps out at me here is that we should pull these regexs out to constants to dedupe (and to only create them once).
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks rather slow. Perhaps we can generate XHTML-compatible markup from the start instead.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @spicyj I imagine it should be quite simple, but perhaps SVG could be an obstacle?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can just read
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alexeyraspopov Well, this evaluates to new DOMParser().parseFromString("<br>", "text/html").body.innerHTML;Unless there is a way to cast a HTMLDocument into XMLDocument, serialization still seems required. |
||
| }; | ||
| } | ||
| else if (document.xmlVersion) { | ||
| // Safari: Same thing, different API | ||
| setInnerHTML = function(node, html) { | ||
| var dom = document.implementation.createHTMLDocument(''); | ||
| dom.body.innerHTML = html; | ||
| node.innerHTML = new XMLSerializer().serializeToString(dom.body).replace(/^<body[^>]*>/, '').replace(/<\/body>$/, ''); | ||
| }; | ||
| } | ||
| // IE8: When updating a just created node with innerHTML only leading | ||
| // whitespace is removed. When updating an existing node with innerHTML | ||
| // whitespace in root TextNodes is also collapsed. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTML format is not supported by Safari browsers (both, desktop and mobile). We need to do something like this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added support to Safari as per your suggestion. Now it works in iBooks.app too! ❤️