Jump to content

User:Splarka/diffreveal.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* Reveal unicode whitespace and invisible formatting characters in diffs, version [0.0.1]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/diffreveal.js
*/

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];
if(mw.config.get('wgAction') == 'view' || mw.config.get('wgAction') == 'submit') {
  $(document).ready(diffRevealInit);
}

function diffRevealInit() {
  var dt = document.querySelectorAll('table.diff');
  if(!dt || dt.length < 1) return;
  dt = dt[0];
  var diff = document.querySelectorAll('span.diffchange-inline');
  for(var i=0;i<diff.length;i++) {
    var chr = utf8array(getText(diff[i]));
    var txt = '';
    for(var j=0;j<chr.length;j++) {
      txt += character(chr[j]);
      //if(chr[j] > 128) {
      if($.inArray(chr[j], eucChars) != -1) {
        txt += '[U+' + hex(chr[j]) + ']';
      }
    }
    //diff[i].title = '' + txt;
    while(diff[i].firstChild) diff[i].removeChild(diff[i].firstChild)
    diff[i].appendChild(document.createTextNode(txt));
  }
}

function getText(object) {
  if (object.nodeType == 3) return object.nodeValue;
  var txt = [];
  var i=0;
  while(object.childNodes[i]) {
    txt[txt.length] = getText(object.childNodes[i]);
    i++;
  }
  return txt.join('');
}

function utf8array(txt) {
  var list = [];
  for(var i=0;i<txt.length;i++) {
    var num = txt.charCodeAt(i);
    if(num >= 55296 && num <= 56319) {
      var num2 = txt.charCodeAt(i+1);
      num = ((num-55296)*1024) + (num2-56320) + 65536;
      list.push(num);
      i++;
    } else {
      list.push(num);
    }
  }
  return list;
}

function hex(num) {
  var hex = num.toString(16).toUpperCase();
  while(hex.length < 4) hex = '0' + hex
  return hex;
}

function character(num) {
  if(num >= 65536 && num <= 1114111) {
    var num1 = Math.floor((num - 65536) / 1024) + 55296;
    var num2 = ((num - 65536) % 1024) + 56320;
    return String.fromCharCode(num1) + String.fromCharCode(num2);
  } else {
    return String.fromCharCode(num);
  }
}