diff --git a/lib/ext.Cite.js b/lib/ext.Cite.js index 2cf17db14..c0336bb45 100644 --- a/lib/ext.Cite.js +++ b/lib/ext.Cite.js @@ -2,26 +2,27 @@ * This file implements and extension tag handling * natively in Parsoid. * ---------------------------------------------------------------------- */ -"use strict"; +'use strict'; require('./core-upgrade.js'); -var Util = require( './mediawiki.Util.js' ).Util, - DU = require( './mediawiki.DOMUtils.js').DOMUtils, - coreutil = require('util'), - defines = require('./mediawiki.parser.defines.js'), - entities = require('entities'); +var Util = require( './mediawiki.Util.js' ).Util; +var DU = require( './mediawiki.DOMUtils.js').DOMUtils; +var coreutil = require('util'); +var defines = require('./mediawiki.parser.defines.js'); +var entities = require('entities'); // define some constructor shortcuts -var KV = defines.KV, - EOFTk = defines.EOFTk, - SelfclosingTagTk = defines.SelfclosingTagTk; +var KV = defines.KV; +var EOFTk = defines.EOFTk; +var SelfclosingTagTk = defines.SelfclosingTagTk; + // FIXME: Move out to some common helper file? // Helper function to process extension source function processExtSource(manager, extToken, opts) { - var extSrc = extToken.getAttribute('source'), - tagWidths = extToken.dataAttribs.tagWidths, - content = extSrc.substring(tagWidths[0], extSrc.length - tagWidths[1]); + var extSrc = extToken.getAttribute('source'); + var tagWidths = extToken.dataAttribs.tagWidths; + var content = extSrc.substring(tagWidths[0], extSrc.length - tagWidths[1]); // FIXME: Should this be specific to the extension // Or is it okay to do this unconditionally for all? @@ -32,8 +33,8 @@ function processExtSource(manager, extToken, opts) { // FIXME: SSS: This stripping maybe be unnecessary after all. // // Strip all leading white-space - var wsMatch = content.match(/^(\s*)([^]*)$/), - leadingWS = wsMatch[1]; + var wsMatch = content.match(/^(\s*)([^]*)$/); + var leadingWS = wsMatch[1]; // Update content to normalized form content = wsMatch[2]; @@ -102,27 +103,27 @@ Ref.prototype.handleRef = function( manager, pipelineOpts, refTok, cb ) { } var refOpts = Object.assign({ - name: null, group: null - }, Util.KVtoHash(refTok.getAttribute("options"), true)), - about = manager.env.newAboutId(), - finalCB = function(toks, contentBody) { - // Marker meta with ref content - var da = Util.clone(refTok.dataAttribs); - // Clear stx='html' so that sanitizer doesn't barf - da.stx = undefined; - da.group = refOpts.group || ''; - da.name = refOpts.name || ''; - da.content = contentBody ? DU.serializeChildren(contentBody) : ''; - da.hasRefInRef = contentBody ? hasRef(contentBody) : false; + name: null, + group: null, + }, Util.KVtoHash(refTok.getAttribute("options"), true)); - toks.push(new SelfclosingTagTk( 'meta', [ - new KV('typeof', 'mw:Extension/ref/Marker'), - new KV('about', about) - ], da)); - - // All done! - cb({tokens: toks, async: false}); - }; + var about = manager.env.newAboutId(); + var finalCB = function(toks, contentBody) { + // Marker meta with ref content + var da = Util.clone(refTok.dataAttribs); + // Clear stx='html' so that sanitizer doesn't barf + da.stx = undefined; + da.group = refOpts.group || ''; + da.name = refOpts.name || ''; + da.content = contentBody ? DU.serializeChildren(contentBody) : ''; + da.hasRefInRef = contentBody ? hasRef(contentBody) : false; + toks.push(new SelfclosingTagTk('meta', [ + new KV('typeof', 'mw:Extension/ref/Marker'), + new KV('about', about) + ], da)); + // All done! + cb({ tokens: toks, async: false }); + }; processExtSource(manager, refTok, { // Full pipeline for processing ref-content @@ -162,18 +163,17 @@ function makeValidIdAttr(val) { } RefGroup.prototype.renderLine = function(refsList, ref) { - var ownerDoc = refsList.ownerDocument, - arrow = ownerDoc.createTextNode('↑'), - li, a, textSpan; + var ownerDoc = refsList.ownerDocument; + var arrow = ownerDoc.createTextNode('↑'); // Generate the li and set ref content first, so the HTML gets parsed. // We then append the rest of the ref nodes before the first node - li = ownerDoc.createElement('li'); + var li = ownerDoc.createElement('li'); DU.addAttributes(li, { 'about': "#" + ref.target, 'id': ref.target }); - textSpan = ownerDoc.createElement('span'); + var textSpan = ownerDoc.createElement('span'); DU.addAttributes(textSpan, { 'id': "mw-reference-text-" + ref.target, 'class': "mw-reference-text" @@ -186,6 +186,7 @@ RefGroup.prototype.renderLine = function(refsList, ref) { span.setAttribute('rel', 'mw:referencedBy'); li.insertBefore(span, textSpan); + var a; // Generate leading linkbacks if (ref.linkbacks.length === 1) { a = ownerDoc.createElement('a'); @@ -236,13 +237,13 @@ ReferencesData.prototype.removeRefGroup = function(groupName) { }; ReferencesData.prototype.add = function(groupName, refName, about, skipLinkback) { - var group = this.getRefGroup(groupName, true), - ref; + var group = this.getRefGroup(groupName, true); + var ref; refName = makeValidIdAttr(refName); - if ( refName && group.indexByName.has( refName ) ) { - ref = group.indexByName.get( refName ); + if (refName && group.indexByName.has(refName)) { + ref = group.indexByName.get(refName); if (ref.content) { - ref.hasMultiples = true; + ref.hasMultiples = true; } } else { // The ids produced Cite.php have some particulars: @@ -250,10 +251,10 @@ ReferencesData.prototype.add = function(groupName, refName, about, skipLinkback) // Refs with names get 'cite_ref-' + name + '_' + index + (backlink num || 0) // Notes (references) whose ref doesn't have a name are 'cite_note-' + index // Notes whose ref has a name are 'cite_note-' + name + '-' + index - var n = this.index, - refKey = (1 + n) + '', - refIdBase = 'cite_ref-' + (refName ? refName + '_' + refKey : refKey), - noteId = 'cite_note-' + (refName ? refName + '-' + refKey : refKey); + var n = this.index; + var refKey = (1 + n) + ''; + var refIdBase = 'cite_ref-' + (refName ? refName + '_' + refKey : refKey); + var noteId = 'cite_note-' + (refName ? refName + '-' + refKey : refKey); // bump index this.index += 1; @@ -360,23 +361,23 @@ References.prototype.handleReferences = function( manager, pipelineOpts, refsTok References.prototype.extractRefFromNode = function(node, refsData, refInRefProcessor, referencesAboutId, referencesGroup, refsInReferencesHTML) { - var nestedInReferences = referencesAboutId !== undefined, - dp = DU.getDataParsoid( node ), - // SSS FIXME: Need to clarify semantics here. - // If both the containing elt as well as the nested elt has - // a group attribute, what takes precedence? - group = dp.group || referencesGroup || '', - refName = dp.name, - about = node.getAttribute("about"), - ref = refsData.add(group, refName, about, nestedInReferences), - nodeType = (node.getAttribute("typeof") || '').replace(/mw:Extension\/ref\/Marker/, ''); + var nestedInReferences = referencesAboutId !== undefined; + var dp = DU.getDataParsoid(node); + // SSS FIXME: Need to clarify semantics here. + // If both the containing elt as well as the nested + // elt has a group attribute, what takes precedence? + var group = dp.group || referencesGroup || ''; + var refName = dp.name; + var about = node.getAttribute("about"); + var ref = refsData.add(group, refName, about, nestedInReferences); + var nodeType = (node.getAttribute("typeof") || '').replace(/mw:Extension\/ref\/Marker/, ''); // Add ref-index linkback - var doc = node.ownerDocument, - span = doc.createElement('span'), - content = dp.content, - dataMW = Util.clone(DU.getDataMw(node)), - body; + var doc = node.ownerDocument; + var span = doc.createElement('span'); + var content = dp.content; + var dataMW = Util.clone(DU.getDataMw(node)); + var body; if (dp.hasRefInRef) { var html = DU.parseHTML(content).body; @@ -451,13 +452,13 @@ References.prototype.extractRefFromNode = function(node, refsData, }; References.prototype.insertReferencesIntoDOM = function(refsNode, refsData, refsInReferencesHTML) { - var about = refsNode.getAttribute('about'), - dp = DU.getDataParsoid( refsNode ), - group = dp.group || '', - src = dp.src || '', // fall back so we don't crash - // Extract ext-source for .. usage - body = Util.extractExtBody("references", src).trim(), - refGroup = refsData.getRefGroup(group); + var about = refsNode.getAttribute('about'); + var dp = DU.getDataParsoid(refsNode); + var group = dp.group || ''; + var src = dp.src || ''; // fall back so we don't crash + // Extract ext-source for .. usage + var body = Util.extractExtBody("references", src).trim(); + var refGroup = refsData.getRefGroup(group); var dataMW = DU.getDataMw(refsNode); if (!Object.keys(dataMW).length) { @@ -501,12 +502,12 @@ References.prototype.insertReferencesIntoDOM = function(refsNode, refsData, refs }; References.prototype.insertMissingReferencesIntoDOM = function(env, refsData, node) { - var doc = node.ownerDocument, - self = this; + var doc = node.ownerDocument; + var self = this; refsData.refGroups.forEach(function(refsValue, refsGroup) { - var ol = doc.createElement('ol'), - dp = DU.getDataParsoid(ol); + var ol = doc.createElement('ol'); + var dp = DU.getDataParsoid(ol); DU.addAttributes(ol, { 'class': 'references', typeof: 'mw:Extension/references',