Properly bump reference anchors

* Adds an index of all the references on a page in order to avoid
   repeating attrs when multiple <references /> tags are present.

 * Update tests to reflect the new behaviour.

Bug: 59782
Change-Id: Ia44bf59a9304788aca170041d3b85f53557151fc
This commit is contained in:
Arlo Breault 2014-01-10 16:13:20 -05:00
parent a372d97939
commit d2ba2d73e7

View file

@ -134,30 +134,32 @@ function RefGroup(group) {
this.indexByName = new Map(); this.indexByName = new Map();
} }
RefGroup.prototype.add = function(refName, about, skipLinkback) { RefGroup.prototype.add = function( references, refName, about, skipLinkback ) {
var ref; var ref;
if ( refName && this.indexByName.has( refName ) ) { if ( refName && this.indexByName.has( refName ) ) {
ref = this.indexByName.get( refName ); ref = this.indexByName.get( refName );
} else { } else {
var n = this.refs.length, var n = references.index,
refKey = (1+n) + ''; refKey = (1+n) + '';
// bump index
references.index += 1;
if (refName) { if (refName) {
refKey = refName + '-' + refKey; refKey = refName + '-' + refKey;
} }
ref = { ref = {
about: about, about: about,
content: null, content: null,
group: this.name, group: this.name,
groupIndex: (1+n), // FIXME -- this seems to be wiki-specific groupIndex: this.refs.length + 1,
index: n, index: n,
key: refKey, key: refKey,
linkbacks: [], linkbacks: [],
name: refName, name: refName,
target: 'cite_note-' + refKey target: 'cite_note-' + refKey
}; };
this.refs[n] = ref; this.refs.push( ref );
if (refName) { if (refName) {
this.indexByName.set( refName, ref ); this.indexByName.set( refName, ref );
} }
@ -230,10 +232,10 @@ function getRefGroup(refGroups, groupName, allocIfMissing) {
function References(cite) { function References(cite) {
this.cite = cite; this.cite = cite;
this.reset(); this.reset( null, true );
} }
References.prototype.reset = function(group) { References.prototype.reset = function( group, resetIndex ) {
if (group) { if (group) {
this.refGroups.set( group, undefined ); this.refGroups.set( group, undefined );
} else { } else {
@ -254,6 +256,11 @@ References.prototype.reset = function(group) {
* ----------------------------------------------------------------- */ * ----------------------------------------------------------------- */
this.nestedRefsHTMLMap = new Map(); this.nestedRefsHTMLMap = new Map();
} }
// restart reference counter
if ( resetIndex ) {
this.index = 0;
}
}; };
/** /**
@ -355,7 +362,7 @@ References.prototype.extractRefFromNode = function(node) {
about = node.getAttribute("about"), about = node.getAttribute("about"),
skipLinkback = dp.tmp.skiplinkback, skipLinkback = dp.tmp.skiplinkback,
refGroup = getRefGroup(this.refGroups, group, true), refGroup = getRefGroup(this.refGroups, group, true),
ref = refGroup.add(refName, about, skipLinkback), ref = refGroup.add(this, refName, about, skipLinkback),
nodeType = (node.getAttribute("typeof") || '').replace(/mw:Extension\/ref\/Marker/, ''); nodeType = (node.getAttribute("typeof") || '').replace(/mw:Extension\/ref\/Marker/, '');
// Add ref-index linkback // Add ref-index linkback
@ -484,7 +491,7 @@ var Cite = function() {
Cite.prototype.resetState = function() { Cite.prototype.resetState = function() {
this.ref.reset(); this.ref.reset();
this.references.reset(); this.references.reset( null, true );
}; };
if (typeof module === "object") { if (typeof module === "object") {