jQuery uses innerHTML
when you pass serialized DOM nodes to $('div').html()
. This works fine as long as your DOCTYPE
is any flavor of html
. However, with the DOCTYPE
of xhtml
your serialized DOM nodes must clear some additional cases before being inserted into the document. According to W3.org, a serialized DOM node containing an Attr
node, Text
node, CDATASection
node, Comment
node, or ProcessingInstruction
node whose data contains characters that are not matched by the XML Char production (including U+003A COLON ":") must raise an INVALID_STATE_ERR
exception.
The W3 also specifies the algorithm that user agents must run when setting the innerHTML
DOM attribute on HTMLElements
and HTMLDocuments
in an XML(XHTML) context. Step 2 in that algorithm is:
If the innerHTML
attribute is being set on an element, the user agent must feed the parser just created the string corresponding to the start tag of that element, declaring all the namespace prefixes that are in scope on that element in the DOM, as well as declaring the default namespace (if any) that is in scope on that element in the DOM.
The user agent doesn't know that you've specified the epub
namespace on a parent element because the current context is outside the root context. Therefore you need to specify the namespace for the serialized DOM nodes when you append to the DOM.
$('div').html('<span xmlns:epub="http://www.idpf.org/2007/ops"
epub:type="pagebreak" id="pagePrefix" title="1"></span>');
jsFiddle here.