[MediaWiki-commits] [Gerrit] mediawiki...parsoid[master]: Preserve original transclusion's parameter order
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/389852 ) Change subject: Preserve original transclusion's parameter order .. Preserve original transclusion's parameter order * Parameters that were previously present aren't reordered. Any updates to params are made in place. * Newly introduced parameters are sorted in between the original parameters at a place where they preserve templateData order partially. * In the absence of templatedata, they will be serialized in the order in which they were added by VE into data-mw. * Updated mocha tests. Change-Id: I2b269d5bfb08efbe56b9293c2aa3e810fb1b29da --- M lib/html2wt/WikitextSerializer.js M tests/mocha/templatedata.js M tests/mockAPI.js 3 files changed, 96 insertions(+), 53 deletions(-) Approvals: jenkins-bot: Verified Arlolra: Looks good to me, approved diff --git a/lib/html2wt/WikitextSerializer.js b/lib/html2wt/WikitextSerializer.js index 2fc2a1a..ad503fc 100644 --- a/lib/html2wt/WikitextSerializer.js +++ b/lib/html2wt/WikitextSerializer.js @@ -413,6 +413,72 @@ }); }; +function createParamComparator(dpArgInfo, tplData, dataMwKeys) { + // Record order of parameters in new data-mw + var newOrder = new Map(Array.from(dataMwKeys).map(function(key, i) { + return [key, { order: i }]; + })); + // Record order of parameters in original wikitext (from data-parsoid) + var origOrder = new Map(dpArgInfo.map(function(argInfo, i) { + return [argInfo.k, { order: i, dist: 0 }]; + })); + // Record order of parameters in templatedata (if present) + var tplDataOrder = new Map(); + var keys = []; + if (tplData && Array.isArray(tplData.paramOrder)) { + var params = tplData.params; + tplData.paramOrder.forEach(function(k, i) { + tplDataOrder.set(k, { order: keys.length }); + keys.push(k); + // Aliases have the same sort order as the main name. + var aliases = params && params[k] && params[k].aliases; + (aliases || []).forEach(function(a) { + tplDataOrder.set(a, { order: keys.length }); + keys.push(a); + }); + }); + } + // Find the closest "original parameter" for each templatedata parameter, + // so that newly-added parameters are placed near the parameters which + // templatedata says they should be adjacent to. + var nearestOrder = new Map(origOrder); + var reduceF = function(acc, val, i) { + if (origOrder.has(val)) { + acc = origOrder.get(val); + } + if (!(nearestOrder.has(val) && nearestOrder.get(val).dist < acc.dist)) { + nearestOrder.set(val, acc); + } + return { order: acc.order, dist: acc.dist + 1 }; + }; + // Find closest original parameter before the key. + keys.reduce(reduceF, { order: -1, dist: 2 * keys.length }); + // Find closest original parameter after the key. + keys.reduceRight(reduceF, { order: origOrder.size, dist: keys.length }); + + // Helper function to return a large number if the given key isn't + // in the sort order map + var big = Math.max(nearestOrder.size, newOrder.size); + var defaultGet = function(map, key) { + return map.has(key) ? map.get(key).order : big; + }; + + return function cmp(a, b) { + // primary key is `nearestOrder` (nearest original parameter) + var aOrder = defaultGet(nearestOrder, a); + var bOrder = defaultGet(nearestOrder, b); + if (aOrder !== bOrder) { return aOrder - bOrder; } + // secondary key is templatedata order + aOrder = defaultGet(tplDataOrder, a); + bOrder = defaultGet(tplDataOrder, b); + if (aOrder !== bOrder) { return aOrder - bOrder; } + // tertiary key is original input order (makes sort stable) + aOrder = defaultGet(newOrder, a); + bOrder = defaultGet(newOrder, b); + return aOrder - bOrder; + }; +} + // See https://github.com/wikimedia/mediawiki-extensions-TemplateData/blob/master/Specification.md // for the templatedata specification. WSP.serializePart = Promise.method(function(node, type, part, tplData) { @@ -540,34 +606,8 @@ kvMap.set(k, { serializeAsNamed: serializeAsNamed, name: name, value: value }); }); }).then(function() { - var paramOrder = []; - if (tplData && Array.isArray(tplData.paramOrder)) { - var params = tplData.params; -
[MediaWiki-commits] [Gerrit] mediawiki...parsoid[master]: Preserve original transclusion's parameter order
Subramanya Sastry has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/389852 ) Change subject: Preserve original transclusion's parameter order .. Preserve original transclusion's parameter order * Parameters that were previously present aren't reordered. Any updates to params are made in place. * Newly introduce parameters are serialized in the order in which they show up in templatedata format. In the absence of templatedata, they will be serialized in the order in which they were added by VE into data-mw. * Updated mocha tests. Change-Id: I2b269d5bfb08efbe56b9293c2aa3e810fb1b29da --- M lib/html2wt/WikitextSerializer.js M tests/mocha/templatedata.js 2 files changed, 13 insertions(+), 10 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid refs/changes/52/389852/1 diff --git a/lib/html2wt/WikitextSerializer.js b/lib/html2wt/WikitextSerializer.js index d404bee..118257b 100644 --- a/lib/html2wt/WikitextSerializer.js +++ b/lib/html2wt/WikitextSerializer.js @@ -550,17 +550,20 @@ }, paramOrder); } var argOrder = [].concat( - // 1. Use tpldata param order as the first guide + // 1. Push keys from data-parsoid first. + //This ensures that param order from the original + //transclusion is preserved. + //(data-parsoid argInfo also present in data-mw + //will get processed). + dpArgInfo.map(function(argInfo) { return argInfo.k; }), + + // 2. Use tpldata param order as the next source of + //ordering information. //Note that: //(a) template arg names are case-sensitive //(b) arg names in tplData.paramOrder already //have their spaces trimmed paramOrder, - - // 2. Push keys from data-parsoid - //data-parsoid argInfo also present in data-mw - //will get processed - dpArgInfo.map(function(argInfo) { return argInfo.k; }), // 3. Push keys from data-mw //data-mw entry not present in data-parsoid argInfo diff --git a/tests/mocha/templatedata.js b/tests/mocha/templatedata.js index 0d81382..c88dfc9 100644 --- a/tests/mocha/templatedata.js +++ b/tests/mocha/templatedata.js @@ -90,7 +90,7 @@ 'wt': { 'no_selser': '{{NoFormatWithParamOrder|f2=foo|f1=foo}}', 'new_content': '{{NoFormatWithParamOrder|f1=foo|f2=foo}}', - 'edited': '{{NoFormatWithParamOrder|f1=BAR|f2=foo}}', + 'edited': '{{NoFormatWithParamOrder|f2=foo|f1=BAR}}', }, }, @@ -134,7 +134,7 @@ 'wt': { 'no_selser': '{{InlineTplWithParamOrder\n|f2 = foo\n|f1 = foo\n}}', 'new_content': '{{InlineTplWithParamOrder|f1=foo|f2=foo}}', - 'edited': '{{InlineTplWithParamOrder|f1=BAR|f2=foo}}', + 'edited': '{{InlineTplWithParamOrder|f2=foo|f1=BAR}}', }, }, @@ -145,7 +145,7 @@ 'wt': { 'no_selser': '{{BlockTplWithParamOrder|f2=foo|f1=foo}}', 'new_content': '{{BlockTplWithParamOrder\n| f1 = foo\n| f2 = foo\n}}', - 'edited': '{{BlockTplWithParamOrder\n| f1 = BAR\n| f2 = foo\n}}', + 'edited': '{{BlockTplWithParamOrder\n| f2 = foo\n| f1 = BAR\n}}', }, }, @@ -167,7 +167,7 @@ 'wt': { 'no_selser': '{{BlockTplWithParamOrder|f2=foo|f1=foo}}SOME TEXT{{InlineTplNoParamOrder\n|f2 = foo\n|f1 = foo\n}}', 'new_content': '{{BlockTplWithParamOrder\n| f1 = foo\n| f2 = foo\n}}SOME TEXT{{InlineTplNoParamOrder|f1=foo|f2=foo}}', - 'edited': '{{BlockTplWithParamOrder\n| f1 = BAR\n| f2 = foo\n}}SOME TEXT{{InlineTplNoParamOrder|f2=foo|f1=foo}}', + 'edited': '{{BlockTplWithParamOrder\n| f2 = foo\n| f1 = BAR\n}}SOME TEXT{{InlineTplNoParamOrder|f2=foo|f1=foo}}', }, }, -- To view, visit https://gerrit.wikimedia.org/r/389852 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I2b269d5bfb08efbe56b9293c2aa3e810fb1b29da Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/parsoid Gerrit-Branch: master Gerrit-Owner: Subramanya Sastry