From d2ba2d73e71814eeed253d3acc50bd617429ee06 Mon Sep 17 00:00:00 2001 From: Arlo Breault Date: Fri, 10 Jan 2014 16:13:20 -0500 Subject: [PATCH] Properly bump reference anchors * Adds an index of all the references on a page in order to avoid repeating attrs when multiple tags are present. * Update tests to reflect the new behaviour. Bug: 59782 Change-Id: Ia44bf59a9304788aca170041d3b85f53557151fc --- lib/ext.Cite.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/ext.Cite.js b/lib/ext.Cite.js index d24d20f19..939a005b4 100644 --- a/lib/ext.Cite.js +++ b/lib/ext.Cite.js @@ -134,30 +134,32 @@ function RefGroup(group) { this.indexByName = new Map(); } -RefGroup.prototype.add = function(refName, about, skipLinkback) { +RefGroup.prototype.add = function( references, refName, about, skipLinkback ) { var ref; if ( refName && this.indexByName.has( refName ) ) { ref = this.indexByName.get( refName ); } else { - var n = this.refs.length, + var n = references.index, refKey = (1+n) + ''; + // bump index + references.index += 1; + if (refName) { refKey = refName + '-' + refKey; } - ref = { about: about, content: null, group: this.name, - groupIndex: (1+n), // FIXME -- this seems to be wiki-specific + groupIndex: this.refs.length + 1, index: n, key: refKey, linkbacks: [], name: refName, target: 'cite_note-' + refKey }; - this.refs[n] = ref; + this.refs.push( ref ); if (refName) { this.indexByName.set( refName, ref ); } @@ -230,10 +232,10 @@ function getRefGroup(refGroups, groupName, allocIfMissing) { function References(cite) { this.cite = cite; - this.reset(); + this.reset( null, true ); } -References.prototype.reset = function(group) { +References.prototype.reset = function( group, resetIndex ) { if (group) { this.refGroups.set( group, undefined ); } else { @@ -254,6 +256,11 @@ References.prototype.reset = function(group) { * ----------------------------------------------------------------- */ 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"), skipLinkback = dp.tmp.skiplinkback, 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/, ''); // Add ref-index linkback @@ -484,7 +491,7 @@ var Cite = function() { Cite.prototype.resetState = function() { this.ref.reset(); - this.references.reset(); + this.references.reset( null, true ); }; if (typeof module === "object") {