Marcoil has uploaded a new change for review. https://gerrit.wikimedia.org/r/160698
Change subject: WIP: Don't emit data-parsoid attributes for template content ...................................................................... WIP: Don't emit data-parsoid attributes for template content (For 70786) Change-Id: I695da6e4485ba62f3a1a4cc9e9fdd1a39637f327 --- M lib/dom.cleanup.js M lib/dom.markFosteredContent.js A lib/dom.removeTransclusionDataParsoid.js M lib/mediawiki.DOMPostProcessor.js M lib/mediawiki.DOMUtils.js 5 files changed, 84 insertions(+), 9 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid refs/changes/98/160698/1 diff --git a/lib/dom.cleanup.js b/lib/dom.cleanup.js index 701b1e6..4105389 100644 --- a/lib/dom.cleanup.js +++ b/lib/dom.cleanup.js @@ -55,7 +55,7 @@ return next; } - dp.tagId = undefined; + delete dp.tagId; // Remove data-parsoid.src from templates and extensions that have // valid data-mw and dsr. This should reduce data-parsoid bloat. @@ -65,16 +65,16 @@ (!dp.tsr || node.getAttribute("data-mw") && dp.dsr && dp.dsr[0] && dp.dsr[1])) { - dp.src = undefined; + delete dp.src; } // Remove tsr if (dp.tsr) { - dp.tsr = undefined; + delete dp.tsr; } // Remove temporary information - dp.tmp = undefined; + delete dp.tmp; // Make dsr zero-range for fostered content // to prevent selser from duplicating this content diff --git a/lib/dom.markFosteredContent.js b/lib/dom.markFosteredContent.js index 903df5c..9c723fd 100644 --- a/lib/dom.markFosteredContent.js +++ b/lib/dom.markFosteredContent.js @@ -137,7 +137,7 @@ DU.deleteNode( c ); } else if ( DU.isElt( c ) ) { dp = DU.getDataParsoid( c ); - dp.inTransclusion = undefined; + delete dp.inTransclusion; if ( c.childNodes.length > 0 ) { markFosteredContent( c, env ); } diff --git a/lib/dom.removeTransclusionDataParsoid.js b/lib/dom.removeTransclusionDataParsoid.js new file mode 100644 index 0000000..010a63e --- /dev/null +++ b/lib/dom.removeTransclusionDataParsoid.js @@ -0,0 +1,57 @@ +"use strict"; + +var DU = require('./mediawiki.DOMUtils.js').DOMUtils; + +function removeTransclusionDataParsoid(node, env, options, atTopLevel, tplInfo) { + // Dont bother with this on sub-pipelines + if (!atTopLevel) { + return; + } + + var c = node.firstChild; + while (c) { + var next = c.nextSibling; + + if (DU.isElt(c)) { + // Identify template/extension content (not interested in "mw:Param" nodes). + // We are interested in the very first node. + if (DU.isTplOrExtToplevelNode(env, c) && + /(^|\s)mw:(Extension|Transclusion)/.test(c.getAttribute("typeof"))) + { + // We know that tplInfo will be null here since we don't + // mark up nested transclusions. + var about = c.getAttribute('about'); + tplInfo = { first: c, last: DU.getAboutSiblings(c, about).last() }; + } + + // Process subtree first + removeTransclusionDataParsoid(c, env, options, atTopLevel, tplInfo); + + // Cannot delete the first node since that carries the transclusion + // information (typeof, data-mw). We could delete and migrate + // the info over, but more pain than worth it. We can reconsider if + // this ever becomes an issue. + if (tplInfo) { + if (c !== tplInfo.first) { + // TODO: We can't remove dp from nodes with stx information + // right now or wt2wt fails + var dp = DU.getDataParsoid(c); + if (!dp.stx) { + DU.removeDataParsoid(c); + } + } + + // Clear tpl info + if (c === tplInfo.last) { + tplInfo = null; + } + } + } + + c = next; + } +} + +if (typeof module === "object") { + module.exports.removeTransclusionDataParsoid = removeTransclusionDataParsoid; +} diff --git a/lib/mediawiki.DOMPostProcessor.js b/lib/mediawiki.DOMPostProcessor.js index 636017c..2c36041 100644 --- a/lib/mediawiki.DOMPostProcessor.js +++ b/lib/mediawiki.DOMPostProcessor.js @@ -27,7 +27,8 @@ unpackDOMFragments = require('./dom.t.unpackDOMFragments.js').unpackDOMFragments, wrapTemplates = require('./dom.wrapTemplates.js').wrapTemplates, stripEmptyElements = require('./dom.stripEmptyElements.js').stripEmptyElements, - lintWikitextFixup = require('./dom.linter.js').logWikitextFixups; + lintWikitextFixup = require('./dom.linter.js').logWikitextFixups, + removeTransclusionDataParsoid = require('./dom.removeTransclusionDataParsoid.js').removeTransclusionDataParsoid; // map from mediawiki metadata names to RDFa property names var metadataMap = { @@ -206,9 +207,15 @@ domVisitor2.addHandler( 'td', tableFixer.stripDoubleTDs.bind( tableFixer, env ) ); domVisitor2.addHandler( 'td', tableFixer.reparseTemplatedAttributes.bind( tableFixer, env ) ); domVisitor2.addHandler( 'th', tableFixer.reparseTemplatedAttributes.bind( tableFixer, env ) ); - // 4. Save data.parsoid into data-parsoid html attribute. - domVisitor2.addHandler( null, cleanupAndSaveDataParsoid.bind( null, env ) ); this.processors.push(domVisitor2.traverse.bind(domVisitor2)); + + // Remove data-parsoid from transcluded content + this.processors.push(removeTransclusionDataParsoid); + + var domVisitor3 = new DOMTraverser(env); + // 4. Save data.parsoid into data-parsoid html attribute. + domVisitor3.addHandler( null, cleanupAndSaveDataParsoid.bind( null, env ) ); + this.processors.push(domVisitor3.traverse.bind(domVisitor3)); } // Inherit from EventEmitter diff --git a/lib/mediawiki.DOMUtils.js b/lib/mediawiki.DOMUtils.js index d901ab3..5021578 100644 --- a/lib/mediawiki.DOMUtils.js +++ b/lib/mediawiki.DOMUtils.js @@ -265,7 +265,8 @@ var val = data[key]; if ( val && val.constructor === String ) { node.setAttribute('data-' + key, val); - } else if (val instanceof Object) { + } else if (val instanceof Object && + Object.keys(val).length > 0) { this.setJSONAttribute(node, 'data-' + key, val); } // Else: throw error? @@ -308,6 +309,16 @@ return this.setJSONAttribute( node, "data-parsoid", dpObj ); }, + /** + * Remove the data-parsoid attribute on a node. + * + * @returns {Node} `node`, without data-parsoid + */ + removeDataParsoid: function(node) { + delete this.getNodeData(node).parsoid; + return node; + }, + getNodeData: function ( node ) { if ( !node.dataobject ) { node.dataobject = {}; -- To view, visit https://gerrit.wikimedia.org/r/160698 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I695da6e4485ba62f3a1a4cc9e9fdd1a39637f327 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/parsoid Gerrit-Branch: master Gerrit-Owner: Marcoil <marc...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits