test: parseTag
This commit is contained in:
@@ -74,7 +74,7 @@ function parseTag (html, onTag, escapeHtml) {
|
||||
rethtml += escapeHtml(html.slice(lastPos, tagStart));
|
||||
currentHtml = html.slice(tagStart, currentPos + 1);
|
||||
currentTagName = getTagName(currentHtml);
|
||||
rethtml += onTag(currentPos,
|
||||
rethtml += onTag(tagStart,
|
||||
rethtml.length,
|
||||
currentTagName,
|
||||
currentHtml,
|
||||
|
||||
56
test/test_html_parser.js
Normal file
56
test/test_html_parser.js
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* 测试 html parser
|
||||
*/
|
||||
|
||||
var assert = require('assert');
|
||||
var parser = require('../lib/parser');
|
||||
var parseTag = parser.parseTag;
|
||||
var parseAttr = parser.parseAttr;
|
||||
|
||||
describe('test HTML parser', function () {
|
||||
|
||||
function escapeHtml (html) {
|
||||
return html.replace(/</g, '<').replace(/>/g, '>');
|
||||
}
|
||||
|
||||
it('#parseTag', function () {
|
||||
var i = 0;
|
||||
var html = parseTag('hello<A href="#">www</A>ccc<b>', function (originPosition, position, tag, html, isClosing) {
|
||||
i++;
|
||||
console.log(arguments);
|
||||
if (i === 1) {
|
||||
// 第1个标签
|
||||
assert.equal(originPosition, 5);
|
||||
assert.equal(position, 5);
|
||||
assert.equal(tag, 'a');
|
||||
assert.equal(html, '<A href="#">');
|
||||
assert.equal(isClosing, false);
|
||||
return '[link]';
|
||||
} else if (i === 2) {
|
||||
// 第2个标签
|
||||
assert.equal(originPosition, 20);
|
||||
assert.equal(position, 14);
|
||||
assert.equal(tag, 'a');
|
||||
assert.equal(html, '</A>');
|
||||
assert.equal(isClosing, true);
|
||||
return '[/link]';
|
||||
} else if (i === 3) {
|
||||
// 第3个标签
|
||||
assert.equal(originPosition, 27);
|
||||
assert.equal(position, 24);
|
||||
assert.equal(tag, 'b');
|
||||
assert.equal(html, '<b>');
|
||||
assert.equal(isClosing, false);
|
||||
return '[B]';
|
||||
} else {
|
||||
throw new Error();
|
||||
}
|
||||
}, escapeHtml);
|
||||
assert.equal(html, 'hello[link]www[/link]ccc[B]');
|
||||
});
|
||||
|
||||
it('#parseAttr', function () {
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user