User:PleaseStand/segregate-refs-dev.js: Difference between revisions

Content deleted Content added
avoid doing the impossible: stopping the unload of the page
avoid extending the built-in String object
Line 85:
(function(){
 
// "Semi-global" variables (function-scoped variables private to this script)
var editForm, refsDiv, refsH2, mainTextbox, refsTextbox, randPrefix, messages,
refsButton, convertButton, complete, unloadHandlerRegistered = false;
Line 94:
}
 
// Add support for settingSetting a slice of a string.
// Extend the string object with new methods
String.prototype.PsSetSlice = function setSlice(t, replacement, indexFrom, indexTo) {
// Begin with the prefix "Ps" to avoid name clashes
 
// Add support for setting a slice of a string.
// (Only works with positive indices.)
String.prototype.PsSetSlice = function(replacement, indexFrom, indexTo) {
if(typeof indexTo == "undefined") {
return thist.slice(0, indexFrom) + replacement;
}
return thist.slice(0, indexFrom) + replacement + thist.slice(indexTo);
};
 
// AddUnquoting supportfrom for unquoting froman HTML-quoted form.
function htmlUnquote(t) {
String.prototype.PsHTMLUnquote = function() {
// Let's use the browser's functionality for the hard work,
// since MediaWiki/PHP supports many different HTML entities.
// (Note: innerHTML is not W3C-standard)
var d = window.document.createElement("div");
d.innerHTML = "<input value=" + thist + "></input>";
return d.firstChild.value;
};
 
// Add support for quotingQuoting using HTML quotes. Chooses single quotes versus
// double quotes depending on which is shorter.
function htmlQuote(t) {
String.prototype.PsHTMLQuote = function() {
// Escape ampersands
var s = thist.replace(/\&/g, "&amp;");
// Try both kinds of quotes
var sQ = "'" + s.replace(/'/g, "&#39;") + "'",
Line 126 ⟶ 122:
// Choose the shorter, preferring double quotes if equal in length
return (sQ.length < dQ.length ? sQ : dQ);
};
 
// OBJECTS
Line 175 ⟶ 171:
var attributes = {}, results;
while((results = attParseRegex.exec(this.parsedRef[1]))) {
attributes[results[1].toLowerCase()] = htmlUnquote(results[2].PsHTMLUnquote();
}
return attributes;
Line 234 ⟶ 230:
refName = randPrefix + (++unnamedRefs).toString(10);
// Change the corresponding ref code
refLong = "<ref name=" + refName.PsHTMLQuotehtmlQuote(refName) +
parser.parsedRef[1] + parser.parsedRef[2];
} else {
Line 272 ⟶ 268:
} else if(!refGroup.length) {
refShort = refPreferred + "name=" +
refName.PsHTMLQuotehtmlQuote(refName) + "/>";
} else {
refShort = refPreferred + "name=" + refName.PsHTMLQuotehtmlQuote(refName) +
" " + "group=" + refGroup.PsHTMLQuotehtmlQuote(refGroup) + "/>";
}
// Otherwise, is the current longcode not empty?
Line 286 ⟶ 282:
if(!refGroup.length) {
refShort = refPreferred + "name=" +
refName.PsHTMLQuotehtmlQuote(refName) + "/>";
} else {
refShort = refPreferred + "name=" + refName.PsHTMLQuotehtmlQuote(refName) +
" " + "group=" + refGroup.PsHTMLQuotehtmlQuote(refGroup) + "/>";
}
} else {
Line 301 ⟶ 297:
}
// Replace the long code with the short code
scanner.wikiText = setSlice(scanner.wikiText.PsSetSlice(, refShort,
scanner.refScanRegex.lastIndex - ref.length,
scanner.refScanRegex.lastIndex);
Line 407 ⟶ 403:
// Replace the short code with the long code
scanner.wikiText = setSlice(scanner.wikiText.PsSetSlice(, refLong,
scanner.refScanRegex.lastIndex - ref.length,
scanner.refScanRegex.lastIndex);
Line 446 ⟶ 442:
for(refName in result.unusedRefs) {
if(has(result.unusedRefs, refName)) {
unusedRefNamesQuoted.push(refName.PsHTMLQuotehtmlQuote(refName));
}
}
Line 560 ⟶ 556:
return false;
}
group = group.PsHTMLUnquotehtmlUnquote(group);
// The first button should disappear