Sha256: 89a7aa87b4e5b0f9f9dea882fd884f2fffd9e98de53dff0e19efee67368dc944

Contents?: true

Size: 1.77 KB

Versions: 21

Compression:

Stored size: 1.77 KB

Contents

var inverseXML = getInverseObj(require("../maps/xml.json")),
    xmlReplacer = getInverseReplacer(inverseXML);

exports.XML = getInverse(inverseXML, xmlReplacer);

var inverseHTML = getInverseObj(require("../maps/entities.json")),
    htmlReplacer = getInverseReplacer(inverseHTML);

exports.HTML = getInverse(inverseHTML, htmlReplacer);

function getInverseObj(obj){
	return Object.keys(obj).sort().reduce(function(inverse, name){
		inverse[obj[name]] = "&" + name + ";";
		return inverse;
	}, {});
}

function getInverseReplacer(inverse){
	var single = [],
	    multiple = [];

	Object.keys(inverse).forEach(function(k){
		if(k.length === 1){
			single.push("\\" + k);
		} else {
			multiple.push(k);
		}
	});

	//TODO add ranges
	multiple.unshift("[" + single.join("") + "]");

	return new RegExp(multiple.join("|"), "g");
}

var re_nonASCII = /[^\0-\x7F]/g,
    re_astralSymbols = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;

function singleCharReplacer(c){
	return "&#x" + c.charCodeAt(0).toString(16).toUpperCase() + ";";
}

function astralReplacer(c){
	// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
	var high = c.charCodeAt(0);
	var low  = c.charCodeAt(1);
	var codePoint = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000;
	return "&#x" + codePoint.toString(16).toUpperCase() + ";";
}

function getInverse(inverse, re){
	function func(name){
		return inverse[name];
	}

	return function(data){
		return data
				.replace(re, func)
				.replace(re_astralSymbols, astralReplacer)
				.replace(re_nonASCII, singleCharReplacer);
	};
}

var re_xmlChars = getInverseReplacer(inverseXML);

function escapeXML(data){
	return data
			.replace(re_xmlChars, singleCharReplacer)
			.replace(re_astralSymbols, astralReplacer)
			.replace(re_nonASCII, singleCharReplacer);
}

exports.escape = escapeXML;

Version data entries

21 entries across 18 versions & 4 rubygems

Version Path
locomotivecms-4.0.0.alpha1 app/javascript/node_modules/entities/lib/encode.js
locomotivecms-3.4.0 app/javascript/node_modules/entities/lib/encode.js
lanes-0.8.0 node_modules/entities/lib/encode.js
xcodebuild-helper-1.2.5 externals/ios-sim-master/node_modules/entities/lib/encode.js
tck-lambdas-0.3.10 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.3.9 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.3.8 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.3.7 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.3.6 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.3.5 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.3.4 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.3.3 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.3.2 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.3.1 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.3.0 lib/tck/lambdas/chistacojs/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.3.0 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.2.0 lib/tck/lambdas/chistacojs/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.2.0 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.1.6 lib/tck/lambdas/chistacojs/source/node_modules/cheerio/node_modules/entities/lib/encode.js
tck-lambdas-0.1.6 lib/tck/lambdas/chistacojs/node_modules/cheerio/node_modules/entities/lib/encode.js