From fd719e02e2a878fa09cdd2f934491c36fe33d8f0 Mon Sep 17 00:00:00 2001 From: "C. Scott Ananian" Date: Fri, 16 Feb 2018 15:46:15 -0500 Subject: [PATCH] Use Promise.async/yield in lib/ext/Cite Change-Id: I08458b0083de8d9aec9673af7f08c4b2e25112e5 --- lib/ext/Cite/index.js | 148 ++++++++++++++++++++---------------------- 1 file changed, 71 insertions(+), 77 deletions(-) diff --git a/lib/ext/Cite/index.js b/lib/ext/Cite/index.js index 1a3f8c8d8..d02a1733a 100644 --- a/lib/ext/Cite/index.js +++ b/lib/ext/Cite/index.js @@ -113,71 +113,68 @@ Ref.prototype.tokenHandler = function(manager, pipelineOpts, refTok, cb) { }; Ref.prototype.serialHandler = { - handle: Promise.method(function(node, state, wrapperUnmodified) { - return state.serializer.serializeExtensionStartTag(node, state) - .then(function(startTagSrc) { - var dataMw = DU.getDataMw(node); - var env = state.env; - var html; - if (!dataMw.body) { - return startTagSrc; // We self-closed this already. - } else if (typeof dataMw.body.html === 'string') { - // First look for the extension's content in data-mw.body.html - html = dataMw.body.html; - } else if (typeof dataMw.body.id === 'string') { - // If the body isn't contained in data-mw.body.html, look if - // there's an element pointed to by body.id. - var bodyElt = node.ownerDocument.getElementById(dataMw.body.id); - if (!bodyElt && env.page.editedDoc) { - // Try to get to it from the main page. - // This can happen when the is inside another - // extension, most commonly inside a . - // The recursive call to serializeDOM puts us inside - // inside a new document. - bodyElt = env.page.editedDoc.getElementById(dataMw.body.id); - } - if (bodyElt) { - // n.b. this is going to drop any diff markers but since - // the dom differ doesn't traverse into extension content - // none should exist anyways. - html = DU.ppToXML(bodyElt, { innerXML: true }); - } else { - // Some extra debugging for VisualEditor - var extraDebug = ''; - var firstA = node.querySelector('a[href]'); - if (firstA && /^#/.test(firstA.getAttribute('href'))) { - var href = firstA.getAttribute('href'); - try { - var ref = node.ownerDocument.querySelector(href); - if (ref) { - extraDebug += ' [own doc: ' + ref.outerHTML + ']'; - } - ref = env.page.editedDoc.querySelector(href); - if (ref) { - extraDebug += ' [main doc: ' + ref.outerHTML + ']'; - } - } catch (e) { } // eslint-disable-line - if (!extraDebug) { - extraDebug = ' [reference ' + href + ' not found]'; + handle: Promise.async(function *(node, state, wrapperUnmodified) { + var startTagSrc = yield state.serializer.serializeExtensionStartTag(node, state); + var dataMw = DU.getDataMw(node); + var env = state.env; + var html; + if (!dataMw.body) { + return startTagSrc; // We self-closed this already. + } else if (typeof dataMw.body.html === 'string') { + // First look for the extension's content in data-mw.body.html + html = dataMw.body.html; + } else if (typeof dataMw.body.id === 'string') { + // If the body isn't contained in data-mw.body.html, look if + // there's an element pointed to by body.id. + var bodyElt = node.ownerDocument.getElementById(dataMw.body.id); + if (!bodyElt && env.page.editedDoc) { + // Try to get to it from the main page. + // This can happen when the is inside another + // extension, most commonly inside a . + // The recursive call to serializeDOM puts us inside + // inside a new document. + bodyElt = env.page.editedDoc.getElementById(dataMw.body.id); + } + if (bodyElt) { + // n.b. this is going to drop any diff markers but since + // the dom differ doesn't traverse into extension content + // none should exist anyways. + html = DU.ppToXML(bodyElt, { innerXML: true }); + } else { + // Some extra debugging for VisualEditor + var extraDebug = ''; + var firstA = node.querySelector('a[href]'); + if (firstA && /^#/.test(firstA.getAttribute('href'))) { + var href = firstA.getAttribute('href'); + try { + var ref = node.ownerDocument.querySelector(href); + if (ref) { + extraDebug += ' [own doc: ' + ref.outerHTML + ']'; } + ref = env.page.editedDoc.querySelector(href); + if (ref) { + extraDebug += ' [main doc: ' + ref.outerHTML + ']'; + } + } catch (e) { } // eslint-disable-line + if (!extraDebug) { + extraDebug = ' [reference ' + href + ' not found]'; } - env.log('error/' + dataMw.name, + } + env.log('error/' + dataMw.name, 'extension src id ' + dataMw.body.id + ' points to non-existent element for:', node.outerHTML, '. More debug info: ', extraDebug); - return ''; // Drop it! - } - } else { - env.log('error', 'Ref body unavailable for: ' + node.outerHTML); return ''; // Drop it! } - return state.serializer.serializeHTML({ - env: state.env, - extName: dataMw.name, - }, html).then(function(src) { - return startTagSrc + src + ''; - }); - }); + } else { + env.log('error', 'Ref body unavailable for: ' + node.outerHTML); + return ''; // Drop it! + } + var src = yield state.serializer.serializeHTML({ + env: state.env, + extName: dataMw.name, + }, html); + return startTagSrc + src + ''; }), }; @@ -664,29 +661,26 @@ References.prototype.insertMissingReferencesIntoDOM = function(refsData, node) { }; References.prototype.serialHandler = { - handle: Promise.method(function(node, state, wrapperUnmodified) { + handle: Promise.async(function *(node, state, wrapperUnmodified) { var dataMw = DU.getDataMw(node); if (dataMw.autoGenerated && state.rtTestMode) { // Eliminate auto-inserted noise in rt-testing - return Promise.resolve(''); + return ''; } else { - return state.serializer.serializeExtensionStartTag(node, state) - .then(function(startTagSrc) { - if (!dataMw.body) { - return startTagSrc; // We self-closed this already. - } else if (typeof dataMw.body.html === 'string') { - return state.serializer.serializeHTML({ - env: state.env, - extName: dataMw.name, - }, dataMw.body.html).then(function(src) { - return startTagSrc + src + ''; - }); - } else { - state.env.log('error', - 'References body unavailable for: ' + node.outerHTML); - return ''; // Drop it! - } - }); + var startTagSrc = yield state.serializer.serializeExtensionStartTag(node, state); + if (!dataMw.body) { + return startTagSrc; // We self-closed this already. + } else if (typeof dataMw.body.html === 'string') { + var src = yield state.serializer.serializeHTML({ + env: state.env, + extName: dataMw.name, + }, dataMw.body.html); + return startTagSrc + src + ''; + } else { + state.env.log('error', + 'References body unavailable for: ' + node.outerHTML); + return ''; // Drop it! + } } }), before: function(node, otherNode, state) {