Parsoid/MediaWiki DOM spec/Template Continuity
Parsoid expresses ranges using about attributes on nodes. The basic definition in the MediaWiki DOM Spec reads:
The first node of a DOM range always has the following attributes:
typeofattribute (ex:mw:Transclusion,mw:Extension/syntaxhighlight)- an
aboutattribute (ex:#mwt1)- a
data-mwattribute encoding additional information about the structure emitting the output. Thedata-mwschema is specific to thetypeofvalue and is documented in more detail in specific sections below.For DOM ranges that have additional DOM nodes following the first node, they are identified by an
aboutattribute whose value is identical to theaboutattribute value of the first node. If nodes on the contiguous chain (not in the nested subtrees) cannot have an attribute set (ie. text or comment nodes), they will be wrapped in span tags so theaboutattribute can be applied and the chain isn't broken.
This description isn't fully complete, however.
Complications of DOM ranges
[edit]There are a few cases that complicate this simple picture.
Section stripping by VisualEditor
[edit]<section> tags are stripped by VisualEditor before it creates its internal data model, and are absent from the HTML sent back to Parsoid for conversion back to wikitext. This simplifies VisualEditor, since it doesn't have to re-parent sub-trees if (for example) an editor adds a new heading in the middle of a paragraph. However, it means that we can't rely on any information stored on the <section> tag. Template continuity must still hold even if the <section> tags are stripped, and none of the typeof information can be stored on the <section> tag. We tweak our wrapper code and expand sections if necessary to ensure this. For example, given this wikitext:
{{1x|
abc
==Foo==
def}}
ghi
We generate this HTML:
<section data-mw-section-id="0" about="#mwt2" typeof="mw:Transclusion"
data-mw='{"parts":[{"template":{"target":{"wt":"1x","href":"./Template:1x"},"params":{"1":{"wt":"\nabc\n==Foo==\ndef"}},"i":0}},"\n\nghi\n"]}'>
<span about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"1x","href":"./Template:1x"},"params":{"1":{"wt":"\nabc\n==Foo==\ndef"}},"i":0}}]}'></span>
<p about="#mwt1">abc</p>
<span about="#mwt1"></span>
</section>
<section data-mw-section-id="-1" about="#mwt2">
<h2 about="#mwt1" id="Foo">Foo</h2>
<span about="#mwt1"></span>
<p about="#mwt1">def</p>
<p>ghi</p>
</section>
Note that:
- We've duplicated the
typeof="mw:Transclusion"onto both the first<section>tag and the first<span>tag, to protect against the<section>tag being stripped by VisualEditor. Further, there are two differentaboutids used for the same transclusion: if the<section>tags are left in the DOM, the about ID is#mwt2and the transcluded range is the two<section>tags (and their contents). The client is expected to ignore the about IDs in the section contents. If the<section>tags are stripped from the DOM, what is left is a region identified instead by#mwt1and which includes 6 sibling nodes:<span /><p/><span/><h2/><span/><p/>.
- For the section tag we've expanded the template-affected range to include the trailing
"\n\nghi\n", which also appears as an additional entry in thedata-mw.partsarray. Note that only the#mwt2region includes theghi; the#mwt1region (used when<section>is stripped) does not. The point to keep in mind is that the template-affected region may be different when sections are stripped.
Template-affected regions are smaller and more accurate when section tags are stripped. Using the "list of siblings" range traversal algorithm from the DOM spec results in regions that may be larger than necessary.
Section edit links insertion
[edit]In addition to the removal of section tags, the post-processing transformations[1] used for Parsoid Read Views adds additional markup to the Parsoid output, and this additional markup may affect range continuity (T407739). Consider this wikitext (the same as the example above, but without the templating):
abc
==Foo==
def
ghi
This generates the following "canonical" HTML:
<section data-mw-section-id="0">
<p>abc</p>
</section>
<section data-mw-section-id="1">
<h2 id="Foo">Foo</h2>
<p>def</p>
<p>ghi</p>
</section>
But if we were to transclude this in a page, after the HandleParsoidSectionLinks transformation this becomes:
<section data-mw-section-id="0" about="#mwt2" typeof="mw:Transclusion" id="mwAQ" data-mw="...">
<p about="#mwt1" typeof="mw:Transclusion" id="mwAg" data-mw="...">abc</p>
</section>
<section data-mw-section-id="-1" about="#mwt2" id="mwAw">
<div class="mw-heading mw-heading2" id="mwAA">
<h2 about="#mwt1" id="Foo">Foo</h2>
<span class="mw-editsection" id="mwCA">
<span class="mw-editsection-bracket" id="mwCQ">[</span>
<a href="..." title="Edit section: Foo" class="mw-editsection-visualeditor" id="mwCg">
<span id="mwCw">edit</span>
</a>
<span class="mw-editsection-divider" id="mwDA"> | </span>
<a href="..." title="Edit section's source code: Foo" id="mwDQ">
<span id="mwDg">edit source</span>
</a>
<span class="mw-editsection-bracket" id="mwDw">]</span>
</span>
</div>
<p about="#mwt1" id="mwBQ">def</p>
<p about="#mwt1" id="mwBw">ghi</p>
</section>
There's a lot of added content there! This output was last changed in phab:T13555 for accessibility, and a fuller description of the markup can be found in Heading HTML changes. Here's a summary of the new output, with boldface+underline for new classes added:
<section><p/></section>
<section><div><h2/><span><a><span/></a><span/><a><span/></a><span/></span></div><p/><p/></section>
Note that none of those tags listed in boldface have about attributes. If we're not doing <section> stripping, the DOM range algorithm still works, since the template-affected region of any template generating a heading will include the top level <section> tags, and all of the newly-added content is inside a <section> tag. You can verify that the #mwt2 range above is correct, containing the two <section> tag siblings.
However, if <section> tag stripping is done, the resulting #mwt1 range is now discontiguous. Using the "list of siblings" DOM range algorithm, the region starts with <p about="#mwt1">abc</p> and then ends prematurely, since the next non-section tag encountered is a <div> without an about attribute.
Other added content
[edit]
Other content added in post-processing causes similar issues to the section-edit-links case above. For mobile we wrap the contents of a section in an additional <div> tag to allow collapsible sections. The output HTML above would instead end with:
<div>
<p about="#mwt1">def</p>
<p about="#mwt1">ghi</p>
</div>
As before, the wrapper <div> added here does not have an about attribute and so would terminate the template-affected range prematurely according to the "list of siblings" algorithm above if <section> wrappers were stripped.
The table of contents is added inline as well during post-processing; it is conceivable that could break a template-affected region.
Localization messages are added inside pre-existing wrapper tags, and so the wrapper tag would preserve whatever about ID is needed for template continuity.
The transformations done for Mobile content and for DiscussionTools may break template continuity; this has not been studied carefully.
Possible solutions
[edit]Through a combination of tweaks to Parsoid output and adjustments to the "list of siblings" range algorithm we can handle these corner cases in three different ways.
Range runs from start to end
[edit]The first solution requires no adjustments to the content generated by Parsoid. Instead we could reword the "list of siblings" range algorithm to be more similar to the ranges standardized in the DOM Range spec. That is, define our ranges not as a series of sibling nodes with matching attributes, but instead by identifying the unique start and end nodes and enumerating "the nodes visited in a tree-order walk between start and end, inclusive". A formal definition can be found in the DOM spec. The "start" node is the first node in tree-order with the appropriate typeof and about attributes, and the "end" node is the last node in tree-order with an about attribute matching the "start" node.
There are a number of benefits to this definition. First, fewer nodes need to be marked with about attributes. The "list of siblings" algorithm mentions the need to <span>-wrap all sibling nodes which cannot hold an about attribute (ie, all non-Element Nodes like Comment or Text). This wrapping would still need to be done on the initial/final nodes, but we would no longer have to wrap intermediate sibling nodes just to maintain about continuity.
Second, the range constructed from unique start/end nodes is much more resilient to wrapper and content insertion, and in particular avoids the need to expand the range to accommodate a wrapper.
The querySelectorAll() method is documented to return results "in document order — that is, parents before children, earlier siblings before later siblings." Therefore, implementing this range algorithm in JavaScript, given a node firstNode, is as simple as:
function makeRange(firstNode) {
const id = firstNode.getAttribute('about');
const lastNode = Array.from(document.querySelectorAll(`[about="${id}"]`)).pop();
const range = new Range();
range.setStartBefore(firstNode);
range.setEndAfter(lastNode);
return range;
}
The Range object returned implements useful methods like cloneContents() which properly handle wrapper nodes while omitting leading or trailing content in those wrappers.
Drawbacks include the fact that the Document order comparison operations in the DOM (compareDocumentPosition, and implicitly in querySelectorAll) may not be well supported in all programming languages (cough, PHP), so reliably and efficiently extracting the "first" and "last" nodes tagged with a given about ID may require some care. Additionally, we would need to update traversal algorithms in Parsoid and its clients; in the interim the unnecessary sibling about attributes would continue to be generated and corner case bugs would exist.
Synthesize/transfer appropriate about IDs for added content
[edit]An alternative is to change the output generated by Parsoid and core's post-processing passes to ensure that a sequence of contiguous siblings with matching about IDs is not broken up. In the case of added content this is not necessarily difficult; a generic replacement for Node.insertBefore() could consider the siblings before and after the inserted content and if its predecessor and successor siblings both contain matching about attributes, then add an appropriate about to the newly-inserted node as well. The case where a wrapper is created around a single node is similar: if the node being wrapped contains an about attribute, then transfer that about attribute to the wrapper node. Things are more complicated when a series of sibling nodes is wrapped, potentially resulting in an expansion of the range and an invalid data-mw.parts array.
A variant on this approach is to generate all possible wrappers in Parsoid before about IDs are assigned, and limit post-processing transformations to (a) filling in an existing wrapper, like the localization passes do, or (b) removing an unnecessary wrapper. One caveat is that Parsoid needs to know which wrappers could be removed, and ensure that crucial range information doesn't live on a deletable wrapper. This is effectively what Parsoid is doing already with <section> tags.
The primary disadvantage of this approach is its fragility: all code which manipulates the DOM must be aware of issues related to template continuity and assign appropriate about IDs—or, in the variant approach, Parsoid must be aware of all ways in which wrappers may be deleted and ensure that alternate ranges are created where needed.
Mark ignorable/added nodes
[edit]A combination of the first and second solutions is to mark added wrappers or inserted content in some way to make them distinguishable, and to then tweak the range algorithm to "skip over" these added nodes. There are many ways that nodes could be marked, but for purposes of discussion we'll use two unique about attribute values, skip and wrapper.[2] The new range definition would be (with changes boldfaced):
The first node of a DOM range always has the following attributes:
typeofattribute (ex:mw:Transclusion,mw:Extension/syntaxhighlight)- an
aboutattribute which begins with a hash (ex:#mwt1)- a
data-mwattribute encoding additional information about the structure emitting the output. Thedata-mwschema is specific to thetypeofvalue and is documented in more detail in specific sections below.For DOM ranges that have additional DOM nodes following the first node, form a sequence of siblings of the first node. If a node in this sequence has
about="wrapper"then it should be replaced by its children in the sequence, recursively. If the first node in the sequence has a parent with about="wrapper" then continue the sibling sequence with the siblings of the parent. Starting from the beginning of the sequence, members with anaboutattribute whose value is either the unique valueskipor identical to theaboutattribute value of the first node belong to the range. If nodes on the sequence of 'siblings' cannot have an attribute set (ie. text or comment nodes), they will be wrapped in span tags so anaboutattribute can be applied and the chain isn't broken.
Using the examples above, the output generated for <section> tag wrappers would look like this:
<section data-mw-section-id="0" about="wrapper" typeof="mw:Transclusion">
<span about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"1x","href":"./Template:1x"},"params":{"1":{"wt":"\nabc\n==Foo==\ndef"}},"i":0}}]}'></span>
<p about="#mwt1">abc</p>
<span about="#mwt1"></span>
</section>
<section data-mw-section-id="-1" about="wrapper">
<h2 about="#mwt1" id="Foo">Foo</h2>
<span about="#mwt1"></span>
<p about="#mwt1">def</p>
<p>ghi</p>
</section>
The output generated for section edit links would look like:
<section data-mw-section-id="0" about="wrapper" id="mwAQ">
<p about="#mwt1" typeof="mw:Transclusion" id="mwAg" data-mw="...">abc</p>
</section>
<section data-mw-section-id="-1" about="wrapper" id="mwAw">
<div class="mw-heading mw-heading2" about="wrapper" id="mwAA">
<h2 about="#mwt1" id="Foo">Foo</h2>
<span class="mw-editsection" about="skip" id="mwCA">
<span class="mw-editsection-bracket" id="mwCQ">[</span>
<a href="..." title="Edit section: Foo" class="mw-editsection-visualeditor" id="mwCg">
<span id="mwCw">edit</span>
</a>
<span class="mw-editsection-divider" id="mwDA"> | </span>
<a href="..." title="Edit section's source code: Foo" id="mwDQ">
<span id="mwDg">edit source</span>
</a>
<span class="mw-editsection-bracket" id="mwDw">]</span>
</span>
</div>
<p about="#mwt1" id="mwBQ">def</p>
<p about="#mwt1" id="mwBw">ghi</p>
</section>
And the output generated for collapsible section wrappers would look like:
<div about="wrapper">
<p about="#mwt1">def</p>
<p about="#mwt1">ghi</p>
</div>
This proposal shares disadvantages of both of the first two proposed solutions: it requires clients to update their range algorithm, and it also requires every postprocessing pass to carefully add the correct about="skip" or about="wrapper" attribute. On the other hand, it handles wrapper nodes more easily than in the "synthesize correct about IDs" proposal.
Recommendation
[edit]At the present time, cscott recommends the first proposal ("range runs from start to end") because this doesn't require any additional code in either Parsoid or post-processing passes. Gadgets using read-view HTML (and other new code bases) are simply cautioned to use the new algorithm instead of the naive "sibling node" algorithm from the MediaWiki DOM Spec if they want correct behavior around headings. In the future, we could simplify the Parsoid codebase by removing the redundant about IDs and typeofs on <section> elements if we migrated Parsoid itself to use this algorithm, but this can be deferred until MediaWiki DOM Spec version 3.0.0.
Notes
[edit]- ↑ VisualEditor uses "canonical" Parsoid output. This then is slightly modified to generate the "read views" HTML used on article pages: URLs are normalized, section edit links and table of contents are added, MCR output is concatenated, etc.
- ↑ Some other alternatives:
typeof="mw:NonTemplate",data-mw={"autogenerated":true},data-mw-tpl-ignore,about=""