Mholloway has uploaded a new change for review. https://gerrit.wikimedia.org/r/299292
Change subject: Light refactoring of transforms.js ...................................................................... Light refactoring of transforms.js *Consolidates externally visible functions in export object *Marks all internal functions with underscore prefix for easy distinction *Reorders functions so exported ones appear at file bottom, near the exports Change-Id: I22654f63ec459303f592eec3d57563a9e27df259 --- M lib/transforms.js 1 file changed, 72 insertions(+), 77 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps refs/changes/92/299292/1 diff --git a/lib/transforms.js b/lib/transforms.js index 0ceb9c8..5aa235f 100644 --- a/lib/transforms.js +++ b/lib/transforms.js @@ -12,6 +12,8 @@ var hideIPA = require('./transformations/hideIPA'); var setMathFormulaImageMaxWidth = require('./transformations/setMathFormulaImageMaxWidth'); +var transforms = {}; + var NodeType = { ELEMENT : 1, @@ -29,30 +31,20 @@ //NOTATION : 12 }; -function rmComments(node) { +function _rmComments(node) { for (var n = 0; n < node.childNodes.length; n++) { var child = node.childNodes[n]; if (child.nodeType === NodeType.COMMENT) { node.removeChild(child); n--; } else if (child.nodeType === NodeType.ELEMENT) { - rmComments(child); + _rmComments(child); } } } -function rmElementsWithSelector(doc, selector) { - var removals = 0, - ps = doc.querySelectorAll(selector) || []; - for (var idx = 0; idx < ps.length; idx++) { - var node = ps[idx]; - node.parentNode.removeChild(node); - removals++; - } -} - -function rmElementsWithSelectors(doc, selectors) { - rmElementsWithSelector(doc, selectors.join(', ')); +function _rmElementsWithSelectors(doc, selectors) { + transforms.rmElementsWithSelector(doc, selectors.join(', ')); } /** @@ -61,7 +53,7 @@ * * @param doc the DOM document to be transformed */ -function rmBracketSpans(doc) { +function _rmBracketSpans(doc) { var ps = doc.querySelectorAll('span:not([class],[style],[id])') || []; for (var idx = 0; idx < ps.length; idx++) { var node = ps[idx]; @@ -72,17 +64,7 @@ } } -// Strip span tags, leaving their inner HTML in place. -function inlineSpanText(doc) { - var ps = doc.querySelectorAll('span') || []; - for (var idx = 0; idx < ps.length; idx++) { - var node = ps[idx]; - var text = doc.createTextNode(node.innerHTML); - node.parentNode.replaceChild(text, node); - } -} - -function rmAttributes(doc, selector, attributeArray) { +function _rmAttributes(doc, selector, attributeArray) { var ps = doc.querySelectorAll(selector) || []; for (var idx = 0; idx < ps.length; idx++) { var node = ps[idx]; @@ -92,7 +74,7 @@ } } -function rewriteUrlAttribute(doc, selector, attribute) { +function _rewriteUrlAttribute(doc, selector, attribute) { var ps = doc.querySelectorAll(selector) || [], value; for (var idx = 0; idx < ps.length; idx++) { @@ -105,7 +87,7 @@ } } -function addClassTo(doc, selector, className) { +function _addClassTo(doc, selector, className) { var ps = doc.querySelectorAll(selector) || []; for (var idx = 0; idx < ps.length; idx++) { var node = ps[idx]; @@ -117,7 +99,7 @@ * Removes reference link backs. * Example: <a href=\"#cite_ref-1\"><span class=\"mw-linkback-text\">↑ </span></a> */ -function removeRefLinkbacks(doc, selector, className) { +function _removeRefLinkbacks(doc, selector, className) { var ps = doc.querySelectorAll('span.mw-linkback-text') || []; for (var idx = 0; idx < ps.length; idx++) { var node = ps[idx]; @@ -128,47 +110,47 @@ } } -function removeUnwantedElements(doc, selectors) { - rmElementsWithSelectors(doc, selectors); - rmBracketSpans(doc); - rmComments(doc); +function _removeUnwantedElements(doc, selectors) { + _rmElementsWithSelectors(doc, selectors); + _rmBracketSpans(doc); + _rmComments(doc); } -function addParsoidSpecificMarkup(doc) { - rewriteUrlAttribute(doc, 'a', 'href'); +function _addParsoidSpecificMarkup(doc) { + _rewriteUrlAttribute(doc, 'a', 'href'); // Set <a class=\'external\" // on all <a rel=\"mw:ExtLink\" // so we get the 'external link' indicators in the UI. - addClassTo(doc, 'a[rel~=mw:ExtLink]', 'external'); + _addClassTo(doc, 'a[rel~=mw:ExtLink]', 'external'); // Set class="image" // on all anchors around images // so the app knows to view these images in the gallery. // See also https://www.mediawiki.org/wiki/Parsoid/MediaWiki_DOM_spec#Images - addClassTo(doc, 'figure > a, span[typeof^=mw:Image] > a', 'image'); + _addClassTo(doc, 'figure > a, span[typeof^=mw:Image] > a', 'image'); } -function applyOptionalParsoidSpecificTransformations(doc) { - rmAttributes(doc, 'a:not([rel=nofollow])', ['rel']); // only leave rel attributes with value nofollow - rmAttributes(doc, 'a', ['about', 'data-mw', 'id', 'typeof', 'title']); - rmAttributes(doc, 'div,ol,p,ul,table', ['about', 'data-mw', 'id', 'typeof']); - rmAttributes(doc, 'blockquote,cite', ['about', 'data-mw', 'id', 'typeof']); - rmAttributes(doc, 'abbr', ['title']); - rmAttributes(doc, 'figure', ['id', 'typeof']); - rmAttributes(doc, 'b,q,td,figcaption', ['id']); - rmAttributes(doc, 'figcaption a[class~=image]', ['class']); // T123527 - rmAttributes(doc, 'i', ['about', 'data-mw', 'id', 'typeof']); - rmAttributes(doc, 'li', ['about']); - rmAttributes(doc, 'img', ['alt', 'data-file-height', 'data-file-type', 'data-file-width', 'id', 'resource']); - rmAttributes(doc, 'span', ['about', 'data-file-type', 'data-mw', 'id', 'itemscope', 'itemtype', 'lang', 'rel', +function _applyOptionalParsoidSpecificTransformations(doc) { + _rmAttributes(doc, 'a:not([rel=nofollow])', ['rel']); // only leave rel attributes with value nofollow + _rmAttributes(doc, 'a', ['about', 'data-mw', 'id', 'typeof', 'title']); + _rmAttributes(doc, 'div,ol,p,ul,table', ['about', 'data-mw', 'id', 'typeof']); + _rmAttributes(doc, 'blockquote,cite', ['about', 'data-mw', 'id', 'typeof']); + _rmAttributes(doc, 'abbr', ['title']); + _rmAttributes(doc, 'figure', ['id', 'typeof']); + _rmAttributes(doc, 'b,q,td,figcaption', ['id']); + _rmAttributes(doc, 'figcaption a[class~=image]', ['class']); // T123527 + _rmAttributes(doc, 'i', ['about', 'data-mw', 'id', 'typeof']); + _rmAttributes(doc, 'li', ['about']); + _rmAttributes(doc, 'img', ['alt', 'data-file-height', 'data-file-type', 'data-file-width', 'id', 'resource']); + _rmAttributes(doc, 'span', ['about', 'data-file-type', 'data-mw', 'id', 'itemscope', 'itemtype', 'lang', 'rel', 'title', 'typeof']); } /** * Nukes stuff from the DOM we don't want. */ -function runAllSectionsTransforms(doc) { +function _runAllSectionsTransforms(doc) { var rmSelectors = [ 'span.Z3988', // Remove <span class=\"Z3988\"></span> 'span.ib-brac', // Remove <span class=\"ib-brac\"></span> @@ -190,16 +172,16 @@ //'span[style~="display:none"]', // Remove <span style=\"display:none;\"> </span> ]; - removeUnwantedElements(doc, rmSelectors); + _removeUnwantedElements(doc, rmSelectors); // Remove reference link backs since we don't want them to appear in the reference dialog; they are not needed. - removeRefLinkbacks(doc); + _removeRefLinkbacks(doc); } /** * Destructive, non-Parsoid-specific transforms previously performed in the app. */ -function removeUnwantedWikiContentForApp(doc) { +function _removeUnwantedWikiContentForApp(doc) { hideRedLinks.hideRedLinks(doc); hideIPA.hideIPA(doc); } @@ -219,41 +201,54 @@ /** * Nukes stuff from the DOM we don't want for pages from Parsoid. */ -function stripUnneededMarkup(doc) { - runAllSectionsTransforms(doc); +transforms.stripUnneededMarkup = function(doc) { + _runAllSectionsTransforms(doc); //runLeadSectionTransforms(doc); - applyOptionalParsoidSpecificTransformations(doc); + _applyOptionalParsoidSpecificTransformations(doc); - removeUnwantedWikiContentForApp(doc); -} + _removeUnwantedWikiContentForApp(doc); +}; -function addRequiredMarkup(doc) { - addParsoidSpecificMarkup(doc); +transforms.addRequiredMarkup = function(doc) { + _addParsoidSpecificMarkup(doc); setMathFormulaImageMaxWidth.setMathFormulaImageMaxWidth(doc); anchorPopUpMediaTransforms.fixVideoAnchor(doc); -} +}; /** * Nukes stuff from the DOM we don't want for the main page, which comes from mobileview. */ -function runMainPageDomTransforms(text) { +transforms.runMainPageDomTransforms = function(text) { var doc = domino.createDocument(text); - runAllSectionsTransforms(doc); + _runAllSectionsTransforms(doc); //runLeadSectionTransforms(doc); return doc.body.innerHTML; -} - -module.exports = { - NodeType: NodeType, - stripUnneededMarkup: stripUnneededMarkup, - addRequiredMarkup: addRequiredMarkup, - runMainPageDomTransforms: runMainPageDomTransforms, - rmElementsWithSelector: rmElementsWithSelector, - inlineSpanText: inlineSpanText, - - // visible for testing only: - _rmBracketSpans: rmBracketSpans, - _rmElementsWithSelectors: rmElementsWithSelectors }; + +transforms.rmElementsWithSelector = function(doc, selector) { + var removals = 0, + ps = doc.querySelectorAll(selector) || []; + for (var idx = 0; idx < ps.length; idx++) { + var node = ps[idx]; + node.parentNode.removeChild(node); + removals++; + } +}; + +// Strip span tags, leaving their inner HTML in place. +transforms.inlineSpanText = function(doc) { + var ps = doc.querySelectorAll('span') || []; + for (var idx = 0; idx < ps.length; idx++) { + var node = ps[idx]; + var text = doc.createTextNode(node.innerHTML); + node.parentNode.replaceChild(text, node); + } +}; + +// Make two internal functions visible for testing +transforms._rmBracketSpans = _rmBracketSpans; +transforms._rmElementsWithSelectors = _rmElementsWithSelectors; + +module.exports = transforms; -- To view, visit https://gerrit.wikimedia.org/r/299292 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I22654f63ec459303f592eec3d57563a9e27df259 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/mobileapps Gerrit-Branch: master Gerrit-Owner: Mholloway <mhollo...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits