Skip to content

Commit 34df290

Browse files
committedDec 4, 2019
Remove static properties from helpers
1 parent ef7cde4 commit 34df290

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed
 

‎src/helpers.js

+39-38
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
/**
22
* Helpers
33
*/
4+
const escapeTest = /[&<>"']/;
5+
const escapeReplace = /[&<>"']/g;
6+
const escapeTestNoEncode = /[<>"']|&(?!#?\w+;)/;
7+
const escapeReplaceNoEncode = /[<>"']|&(?!#?\w+;)/g;
8+
const escapeReplacements = {
9+
'&': '&amp;',
10+
'<': '&lt;',
11+
'>': '&gt;',
12+
'"': '&quot;',
13+
"'": '&#39;'
14+
};
15+
const getEscapeReplacement = (ch) => escapeReplacements[ch];
416
function escape(html, encode) {
517
if (encode) {
6-
if (escape.escapeTest.test(html)) {
7-
return html.replace(escape.escapeReplace, escape.getReplacement);
18+
if (escapeTest.test(html)) {
19+
return html.replace(escapeReplace, getEscapeReplacement);
820
}
921
} else {
10-
if (escape.escapeTestNoEncode.test(html)) {
11-
return html.replace(escape.escapeReplaceNoEncode, escape.getReplacement);
22+
if (escapeTestNoEncode.test(html)) {
23+
return html.replace(escapeReplaceNoEncode, getEscapeReplacement);
1224
}
1325
}
1426

1527
return html;
1628
}
17-
escape.escapeTest = /[&<>"']/;
18-
escape.escapeReplace = /[&<>"']/g;
19-
escape.escapeTestNoEncode = /[<>"']|&(?!#?\w+;)/;
20-
escape.escapeReplaceNoEncode = /[<>"']|&(?!#?\w+;)/g;
21-
escape.replacements = {
22-
'&': '&amp;',
23-
'<': '&lt;',
24-
'>': '&gt;',
25-
'"': '&quot;',
26-
"'": '&#39;'
27-
};
28-
escape.getReplacement = (ch) => escape.replacements[ch];
29+
30+
const unescapeTest = /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig;
2931

3032
function unescape(html) {
3133
// explicitly match decimal, hex, and named HTML entities
32-
return html.replace(unescape.unescapeTest, (_, n) => {
34+
return html.replace(unescapeTest, (_, n) => {
3335
n = n.toLowerCase();
3436
if (n === 'colon') return ':';
3537
if (n.charAt(0) === '#') {
@@ -40,15 +42,15 @@ function unescape(html) {
4042
return '';
4143
});
4244
}
43-
unescape.unescapeTest = /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig;
4445

46+
const caret = /(^|[^\[])\^/g;
4547
function edit(regex, opt) {
4648
regex = regex.source || regex;
4749
opt = opt || '';
4850
const obj = {
4951
replace: (name, val) => {
5052
val = val.source || val;
51-
val = val.replace(edit.caret, '$1');
53+
val = val.replace(caret, '$1');
5254
regex = regex.replace(name, val);
5355
return obj;
5456
},
@@ -58,14 +60,15 @@ function edit(regex, opt) {
5860
};
5961
return obj;
6062
}
61-
edit.caret = /(^|[^\[])\^/g;
6263

64+
const nonWordAndColonTest = /[^\w:]/g;
65+
const originIndependentUrl = /^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;
6366
function cleanUrl(sanitize, base, href) {
6467
if (sanitize) {
6568
let prot;
6669
try {
6770
prot = decodeURIComponent(unescape(href))
68-
.replace(cleanUrl.protocol, '')
71+
.replace(nonWordAndColonTest, '')
6972
.toLowerCase();
7073
} catch (e) {
7174
return null;
@@ -74,7 +77,7 @@ function cleanUrl(sanitize, base, href) {
7477
return null;
7578
}
7679
}
77-
if (base && !cleanUrl.originIndependentUrl.test(href)) {
80+
if (base && !originIndependentUrl.test(href)) {
7881
href = resolveUrl(base, href);
7982
}
8083
try {
@@ -84,44 +87,42 @@ function cleanUrl(sanitize, base, href) {
8487
}
8588
return href;
8689
}
87-
cleanUrl.protocol = /[^\w:]/g;
88-
cleanUrl.originIndependentUrl = /^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;
90+
91+
const baseUrls = {};
92+
const justDomain = /^[^:]+:\/*[^/]*$/;
93+
const protocol = /^([^:]+:)[\s\S]*$/;
94+
const domain = /^([^:]+:\/*[^/]*)[\s\S]*$/;
8995

9096
function resolveUrl(base, href) {
91-
if (!resolveUrl.baseUrls[' ' + base]) {
97+
if (!baseUrls[' ' + base]) {
9298
// we can ignore everything in base after the last slash of its path component,
9399
// but we might need to add _that_
94100
// https://tools.ietf.org/html/rfc3986#section-3
95-
if (resolveUrl.justDomain.test(base)) {
96-
resolveUrl.baseUrls[' ' + base] = base + '/';
101+
if (justDomain.test(base)) {
102+
baseUrls[' ' + base] = base + '/';
97103
} else {
98-
resolveUrl.baseUrls[' ' + base] = rtrim(base, '/', true);
104+
baseUrls[' ' + base] = rtrim(base, '/', true);
99105
}
100106
}
101-
base = resolveUrl.baseUrls[' ' + base];
107+
base = baseUrls[' ' + base];
102108
const relativeBase = base.indexOf(':') === -1;
103109

104110
if (href.substring(0, 2) === '//') {
105111
if (relativeBase) {
106112
return href;
107113
}
108-
return base.replace(resolveUrl.protocol, '$1') + href;
114+
return base.replace(protocol, '$1') + href;
109115
} else if (href.charAt(0) === '/') {
110116
if (relativeBase) {
111117
return href;
112118
}
113-
return base.replace(resolveUrl.domain, '$1') + href;
119+
return base.replace(domain, '$1') + href;
114120
} else {
115121
return base + href;
116122
}
117123
}
118-
resolveUrl.baseUrls = {};
119-
resolveUrl.justDomain = /^[^:]+:\/*[^/]*$/;
120-
resolveUrl.protocol = /^([^:]+:)[\s\S]*$/;
121-
resolveUrl.domain = /^([^:]+:\/*[^/]*)[\s\S]*$/;
122124

123-
function noop() {}
124-
noop.exec = noop;
125+
const noopTest = { exec: function noopTest() {} };
125126

126127
function merge(obj) {
127128
let i = 1,
@@ -233,7 +234,7 @@ module.exports = {
233234
edit,
234235
cleanUrl,
235236
resolveUrl,
236-
noop,
237+
noopTest,
237238
merge,
238239
splitCells,
239240
rtrim,

‎src/rules.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const {
2-
noop,
2+
noopTest,
33
edit,
44
merge
55
} = require('./helpers.js');
@@ -26,8 +26,8 @@ const block = {
2626
+ '|</(?!script|pre|style)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:\\n{2,}|$)' // (7) closing tag
2727
+ ')',
2828
def: /^ {0,3}\[(label)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,
29-
nptable: noop,
30-
table: noop,
29+
nptable: noopTest,
30+
table: noopTest,
3131
lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
3232
// regex template, placeholders will be replaced according to different paragraph
3333
// interruption rules of commonmark and the original markdown spec:
@@ -114,7 +114,7 @@ block.pedantic = merge({}, block.normal, {
114114
.getRegex(),
115115
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
116116
heading: /^ *(#{1,6}) *([^\n]+?) *(?:#+ *)?(?:\n+|$)/,
117-
fences: noop, // fences not supported
117+
fences: noopTest, // fences not supported
118118
paragraph: edit(block.normal._paragraph)
119119
.replace('hr', block.hr)
120120
.replace('heading', ' *#{1,6} *[^\n]')
@@ -132,7 +132,7 @@ block.pedantic = merge({}, block.normal, {
132132
const inline = {
133133
escape: /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,
134134
autolink: /^<(scheme:[^\s\x00-\x1f<>]*|email)>/,
135-
url: noop,
135+
url: noopTest,
136136
tag: '^comment'
137137
+ '|^</[a-zA-Z][\\w:-]*\\s*>' // self-closing tag
138138
+ '|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>' // open tag
@@ -146,7 +146,7 @@ const inline = {
146146
em: /^_([^\s_])_(?!_)|^\*([^\s*<\[])\*(?!\*)|^_([^\s<][\s\S]*?[^\s_])_(?!_|[^\spunctuation])|^_([^\s_<][\s\S]*?[^\s])_(?!_|[^\spunctuation])|^\*([^\s<"][\s\S]*?[^\s\*])\*(?!\*|[^\spunctuation])|^\*([^\s*"<\[][\s\S]*?[^\s])\*(?!\*)/,
147147
code: /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,
148148
br: /^( {2,}|\\)\n(?!\s*$)/,
149-
del: noop,
149+
del: noopTest,
150150
text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*]|\b_|$)|[^ ](?= {2,}\n))|(?= {2,}\n))/
151151
};
152152

0 commit comments

Comments
 (0)
Please sign in to comment.