Utilisateur:A2/nbspfixer.js
Apparence
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;
Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5./* "Evil Unicode Conversion" script thingy for invisible unicode. [Version 0.0.5]
Converts trouble unicode characters to hexidecimal entities, and then back to unicode on page save.
NOTES:
1. It may be tempting to leave them as entities on save, but certain MediaWiki: messages don't take kindly to it.
2. Any of these registered characters that exist in the document as entities before load will be converted to decimal
entities. This allows pre-existing cases to remain un-reconverted on document save. So #x202e; would become #8238; on
load. Note that because of this, the ampersand escaping is no longer required.
3. This list now includes most characters of type:
* Unicode spaces, whitespaces, non-breaking spaces.
* Directional control, directional override, BiDi formatting.
* Paragraph/newline formatting and visible-zero-width.
* Silly unicode not suitable for markup.
*/
if(mw.config.get('wgAction') == 'edit' || mw.config.get('wgAction') == 'submit') {
var eucTextbox = 'wpTextbox1';
} else if(mw.config.get('wgCanonicalSpecialPageName') == 'Upload') {
var eucTextbox = 'wpUploadDescription';
}
if(window.eucTextbox) addOnloadHook(unconvertEvilUnicodeChars);
var eucChars = [160,173,847,1536,1537,1538,1539,1757,1807,3852,4447,4448,5760,6155,6156,6157,6158,8192,8193,8194,8195,8196,8197,8198,8199,8200,8201,8202,8203,8204,8205,8206,8207,8209,8232,8233,8234,8235,8236,8237,8238,8239,8239,8260,8287,8288,8289,8290,8291,8292,8298,8299,8300,8301,8302,8303,12272,12273,12274,12275,12276,12277,12278,12279,12280,12281,12282,12283,12288,12350,65279,65408,65532]
function unconvertEvilUnicodeChars() {
var obj = document.getElementById(eucTextbox);
if(!obj) return
if(obj.readOnly) return
//add handlers to convert just before save/preview/upload
var eucBtns = ['wpSave','wpPreview','wpDiff','wpUpload'];
var numbut = 0;
for(var i=0;i<eucBtns.length;i++) {
var btn = getElementByIdOrName(eucBtns[i]);
if(btn) {
$(btn).click(convertEvilUnicodeChars);
numbut++;
}
}
if(numbut == 0) return
var token = getElementByIdOrName('wpEditToken');
if(token.value.indexOf('!euc!') == -1) {
token.value += '!euc!';
} else {
return;
}
if(window.eucCharsCustom) eucChars = eucCharsCustom
var str = obj.value;
for(var i=0;i<eucChars.length;i++) {
var pattern = new RegExp('&#x' + hx4(eucChars[i]) + ';','g');
if(str.search(pattern)!=-1) str = str.replace(pattern,'&#' + eucChars[i] + ';')
}
for(var i=0;i<eucChars.length;i++) {
var pattern = new RegExp(String.fromCharCode(eucChars[i]),'g');
if(str.search(pattern)!=-1) str = str.replace(pattern,'&#x' + hx4(eucChars[i]) + ';')
}
obj.value = str;
}
function convertEvilUnicodeChars() {
var token = getElementByIdOrName('wpEditToken');
if(token.value.indexOf('!euc!') != -1) {
token.value = token.value.replace(/!euc!/,'');
} else {
return;
}
if(window.eucCharsCustom) eucChars = eucCharsCustom
var obj = document.getElementById(eucTextbox);
var str = obj.value;
for(var i=0;i<eucChars.length;i++) {
var pattern = new RegExp('&#x' + hx4(eucChars[i]) + ';','g');
if(str.search(pattern)!=-1) str = str.replace(pattern,String.fromCharCode(eucChars[i]))
}
obj.value = str;
}
function hx4(dec) {
var h = dec.toString(16);
while(h.length < 4) h = '0' + h;
return h;
}
//Needed because the upload form submit has no ID
function getElementByIdOrName(idorname,par) {
var parent = (!par) ? document : par
if(parent.getElementById(idorname)) {
return parent.getElementById(idorname);
} else if(parent.getElementsByName(idorname)[0]) {
return parent.getElementsByName(idorname)[0];
} else {
return false;
}
}