2018-02-13 17:19:32 +00:00
|
|
|
/**
|
2018-02-27 20:31:30 +00:00
|
|
|
* This module implements `<ref>` and `<references>` extension tag handling
|
2016-01-13 23:34:40 +00:00
|
|
|
* natively in Parsoid.
|
2018-02-13 17:19:32 +00:00
|
|
|
* @module ext/Cite
|
|
|
|
*/
|
2017-04-27 17:45:54 +00:00
|
|
|
|
2016-01-13 23:34:40 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-01-31 02:38:30 +00:00
|
|
|
const ParsoidExtApi = module.parent.require('./extapi.js').versionCheck('^0.10.0');
|
|
|
|
const {
|
|
|
|
ContentUtils,
|
|
|
|
DOMDataUtils,
|
|
|
|
DOMUtils,
|
|
|
|
TokenUtils,
|
|
|
|
WTUtils,
|
|
|
|
Promise,
|
|
|
|
Sanitizer,
|
|
|
|
} = ParsoidExtApi;
|
2016-01-13 23:34:40 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-27 20:31:30 +00:00
|
|
|
* Simple token transform version of the Ref extension tag.
|
2016-01-13 23:34:40 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
function Ref(cite) {
|
|
|
|
this.cite = cite;
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasRef(node) {
|
|
|
|
var c = node.firstChild;
|
|
|
|
while (c) {
|
2018-10-31 20:05:24 +00:00
|
|
|
if (DOMUtils.isElt(c)) {
|
|
|
|
if (WTUtils.isSealedFragmentOfType(c, 'ref')) {
|
2016-01-13 23:34:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (hasRef(c)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c = c.nextSibling;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-09 04:37:17 +00:00
|
|
|
Ref.prototype.toDOM = function(state, content, args) {
|
2017-12-19 19:09:11 +00:00
|
|
|
// Drop nested refs entirely, unless we've explicitly allowed them
|
2018-08-07 13:27:19 +00:00
|
|
|
if (state.parseContext.extTag === 'ref' &&
|
|
|
|
!(state.parseContext.extTagOpts && state.parseContext.extTagOpts.allowNestedRef)
|
|
|
|
) {
|
2018-06-09 04:37:17 +00:00
|
|
|
return null;
|
2017-12-19 19:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The one supported case for nested refs is from the {{#tag:ref}} parser
|
|
|
|
// function. However, we're overly permissive here since we can't
|
2018-06-09 04:37:17 +00:00
|
|
|
// distinguish when that's nested in another template.
|
|
|
|
// The php preprocessor did our expansion.
|
|
|
|
const allowNestedRef = state.parseContext.inTemplate && state.parseContext.extTag !== 'ref';
|
|
|
|
|
2018-11-19 17:02:29 +00:00
|
|
|
return ParsoidExtApi.parseTokenContentsToDOM(state, args, '', content, {
|
2018-06-09 04:37:17 +00:00
|
|
|
// NOTE: sup's content model requires it only contain phrasing
|
|
|
|
// content, not flow content. However, since we are building an
|
|
|
|
// in-memory DOM which is simply a tree data structure, we can
|
|
|
|
// nest flow content in a <sup> tag.
|
|
|
|
wrapperTag: 'sup',
|
|
|
|
inTemplate: state.parseContext.inTemplate,
|
2018-08-07 13:27:19 +00:00
|
|
|
extTag: 'ref',
|
|
|
|
extTagOpts: {
|
|
|
|
allowNestedRef: !!allowNestedRef,
|
|
|
|
},
|
2018-09-13 15:23:17 +00:00
|
|
|
// FIXME: One-off PHP parser state leak.
|
|
|
|
// This needs a better solution.
|
|
|
|
inPHPBlock: true,
|
2016-01-13 23:34:40 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-01-26 21:17:54 +00:00
|
|
|
Ref.prototype.serialHandler = {
|
2018-02-16 20:46:15 +00:00
|
|
|
handle: Promise.async(function *(node, state, wrapperUnmodified) {
|
|
|
|
var startTagSrc = yield state.serializer.serializeExtensionStartTag(node, state);
|
2018-10-31 20:05:24 +00:00
|
|
|
var dataMw = DOMDataUtils.getDataMw(node);
|
2018-02-16 20:46:15 +00:00
|
|
|
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 <ref> is inside another
|
|
|
|
// extension, most commonly inside a <references>.
|
|
|
|
// 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.
|
2019-02-19 18:05:10 +00:00
|
|
|
DOMDataUtils.visitAndStoreDataAttribs(bodyElt);
|
|
|
|
html = ContentUtils.toXML(bodyElt, { innerXML: true });
|
|
|
|
DOMDataUtils.visitAndLoadDataAttribs(bodyElt);
|
2018-02-16 20:46:15 +00:00
|
|
|
} 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 + ']';
|
2016-01-26 21:17:54 +00:00
|
|
|
}
|
2018-02-16 20:46:15 +00:00
|
|
|
} catch (e) { } // eslint-disable-line
|
|
|
|
if (!extraDebug) {
|
|
|
|
extraDebug = ' [reference ' + href + ' not found]';
|
2016-01-26 21:17:54 +00:00
|
|
|
}
|
2018-02-16 20:46:15 +00:00
|
|
|
}
|
|
|
|
env.log('error/' + dataMw.name,
|
2016-01-26 21:17:54 +00:00
|
|
|
'extension src id ' + dataMw.body.id +
|
|
|
|
' points to non-existent element for:', node.outerHTML,
|
|
|
|
'. More debug info: ', extraDebug);
|
|
|
|
return ''; // Drop it!
|
|
|
|
}
|
2018-02-16 20:46:15 +00:00
|
|
|
} 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,
|
2018-08-24 21:34:26 +00:00
|
|
|
// FIXME: One-off PHP parser state leak.
|
|
|
|
// This needs a better solution.
|
|
|
|
inPHPBlock: true,
|
2018-02-16 20:46:15 +00:00
|
|
|
}, html);
|
|
|
|
return startTagSrc + src + '</' + dataMw.name + '>';
|
2016-01-26 21:17:54 +00:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2017-12-18 16:53:25 +00:00
|
|
|
Ref.prototype.lintHandler = function(ref, env, tplInfo, domLinter) {
|
2018-01-22 23:37:48 +00:00
|
|
|
// Don't lint the content of ref in ref, since it can lead to cycles
|
|
|
|
// using named refs
|
2018-10-31 20:05:24 +00:00
|
|
|
if (WTUtils.fromExtensionContent(ref, 'references')) { return ref.nextNode; }
|
2018-01-22 23:37:48 +00:00
|
|
|
|
2017-12-18 16:53:25 +00:00
|
|
|
var linkBackId = ref.firstChild.getAttribute('href').replace(/[^#]*#/, '');
|
2018-01-08 21:56:57 +00:00
|
|
|
var refNode = ref.ownerDocument.getElementById(linkBackId);
|
|
|
|
if (refNode) {
|
|
|
|
// Ex: Buggy input wikitext without ref content
|
|
|
|
domLinter(refNode.lastChild, env, tplInfo.isTemplated ? tplInfo : null);
|
|
|
|
}
|
2017-12-18 16:53:25 +00:00
|
|
|
return ref.nextNode;
|
|
|
|
};
|
|
|
|
|
2016-01-13 23:34:40 +00:00
|
|
|
/**
|
2018-02-27 20:31:30 +00:00
|
|
|
* Helper class used by `<references>` implementation.
|
2018-02-13 17:19:32 +00:00
|
|
|
* @class
|
2016-01-13 23:34:40 +00:00
|
|
|
*/
|
|
|
|
function RefGroup(group) {
|
|
|
|
this.name = group || '';
|
|
|
|
this.refs = [];
|
|
|
|
this.indexByName = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeValidIdAttr(val) {
|
|
|
|
// Looks like Cite.php doesn't try to fix ids that already have
|
|
|
|
// a "_" in them. Ex: name="a b" and name="a_b" are considered
|
|
|
|
// identical. Not sure if this is a feature or a bug.
|
2017-09-12 22:19:06 +00:00
|
|
|
// It also considers entities equal to their encoding
|
|
|
|
// (i.e. '&' === '&'), which is done:
|
|
|
|
// in PHP: Sanitizer#decodeTagAttributes and
|
|
|
|
// in Parsoid: ExtensionHandler#normalizeExtOptions
|
|
|
|
return Sanitizer.escapeIdForAttribute(val);
|
2016-01-13 23:34:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 23:00:17 +00:00
|
|
|
RefGroup.prototype.renderLine = function(env, refsList, ref) {
|
2016-01-13 23:34:40 +00:00
|
|
|
var ownerDoc = refsList.ownerDocument;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
var li = ownerDoc.createElement('li');
|
2018-10-31 20:05:24 +00:00
|
|
|
DOMDataUtils.addAttributes(li, {
|
2016-01-13 23:34:40 +00:00
|
|
|
'about': "#" + ref.target,
|
|
|
|
'id': ref.target,
|
2018-08-17 20:01:37 +00:00
|
|
|
'class': ['rtl', 'ltr'].includes(ref.dir) ? 'mw-cite-dir-' + ref.dir : undefined,
|
2016-01-13 23:34:40 +00:00
|
|
|
});
|
|
|
|
var reftextSpan = ownerDoc.createElement('span');
|
2018-10-31 20:05:24 +00:00
|
|
|
DOMDataUtils.addAttributes(reftextSpan, {
|
2016-01-13 23:34:40 +00:00
|
|
|
'id': "mw-reference-text-" + ref.target,
|
|
|
|
'class': "mw-reference-text",
|
|
|
|
});
|
2017-10-20 03:01:48 +00:00
|
|
|
if (ref.content) {
|
2018-06-09 04:37:17 +00:00
|
|
|
var content = env.fragmentMap.get(ref.content)[0];
|
2018-10-31 20:05:24 +00:00
|
|
|
DOMUtils.migrateChildrenBetweenDocs(content, reftextSpan);
|
2018-12-17 23:04:23 +00:00
|
|
|
DOMDataUtils.visitAndLoadDataAttribs(reftextSpan);
|
2017-10-20 03:01:48 +00:00
|
|
|
}
|
2016-01-13 23:34:40 +00:00
|
|
|
li.appendChild(reftextSpan);
|
|
|
|
|
|
|
|
// Generate leading linkbacks
|
|
|
|
var createLinkback = function(href, group, text) {
|
|
|
|
var a = ownerDoc.createElement('a');
|
2017-04-25 14:41:15 +00:00
|
|
|
var s = ownerDoc.createElement('span');
|
2016-01-13 23:34:40 +00:00
|
|
|
var textNode = ownerDoc.createTextNode(text + " ");
|
2016-12-16 23:00:17 +00:00
|
|
|
a.setAttribute('href', env.page.titleURI + '#' + href);
|
2017-04-25 14:41:15 +00:00
|
|
|
s.setAttribute('class', 'mw-linkback-text');
|
2016-01-13 23:34:40 +00:00
|
|
|
if (group) {
|
|
|
|
a.setAttribute('data-mw-group', group);
|
|
|
|
}
|
2017-04-25 14:41:15 +00:00
|
|
|
s.appendChild(textNode);
|
|
|
|
a.appendChild(s);
|
2016-01-13 23:34:40 +00:00
|
|
|
return a;
|
|
|
|
};
|
|
|
|
if (ref.linkbacks.length === 1) {
|
2016-12-16 23:00:17 +00:00
|
|
|
var linkback = createLinkback(ref.id, ref.group, '↑');
|
2016-01-13 23:34:40 +00:00
|
|
|
linkback.setAttribute('rel', 'mw:referencedBy');
|
|
|
|
li.insertBefore(linkback, reftextSpan);
|
|
|
|
} else {
|
|
|
|
// 'mw:referencedBy' span wrapper
|
|
|
|
var span = ownerDoc.createElement('span');
|
|
|
|
span.setAttribute('rel', 'mw:referencedBy');
|
|
|
|
li.insertBefore(span, reftextSpan);
|
|
|
|
|
|
|
|
ref.linkbacks.forEach(function(lb, i) {
|
2016-12-16 23:00:17 +00:00
|
|
|
span.appendChild(createLinkback(lb, ref.group, i + 1));
|
2016-01-13 23:34:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Space before content node
|
|
|
|
li.insertBefore(ownerDoc.createTextNode(' '), reftextSpan);
|
|
|
|
|
|
|
|
// Add it to the ref list
|
|
|
|
refsList.appendChild(li);
|
|
|
|
};
|
|
|
|
|
2018-02-13 17:19:32 +00:00
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
*/
|
2016-12-16 23:00:17 +00:00
|
|
|
function ReferencesData(env) {
|
2016-01-13 23:34:40 +00:00
|
|
|
this.index = 0;
|
2016-12-16 23:00:17 +00:00
|
|
|
this.env = env;
|
2016-01-13 23:34:40 +00:00
|
|
|
this.refGroups = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
ReferencesData.prototype.getRefGroup = function(groupName, allocIfMissing) {
|
|
|
|
groupName = groupName || '';
|
|
|
|
if (!this.refGroups.has(groupName) && allocIfMissing) {
|
|
|
|
this.refGroups.set(groupName, new RefGroup(groupName));
|
|
|
|
}
|
|
|
|
return this.refGroups.get(groupName);
|
|
|
|
};
|
|
|
|
|
|
|
|
ReferencesData.prototype.removeRefGroup = function(groupName) {
|
|
|
|
if (groupName !== null && groupName !== undefined) {
|
|
|
|
// '' is a valid group (the default group)
|
|
|
|
this.refGroups.delete(groupName);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-20 03:01:48 +00:00
|
|
|
ReferencesData.prototype.add = function(env, groupName, refName, about, skipLinkback) {
|
2016-01-13 23:34:40 +00:00
|
|
|
var group = this.getRefGroup(groupName, true);
|
|
|
|
refName = makeValidIdAttr(refName);
|
2017-10-20 03:01:48 +00:00
|
|
|
|
|
|
|
var ref;
|
2016-01-13 23:34:40 +00:00
|
|
|
if (refName && group.indexByName.has(refName)) {
|
|
|
|
ref = group.indexByName.get(refName);
|
2018-08-16 23:34:04 +00:00
|
|
|
if (ref.content && !ref.hasMultiples) {
|
2016-01-13 23:34:40 +00:00
|
|
|
ref.hasMultiples = true;
|
2017-10-20 03:01:48 +00:00
|
|
|
// Use the non-pp version here since we've already stored attribs
|
|
|
|
// before putting them in the map.
|
2018-10-31 20:05:24 +00:00
|
|
|
ref.cachedHtml = ContentUtils.toXML(env.fragmentMap.get(ref.content)[0], { innerXML: true });
|
2016-01-13 23:34:40 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The ids produced Cite.php have some particulars:
|
|
|
|
// Simple refs get 'cite_ref-' + index
|
|
|
|
// 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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
ref = {
|
|
|
|
about: about,
|
|
|
|
content: null,
|
2018-08-17 20:01:37 +00:00
|
|
|
dir: '',
|
2016-01-13 23:34:40 +00:00
|
|
|
group: group.name,
|
|
|
|
groupIndex: group.refs.length + 1,
|
|
|
|
index: n,
|
|
|
|
key: refIdBase,
|
|
|
|
id: (refName ? refIdBase + '-0' : refIdBase),
|
|
|
|
linkbacks: [],
|
|
|
|
name: refName,
|
|
|
|
target: noteId,
|
2017-10-20 03:01:48 +00:00
|
|
|
hasMultiples: false,
|
|
|
|
// Just used for comparison when we have multiples
|
|
|
|
cachedHtml: '',
|
2016-01-13 23:34:40 +00:00
|
|
|
};
|
|
|
|
group.refs.push(ref);
|
|
|
|
if (refName) {
|
|
|
|
group.indexByName.set(refName, ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!skipLinkback) {
|
|
|
|
ref.linkbacks.push(ref.key + '-' + ref.linkbacks.length);
|
|
|
|
}
|
|
|
|
return ref;
|
|
|
|
};
|
|
|
|
|
2018-02-13 17:19:32 +00:00
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
*/
|
2016-01-13 23:34:40 +00:00
|
|
|
function References(cite) {
|
|
|
|
this.cite = cite;
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:23:04 +00:00
|
|
|
var createReferences = function(env, doc, body, refsOpts, modifyDp, autoGenerated) {
|
2018-01-05 21:52:58 +00:00
|
|
|
var ol = doc.createElement('ol');
|
2018-01-05 23:04:54 +00:00
|
|
|
ol.classList.add('mw-references');
|
|
|
|
ol.classList.add('references');
|
|
|
|
|
2018-01-05 21:52:58 +00:00
|
|
|
if (body) {
|
2018-10-31 20:05:24 +00:00
|
|
|
DOMUtils.migrateChildren(body, ol);
|
2018-01-05 21:52:58 +00:00
|
|
|
}
|
2018-01-05 23:04:54 +00:00
|
|
|
|
|
|
|
// Support the `responsive` parameter
|
|
|
|
var rrOpts = env.conf.wiki.responsiveReferences;
|
|
|
|
var responsiveWrap = rrOpts.enabled;
|
|
|
|
if (refsOpts.responsive !== null) {
|
|
|
|
responsiveWrap = refsOpts.responsive !== '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
var frag;
|
|
|
|
if (responsiveWrap) {
|
|
|
|
var div = doc.createElement('div');
|
|
|
|
div.classList.add('mw-references-wrap');
|
|
|
|
div.appendChild(ol);
|
|
|
|
frag = div;
|
|
|
|
} else {
|
|
|
|
frag = ol;
|
|
|
|
}
|
|
|
|
|
2018-06-09 04:37:17 +00:00
|
|
|
if (autoGenerated) {
|
2018-10-31 20:05:24 +00:00
|
|
|
DOMDataUtils.addAttributes(frag, {
|
2018-06-09 04:37:17 +00:00
|
|
|
typeof: 'mw:Extension/references',
|
|
|
|
about: env.newAboutId(),
|
|
|
|
});
|
|
|
|
}
|
2018-01-05 23:04:54 +00:00
|
|
|
|
2018-10-31 20:05:24 +00:00
|
|
|
var dp = DOMDataUtils.getDataParsoid(frag);
|
2018-01-05 21:52:58 +00:00
|
|
|
if (refsOpts.group) { // No group for the empty string either
|
|
|
|
dp.group = refsOpts.group;
|
|
|
|
ol.setAttribute('data-mw-group', refsOpts.group);
|
|
|
|
}
|
|
|
|
if (typeof modifyDp === 'function') {
|
|
|
|
modifyDp(dp);
|
|
|
|
}
|
|
|
|
|
2018-01-05 23:04:54 +00:00
|
|
|
return frag;
|
2018-01-05 21:52:58 +00:00
|
|
|
};
|
|
|
|
|
2018-06-09 04:37:17 +00:00
|
|
|
References.prototype.toDOM = function(state, content, args) {
|
2018-11-19 17:02:29 +00:00
|
|
|
return ParsoidExtApi.parseTokenContentsToDOM(state, args, '', content, {
|
2018-06-09 04:37:17 +00:00
|
|
|
wrapperTag: 'div',
|
|
|
|
extTag: 'references',
|
|
|
|
inTemplate: state.parseContext.inTemplate,
|
|
|
|
}).then(function(doc) {
|
|
|
|
var refsOpts = Object.assign({
|
|
|
|
group: null,
|
|
|
|
responsive: null,
|
2018-10-30 22:58:14 +00:00
|
|
|
}, TokenUtils.kvToHash(args, true));
|
2018-06-09 04:37:17 +00:00
|
|
|
|
2019-02-15 19:23:04 +00:00
|
|
|
var frag = createReferences(state.env, doc, doc.body, refsOpts, function(dp) {
|
2018-06-09 04:37:17 +00:00
|
|
|
dp.src = state.extToken.getAttribute('source');
|
2018-08-07 13:47:24 +00:00
|
|
|
// Redundant - also present on doc.body.firstChild, but feels cumbersome to use
|
2018-08-07 04:31:53 +00:00
|
|
|
dp.selfClose = state.extToken.dataAttribs.selfClose;
|
2016-01-13 23:34:40 +00:00
|
|
|
});
|
2018-06-09 04:37:17 +00:00
|
|
|
doc.body.appendChild(frag);
|
2016-01-13 23:34:40 +00:00
|
|
|
|
2018-06-09 04:37:17 +00:00
|
|
|
return doc;
|
2016-01-13 23:34:40 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-23 21:35:49 +00:00
|
|
|
var _processRefs;
|
|
|
|
|
|
|
|
References.prototype.extractRefFromNode = function(node, refsData, cite,
|
2018-02-15 20:01:53 +00:00
|
|
|
referencesAboutId, referencesGroup, nestedRefsHTML) {
|
2017-10-20 03:01:48 +00:00
|
|
|
var env = refsData.env;
|
2018-08-16 22:09:36 +00:00
|
|
|
var doc = node.ownerDocument;
|
2016-01-13 23:34:40 +00:00
|
|
|
var nestedInReferences = referencesAboutId !== undefined;
|
2018-06-09 04:37:17 +00:00
|
|
|
|
2018-08-16 22:09:36 +00:00
|
|
|
// This is data-parsoid from the dom fragment node that's gone through
|
|
|
|
// dsr computation and template wrapping.
|
2018-10-31 20:05:24 +00:00
|
|
|
var nodeDp = DOMDataUtils.getDataParsoid(node);
|
2018-08-16 22:09:36 +00:00
|
|
|
var typeOf = node.getAttribute('typeof');
|
|
|
|
var isTplWrapper = /\bmw:Transclusion\b/.test(typeOf);
|
|
|
|
var nodeType = (typeOf || '').replace(/mw:DOMFragment\/sealed\/ref/, '');
|
|
|
|
var content = nodeDp.html;
|
2018-10-31 20:05:24 +00:00
|
|
|
var tplDmw = isTplWrapper ? DOMDataUtils.getDataMw(node) : null;
|
2018-08-16 22:09:36 +00:00
|
|
|
|
|
|
|
// This is the <sup> that's the meat of the sealed fragment
|
|
|
|
var c = env.fragmentMap.get(content)[0];
|
|
|
|
// All the actions that require loaded data-attributes on `c` are done
|
|
|
|
// here so that we can quickly store those away for later.
|
2018-12-17 23:04:23 +00:00
|
|
|
DOMDataUtils.visitAndLoadDataAttribs(c);
|
2018-10-31 20:05:24 +00:00
|
|
|
var cDp = DOMDataUtils.getDataParsoid(c);
|
|
|
|
var refDmw = DOMDataUtils.getDataMw(c);
|
2018-08-16 22:09:36 +00:00
|
|
|
if (!cDp.empty && hasRef(c)) { // nested ref-in-ref
|
2019-02-15 19:23:04 +00:00
|
|
|
_processRefs(env, cite, refsData, c);
|
2018-06-09 04:37:17 +00:00
|
|
|
}
|
2018-12-17 23:04:23 +00:00
|
|
|
DOMDataUtils.visitAndStoreDataAttribs(c);
|
2018-06-09 04:37:17 +00:00
|
|
|
|
2018-12-05 20:49:54 +00:00
|
|
|
// Use the about attribute on the wrapper with priority, since it's
|
|
|
|
// only added when the wrapper is a template sibling.
|
|
|
|
const about = node.getAttribute('about') || c.getAttribute('about');
|
|
|
|
|
2018-08-16 22:09:36 +00:00
|
|
|
// FIXME(SSS): Need to clarify semantics here.
|
2016-01-13 23:34:40 +00:00
|
|
|
// If both the containing <references> elt as well as the nested <ref>
|
|
|
|
// elt has a group attribute, what takes precedence?
|
2018-06-09 04:37:17 +00:00
|
|
|
var group = refDmw.attrs.group || referencesGroup || '';
|
|
|
|
var refName = refDmw.attrs.name || '';
|
2017-10-20 03:01:48 +00:00
|
|
|
var ref = refsData.add(env, group, refName, about, nestedInReferences);
|
2016-01-13 23:34:40 +00:00
|
|
|
|
|
|
|
// Add ref-index linkback
|
2017-12-16 02:51:32 +00:00
|
|
|
var linkBack = doc.createElement('sup');
|
2018-06-09 04:37:17 +00:00
|
|
|
|
2018-08-16 22:09:36 +00:00
|
|
|
// FIXME: Lot of useless work for an edge case
|
|
|
|
if (cDp.empty) {
|
2018-06-09 04:37:17 +00:00
|
|
|
// Discard wrapper if there was no input wikitext
|
|
|
|
content = null;
|
2018-08-16 22:09:36 +00:00
|
|
|
if (cDp.selfClose) {
|
2018-08-07 13:47:24 +00:00
|
|
|
refDmw.body = undefined;
|
|
|
|
} else {
|
|
|
|
refDmw.body = { 'html': '' };
|
|
|
|
}
|
2018-06-09 04:37:17 +00:00
|
|
|
} else {
|
2016-01-13 23:34:40 +00:00
|
|
|
// If there are multiple <ref>s with the same name, but different content,
|
|
|
|
// the content of the first <ref> shows up in the <references> section.
|
|
|
|
// in order to ensure lossless RT-ing for later <refs>, we have to record
|
|
|
|
// HTML inline for all of them.
|
2017-10-20 03:01:48 +00:00
|
|
|
var html = '';
|
|
|
|
var contentDiffers = false;
|
|
|
|
if (ref.hasMultiples) {
|
|
|
|
// Use the non-pp version here since we've already stored attribs
|
|
|
|
// before putting them in the map.
|
2018-10-31 20:05:24 +00:00
|
|
|
html = ContentUtils.toXML(c, { innerXML: true });
|
2017-10-20 03:01:48 +00:00
|
|
|
contentDiffers = html !== ref.cachedHtml;
|
|
|
|
}
|
|
|
|
if (contentDiffers) {
|
2018-06-09 04:37:17 +00:00
|
|
|
refDmw.body = { 'html': html };
|
2016-01-13 23:34:40 +00:00
|
|
|
} else {
|
2018-06-09 04:37:17 +00:00
|
|
|
refDmw.body = { 'id': "mw-reference-text-" + ref.target };
|
2016-01-13 23:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:05:24 +00:00
|
|
|
DOMDataUtils.addAttributes(linkBack, {
|
2016-01-13 23:34:40 +00:00
|
|
|
'about': about,
|
|
|
|
'class': 'mw-ref',
|
|
|
|
'id': nestedInReferences ? undefined :
|
2018-02-15 20:01:53 +00:00
|
|
|
(ref.name ? ref.linkbacks[ref.linkbacks.length - 1] : ref.id),
|
2016-01-13 23:34:40 +00:00
|
|
|
'rel': 'dc:references',
|
|
|
|
'typeof': nodeType,
|
|
|
|
});
|
2018-10-31 20:05:24 +00:00
|
|
|
DOMDataUtils.addTypeOf(linkBack, "mw:Extension/ref");
|
2016-01-13 23:34:40 +00:00
|
|
|
var dataParsoid = {
|
2018-08-16 22:09:36 +00:00
|
|
|
src: nodeDp.src,
|
|
|
|
dsr: nodeDp.dsr,
|
|
|
|
pi: nodeDp.pi,
|
2016-01-13 23:34:40 +00:00
|
|
|
};
|
2018-10-31 20:05:24 +00:00
|
|
|
DOMDataUtils.setDataParsoid(linkBack, dataParsoid);
|
2018-06-09 04:37:17 +00:00
|
|
|
if (isTplWrapper) {
|
2018-10-31 20:05:24 +00:00
|
|
|
DOMDataUtils.setDataMw(linkBack, tplDmw);
|
2018-06-09 04:37:17 +00:00
|
|
|
} else {
|
2018-10-31 20:05:24 +00:00
|
|
|
DOMDataUtils.setDataMw(linkBack, refDmw);
|
2018-06-09 04:37:17 +00:00
|
|
|
}
|
2016-01-13 23:34:40 +00:00
|
|
|
|
|
|
|
// refLink is the link to the citation
|
|
|
|
var refLink = doc.createElement('a');
|
2018-10-31 20:05:24 +00:00
|
|
|
DOMDataUtils.addAttributes(refLink, {
|
2017-10-20 03:01:48 +00:00
|
|
|
'href': env.page.titleURI + '#' + ref.target,
|
2016-01-13 23:34:40 +00:00
|
|
|
'style': 'counter-reset: mw-Ref ' + ref.groupIndex + ';',
|
|
|
|
});
|
|
|
|
if (ref.group) {
|
|
|
|
refLink.setAttribute('data-mw-group', ref.group);
|
|
|
|
}
|
|
|
|
|
|
|
|
// refLink-span which will contain a default rendering of the cite link
|
|
|
|
// for browsers that don't support counters
|
|
|
|
var refLinkSpan = doc.createElement('span');
|
|
|
|
refLinkSpan.setAttribute('class', 'mw-reflink-text');
|
|
|
|
refLinkSpan.appendChild(doc.createTextNode("[" +
|
2017-04-27 16:20:49 +00:00
|
|
|
(ref.group ? ref.group + " " : "") + ref.groupIndex + "]"));
|
2016-01-13 23:34:40 +00:00
|
|
|
refLink.appendChild(refLinkSpan);
|
2017-12-16 02:51:32 +00:00
|
|
|
linkBack.appendChild(refLink);
|
2016-01-13 23:34:40 +00:00
|
|
|
|
|
|
|
if (!nestedInReferences) {
|
2017-12-16 02:51:32 +00:00
|
|
|
node.parentNode.replaceChild(linkBack, node);
|
2016-01-13 23:34:40 +00:00
|
|
|
} else {
|
2017-10-25 19:00:58 +00:00
|
|
|
// We don't need to delete the node now since it'll be removed in
|
|
|
|
// `insertReferencesIntoDOM` when all the children all cleaned out.
|
2018-10-31 20:05:24 +00:00
|
|
|
nestedRefsHTML.push(ContentUtils.ppToXML(linkBack), '\n');
|
2016-01-13 23:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the first content to compare multiple <ref>s with the same name.
|
|
|
|
if (!ref.content) {
|
|
|
|
ref.content = content;
|
2018-08-17 20:01:37 +00:00
|
|
|
ref.dir = (refDmw.attrs.dir || '').toLowerCase();
|
2016-01-13 23:34:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-26 19:24:56 +00:00
|
|
|
References.prototype.insertReferencesIntoDOM = function(refsNode, refsData, nestedRefsHTML, autoGenerated) {
|
2017-09-25 16:09:36 +00:00
|
|
|
var env = refsData.env;
|
2018-06-09 04:37:17 +00:00
|
|
|
var isTplWrapper = /\bmw:Transclusion\b/.test(refsNode.getAttribute('typeof'));
|
2018-10-31 20:05:24 +00:00
|
|
|
var dp = DOMDataUtils.getDataParsoid(refsNode);
|
2016-01-13 23:34:40 +00:00
|
|
|
var group = dp.group || '';
|
2018-06-09 04:37:17 +00:00
|
|
|
if (!isTplWrapper) {
|
2018-10-31 20:05:24 +00:00
|
|
|
var dataMw = DOMDataUtils.getDataMw(refsNode);
|
2018-06-09 04:37:17 +00:00
|
|
|
if (!Object.keys(dataMw).length) {
|
2018-11-30 21:44:06 +00:00
|
|
|
// FIXME: This can be moved to `insertMissingReferencesIntoDOM`
|
|
|
|
console.assert(autoGenerated);
|
2018-06-09 04:37:17 +00:00
|
|
|
dataMw = {
|
|
|
|
'name': 'references',
|
|
|
|
'attrs': {
|
|
|
|
'group': group || undefined, // Dont emit empty keys
|
|
|
|
},
|
|
|
|
};
|
2018-10-31 20:05:24 +00:00
|
|
|
DOMDataUtils.setDataMw(refsNode, dataMw);
|
2018-06-09 04:37:17 +00:00
|
|
|
}
|
2016-01-13 23:34:40 +00:00
|
|
|
|
2016-03-24 20:19:14 +00:00
|
|
|
// Mark this auto-generated so that we can skip this during
|
|
|
|
// html -> wt and so that clients can strip it if necessary.
|
|
|
|
if (autoGenerated) {
|
|
|
|
dataMw.autoGenerated = true;
|
2016-08-26 19:24:56 +00:00
|
|
|
} else if (nestedRefsHTML.length > 0) {
|
|
|
|
dataMw.body = { 'html': '\n' + nestedRefsHTML.join('') };
|
2018-08-07 04:31:53 +00:00
|
|
|
} else if (!dp.selfClose) {
|
|
|
|
dataMw.body = { 'html': '' };
|
|
|
|
} else {
|
2018-06-09 04:37:17 +00:00
|
|
|
dataMw.body = undefined;
|
2016-02-05 18:54:28 +00:00
|
|
|
}
|
2018-08-07 04:31:53 +00:00
|
|
|
dp.selfClose = undefined;
|
2016-01-13 23:34:40 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 23:04:54 +00:00
|
|
|
var refGroup = refsData.getRefGroup(group);
|
|
|
|
|
2018-06-09 04:37:17 +00:00
|
|
|
// Deal with responsive wrapper
|
2018-01-05 23:04:54 +00:00
|
|
|
if (refsNode.classList.contains('mw-references-wrap')) {
|
|
|
|
var rrOpts = env.conf.wiki.responsiveReferences;
|
|
|
|
if (refGroup && refGroup.refs.length > rrOpts.threshold) {
|
|
|
|
refsNode.classList.add('mw-references-columns');
|
|
|
|
}
|
|
|
|
refsNode = refsNode.firstChild;
|
|
|
|
}
|
|
|
|
|
2016-01-13 23:34:40 +00:00
|
|
|
// Remove all children from the references node
|
|
|
|
//
|
|
|
|
// Ex: When {{Reflist}} is reused from the cache, it comes with
|
|
|
|
// a bunch of references as well. We have to remove all those cached
|
|
|
|
// references before generating fresh references.
|
|
|
|
while (refsNode.firstChild) {
|
|
|
|
refsNode.removeChild(refsNode.firstChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (refGroup) {
|
2018-12-07 00:08:50 +00:00
|
|
|
refGroup.refs.forEach(ref => refGroup.renderLine(env, refsNode, ref));
|
2016-01-13 23:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the group from refsData
|
|
|
|
refsData.removeRefGroup(group);
|
|
|
|
};
|
|
|
|
|
2018-02-13 17:19:32 +00:00
|
|
|
/**
|
|
|
|
* Process `<ref>`s left behind after the DOM is fully processed.
|
|
|
|
* We process them as if there was an implicit `<references />` tag at
|
|
|
|
* the end of the DOM.
|
|
|
|
*/
|
2016-12-16 23:00:17 +00:00
|
|
|
References.prototype.insertMissingReferencesIntoDOM = function(refsData, node) {
|
|
|
|
var env = refsData.env;
|
2016-01-13 23:34:40 +00:00
|
|
|
var doc = node.ownerDocument;
|
|
|
|
|
2018-02-05 20:41:51 +00:00
|
|
|
refsData.refGroups.forEach((refsValue, refsGroup) => {
|
2019-02-15 19:23:04 +00:00
|
|
|
var frag = createReferences(env, doc, null, {
|
2018-01-05 21:52:58 +00:00
|
|
|
group: refsGroup,
|
|
|
|
responsive: null,
|
|
|
|
}, function(dp) {
|
|
|
|
// The new references come out of "nowhere", so to make selser work
|
|
|
|
// propertly, add a zero-sized DSR pointing to the end of the document.
|
|
|
|
dp.dsr = [env.page.src.length, env.page.src.length, 0, 0];
|
2018-06-09 04:37:17 +00:00
|
|
|
}, true);
|
2016-01-13 23:34:40 +00:00
|
|
|
|
|
|
|
// Add a \n before the <ol> so that when serialized to wikitext,
|
|
|
|
// each <references /> tag appears on its own line.
|
|
|
|
node.appendChild(doc.createTextNode("\n"));
|
2018-01-05 23:04:54 +00:00
|
|
|
node.appendChild(frag);
|
2018-01-05 21:52:58 +00:00
|
|
|
|
2018-02-05 20:41:51 +00:00
|
|
|
this.insertReferencesIntoDOM(frag, refsData, [""], true);
|
2016-01-13 23:34:40 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-01-26 21:17:54 +00:00
|
|
|
References.prototype.serialHandler = {
|
2018-02-16 20:46:15 +00:00
|
|
|
handle: Promise.async(function *(node, state, wrapperUnmodified) {
|
2018-10-31 20:05:24 +00:00
|
|
|
var dataMw = DOMDataUtils.getDataMw(node);
|
2016-03-24 20:19:14 +00:00
|
|
|
if (dataMw.autoGenerated && state.rtTestMode) {
|
2016-01-13 23:34:40 +00:00
|
|
|
// Eliminate auto-inserted <references /> noise in rt-testing
|
2018-02-16 20:46:15 +00:00
|
|
|
return '';
|
2016-01-13 23:34:40 +00:00
|
|
|
} else {
|
2018-02-16 20:46:15 +00:00
|
|
|
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 + '</' + dataMw.name + '>';
|
|
|
|
} else {
|
|
|
|
state.env.log('error',
|
|
|
|
'References body unavailable for: ' + node.outerHTML);
|
|
|
|
return ''; // Drop it!
|
|
|
|
}
|
2016-01-13 23:34:40 +00:00
|
|
|
}
|
|
|
|
}),
|
2018-08-19 17:20:45 +00:00
|
|
|
// FIXME: LEAKY -- Should we expose newline constraints to extensions?
|
2016-01-13 23:34:40 +00:00
|
|
|
before: function(node, otherNode, state) {
|
|
|
|
// Serialize new references tags on a new line.
|
2018-10-31 20:05:24 +00:00
|
|
|
if (WTUtils.isNewElt(node)) {
|
2016-01-13 23:34:40 +00:00
|
|
|
return { min: 1, max: 2 };
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-12-18 16:53:25 +00:00
|
|
|
References.prototype.lintHandler = function(refs, env, tplInfo, domLinter) {
|
|
|
|
// Nothing to do
|
|
|
|
//
|
|
|
|
// FIXME: Not entirely true for scenarios where the <ref> tags
|
|
|
|
// are defined in the references section that is itself templated.
|
|
|
|
//
|
|
|
|
// {{1x|<references>\n<ref name='x'><b>foo</ref>\n</references>}}
|
|
|
|
//
|
|
|
|
// In this example, the references tag has the right tplInfo and
|
|
|
|
// when the <ref> tag is processed in the body of the article where
|
|
|
|
// it is accessed, there is no relevant template or dsr info available.
|
|
|
|
//
|
|
|
|
// Ignoring for now.
|
|
|
|
return refs.nextNode;
|
|
|
|
};
|
|
|
|
|
2018-02-13 17:19:32 +00:00
|
|
|
/**
|
2016-01-13 23:34:40 +00:00
|
|
|
* This handles wikitext like this:
|
2018-02-13 17:19:32 +00:00
|
|
|
* ```
|
2016-01-13 23:34:40 +00:00
|
|
|
* <references> <ref>foo</ref> </references>
|
|
|
|
* <references> <ref>bar</ref> </references>
|
2018-02-13 17:19:32 +00:00
|
|
|
* ```
|
|
|
|
* @private
|
|
|
|
*/
|
2017-10-23 21:35:49 +00:00
|
|
|
var _processRefsInReferences = function(cite, refsData, node, referencesId,
|
2018-02-15 20:01:53 +00:00
|
|
|
referencesGroup, nestedRefsHTML) {
|
2016-01-13 23:34:40 +00:00
|
|
|
var child = node.firstChild;
|
|
|
|
while (child !== null) {
|
|
|
|
var nextChild = child.nextSibling;
|
2018-10-31 20:05:24 +00:00
|
|
|
if (DOMUtils.isElt(child)) {
|
|
|
|
if (WTUtils.isSealedFragmentOfType(child, 'ref')) {
|
2017-10-23 21:35:49 +00:00
|
|
|
cite.references.extractRefFromNode(child, refsData, cite,
|
2016-01-13 23:34:40 +00:00
|
|
|
referencesId, referencesGroup, nestedRefsHTML);
|
2017-10-20 21:09:16 +00:00
|
|
|
} else if (child.hasChildNodes()) {
|
2016-01-13 23:34:40 +00:00
|
|
|
_processRefsInReferences(cite, refsData,
|
|
|
|
child, referencesId, referencesGroup, nestedRefsHTML);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
child = nextChild;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-15 19:23:04 +00:00
|
|
|
_processRefs = function(env, cite, refsData, node) {
|
2016-01-13 23:34:40 +00:00
|
|
|
var child = node.firstChild;
|
|
|
|
while (child !== null) {
|
|
|
|
var nextChild = child.nextSibling;
|
2018-10-31 20:05:24 +00:00
|
|
|
if (DOMUtils.isElt(child)) {
|
|
|
|
if (WTUtils.isSealedFragmentOfType(child, 'ref')) {
|
2017-10-23 21:35:49 +00:00
|
|
|
cite.references.extractRefFromNode(child, refsData, cite);
|
2018-06-09 04:37:17 +00:00
|
|
|
} else if ((/(?:^|\s)mw:Extension\/references(?=$|\s)/).test(child.getAttribute('typeOf'))) {
|
2016-01-13 23:34:40 +00:00
|
|
|
var referencesId = child.getAttribute("about");
|
2018-10-31 20:05:24 +00:00
|
|
|
var referencesGroup = DOMDataUtils.getDataParsoid(child).group;
|
2016-08-26 19:24:56 +00:00
|
|
|
var nestedRefsHTML = [];
|
2016-01-13 23:34:40 +00:00
|
|
|
_processRefsInReferences(cite, refsData,
|
|
|
|
child, referencesId, referencesGroup, nestedRefsHTML);
|
|
|
|
cite.references.insertReferencesIntoDOM(child, refsData, nestedRefsHTML);
|
|
|
|
} else {
|
2017-02-02 23:27:55 +00:00
|
|
|
// inline media -- look inside the data-mw attribute
|
2018-10-31 20:05:24 +00:00
|
|
|
if (WTUtils.isInlineMedia(child)) {
|
2016-01-13 23:34:40 +00:00
|
|
|
/* -----------------------------------------------------------------
|
Add media info in a post-processing pass
The basic idea here is to generate the media structure in the token
stream using a stuffed span with a redlink, as in T169975, and
augmenting the nodes on the DOM once the media info has been fetched.
A redlink is justified as the canonical representation of the media
elements before info is fetched because it's the fallback if fetching
fails and the media type is unknown until the info is retrieved.
Most options are stored in data-mw until the media type is fetched and
it's determined that they're applicable. This is a bit of a reversion
of how things were done before where inapplicable options were removed
post-facto.
For consistency and styling's sake, figcaptions are now always added to
block figures.
The pass has to be run before generating headings anchor, since that
depends on the text content (ie. redlinks). This rearranges things in
the post-processor and adds another pass.
The post-processing pass to add media info is run on subpipelines as
well as the top level so that the media info is present in cases where
we embed HTML in data-mw (which is currently skipped by the top level
only passes, except for the cite extension, which has special handling,
see T214994) and to avoid an additional post-processing pass for the
gallery extension, which scales media of packed galleries. This comes
at the cost of making additional queries for each pipeline and requires
the add media pass to be idempotent.
Filed T214241 for figuring out what to do about data-mw info being
clobbered by template annotations.
The newly failing blacklisted tests are from roundtripping media options
in galleries, which requires a general refactor for support. See the
FIXMEs added there.
Performance should be expected to regress by the amount of work we're
able to overlap in the async phase of the pipeline while the media info
is being fetched. Considering a lot of that work is caught up waiting
for the batch to return (other async requests are found in the same
batch), this doesn't turn out to be much in practice in the average
case.
Bug: T153080
Bug: T169975
Change-Id: I856ee962b70cef1f8d49652396ea5264e11a8ade
2018-09-20 16:21:45 +00:00
|
|
|
* FIXME(subbu): This works but feels very special-cased in 2 ways:
|
2016-01-13 23:34:40 +00:00
|
|
|
*
|
|
|
|
* 1. special cased to images vs. any node that might have
|
|
|
|
* serialized HTML embedded in data-mw
|
|
|
|
* 2. special cased to global cite handling -- the general scenario
|
|
|
|
* is DOM post-processors that do different things on the
|
|
|
|
* top-level vs not.
|
|
|
|
* - Cite needs to process these fragments in the context of the
|
|
|
|
* top-level page, and has to be done in order of how the nodes
|
|
|
|
* are encountered.
|
|
|
|
* - DOM cleanup can be done on embedded fragments without
|
|
|
|
* any page-level context and in any order.
|
|
|
|
* - So, some variability here.
|
|
|
|
*
|
|
|
|
* We should be running dom.cleanup.js passes on embedded html
|
|
|
|
* in data-mw and other attributes. Since correctness doesn't
|
|
|
|
* depend on that cleanup, I am not adding more special-case
|
|
|
|
* code in dom.cleanup.js.
|
|
|
|
*
|
|
|
|
* Doing this more generically will require creating a DOMProcessor
|
|
|
|
* class and adding state to it.
|
Add media info in a post-processing pass
The basic idea here is to generate the media structure in the token
stream using a stuffed span with a redlink, as in T169975, and
augmenting the nodes on the DOM once the media info has been fetched.
A redlink is justified as the canonical representation of the media
elements before info is fetched because it's the fallback if fetching
fails and the media type is unknown until the info is retrieved.
Most options are stored in data-mw until the media type is fetched and
it's determined that they're applicable. This is a bit of a reversion
of how things were done before where inapplicable options were removed
post-facto.
For consistency and styling's sake, figcaptions are now always added to
block figures.
The pass has to be run before generating headings anchor, since that
depends on the text content (ie. redlinks). This rearranges things in
the post-processor and adds another pass.
The post-processing pass to add media info is run on subpipelines as
well as the top level so that the media info is present in cases where
we embed HTML in data-mw (which is currently skipped by the top level
only passes, except for the cite extension, which has special handling,
see T214994) and to avoid an additional post-processing pass for the
gallery extension, which scales media of packed galleries. This comes
at the cost of making additional queries for each pipeline and requires
the add media pass to be idempotent.
Filed T214241 for figuring out what to do about data-mw info being
clobbered by template annotations.
The newly failing blacklisted tests are from roundtripping media options
in galleries, which requires a general refactor for support. See the
FIXMEs added there.
Performance should be expected to regress by the amount of work we're
able to overlap in the async phase of the pipeline while the media info
is being fetched. Considering a lot of that work is caught up waiting
for the batch to return (other async requests are found in the same
batch), this doesn't turn out to be much in practice in the average
case.
Bug: T153080
Bug: T169975
Change-Id: I856ee962b70cef1f8d49652396ea5264e11a8ade
2018-09-20 16:21:45 +00:00
|
|
|
*
|
|
|
|
* See T214994
|
2016-01-13 23:34:40 +00:00
|
|
|
* ----------------------------------------------------------------- */
|
2018-10-31 20:05:24 +00:00
|
|
|
var dmw = DOMDataUtils.getDataMw(child);
|
2016-01-13 23:34:40 +00:00
|
|
|
var caption = dmw.caption;
|
|
|
|
if (caption) {
|
|
|
|
// Extract the caption HTML, build the DOM, process refs,
|
2016-03-04 00:26:37 +00:00
|
|
|
// serialize to HTML, update the caption HTML.
|
2019-02-15 19:23:04 +00:00
|
|
|
var captionDOM = ContentUtils.ppToDOM(env, caption);
|
|
|
|
_processRefs(env, cite, refsData, captionDOM);
|
2018-10-31 20:05:24 +00:00
|
|
|
dmw.caption = ContentUtils.ppToXML(captionDOM, { innerXML: true });
|
2016-01-13 23:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-20 21:09:16 +00:00
|
|
|
if (child.hasChildNodes()) {
|
2019-02-15 19:23:04 +00:00
|
|
|
_processRefs(env, cite, refsData, child);
|
2016-01-13 23:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
child = nextChild;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Native Parsoid implementation of the Cite extension
|
2018-02-27 20:31:30 +00:00
|
|
|
* that ties together `<ref>` and `<references>`.
|
2016-01-13 23:34:40 +00:00
|
|
|
*/
|
|
|
|
var Cite = function() {
|
|
|
|
this.ref = new Ref(this);
|
|
|
|
this.references = new References(this);
|
|
|
|
this.config = {
|
2018-08-29 22:14:36 +00:00
|
|
|
name: 'cite',
|
2018-08-15 22:13:00 +00:00
|
|
|
domProcessors: {
|
2018-12-07 00:08:50 +00:00
|
|
|
wt2htmlPostProcessor: (...args) => this._wt2htmlPostProcessor(...args),
|
|
|
|
html2wtPreProcessor: (...args) => this._html2wtPreProcessor(...args),
|
2016-12-10 02:59:54 +00:00
|
|
|
},
|
2016-01-13 23:34:40 +00:00
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
name: 'ref',
|
2018-12-07 00:08:50 +00:00
|
|
|
toDOM: (...args) => this.ref.toDOM(...args),
|
2018-11-30 01:21:49 +00:00
|
|
|
fragmentOptions: {
|
|
|
|
unwrapFragment: false,
|
|
|
|
},
|
2018-08-29 22:14:36 +00:00
|
|
|
serialHandler: this.ref.serialHandler, // FIXME: Rename to toWikitext
|
2017-12-18 16:53:25 +00:00
|
|
|
lintHandler: this.ref.lintHandler,
|
2018-08-19 17:20:45 +00:00
|
|
|
// FIXME: Do we need (a) domDiffHandler (b) ... others ...
|
2016-01-13 23:34:40 +00:00
|
|
|
}, {
|
|
|
|
name: 'references',
|
2018-12-07 00:08:50 +00:00
|
|
|
toDOM: (...args) => this.references.toDOM(...args),
|
2016-01-26 21:17:54 +00:00
|
|
|
serialHandler: this.references.serialHandler,
|
2017-12-18 16:53:25 +00:00
|
|
|
lintHandler: this.references.lintHandler,
|
2016-01-13 23:34:40 +00:00
|
|
|
},
|
|
|
|
],
|
2017-09-11 22:40:29 +00:00
|
|
|
styles: [
|
|
|
|
'ext.cite.style',
|
|
|
|
'ext.cite.styles',
|
|
|
|
],
|
2016-01-13 23:34:40 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-02-13 17:19:32 +00:00
|
|
|
/**
|
2018-08-30 23:11:56 +00:00
|
|
|
* wt -> html DOM PostProcessor
|
2018-02-13 17:19:32 +00:00
|
|
|
*/
|
2018-08-30 23:11:56 +00:00
|
|
|
Cite.prototype._wt2htmlPostProcessor = function(body, env, options, atTopLevel) {
|
2016-01-13 23:34:40 +00:00
|
|
|
if (atTopLevel) {
|
2016-12-16 23:00:17 +00:00
|
|
|
var refsData = new ReferencesData(env);
|
2019-02-15 19:23:04 +00:00
|
|
|
_processRefs(env, this, refsData, body);
|
2018-08-30 23:11:56 +00:00
|
|
|
this.references.insertMissingReferencesIntoDOM(refsData, body);
|
2016-01-13 23:34:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-30 23:11:56 +00:00
|
|
|
/**
|
|
|
|
* html -> wt DOM PreProcessor
|
|
|
|
*
|
|
|
|
* This is to reconstitute page-level information from local annotations
|
|
|
|
* left behind by editing clients.
|
|
|
|
*
|
|
|
|
* Editing clients add inserted: true or deleted: true properties to a <ref>'s
|
|
|
|
* data-mw object. These are no-ops for non-named <ref>s. For named <ref>s,
|
|
|
|
* - for inserted refs, we might want to de-duplicate refs.
|
|
|
|
* - for deleted refs, if the primary ref was deleted, we have to transfer
|
|
|
|
* the primary ref designation to another instance of the named ref.
|
|
|
|
*/
|
|
|
|
Cite.prototype._html2wtPreProcessor = function(env, body) {
|
|
|
|
// TODO
|
|
|
|
};
|
2016-01-13 23:34:40 +00:00
|
|
|
|
|
|
|
if (typeof module === "object") {
|
2016-04-21 18:52:34 +00:00
|
|
|
module.exports = Cite;
|
2016-01-13 23:34:40 +00:00
|
|
|
}
|