This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 26396 - innerHTML depends on node document; doesn't look at element context
Summary: innerHTML depends on node document; doesn't look at element context
Status: RESOLVED WORKSFORME
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: DOM Parsing and Serialization (show other bugs)
Version: unspecified
Hardware: PC All
: P2 normal
Target Milestone: ---
Assignee: Travis Leithead [MSFT]
QA Contact: public-webapps-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-07-21 10:03 UTC by Dirk Schulze
Modified: 2014-10-13 22:49 UTC (History)
5 users (show)

See Also:


Attachments

Description Dirk Schulze 2014-07-21 10:03:47 UTC
http://domparsing.spec.whatwg.org/#innerhtml

[[
On getting, if the context object's node document is an HTML document, then the attribute must return the result of running the HTML fragment serialization algorithm on the context object; otherwise, the context object's node document is an XML document,
]]

Currently innerHMTML just looks at the node document. This is maybe not enough. The elements context itself seems to be interesting as well to set the namespace of elements.

<div><svg id="svg"></svg></div>
<script>
document.getElementById('svg').innerHTML = '<g><rect width="100" height="100"/></g>';
</script>

doesn't work.

while

<div><svg id="svg"></svg></div>
<script>
document.getElementById('svg').innerHTML = '<svg><g><rect width="100" height="100"/></g></svg>';
</script>

works in most browsers. (In all, if it gets called on <div>.)

It gets of course more complicated when innerHTML is called on an element within <foreignObject>:

    <foreignObject width="100" height="50"
                   requiredExtensions="http://www.w3.org/1999/xhtml">
      <!-- XHTML content goes here -->
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>Here is a paragraph that requires word wrap</p>
      </body>
    </foreignObject>

Here the node document is probably not the SVG root document.

Also, for standalone SVG files, it would be great if authors don't need to specify the SVG namespace on elements in the "value" string. With the text above, it seems to be necessary. At least it is not clear to me how the output would be an SVG element.

Especially for HTML documents with SVG content, this issue has been brought up to the SVG WG many times.
Comment 1 Simon Pieters 2014-08-04 13:15:34 UTC
Your tests are *setting*, not getting.

Your first example should work per spec. Browsers might not have implemented it yet. I think it's http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#adjusted-current-node and thereabouts.
Comment 2 Travis Leithead [MSFT] 2014-10-13 22:49:13 UTC
The current text in fragment parsing algorithm choses the fragment parsing algorithm (from HTML5) by the document alone. 

See: http://www.w3.org/TR/DOM-Parsing/#dfn-concept-parse-fragment

In the case of HTML documents, it does look like the "adjusted current node" concept is baked into that algorithm, and it does handle SVG and foreignObject:

http://www.w3.org/TR/html5/single-page.html#html-integration-point

For XHTML/XML/SVG/MathML documents, I don't believe it's possible to invoke the HTML fragment parsing algorithm from within that context. (I believe all your examples were text/html documents, right?