Jdlrobson has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/365191 )
Change subject: Refactor: Generalise hideRedLinks ...................................................................... Refactor: Generalise hideRedLinks hideRedLinks can be generalised to flatten any kind of element This will be extremely useful for the proposed HTML summary endpoint web is planning. Additional changes: * hideRedLinks is no longer exported as an object Change-Id: I708b81b450876470a2132be985c9b53a007b78b2 --- A lib/transformations/flattenElements.js M lib/transformations/hideRedLinks.js M lib/transforms.js 3 files changed, 35 insertions(+), 9 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps refs/changes/91/365191/1 diff --git a/lib/transformations/flattenElements.js b/lib/transformations/flattenElements.js new file mode 100644 index 0000000..cb938f8 --- /dev/null +++ b/lib/transformations/flattenElements.js @@ -0,0 +1,30 @@ +/** + * DOM transformation shared with app. Let's keep this in sync with the app. + * Last sync: Android repo 3d5b441 www/js/transforms/hideRedLinks.js + * + * The main change from the original Android app file is to use + * content.createElement() instead of document.createElement(). + */ + +'use strict'; + +/** + * Replace all elements in Document `content` that match the + * css selector, replacing them with span tags. + * @param {!Document} content + * @param {?string} selector + */ +function flattenElements(content, selector) { + const elements = content.querySelectorAll(selector || 'a'); + for (let i = 0; i < elements.length; i++) { + const element = elements[i]; + const replacementSpan = content.createElement('span'); + replacementSpan.innerHTML = element.innerHTML; + if (element.getAttribute('class')) { + replacementSpan.setAttribute('class', element.getAttribute('class')); + } + element.parentNode.replaceChild(replacementSpan, element); + } +} + +module.exports = flattenElements; diff --git a/lib/transformations/hideRedLinks.js b/lib/transformations/hideRedLinks.js index b0355d6..b910196 100644 --- a/lib/transformations/hideRedLinks.js +++ b/lib/transformations/hideRedLinks.js @@ -7,16 +7,10 @@ */ 'use strict'; +const flattenElements = require('./flattenElements'); function hideRedLinks(content) { - const redLinks = content.querySelectorAll('a.new'); - for (let i = 0; i < redLinks.length; i++) { - const redLink = redLinks[i]; - const replacementSpan = content.createElement('span'); - replacementSpan.innerHTML = redLink.innerHTML; - replacementSpan.setAttribute('class', redLink.getAttribute('class')); - redLink.parentNode.replaceChild(replacementSpan, redLink); - } + flattenElements(content, 'a.new'); } module.exports = { diff --git a/lib/transforms.js b/lib/transforms.js index 23d0248..be7ff1e 100644 --- a/lib/transforms.js +++ b/lib/transforms.js @@ -15,6 +15,7 @@ const extractInfobox = require('./transformations/extractInfobox'); const extractPageIssues = require('./transformations/extractPageIssues'); const extractLeadIntroduction = require('./transformations/extractLeadIntroduction'); +const flattenElements = require('./transformations/flattenElements'); const transforms = {}; @@ -271,7 +272,7 @@ * Destructive, non-Parsoid-specific transforms previously performed in the app. */ function _removeUnwantedWikiContentForApp(doc, legacy) { - hideRedLinks.hideRedLinks(doc); + hideRedLinks(doc); hideIPA.hideIPA(doc, legacy); } @@ -341,5 +342,6 @@ transforms.extractInfobox = extractInfobox; transforms.extractPageIssues = extractPageIssues; transforms.extractLeadIntroduction = extractLeadIntroduction; +transforms.flattenElements = flattenElements; module.exports = transforms; -- To view, visit https://gerrit.wikimedia.org/r/365191 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I708b81b450876470a2132be985c9b53a007b78b2 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/mobileapps Gerrit-Branch: master Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits