-
Notifications
You must be signed in to change notification settings - Fork 136
null replacement option available #48
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ exports._getPrivFilters = function () { | |
| // By CSS: (Tab|NewLine|colon|semi|lpar|rpar|apos|sol|comma|excl|ast|midast);|(quot|QUOT) | ||
| // By URI_PROTOCOL: (Tab|NewLine); | ||
| var SENSITIVE_HTML_ENTITIES = /&(?:#([xX][0-9A-Fa-f]+|\d+);?|(Tab|NewLine|colon|semi|lpar|rpar|apos|sol|comma|excl|ast|midast|ensp|emsp|thinsp);|(nbsp|amp|AMP|lt|LT|gt|GT|quot|QUOT);?)/g, | ||
| SENSITIVE_NAMED_REF_MAP = {Tab: '\t', NewLine: '\n', colon: ':', semi: ';', lpar: '(', rpar: ')', apos: '\'', sol: '/', comma: ',', excl: '!', ast: '*', midast: '*', ensp: '\u2002', emsp: '\u2003', thinsp: '\u2009', nbsp: '\xA0', amp: '&', lt: '<', gt: '>', quot: '"', QUOT: '"'}; | ||
| SENSITIVE_NAMED_REF_MAP = {Tab: '\t', NewLine: '\n', colon: ':', semi: ';', lpar: '(', rpar: ')', apos: '\'', sol: '/', comma: ',', excl: '!', ast: '*', midast: '*', ensp: '\u2002', emsp: '\u2003', thinsp: '\u2009', nbsp: '\xA0', amp: '&', AMP: '&', lt: '<', LT: '<', gt: '>', GT: '>', quot: '"', QUOT: '"'}; | ||
|
|
||
| // var CSS_VALID_VALUE = | ||
| // /^(?: | ||
|
|
@@ -61,15 +61,17 @@ exports._getPrivFilters = function () { | |
| URI_PROTOCOL_NAMED_REF_MAP = {Tab: '\t', NewLine: '\n'}; | ||
|
|
||
| var x, | ||
| strReplace = function (s, regexp, callback) { | ||
| _strReplace = function (s, regexp, callback) { | ||
| return s === undefined ? 'undefined' | ||
| : s === null ? 'null' | ||
| : s === null ? 'null' | ||
| : s.toString().replace(regexp, callback); | ||
| }, | ||
| // only the five basic contextual filters yd, yc, yavu, yavs, yavd will be relying on strReplace | ||
| strReplace = _strReplace, | ||
| fromCodePoint = String.fromCodePoint || function(codePoint) { | ||
| if (arguments.length === 0) { | ||
| return ''; | ||
| } | ||
| // the following is dead code as we always provide codePoint | ||
| // if (arguments.length === 0) { return ''; } | ||
|
|
||
| if (codePoint <= 0xFFFF) { // BMP code point | ||
| return String.fromCharCode(codePoint); | ||
| } | ||
|
|
@@ -80,6 +82,29 @@ exports._getPrivFilters = function () { | |
| return String.fromCharCode((codePoint >> 10) + 0xD800, (codePoint % 0x400) + 0xDC00); | ||
| }; | ||
|
|
||
| // patch document.write() and document.writeln() to properly handle NULL for IE 9 or below | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we never know the exact implementation of document.write in different browsers, it does not look good to override any DOM object and its api.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this polyfill targets only those IE7-9, where string after a NULL char will be skipped. other browsers are not affected. It basically wraps the original |
||
| /*jshint -W030 */ | ||
| typeof document !== 'undefined' && function () { | ||
| var doc=document,b=doc.createElement('b'),w=doc.write,wl=doc.writeln, patch; | ||
| b.innerHTML='\x001'; | ||
| if (!b.innerHTML.length && w) { | ||
| patch = function(original) { | ||
| return function() { | ||
| var args = arguments, i = 0, len = args.length, s; | ||
| // replace every NULL char with \uFFFD in every argument | ||
| for (; i < len; i++) { | ||
| if (typeof (s = args[i]) === 'string') { | ||
| args[i] = s.replace(NULL, '\uFFFD'); | ||
| } | ||
| } | ||
| return Function.prototype.apply.call(original, doc, args); | ||
| }; | ||
| }; | ||
| /*jshint -W030 */ | ||
| doc.write = patch(w); | ||
| doc.writeln = patch(wl); | ||
| } | ||
| }(); | ||
|
|
||
| function getProtocol(s) { | ||
| s = s.split(URI_PROTOCOL_COLON, 2); | ||
|
|
@@ -157,7 +182,7 @@ exports._getPrivFilters = function () { | |
| : (num >= 0xD800 && num <= 0xDFFF) || num === 0x0D ? '\uFFFD' | ||
| : x.frCoPt(num); | ||
| } | ||
| return namedRefMap[named || named1] || m; | ||
| return namedRefMap[named || named1]; | ||
| } | ||
|
|
||
| return s === undefined ? 'undefined' | ||
|
|
@@ -182,6 +207,20 @@ exports._getPrivFilters = function () { | |
| } | ||
|
|
||
| return (x = { | ||
| config: function(options) { | ||
| options = options || {}; | ||
|
|
||
| if (options.replaceNull === true) { | ||
| // change strReplace so that it always replace NULL with \uFFFD at last if any | ||
| strReplace = function (s, regexp, callback) { | ||
| return s === undefined ? 'undefined' | ||
| : s === null ? 'null' | ||
| : s.toString().replace(regexp, callback).replace(NULL, '\uFFFD'); | ||
| }; | ||
| } else if (options.replaceNull === false) { | ||
| strReplace = _strReplace; | ||
| } | ||
| }, | ||
| // turn invalid codePoints and that of non-characters to \uFFFD, and then fromCodePoint() | ||
| frCoPt: function(num) { | ||
| return num === undefined || num === null ? '' : | ||
|
|
@@ -217,7 +256,7 @@ exports._getPrivFilters = function () { | |
| * | ||
| */ | ||
| y: function(s) { | ||
| return strReplace(s, SPECIAL_HTML_CHARS, function (m) { | ||
| return _strReplace(s, SPECIAL_HTML_CHARS, function (m) { | ||
| return m === '&' ? '&' | ||
| : m === '<' ? '<' | ||
| : m === '>' ? '>' | ||
|
|
@@ -229,7 +268,7 @@ exports._getPrivFilters = function () { | |
|
|
||
| // This filter is meant to introduce double-encoding, and should be used with extra care. | ||
| ya: function(s) { | ||
| return strReplace(s, AMP, '&'); | ||
| return _strReplace(s, AMP, '&'); | ||
| }, | ||
|
|
||
| // FOR DETAILS, refer to inHTMLData() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why only
ltetc. is case sensitive? how about other pattern likeLtetc?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to http://dev.w3.org/html5/html-author/charref,
Ltis not a valid charref. Did you find it accepted by any browsers?those decoding of & < > " are actually security non-critical, as no regexp is trying to match them. they're there only for those who're interested in using the html decoder (i.e.,
_privFilters.d()).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as per an offline discussion. adding more comments to explain this list.