Created
August 16, 2013 15:03
-
-
Save dax/6250691 to your computer and use it in GitHub Desktop.
Our test of the wrapped node-htmlparser2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var should = require('should'), | |
sinon = require('sinon'), | |
Factory = require('./object-factory'), | |
helper = require('./test-helper'), | |
htmlModule = require('../common/content/html'), | |
HTML = htmlModule.HTML, | |
currentEventIndex = 0; | |
function stubAndCheck(html, eventName, expectedEvents) { | |
html.processors = [html.processors[0]]; | |
html.processors[0][eventName] = function(event) { | |
event.index = currentEventIndex; | |
event.should.include(expectedEvents[currentEventIndex++]); | |
}; | |
} | |
function testParsing(body, expectedEvents, done) { | |
var fzrequest = Factory.build('fasterizeRequest'), | |
html = new HTML(body, fzrequest, {"content-type":"text/html"}); | |
stubAndCheck(html, 'processOpenTag', expectedEvents); | |
stubAndCheck(html, 'processCloseTag', expectedEvents); | |
stubAndCheck(html, 'processComment', expectedEvents); | |
stubAndCheck(html, 'processText', expectedEvents); | |
html.parse(function(errors) { | |
should.not.exist(errors); | |
done(); | |
}); | |
} | |
describe("HTML parser2 tests", function () { | |
beforeEach(function() { | |
var configs = helper.setupCommonModules(); | |
var defaultCustomerConfig = configs[0]; | |
defaultCustomerConfig.data.updateDefaultConfig({rules: { | |
lazyload: {active:true}, | |
debug: {active: false}, | |
sharding: {active: true}, | |
concatcss: {active: true}, | |
concatjs: {active: true}, | |
deferjs: {active:false} | |
}}); | |
currentEventIndex = 0; | |
}); | |
describe("#parse", function() { | |
it("should parse HTML and give no errors", function(done){ | |
testParsing(['<html><head><script type="text/javascript">var foo=1</script>', // 61 | start: 0, end: 60 | |
'<style>', // 7 | start: 63, end: 69 | |
'<!--', // 4 | start: 72, end: 75 | |
'foo', // 3 | start: 78, end: 80 | |
'-->', // 3 | start: 83, end: 85 | |
'</style></head><body>hello world<!--', // 36 | start: 88, end: 123 | |
'comment <test></test>--></body></html>' // 24 | start: 126, end: 163 | |
].join('\r\n'), | |
[ | |
{eventType: 'OpenTag', name: 'html', attributes: {}, | |
endIndex: 5, startIndex: 0, index: 0}, | |
{eventType: 'OpenTag', name: 'head', attributes: {}, | |
endIndex: 11, startIndex: 6, index: 1}, | |
{eventType: 'OpenTag', name: 'script', attributes: { type: 'text/javascript' }, | |
endIndex: 42, startIndex: 12, index: 2}, | |
{eventType: 'Text', data: 'var foo=1', | |
endIndex: 51, startIndex: 43, index: 3}, | |
{eventType: 'CloseTag', name: 'script', | |
endIndex: 60, startIndex: 52, index: 4}, | |
{eventType: 'Text', data: '\r\n', | |
endIndex: 62, startIndex: 61, index: 5}, | |
{eventType: 'OpenTag', name: 'style', attributes: {}, | |
endIndex: 69, startIndex: 63, index: 6}, | |
{eventType: 'Text', data: '\r\n<!--\r\nfoo\r\n-->\r\n', | |
endIndex: 87, startIndex: 70, index: 7}, | |
{eventType: 'CloseTag', name: 'style', | |
endIndex: 95, startIndex: 88, index: 8}, | |
{eventType: 'CloseTag', name: 'head', | |
endIndex: 102, startIndex: 96, index: 9}, | |
{eventType: 'OpenTag', name: 'body', attributes: {}, | |
endIndex: 108, startIndex: 103, index: 10}, | |
{eventType: 'Text', data: 'hello world', | |
endIndex: 119, startIndex: 109, index: 11}, | |
{eventType: 'Comment', data: '\r\ncomment <test></test>', | |
endIndex: 149, startIndex: 120, index: 12}, | |
{eventType: 'CloseTag', name: 'body', | |
endIndex: 156, startIndex: 150, index: 13}, | |
{eventType: 'CloseTag', name: 'html', | |
endIndex: 163, startIndex: 157, index: 14}], | |
done); | |
}); | |
it("should parse 2 consecutive comments without merging them", function(done){ | |
testParsing('<!--comment <test></test>--><!-- other comment -->', | |
[ | |
{eventType: 'Comment', data: 'comment <test></test>', | |
endIndex: 27, startIndex: 0, index: 0}, | |
{eventType: 'Comment', data: ' other comment ', | |
endIndex: 49, startIndex: 28, index: 1} | |
], | |
done); | |
}); | |
it("should correctly parse the CDATA", function (done) { | |
testParsing("<script type='text/javascript'><!--//<![CDATA[\nvar d=3;\n//]]>--></script>", | |
[ | |
{ eventType: 'OpenTag', name: 'script', attributes: { type: 'text/javascript' }, | |
endIndex: 30, startIndex: 0, index: 0 }, | |
{ eventType: 'Text', data: '<!--//<![CDATA[\nvar d=3;\n//]]>-->', | |
endIndex: 63, startIndex: 31, index: 1 }, | |
{ eventType: 'CloseTag', name: 'script', | |
endIndex: 72, startIndex: 64, index: 2 } | |
], | |
done); | |
}); | |
it("should ignore '>' when in an attribute value", function (done) { | |
testParsing('<!doctype html>\n<img src="arrow_pink_bottom.gif" alt=">" style="height:42" /><test />', | |
[ | |
{ eventType: 'Text', data: '\n', | |
endIndex: 15, startIndex: 14, index: 0 }, | |
{ eventType: 'OpenTag', name: 'img', | |
attributes: { | |
src: 'arrow_pink_bottom.gif', | |
alt: '>', | |
style: 'height:42' | |
}, | |
endIndex: 76, startIndex: 16, index: 1 | |
}, | |
{ eventType: 'CloseTag', name: 'img', | |
endIndex: 76, startIndex: 16, index: 2 }, | |
{ eventType: 'OpenTag', name: 'test', attributes: {}, | |
endIndex: 84, startIndex: 77, index: 3 }, | |
{ eventType: 'CloseTag', name: 'test', | |
endIndex: 84, startIndex: 77, index: 4 } | |
], | |
done); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment