MediaWiki:Gadget-Numerakri.js

/** * Convert Arabic numerals to Devanagari. * Thanks to Daniel Friesen. * * @revision 2012-07-05 * @author Timo Tijhof, 2012 */ /*jshint browser:true, boss:true */ /*globals jQuery, NodeFilter */ jQuery(document).ready(function ($) {	"use strict";	// Cache & utilities	var el, w,		rConvert = /[0-9]/g,		convertMap = {'0':'०','1':'१','2':'२','3':'३','4':'४','5':'५','6':'६','7':'७','8':'८','9':'९'};

function convertMatch (match) { return convertMap[match]; }

function handleTextNode(el) { var n = el.parentNode.nodeName.toLowerCase; // Node.TEXT_NODE = 3 if (el.nodeType === 3 && n !== 'input' && n !== 'textarea') { el.nodeValue = el.nodeValue.replace(rConvert, convertMatch); }	}

// Fallback for document.createTreeWalker for older browsers (IE6-8) function walkTheDOM(node, func) { func(node); node = node.firstChild; while (node) { walkTextNodes(node, func); node = node.nextSibling; }	}

// Replace numerals if (document.createTreeWalker) { w = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false); while (el = w.nextNode) { handleTextNode(el); }	} else { walkTheDOM(document.body, handleTextNode); } });