Skip to content

Commit 7b57dd4

Browse files
author
Jiang Shang
committed
修复 specialhtml 的bug
1 parent fa0d3cf commit 7b57dd4

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

dist/Parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ var Parser = (function () {
254254
return this.htmlspecialchars(unsafeTagMatches[0]);
255255
}
256256
}
257-
257+
console.log(text);
258258
text = text.replace(/</g, '&lt;');
259259
text = text.replace(/>/g, '&gt;');
260260

hyperdown.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,14 @@
290290
return '<a href="' + p1 + '">' + p1 + '</a>';
291291
});
292292

293-
// encode unsafe tags
294-
var unsafeTagMatches = /<(\/?)([a-z0-9-]+)(\s+[^>]*)?>/i.exec(text);
295-
if (unsafeTagMatches) {
296-
var whiteLists = this.commonWhiteList + '|' + whiteList;
297-
if (whiteLists.toLowerCase().indexOf(unsafeTagMatches[2].toLowerCase()) !== -1) {
298-
return this.makeHolder(unsafeTagMatches[0]);
293+
text = text.replace(/<(\/?)([a-z0-9-]+)(\s+[^>]*)?>/ig, function (match, p1, p2, p3) {
294+
var whiteLists = _this3.commonWhiteList + '|' + whiteList;
295+
if (whiteLists.toLowerCase().indexOf(p2.toLowerCase()) !== -1) {
296+
return _this3.makeHolder(match);
299297
} else {
300-
return this.htmlspecialchars(unsafeTagMatches[0]);
298+
return _this3.htmlspecialchars(match);
301299
}
302-
}
300+
});
303301

304302
text = text.replace(/</g, '&lt;');
305303
text = text.replace(/>/g, '&gt;');

src/Parser.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,14 @@ export default class Parser {
188188
return `<a href="${p1}">${p1}</a>`
189189
})
190190

191-
// encode unsafe tags
192-
let unsafeTagMatches = /<(\/?)([a-z0-9-]+)(\s+[^>]*)?>/i.exec(text)
193-
if (unsafeTagMatches) {
191+
text = text.replace(/<(\/?)([a-z0-9-]+)(\s+[^>]*)?>/ig, (match, p1, p2, p3) => {
194192
let whiteLists = this.commonWhiteList + '|' + whiteList
195-
if (whiteLists.toLowerCase().indexOf(unsafeTagMatches[2].toLowerCase()) !== -1) {
196-
return this.makeHolder(unsafeTagMatches[0])
193+
if(whiteLists.toLowerCase().indexOf(p2.toLowerCase()) !== -1 ) {
194+
return this.makeHolder(match)
197195
} else {
198-
return this.htmlspecialchars(unsafeTagMatches[0])
196+
return this.htmlspecialchars(match)
199197
}
200-
}
198+
})
201199

202200
text = text.replace(/</g, '&lt;')
203201
text = text.replace(/>/g, '&gt;')

test/test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,8 @@ describe('HyperDown.js', function() {
9494
var codeInList = "1. 如下代码片段中的空检查是否有意义?\n Question * q = new Question;\n if (q == NULL ) {\n }\n 从上面我们了解到,\`operator new\` 在分配内存失败的情况下会调用 \`new_handler\` 尝试让系统释放点内存,然后再次尝试申请内存。如果这时系统中内存确实紧张,即使调用。";
9595
assert.equal('<ol><li><p>如下代码片段中的空检查是否有意义?<br> Question * q = new Question;</p></li></ol>', parser.makeHtml(codeInList));
9696
});
97+
it('specialhtml', function() {
98+
assert.equal('<p>&lt;li&gt;asdf</p>', parser.makeHtml('<li>asdf'));
99+
});
97100
});
98101
});

0 commit comments

Comments
 (0)