Skip to content

Commit

Permalink
Strip legacy section IDs from inside headings
Browse files Browse the repository at this point in the history
We emit <span id="..." typeof="mw:FallbackId"></span> inside headings
when necessary to provide backward compatibility with pre-HTML5
section ids.  These are mostly harmless inside VE --- they become hidden
alienated nodes, which are then safely ignored by Parsoid during html2wt
even if they manage to migrate outside of a heading --- but it's a
little safer to strip them.

Change-Id: I07d0fcf54427ab02d4930cff183d3e5579a99306
  • Loading branch information
cscott committed Dec 12, 2017
1 parent a5a3b0c commit b85caca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/ve-mw/ce/nodes/ve.ce.MWTransclusionNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ ve.ce.MWTransclusionNode.static.filterRendering = function ( contentNodes ) {
// Unwrap Parsoid sections (which probably shouldn't exist: T181226)
contentNodes.forEach( function ( node ) { wrapper.appendChild( node ); } );
ve.unwrapParsoidSections( wrapper );
ve.stripParsoidFallbackIds( wrapper );
contentNodes = Array.prototype.slice.call( wrapper.childNodes );

function isAutoGenerated( node ) {
Expand Down
2 changes: 2 additions & 0 deletions modules/ve-mw/init/ve.init.mw.Target.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ ve.init.mw.Target.static.parseDocument = function ( documentString, mode ) {
doc = ve.parseXhtml( documentString );
// Strip Parsoid sections
ve.unwrapParsoidSections( doc.body );
// Strip legacy IDs, for example in section headings
ve.stripParsoidFallbackIds( doc.body );
}
// Fix relative or missing base URL if needed
this.fixBase( doc );
Expand Down
12 changes: 12 additions & 0 deletions modules/ve-mw/ve.MWutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ ve.unwrapParsoidSections = function ( element ) {
parent.removeChild( section );
} );
};

/**
* Strip legacy (non-HTML5) IDs; typically found as section IDs inside
* headings.
*
* @param {HTMLElement} element Parent element, e.g. document body
*/
ve.stripParsoidFallbackIds = function ( element ) {
Array.prototype.forEach.call( element.querySelectorAll( 'span[typeof="mw:FallbackId"][id]:empty' ), function ( legacySpan ) {
legacySpan.parentNode.removeChild( legacySpan );
} );
};

0 comments on commit b85caca

Please sign in to comment.