Browse Source

Fixed replaceEntities, default preset, improved coverage

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
26a22b182a
  1. 39
      lib/common/utils.js
  2. 1
      lib/configs/default.js
  3. 3
      lib/rules_core/replacements.js
  4. 11
      test/fixtures/remarkable/commonmark_extras.txt
  5. 7
      test/fixtures/remarkable/typographer.txt
  6. 2
      test/fixtures/remarkable/xss.txt
  7. 12
      test/misc.js

39
lib/common/utils.js

@ -11,20 +11,18 @@ function isString(obj) { return _class(obj) === '[object String]'; }
// //
function assign(obj /*from1, from2, from3, ...*/) { function assign(obj /*from1, from2, from3, ...*/) {
var sources = Array.prototype.slice.call(arguments, 1); var sources = Array.prototype.slice.call(arguments, 1);
while (sources.length) {
var source = sources.shift(); sources.forEach(function (source) {
if (!source) { continue; } if (!source) { return; }
if (typeof source !== 'object') { if (typeof source !== 'object') {
throw new TypeError(source + 'must be object'); throw new TypeError(source + 'must be object');
} }
for (var p in source) { Object.keys(source).forEach(function (key) {
if (source.hasOwnProperty(p)) { obj[key] = source[key];
obj[p] = source[p]; });
} });
}
}
return obj; return obj;
} }
@ -66,18 +64,31 @@ function fromCodePoint(c) {
return String.fromCharCode(c); return String.fromCharCode(c);
} }
var NAMED_ENTITY_RE = /&([a-z][a-z0-9]{1,31});/gi; var NAMED_ENTITY_RE = /&([a-z#][a-z0-9]{1,31});/gi;
var DIGITAL_ENTITY_TEST_RE = /^#((?:x[a-f0-9]{1,8}|[0-9]{1,8}))/i;
var entities = require('./entities'); var entities = require('./entities');
function replaceEntities(str) { function replaceEntityPattern(match, name) {
if (str.indexOf('&') < 0) { return str; } var code = 0;
return str.replace(NAMED_ENTITY_RE, function(match, name) {
if (entities.hasOwnProperty(name)) { if (entities.hasOwnProperty(name)) {
return entities[name]; return entities[name];
} else if (name.charCodeAt(0) === 0x23/* # */ && DIGITAL_ENTITY_TEST_RE.test(name)) {
code = name[1].toLowerCase() === 'x' ?
parseInt(name.slice(2), 16)
:
parseInt(name.slice(1), 10);
if (isValidEntityCode(code)) {
return fromCodePoint(code);
}
} }
return match; return match;
}); }
function replaceEntities(str) {
if (str.indexOf('&') < 0) { return str; }
return str.replace(NAMED_ENTITY_RE, replaceEntityPattern);
} }

1
lib/configs/default.js

@ -36,6 +36,7 @@ module.exports = {
'inline', 'inline',
'references', 'references',
'replacements', 'replacements',
'linkify',
'smartquotes', 'smartquotes',
'references', 'references',
'abbr2' 'abbr2'

3
lib/rules_core/replacements.js

@ -20,10 +20,7 @@ function replaceScopedAbbr(str) {
if (str.indexOf('(') < 0) { return str; } if (str.indexOf('(') < 0) { return str; }
return str.replace(SCOPED_ABBR_RE, function(match, name) { return str.replace(SCOPED_ABBR_RE, function(match, name) {
if (SCOPED_ABBR.hasOwnProperty(name.toLowerCase())) {
return SCOPED_ABBR[name.toLowerCase()]; return SCOPED_ABBR[name.toLowerCase()];
}
return match;
}); });
} }

11
test/fixtures/remarkable/commonmark_extras.txt

@ -96,3 +96,14 @@ Not a list item
. .
<p>1.list</p> <p>1.list</p>
. .
Coverage. Direcctive can terminate paragraph.
.
a
<?php
.
<p>a</p>
<?php
.

7
test/fixtures/remarkable/typographer.txt

@ -1,3 +1,10 @@
.
(bad)
.
<p>(bad)</p>
.
copyright copyright
. .
(c) (C) (c) (C)

2
test/fixtures/remarkable/xss.txt

@ -42,7 +42,7 @@ Should not allow some protocols in links and images
. .
[xss link](&#34;&#62;&#60;script&#62;alert&#40;&#34;xss&#34;&#41;&#60;/script&#62;) [xss link](&#34;&#62;&#60;script&#62;alert&#40;&#34;xss&#34;&#41;&#60;/script&#62;)
. .
<p><a href="&amp;#34;&amp;#62;&amp;#60;script&amp;#62;alert&amp;#40;&amp;#34;xss&amp;#34;&amp;#41;&amp;#60;/script&amp;#62;">xss link</a></p> <p><a href="%22%3E%3Cscript%3Ealert(%22xss%22)%3C/script%3E">xss link</a></p>
. .

12
test/misc.js

@ -29,6 +29,18 @@ describe('Utils', function () {
assert.strictEqual(isValidEntityCode(0x7F), false); assert.strictEqual(isValidEntityCode(0x7F), false);
}); });
it('replaceEntities', function () {
var replaceEntities = require('../lib/common/utils').replaceEntities;
assert.strictEqual(replaceEntities('&amp;'), '&');
assert.strictEqual(replaceEntities('&#32;'), ' ');
assert.strictEqual(replaceEntities('&#x20;'), ' ');
assert.strictEqual(replaceEntities('&amp;&amp;'), '&&');
assert.strictEqual(replaceEntities('&am;'), '&am;');
assert.strictEqual(replaceEntities('&#00;'), '&#00;');
});
it('assign', function () { it('assign', function () {
var assign = require('../lib/common/utils').assign; var assign = require('../lib/common/utils').assign;

Loading…
Cancel
Save