Keep track of ID's uses on the page, and make unique ones if there are repeats. Resolves issue #240.

This commit is contained in:
Davide P. Cervone 2014-02-15 11:15:07 -05:00
parent 86792eaf44
commit 6d6eccead0

View File

@ -29,6 +29,8 @@ MathJax.Extension["TeX/AMSmath"] = {
number: 0, // current equation number
startNumber: 0, // current starting equation number (for when equation is restarted)
IDs: {}, // IDs used in previous equations
eqIDs: {}, // IDs used in this equation
labels: {}, // the set of labels
eqlabels: {}, // labels in the current equation
refs: [] // array of jax with unresolved references
@ -187,7 +189,7 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
global.label = label;
if (AMS.labels[label] || AMS.eqlabels[label])
{TEX.Error(["MultipleLabel","Label '%1' multiply defined",label])}
AMS.eqlabels[label] = "???"; // will be replaced by tag value later
AMS.eqlabels[label] = {tag:"???", id:""}; // will be replaced by tag value later
}
},
@ -197,11 +199,10 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
HandleRef: function (name,eqref) {
var label = this.GetArgument(name);
var ref = AMS.labels[label] || AMS.eqlabels[label];
if (!ref) {ref = "??"; AMS.badref = !AMS.refUpdate}
var tag = ref; if (eqref) {tag = CONFIG.formatTag(tag)}
if (CONFIG.useLabelIds) {ref = label}
if (!ref) {ref = {tag:"???",id:""}; AMS.badref = !AMS.refUpdate}
var tag = ref.tag; if (eqref) {tag = CONFIG.formatTag(tag)}
this.Push(MML.mrow.apply(MML,this.InternalMath(tag)).With({
href:CONFIG.formatURL(CONFIG.formatID(ref)), "class":"MathJax_ref"
href:CONFIG.formatURL(ref.id), "class":"MathJax_ref"
}));
},
@ -423,7 +424,7 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
if (!global.notag) {
AMS.number++; global.tagID = CONFIG.formatNumber(AMS.number.toString());
var mml = TEX.Parse("\\text{"+CONFIG.formatTag(global.tagID)+"}",{}).mml();
global.tag = MML.mtd(mml.With({id:CONFIG.formatID(global.tagID)}));
global.tag = MML.mtd(mml).With({id:CONFIG.formatID(global.tagID)});
}
},
@ -433,9 +434,20 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
getTag: function () {
var global = this.global, tag = global.tag; global.tagged = true;
if (global.label) {
AMS.eqlabels[global.label] = global.tagID;
if (CONFIG.useLabelIds) {tag.id = CONFIG.formatID(global.label)}
AMS.eqlabels[global.label] = {tag:global.tagID, id:tag.id};
}
//
// Check for repeated ID's (either in the document or as
// a previous tag) and find a unique related one. (#240)
//
if (document.getElementById(tag.id) || AMS.IDs[tag.id] || AMS.eqIDs[tag.id]) {
var i = 0, ID;
do {i++; ID = tag.id+"_"+i}
while (document.getElementById(ID) || AMS.IDs[ID] || AMS.eqIDs[ID]);
tag.id = ID; if (global.label) {AMS.eqlabels[global.label].id = ID}
}
AMS.eqIDs[tag.id] = 1;
delete global.tag; delete global.tagID; delete global.label;
return tag;
},
@ -586,12 +598,13 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
TEX.prefilterHooks.Add(function (data) {
AMS.display = data.display;
AMS.number = AMS.startNumber; // reset equation numbers (in case the equation restarted)
AMS.eqlabels = {}; AMS.badref = false;
AMS.eqlabels = AMS.eqIDs = {}; AMS.badref = false;
if (AMS.refUpdate) {AMS.number = data.script.MathJax.startNumber}
});
TEX.postfilterHooks.Add(function (data) {
data.script.MathJax.startNumber = AMS.startNumber;
AMS.startNumber = AMS.number; // equation numbers for next equation
MathJax.Hub.Insert(AMS.IDs,AMS.eqIDs); // save IDs from this equation
MathJax.Hub.Insert(AMS.labels,AMS.eqlabels); // save labels from this equation
if (AMS.badref && !data.math.texError) {AMS.refs.push(data.script)} // reprocess later
});
@ -619,7 +632,7 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
//
TEX.resetEquationNumbers = function (n,keepLabels) {
AMS.startNumber = (n || 0);
if (!keepLabels) {AMS.labels = {}}
if (!keepLabels) {AMS.labels = AMS.IDs = {}}
}
/******************************************************************************/