forked from w3c/DOM-Parsing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1857 lines (1414 loc) · 89 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head><meta charset=UTF-8>
<title>DOM Parsing and Serialization</title>
<style>
/* Switch statement */
dl.switch dt::before {
content: "↪ ";
margin-left: 1em;
}
/* Better spacing around various lists (implied paragraph children) */
ol > li, section:not(#toc) ul > li, section dl > dt {
margin: 1em 0;
}
var { color: maroon; }
/* domintro styling */
dl.domintro {
background-color: rgb(221, 255, 221);
padding: 1em 0.5em 1em 2em;
clear: both;
}
dl.domintro dt {
color: black;
}
dl.domintro > dd {
color: green;
}
dl.domintro::before {
float: right;
background-color: white;
display: block;
border: 2px solid black;
color: green;
margin-top: -20px;
padding: 2px;
content: "This box is non-normative. Implementation requirements are given below this box.";
}
</style>
<script type="text/javascript" src='https://www.w3.org/Tools/respec/respec-w3c-common' class='remove' async></script>
<script type="text/javascript" src="respecConfig.js" class='remove'></script>
</head>
<body>
<section id="abstract">
This specification defines APIs for the parsing and serializing of HTML and XML-based DOM nodes
for web applications.
</section>
<section id="sotd"></section>
<section id="crec" class="introductory">
<h2>Candidate Recommendation Exit Criteria</h2>
This specification will not advance to Proposed Recommendation before the spec's
<a href="http://w3c-test.org/domparsing/">test suite</a> is completed and two or more independent
implementations pass each test, although no single implementation must pass each test. We expect
to meet this criteria no sooner than 24 October 2014. The group will also create an
<a href="https://dvcs.w3.org/hg/innerhtml/raw-file/tip/implementationReport.html">Implementation
Report</a>.
</section>
<section id="conformance" class=introductory>
<p>The IDL fragments in this specification must be interpreted as required for conforming IDL
fragments, as described in the Web IDL specification. [[!WEBIDL]]
<p>Requirements phrased in the imperative as part of algorithms (such as "strip any leading space
characters" or "return false and terminate these steps") are to be interpreted with the meaning of
the key word ("must", "should", "may", etc) used in introducing the algorithm.
<p>Conformance requirements phrased as algorithms or specific steps may be implemented in any
manner, so long as the end result is equivalent. (In particular, the algorithms defined in this
specification are intended to be easy to follow, and not intended to be performant.)
<p>User agents may impose implementation-specific limits on otherwise unconstrained inputs, e.g.
to prevent denial of service attacks, to guard against running out of memory, or to work around
platform-specific limitations.
<p>When a method or an attribute is said to call another method or attribute, the user agent must
invoke its internal API for that attribute or method so that e.g. the author can't change the
behavior by overriding attributes or methods with custom properties or functions in ECMAScript.
[[ECMA-262]]
<p>Unless otherwise stated, string comparisons are done in a <a>case-sensitive</a> manner.
<p>If an algorithm calls into another algorithm, any exception that is thrown by the latter
(unless it is explicitly caught), must cause the former to terminate, and the exception to be
propagated up to its caller.
</section>
<section class=introductory>
<h2>Extensibility</h2>
<p>Vendor-specific proprietary extensions to this specification are strongly discouraged. Authors
must not use such extensions, as doing so reduces interoperability and fragments the user base,
allowing only users of specific user agents to access the content in question.
<p>If vendor-specific extensions are needed, the members should be prefixed by vendor-specific
strings to prevent clashes with future versions of this specification. Extensions must be defined
so that the use of extensions neither contradicts nor causes the non-conformance of functionality
defined in the specification.
<p>When vendor-neutral extensions to this specification are needed, either this specification can
be updated accordingly, or an extension specification can be written that overrides the
requirements in this specification. Such an extension specification becomes an
<dfn>applicable specification</dfn> for the purposes of conformance requirements in this
specification.
<!-- http://www.w3.org/mid/[email protected] -->
</section>
<section><h2>Introduction</h2>
<p>A document object model (DOM) is an in-memory representation of various types of <a>Node</a>s
where each <a>Node</a> is connected in a tree. The [[HTML5]] and [[DOM4]] specifications describe
DOM and its <a>Node</a>s is greater detail.
<p><dfn>Parsing</dfn> is the term used for converting a string representation of a DOM into an
actual DOM, and <dfn>Serializing</dfn> is the term used to transform a DOM back into a string.
This specification concerns itself with defining various APIs for both parsing and serializing a
DOM.
<div class=example>For example: the <a for="Element">innerHTML</a> API is a common way to both
parse and serialize a DOM (it does both). If a particular <a>Node</a>, has the following in-memory
DOM:
<pre>
HTMLDivElement (nodeName: "div")
┃
┣━ HTMLSpanElement (nodeName: "span")
┃ ┃
┃ ┗━ Text (data: "some ")
┃
┗━ HTMLElement (nodeName: "em")
┃
┗━ Text (data: "text!")
</pre>
And the <code>HTMLDivElement</code> node is stored in a variable <code><var>myDiv</var></code>,
then to serialize <code><var>myDiv</var></code>'s children simply <em>get</em> (read) the
<a>Element</a>'s <a for="Element">innerHTML</a> property (this triggers the serialization):
<pre class=javascript>
var serializedChildren = myDiv.innerHTML;
// serializedChildren has the value:
// "<span>some </span><em>text!</em>"
</pre>
<p>To parse new children for <code><var>myDiv</var></code> from a string (replacing its existing
children), simply <em>set</em> the <a for="Element">innerHTML</a> property (this triggers
parsing of the assigned string):
<pre class=javascript>
myDiv.innerHTML = "<span>new</span><em>children!</em>";
</pre>
</div>
<p>This specification describes two flavors of <a>parsing</a> and <a>serializing</a>: HTML and
XML (with XHTML being a type of XML). Each follows the rules of its respective markup language.
The above example shows HTML parsing and serialization. The specific algorithms for HTML parsing
and serializing are defined in the [[HTML5]] specification. This specification contains the
algorithm for XML serializing. The grammar for XML parsing is described in the [[XML10]]
specification.
<p><dfn>Round-tripping</dfn> a DOM means to serialize and then immediately parse the serialized
string back into a DOM. Ideally, this process does not result in any data loss with respect to the
identity and attributes of the <a>Node</a> in the DOM.
<a>Round-tripping</a> is especially tricky for an XML serialization, which must be concerned with
preserving the <a>Node</a>'s namespace identity in the serialization (wereas namespaces are
ignored in HTML).
<div class=example>Consider the XML serialization of the following in-memory DOM:
<pre>
Element (nodeName: "root")
┃
┗━ HTMLScriptElement (nodeName: "script")
┃
┗━ Text (data: "alert('hello world')")
</pre>
An XML serialization must include the <code>HTMLScriptElement</code> <a>Node</a>'s
<a data-lt="concept namespace">namespace</a> in order to preserve the identity of the
<code>script</code> element, and to allow the serialized string to
<a data-lt="round-tripping">round-trip</a> through an XML parser. Assuming that <code>root</code>
is in a variable named <code><var>root</var></code>:
<pre class=javascript>
var xmlSerialization = new XMLSerializer().serializeToString(root);
// xmlSerialization has the value:
// "<root><script xmlns="http://www.w3.org/1999/xhtml">alert('hello world')</script></root>"
</pre>
</div>
<p>The term <dfn>context object</dfn> means the object on which the API being discussed was
called.
<p>The following terms are understood to represent their respective namespaces in this
specification (and makes it easier to read):
<ul>
<li>The <dfn>HTML namespace</dfn> is <code>http://www.w3.org/1999/xhtml</code>
<li>The <dfn>XML namespace</dfn> is <code>http://www.w3.org/XML/1998/namespace</code>
<li>The <dfn>XMLNS namespace</dfn> is <code>http://www.w3.org/2000/xmlns/</code>
</ul>
</section><!-- end introduction -->
<section><h1>APIs for parsing and serializing DOM</h1>
<section><h2>The <code>DOMParser</code> interface</h2>
<pre class=idl>
[Constructor, Exposed=Window]
interface DOMParser {
[NewObject] Document parseFromString(DOMString str, SupportedType type);
};
enum SupportedType {
"text/html",
"text/xml",
"application/xml",
"application/xhtml+xml",
"image/svg+xml"
};
</pre>
<dl class=domintro>
<dt><var>domparser</var> = new <a for=DOMParser>DOMParser</a> ()</var>
<dd>Constructs a new DOMParser object.
<dt><var>document</var> = <var>domparser</var> . <a for=DOMParser>parseFromString</a> ( <var>str</var>, <var>type</var> )
<dd>Parse <var>str</var> using a parser that matches <var>type</var>'s supported MIME types
(either XML or HTML), and return a <a>Document</a> object contained the parsed content if
successful. If not successful, returns a <a>Document</a> describing the error.
<dd>If <var>type</var> does not match a value in the <a>SupportedType</a> enumeration, an
exception is thrown [[WEBIDL]].
</dl>
<p>The <dfn><code>DOMParser</code></dfn> constructor must return a new <a>DOMParser</a> object.
<p>The
<code><dfn for=DOMParser>parseFromString</dfn>(<var>str</var>, <var>type</var>)</code></dfn>
method must run these steps, depending on <var>type</var>:
<dl class=switch>
<dt>"<dfn for=SupportedType><code>text/html</code></dfn>"
<dd>Parse <var>str</var> with an <code><a>HTML parser</a></code>, and return the newly created
<a>Document</a>.
<p>The <a>scripting flag</a> must be set to "disabled".
<p class=note><code><a>meta</a></code> elements are not taken into account for the encoding
used, as a Unicode stream is passed into the parser.
<p class=note><code><a>script</a></code> elements get marked unexecutable and the contents of
<code><a>noscript</a></code> get parsed as markup.
<dt>"<dfn for=SupportedType><code>text/xml</code></dfn>"
<dt>"<dfn for=SupportedType><code>application/xml</code></dfn>"
<dt>"<dfn for=SupportedType><code>application/xhtml+xml</code></dfn>"
<dt>"<dfn for=SupportedType><code>image/svg+xml</code></dfn>"
<dd>
<ol>
<li>Parse <var>str</var> with a namespace-enabled <code><a>XML parser</a></code>.
<p class=note>For all XHTML <code><a>script</a></code> elements parsed using the
<code><a>XML parser</a></code>, the equivalent of the <a>scripting flag</a> must be set to
"disabled".</p>
<li>If the previous step didn't return an error, return the newly created <a>Document</a>.
<li>Let <var>document</var> be a newly-created <a>XML Document</a>. <span class=note>The
<var>document</var> will use the <code><a>Document</a></code> interface rather than the
<code><a>XMLDocument</a></code> interface.</span>
<li>Let <var>root</var> be a new <code><a>Element</a></code>, with its <a>local name</a> set
to "<code>parsererror</code>" and its <a data-lt="concept namespace">namespace</a> set to
"<code>http://www.mozilla.org/newlayout/xml/parsererror.xml</code>".
<p>At this point user agents may <a>append</a> nodes to <var>root</var>, for example to
describe the nature of the error.
<li><a>Append</a> <var>root</var> to <var>document</var>.
<li>Return the value of <var>document</var>.
</ol>
</dl>
<p>In any case, the returned <a>Document</a>'s <a>content type</a> must be the <var>type</var>
argument. Additionally, the <a>Document</a> must have a <a>URL</a> value equal to the URL of the
<a>active document</a>, and a <a>location</a> value of <code>null</code>.
<p class=note>The returned <a>Document</a>'s <a>encoding</a> is the default, UTF-8.
</section><!-- end DOMParser interface -->
<section><h2>The <code>XMLSerializer</code> interface</h2>
<pre class="idl">
[Constructor, Exposed=Window]
interface XMLSerializer {
DOMString serializeToString(Node root);
};
</pre>
<dl class=domintro>
<dt><var>xmlserializer</var> = new <a for=XMLSerializer>XMLSerializer</a> ()</var>
<dd>Constructs a new XMLSerializer object.
<dt><var>string</var> = <var>xmlserializer</var> . <a for=XMLSerializer>serializeToString</a> ( <var>root</var> )
<dd>Serializes <var>root</var> into a string using an XML serialization. Throws a
<a>TypeError</a> exception if <var>root</var> is not a <a>Node</a> or an <a>Attr</a> object.
</dl>
<p>The <dfn><code>XMLSerializer</code></dfn>() constructor must return a new <a>XMLSerializer</a>
object.
<p>The <dfn for="XMLSerializer"><code>serializeToString</code></dfn>(<var>root</var>) method must
produce an <a>XML serialization</a> of <var>root</var> passing a value of <code>false</code> for
the <a><var>require well-formed</var></a> parameter, and return the result.
</section><!-- end XMLSerializer interface -->
<section><h2>Extensions to the <code><a>Element</a></code> interface</h2>
<pre class="idl">
partial interface Element {
[CEReactions, TreatNullAs=EmptyString] attribute DOMString innerHTML;
[CEReactions, TreatNullAs=EmptyString] attribute DOMString outerHTML;
[CEReactions] void insertAdjacentHTML(DOMString position, DOMString text);
};
</pre>
<!-- innerHTML -->
<p>The <dfn for="Element"><code>innerHTML</code></dfn> IDL attribute represents the markup of the
<code><a>Element</a></code>'s contents.
<dl class=domintro>
<dt><var>element</var> . <a for="Element">innerHTML</a> [ = <var>value</var> ]
<dd>Returns a fragment of HTML or XML that represents the element's contents.
<p>Can be set, to replace the contents of the element with nodes parsed from the given string.
<p>In the case of an <a>XML document</a>, throws a "<code><a>InvalidStateError</a></code>"
<code><a>DOMException</a></code> if the <code><a>Element</a></code> cannot be serialized to XML,
or a "<code><a>SyntaxError</a></code>" <code><a>DOMException</a></code> if the given string is
not well-formed.
</dl>
<p>On getting, return the result of invoking the <a>fragment serializing algorithm</a> on the
<a>context object</a> providing <code>true</code> for the <a><var>require well-formed</var></a> flag (this
might throw an exception instead of returning a string).
<p>On setting, these steps must be run:
<ol>
<li>Let <var>fragment</var> be the result of invoking the <a>fragment parsing algorithm</a> with
the new value as <var>markup</var>, and the <a>context object</a> as the
<var>context element</var>.
<li>If the <a>context object</a> is a <code><a>template</a></code> element, then let
<a>context object</a> be the <code><a>template</a></code>'s <a>template contents</a> (a
<code><a>DocumentFragment</a></code>).
<p class=note>Setting <a for="Element">innerHTML</a> on a <a>template</a> element will replace
all the nodes in its <a>template contents</a>
(<a>template</a>.<a data-lt="template contents">content</a>) rather than its <a>children</a>.</p>
<li><a>Replace all</a> with <var>fragment</var> within the <a>context object</a>.
</ol>
<!-- outerHTML -->
<p>The <dfn for="Element"><code>outerHTML</code></dfn> IDL attribute represents the markup of the
<code><a>Element</a></code> and its contents.
<dl class=domintro>
<dt><var>element</var> . <a for="Element">outerHTML</a> [ = <var>value</var> ]
<dd>Returns a fragment of HTML or XML that represents the element and its contents.
<p>Can be set, to replace the element with nodes parsed from the given string.
<p>In the case of an <a>XML document</a>, throws a "<code><a>InvalidStateError</a></code>"
<code><a>DOMException</a></code> if the element cannot be serialized to XML, or a
"<code><a>SyntaxError</a></code>" <code><a>DOMException</a></code> if the given string is not
well-formed.
<p>Throws a "<code><a>NoModificationAllowedError</a></code>" <code><a>DOMException</a></code>
if the parent of the element is a <code><a>Document</a></code>.
</dl>
<p>On getting, return the result of invoking the <a>fragment serializing algorithm</a> on a
fictional node whose only child is the <a>context object</a> providing <code>true</code> for the
<a><var>require well-formed</var></a> flag (this might throw an exception instead of returning a string).
<p>On setting, the following steps must be run:
<ol>
<li>Let <var>parent</var> be the <a>context object</a>'s <a>parent</a>.
<li>If <var>parent</var> is null, terminate these steps. There would be no way to obtain a
reference to the nodes created even if the remaining steps were run.
<li>If <var>parent</var> is a <code><a>Document</a></code>, throw a
"<code><a>NoModificationAllowedError</a></code>" <code><a>DOMException</a></code>.
<li>If <var>parent</var> is a <code><a>DocumentFragment</a></code>, let <var>parent</var> be a
new <code><a>Element</a></code> with:
<ul>
<li><code>body</code> as its <a>local name</a>,
<li>The <a>HTML namespace</a> as its <a data-lt="concept namespace">namespace</a>, and
<li>The <a>context object</a>'s <a>node document</a> as its <a>node document</a>.
</ul>
<li>Let <var>fragment</var> be the result of invoking the <a>fragment parsing algorithm</a> with
the new value as <var>markup</var>, and <var>parent</var> as the <var>context element</var>.
<li><a>Replace</a> the <a>context object</a> with <var>fragment</var> within the
<a>context object</a>'s <a>parent</a>.
</ol>
<!-- insertAdjacentHTML -->
<dl class=domintro>
<dt><var>element</var> . <a for="Element">insertAdjacentHTML</a> ( <var>position</var>, <var>text</var> )
<dd>Parses the given string <var>text</var> as HTML or XML and inserts the resulting nodes into
the tree in the position given by the <var>position</var> argument, as follows:
<dl>
<dt>"beforebegin"
<dd>Before the element itself (i.e., after <var>element</var>'s previous sibling)
<dt>"afterbegin"
<dd>Just inside the element, before its first child.
<dt>"beforeend"
<dd>Just inside the element, after its last child.
<dt>"afterend"
<dd>After the element itself (i.e., before <var>element</var>'s next sibling)
</dl>
<p>Throws a "<code><a>SyntaxError</a></code>" <code><a>DOMException</a></code> if the arguments
have invalid values (e.g., in the case of an <a>XML document</a>, if the given string is not
well-formed).
<p>Throws a "<code><a>NoModificationAllowedError</a></code>" <code><a>DOMException</a></code> if
the given position isn't possible (e.g. inserting elements after the root element of a
<code><a>Document</a></code>).
</dl>
<p>The
<dfn for="Element" data-lt="insertAdjacentHTML"><code>insertAdjacentHTML(<var>position</var>, <var>text</var>)</code></dfn>
method must run these steps:
<ol>
<li>Use the first matching item from this list:
<dl class=switch>
<dt>If <var>position</var> is an <a>ASCII case-insensitive</a> match for the string
"<code>beforebegin</code>"
<dt>If <var>position</var> is an <a>ASCII case-insensitive</a> match for the string
"<code>afterend</code>"
<dd>Let <var>context</var> be the <a>context object</a>'s <a>parent</a>.
<p>If <var>context</var> is null or a <a>Document</a>, throw a
"<code><a>NoModificationAllowedError</a></code>" <code><a>DOMException</a></code>.
<dt>If <var>position</var> is an <a>ASCII case-insensitive</a> match for the string
"<code>afterbegin</code>"
<dt>If <var>position</var> is an <a>ASCII case-insensitive</a> match for the string
"<code>beforeend</code>"
<dd>Let <var>context</var> be the <a>context object</a>.
<dt>Otherwise
<dd>Throw a "<code><a>SyntaxError</a></code>" <code><a>DOMException</a></code>.
</dl>
<li>If <var>context</var> is not an <code><a>Element</a></code> or the following are all true:
<ul>
<li><var>context</var>'s <a>node document</a> is an <a>HTML document</a>,
<li><var>context</var>'s <a>local name</a> is "<code>html</code>", and
<li><var>context</var>'s <a data-lt="concept namespace">namespace</a> is the
<a>HTML namespace</a>;
</ul>
<p>let <var>context</var> be a new <code><a>Element</a></code> with
<ul>
<li><code>body</code> as its <a>local name</a>,
<li>The <a>HTML namespace</a> as its <a data-lt="concept namespace">namespace</a>, and
<li>The <a>context object</a>'s <a>node document</a> as its <a>node document</a>.
</ul>
<li>Let <var>fragment</var> be the result of invoking the <a>fragment parsing algorithm</a> with
<var>text</var> as <var>markup</var>, and <var>context</var> as the <var>context element</var>.
<li>Use the first matching item from this list:
<dl class=switch>
<dt>If <var>position</var> is an <a>ASCII case-insensitive</a> match for the string
"<code>beforebegin</code>"
<dd><a>Insert</a> <var>fragment</var> into the <a>context object</a>'s <a>parent</a> before
the <a>context object</a>.
<dt>If <var>position</var> is an <a>ASCII case-insensitive</a> match for the string
"<code>afterbegin</code>"
<dd><a>Insert</a> <var>fragment</var> into the <a>context object</a> before its
<a>first child</a>.
<dt>If <var>position</var> is an <a>ASCII case-insensitive</a> match for the string
"<code>beforeend</code>"
<dd><a>Append</a> <var>fragment</var> to the <a>context object</a>.
<dt>If <var>position</var> is an <a>ASCII case-insensitive</a> match for the string
"<code>afterend</code>"
<dd><a>Insert</a> <var>fragment</var> into the <a>context object</a>'s <a>parent</a> before
the <a>context object</a>'s <a>next sibling</a>.
</dl>
</ol>
<p class=note>No special handling for <code><a>template</a></code> elements is included in the
above "<code>afterbegin</code>" and "<code>beforeend</code>" cases. As with other direct
<a>Node</a>-manipulation APIs (and unlike <a for="Element">innerHTML</a>),
<a for="Element">insertAdjacentHTML</a> does not include any special handling for
<code><a>template</a></code> elements. In most cases you will wish to use
<a>template</a>.<a data-lt="template contents">content</a>.<a for="Element">insertAdjacentHTML</a>
instead of directly manipulating the <a>child nodes</a> of a <code><a>template</a></code>
element.</p>
</section><!-- end Extensions to the Element interface -->
<section><h2>Extensions to the <code><a>Range</a></code> interface</h2>
<pre class="idl">
partial interface Range {
[CEReactions, NewObject] DocumentFragment createContextualFragment(DOMString fragment);
};
</pre>
<dl class=domintro>
<dt><var>docFragment</var> = <var>range</var> . <a for="Range">createContextualFragment</a> ( <var>fragment</var> )
<dd>Returns a <code><a>DocumentFragment</a></code>, created from the markup string
<var>fragment</var> using <var>range</var>'s start node as the context in which
<var>fragment</var> is parsed.
</dl>
<p>The <dfn for="Range"><code>createContextualFragment(<var>fragment</var>)</code></dfn> method
must run these steps:
<ol>
<li>Let <var>node</var> be the <a>context object</a>'s <a>start node</a>.
<p>Let <var>element</var> be as follows, depending on <var>node</var>'s interface:
<dl class=switch>
<dt><code><a>Document</a></code>
<dt><code><a>DocumentFragment</a></code>
<dd>null
<dt><code><a>Element</a></code>
<dd><var>node</var>
<dt><code><a>Text</a></code>
<dt><code><a>Comment</a></code>
<dd><var>node</var>'s <a>parent element</a>
<dt><code><a>DocumentType</a></code>
<dt><code><a>ProcessingInstruction</a></code>
<dd>[[DOM4]] prevents this case.
</dl>
<li>If either <var>element</var> is null or the following are all true:
<ul>
<li><var>element</var>'s <a>node document</a> is an <a>HTML document</a>,
<li><var>element</var>'s <a>local name</a> is "<code>html</code>", and
<li><var>element</var>'s <a data-lt="concept namespace">namespace</a> is the
<a>HTML namespace</a>;
</ul>
<p>let <var>element</var> be a new <a>Element</a> with
<ul>
<li>"<code>body</code>" as its <a>local name</a>,
<li>The <a>HTML namespace</a> as its <a data-lt="concept namespace">namespace</a>, and
<li>The <a>context object</a>'s <a>node document</a> as its <a>node document</a>.
</ul>
<li>Let <var>fragment node</var> be the result of invoking the <a>fragment parsing algorithm</a>
with <var>fragment</var> as <var>markup</var>, and <var>element</var> as the
<var>context element</var>.
<li>Unmark all scripts in <var>fragment node</var> as "already started" and as "parser-inserted".
<li>Return the value of <var>fragment node</var>.
</ol>
</section><!-- end Extensions to the Range interface -->
<!-- Dropping this extention as it is not implemented, nor does it appear that any browser
is currently interested in supporting it. Perhaps it can come back in a V2 of this spec
if browsers become interested.
<section><h2>Extensions to the <code><a>Text</a></code> interface</h2>
<pre class="idl">
partial interface Text {
attribute boolean serializeAsCDATA;
};
</pre>
<dl class=domintro>
<dt><var>text</var> . <a for="Text">serializeAsCDATA</a> [ = <var>value</var> ]
<dd>Controls whether, in XML, this node is serialized as a CDATA section.
</dl>
<p><code><a>Text</a></code> nodes have an additional associated flag, the
<dfn>serialize as CDATA flag</dfn>.
<p>The <dfn for="Text"><code>serializeAsCDATA</code></dfn> attribute must return true if the
<a>context object</a> has its <a>serialize as CDATA flag</a> set, or false otherwise.
<p>Setting the <a for="Text">serializeAsCDATA</a> attribute must, if the new value is true, set
the <a>context object</a>'s <a>serialize as CDATA flag</a>, or unset it otherwise.
</section><!-- end Extensions to the Text interface -->
</section><!-- end APIs for DOM Parsing and Serializing -->
<section><h1>Algorithms for parsing and serializing</h1>
<section><h2>Parsing</h2>
<p>The following steps form the <dfn>fragment parsing algorithm</dfn>, whose arguments are a
<var>markup</var> string and a <var>context element</var>:
<ol>
<li>If the <var>context element</var>'s <a>node document</a> is an <a>HTML document</a>: let
<var>algorithm</var> be the <a>HTML fragment parsing algorithm</a>.
<p>If the <var>context element</var>'s <a>node document</a> is an <a>XML document</a>: let
<var>algorithm</var> be the <a>XML fragment parsing algorithm</a>.
<li>Let <var>new children</var> be the result of invoking <var>algorithm</var> with
<var>markup</var> as the <var>input</var>, and <var>context element</var> as the
<var><a>context</a></var> element.
<li>Let <var>fragment</var> be a new <code><a>DocumentFragment</a></code> whose
<a>node document</a> is <var>context element</var>'s <a>node document</a>.
<li><a>Append</a> each <a>Node</a> in <var>new children</var> to <var>fragment</var> (in
<a>tree order</a>).
<p class=note>This ensures the <a>node document</a> for the new <a>nodes</a> is correct.</p>
<li>Return the value of <var>fragment</var>.
</ol>
</section><!-- end Parsing -->
<section><h2>Serializing</h2>
<p>The following steps form the <dfn>fragment serializing algorithm</dfn>, whose arguments are a
<code><a>Node</a></code> <var>node</var> and a flag <dfn>require well-formed</dfn>:
<ol>
<li>Let <var>context document</var> be the value of <var>node</var>'s <a>node document</a>.
<li>If <var>context document</var> is an <a>HTML document</a>, return an
<a>HTML serialization</a> of <var>node</var>.
<li>Otherwise, <var>context document</var> is an <a>XML document</a>; return an
<a>XML serialization</a> of <var>node</var> passing the flag <a><var>require well-formed</var></a>.
<p class=note>The <a>XML serialization</a> defined in this document conforms to the requirements
of the <a>XML fragment serialization algorithm</a> defined in [[HTML5]].</p>
</ol>
<p>To produce an <dfn>HTML serialization</dfn> of a <code><a>Node</a></code> <var>node</var>, the
user agent must run the <a>HTML fragment serialization algorithm</a> on <var>node</var> and return
the string produced.
<section><h2>XML Serialization</h2>
<p>An <a>XML serialization</a> differs from an <a>HTML serialization</a> in the following ways:
<ul>
<li><a>Elements</a> and <a data-lt="attribute">attributes</a> will always be serialized such
that their <code>namespaceURI</code> is preserved. In some cases this means that an existing
<code>prefix</code>, prefix declaration attribute or default namespace declaration attribute
might be dropped, substituted or changed. An <a>HTML serialization</a> does not attempt to
preserve the <code>namespaceURI</code>.
<li><a>Elements</a> not in the <a>HTML namespace</a> containing no <a>children</a>, are
serialized using the <a>empty-element tag</a> syntax (i.e., according to the XML
<a>EmptyElemTag</a> production).
</ul>
<p>Otherwise, the algorithm for producing an <a>XML serialization</a> is designed to produce a
serialization that is compatible with the <a>HTML parser</a>. For example, elements in the
<a>HTML namespace</a> that contain no <a>child nodes</a> are serialized with an explicit begin and end
tag rather than using the <a>empty-element tag</a> syntax.
<p class=note>Per [[DOM4]], <code><a>Attr</a></code> objects do not inherit from <a>Node</a>, and
thus cannot be serialized by the <a>XML serialization algorithm</a>. An attempt to serialize an
<a>Attr</a> object will result in an empty string.
<p>To produce an <dfn>XML serialization</dfn> of a <code><a>Node</a></code> <var>node</var> given
a flag <a><var>require well-formed</var></a>, run the following steps:
<ol>
<li>Let <a><var>namespace</var></a> be a <a>context namespace</a> with value <code>null</code>.
The <dfn>context namespace</dfn> tracks the <a>XML serialization</a> algorithm's current default
namespace. The <a>context namespace</a> is changed when either an <a>Element</a> <a>Node</a> has
a default namespace declaration, or the algorithm generates a default namespace declaration for
the <a>Element</a> <a>Node</a> to match its own namespace. The algorithm assumes no namespace
(<code>null</code>) to start.
<li>Let <a><var>prefix map</var></a> be a new <a>namespace prefix map</a>.
<li><a>Add</a> the <a>XML namespace</a> with prefix value "<code>xml</code>" to
<a><var>prefix map</var></a>.
<li>Let <a><var>prefix index</var></a> be a <a>generated namespace prefix index</a> with value
<code>1</code>. The <dfn>generated namespace prefix index</dfn> is used to generate a new unique
prefix value when no suitable existing namespace prefix is available to serialize a
<var>node</var>'s <code>namespaceURI</code> (or the <code>namespaceURI</code> of one of
<var>node</var>'s attributes). <span class=note>See the <a>generate a prefix</a> algorithm.
</span>
<li>Return the result of running the <a>XML serialization algorithm</a> on <var>node</var>
passing the <a>context namespace</a> <a><var>namespace</var></a>, <a>namespace prefix map</a>
<a><var>prefix map</var></a>, <a>generated namespace prefix index</a> reference to
<a><var>prefix index</var></a>, and the flag <a><var>require well-formed</var></a>. If an
<dfn data-lt="throw an exception">exception</dfn> occurs during the execution of the algorithm,
then catch that exception and throw an "<code><a>InvalidStateError</a></code>"
<code><a>DOMException</a></code>.
</ol>
<p>Each of the following algorithms for <dfn>producing an XML serialization of a DOM node</dfn>
take as input a <var>node</var> to serialize and the following arguments:
<ul>
<li>A <a>context namespace</a> <dfn><var>namespace</var></dfn>
<li>A <a>namespace prefix map</a> <dfn><var>prefix map</var></dfn>
<li>A <a>generated namespace prefix index</a> <dfn><var>prefix index</var></dfn>
<li>The <a><var>require well-formed</var></a> flag
</ul>
<p>The <dfn>XML serialization algorithm</dfn>
<a data-lt="producing an XML serialization of a DOM node">produces an XML serialization of an arbitrary DOM node</a>
<var>node</var> based on the <var>node</var>'s interface type. Each referenced algorithm is
to be passed the arguments as they were recieved by the caller and return their result to the
caller. Re-throw any exceptions. If <var>node</var>'s interface is:</p>
<dl class=switch>
<dt><code><a>Element</a></code>
<dd>Run the algorithm for <a>XML serializing an Element node</a> <var>node</var>.
<dt><code><a>Document</a></code>
<dd>Run the algorithm for <a>XML serializing a Document node</a> <var>node</var>.
<dt><code><a>Comment</a></code>
<dd>Run the algorithm for <a>XML serializing a Comment node</a> <var>node</var>.
<!--<dt><code><a>CDATASection</a></code>
<dd>Run the algorithm for <a>XML serializing a CDATASection node</a> <var>node</var>.-->
<dt><code><a>Text</a></code>
<dd>Run the algorithm for <a>XML serializing a Text node</a> <var>node</var>.
<dt><code><a>DocumentFragment</a></code>
<dd>Run the algorithm for <a>XML serializing a DocumentFragment node</a> <var>node</var>.
<dt><code><a>DocumentType</a></code>
<dd>Run the algorithm for <a>XML serializing a DocumentType node</a> <var>node</var>.
<dt><code><a>ProcessingInstruction</a></code>
<dd>Run the algorithm for <a>XML serializing a ProcessingInstruction node</a> <var>node</var>.
<dt>An <code><a>Attr</a></code> object
<dd>Return an empty string.
<dt>Anything else
<dd>Throw a <a>TypeError</a>. Only <a>Node</a>s and <a>Attr</a> objects can be serialized by
this algorithm.
</dl>
Each of the above referenced algorithms are detailed in the sections that follow.
<section><h2><dfn>XML serializing an Element node</dfn></h2>
The algorithm for <a>producing an XML serialization of a DOM node</a> of type <a>Element</a> is as
follows:
<ol>
<li>If the <a><var>require well-formed</var></a> flag is set (its value is <code>true</code>),
and this <var>node</var>'s <code><a>localName</a></code> attribute contains the character
"<code>:</code>" (U+003A COLON) or does not match the XML <a>Name</a> production, then
<a>throw an exception</a>; the serialization of this <var>node</var> would not be a well-formed
element.
<li>Let <var>markup</var> be the string "<code><</code>" (U+003C LESS-THAN SIGN).
<li>Let <dfn><var>qualified name</var></dfn> be an empty string.
<li>Let <dfn><var>skip end tag</var></dfn> be a boolean flag with value <code>false</code>.
<li>Let <dfn><var>ignore namespace definition attribute</var></dfn> be a boolean flag with value
<code>false</code>.
<li>Given <a><var>prefix map</var></a>, <a>copy a namespace prefix map</a> and let
<var>map</var> be the result.
<li>Let <dfn><var>local prefixes map</var></dfn> be an empty map. The map has unique <a>Node</a>
<code>prefix</code> strings as its keys, with corresponding <code>namespaceURI</code>
<a>Node</a> values as the map's key values (in this map, the <code>null</code> namespace is
represented by the empty string).
<p class=note>This map is local to each element. It is used to ensure there are no conflicting
prefixes should a new namespace <code>prefix</code> attribute need to be
<a data-lt="generate a prefix">generated</a>. It is also used to enable skipping of duplicate
prefix definitions when
<a data-lt="XML serialization of the attributes">writing an element's attributes</a>: the map
allows the algorithm to distinguish between a <code>prefix</code> in the
<a>namespace prefix map</a> that might be locally-defined (to the current <a>Element</a>) and
one that is not.</p>
<li>Let <dfn><var>local default namespace</var></dfn> be the result of
<a>recording the namespace information</a> for <var>node</var> given <var>map</var> and
<a><var>local prefixes map</var></a>.
<p class="note">The above step will update <var>map</var> with any found namespace prefix
definitions, add the found prefix definitions to the <a><var>local prefixes map</var></a> and
return a <a><var>local default namespace</var></a> value defined by a default namespace attribute if one
exists. Otherwise it returns <code>null</code>.</p>
<li>Let <dfn><var>inherited ns</var></dfn> be a copy of <a><var>namespace</var></a>.
<li>Let <var>ns</var> be the value of <var>node</var>'s
<code><a data-lt="element namespaceURI">namespaceURI</a></code> attribute.
<li>If <a><var>inherited ns</var></a> is equal to <var>ns</var>, then:
<ol>
<li>If <a><var>local default namespace</var></a> is not <code>null</code>, then set
<a><var>ignore namespace definition attribute</var></a> to <code>true</code>.
<li>If <var>ns</var> is the <a>XML namespace</a>, then append to
<a><var>qualified name</var></a> the concatenation of the string "<code>xml:</code>" and the
value of <var>node</var>'s <code><a>localName</a></code>.
<li>Otherwise, append to <a><var>qualified name</var></a> the value of <var>node</var>'s
<code><a>localName</a></code>. <span class=note>The <var>node</var>'s
<code><a data-lt="element prefix">prefix</a></code> if it exists, is dropped.</span>
<li>Append the value of <a><var>qualified name</var></a> to <var>markup</var>.
</ol>
<li>Otherwise, <a><var>inherited ns</var></a> is not equal to <var>ns</var> (the <var>node</var>'s
own namespace is different from the context namespace of its parent). Run these sub-steps:
<!-- The serialization algorithm must differentiate this node's namespace from it's parent's
default namespace. There are two ways to do this: (1) [preferred due to assumed minimum length]
use a namespace prefix if one is available or (2) use a default namespace declaration. Both
cases can run into conflicts with existing attributes on the element and are handled
accordingly. -->
<ol>
<li>Let <var>prefix</var> be the value of <var>node</var>'s
<code><a data-lt="element prefix">prefix</a></code> attribute.
<li>Let <var>candidate prefix</var> be the result of
<a>retrieving a preferred prefix string</a> <var>prefix</var> from <var>map</var></a> given
namespace <var>ns</var>.
<p class=note>The above may return <code>null</code> if no namespace key <var>ns</var> exists
in <var>map</var>.</p>
<li>If the value of <var>prefix</var> matches "<code>xmlns</code>", then run the following
steps:
<ol>
<li>If the <a><var>require well-formed</var></a> flag is set, then throw an error. An
<a>Element</a> with <code><a data-lt="element prefix">prefix</a></code> "<code>xmlns</code>"
will not legally round-trip in a conforming <a>XML parser</a>.
<li>Let <var>candidate prefix</var> be the value of <var>prefix</var>.
</ol>
<!-- Found a suitable prefix to use, either locally, or inherited through a parent node that
matches the node's namespaceURI. This prefix will be used in serialization even if the node
doesn't have a real prefix. -->
<li><dfn>Found a suitable namespace prefix</dfn>: if <var>candidate prefix</var> is not
<code>null</code> (a namespace prefix is defined which maps to <var>ns</var>), then:
<p class=note>The following may serialize a different
<code><a data-lt="element prefix">prefix</a></code> than the <a>Element</a>'s existing
<code><a data-lt="element prefix">prefix</a></code> if it already had one. However, the
<a>retrieving a preferred prefix string</a> algorithm already tried to match the existing
prefix if possible.</p>
<ol>
<li>Append to <a><var>qualified name</var></a> the concatenation of
<var>candidate prefix</var>, "<code>:</code>" (U+003A COLON), and <var>node</var>'s <code>
<a>localName</a></code>. <span class="note">There exists on this <var>node</var> or the
<var>node</var>'s ancestry a namespace prefix definition that defines the <var>node</var>'s
namespace.</span>
<li>If the <a><var>local default namespace</var></a> is not <code>null</code> (there exists
a locally-defined default namespace declaration attribute) and its value is not the
<a>XML namespace</a>, then let <a><var>inherited ns</var></a> get the value of
<a><var>local default namespace</var></a> unless the
<a><var>local default namespace</var></a> is the empty string in which case let it get
<code>null</code> (the <a>context namespace</a> is changed to the declared default, rather
than this <var>node</var>'s own namespace).
<p class=note>Any default namespace definitions or namespace prefixes that define the
<a>XML namespace</a> are omitted when serializing this node's attributes.</p>
<li>Append the value of <a><var>qualified name</var></a> to <var>markup</var>.
</ol>
<li>Otherwise, if <var>prefix</var> is not <code>null</code>, then:
<p class=note>By this step, there is no namespace or prefix mapping declaration in this
<var>node</var> (or any parent <var>node</var> visited by this algorithm) that defines
<var>prefix</var> otherwise the step labelled <a>Found a suitable namespace prefix</a> would
have been followed. The sub-steps that follow will create a new namespace prefix declaration
for <var>prefix</var> and ensure that <var>prefix</var> does not conflict with an existing
namespace prefix declaration of the same <code>localName</code> in <var>node</var>'s
<a>attribute list</a>.</p>
<ol>
<li>If the <a><var>local prefixes map</var></a> contains a key matching <var>prefix</var>,
then let <var>prefix</var> be the result of <a>generating a prefix</a> providing as input
<var>map</var>, <var>ns</var>, and <a>prefix index</a>.
<li><a>Add</a> <var>prefix</var> to <var>map</var> given namespace <var>ns</var>.
<li>Append to <a><var>qualified name</var></a> the concatenation of <var>prefix</var>,
"<code>:</code>" (U+003A COLON), and <var>node</var>'s <code><a>localName</a></code>.
<li>Append the value of <a><var>qualified name</var></a> to <var>markup</var>.
<li>Append the following to <var>markup</var>, in the order listed:
<p class=note>The following serializes a namespace prefix declaration for <var>prefix</var>
which was just added to the <var>map</var>.</p>
<ol>
<li>"<code> </code>" (U+0020 SPACE);
<li>The string "<code>xmlns:</code>";
<li>The value of <var>prefix</var>;
<li>"<code>="</code>" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);
<li>The result of <a>serializing an attribute value</a> given <var>ns</var> and the
<a><var>require well-formed</var></a> flag as input;
<li>"<code>"</code>" (U+0022 QUOTATION MARK).
<li>If <a><var>local default namespace</var></a> is not <code>null</code> (there exists a
locally-defined default namespace declaration attribute), then let
<a><var>inherited ns</var></a> get the value of <a><var>local default namespace</var></a>
unless the <a><var>local default namespace</var></a> is the empty string in which case let
it get <code>null</code>.
</ol>
</ol>
<li>Otherwise, if <a><var>local default namespace</var></a> is <code>null</code>, or
<a><var>local default namespace</var></a> is not <code>null</code> and its value is not equal
to <var>ns</var>, then:
<p class=note>At this point, the namespace for this node still needs to be serialized, but
there's no <code>prefix</code> (or <var>candidate prefix</var>) availble; the following uses
the default namespace declaration to define the namespace--optionally replacing an existing
default declaration if present.</p>
<ol>
<li>Set the <a><var>ignore namespace definition attribute</var></a> flag to
<code>true</code>.
<li>Append to <a><var>qualified name</var></a> the value of <var>node</var>'s
<code><a>localName</a></code>.
<li>Let the value of <a><var>inherited ns</var></a> be <var>ns</var>.
<p class=note>The new default namespace will be used in the serialization to define this
<var>node</var>'s namespace and act as the <a>context namespace</a> for its
<a>children</a>.</p>
<li>Append the value of <a><var>qualified name</var></a> to <var>markup</var>.
<li>Append the following to <var>markup</var>, in the order listed:
<p class=note>The following serializes the new (or replacement) default namespace