• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var assert = require('assert'),
2    path = require('path'),
3    HTML = require('../../lib/common/html'),
4    JsDomParser = require('../../index').JsDomParser,
5    Serializer = require('../../index').TreeSerializer,
6    TestUtils = require('../test_utils');
7
8exports['State guard'] = function () {
9    var docHtml = '<script>Yoyo</script>',
10        getParsingUnit = function () {
11            var parsing = JsDomParser.parseDocument(docHtml);
12
13            parsing.done(function noop() {
14                //NOTE: do nothing =)
15            });
16
17            return parsing;
18        };
19
20    assert.throws(function () {
21        var parsing = getParsingUnit();
22
23        parsing.suspend();
24        parsing.suspend();
25    });
26
27    assert.throws(function () {
28        var parsing = getParsingUnit();
29
30        parsing.resume();
31    });
32};
33
34exports['Reentrancy'] = function (done) {
35    var serializer = new Serializer(),
36        asyncAssertionCount = 0,
37        docHtml1 = '<!DOCTYPE html><html><head><script>Yoyo</script></head><body></body></html>',
38        docHtml2 = '<!DOCTYPE html><html><head></head><body>Beep boop</body></html>',
39        fragments = [
40            '<div>Hey ya!</div>',
41            '<p><a href="#"></a></p>',
42            '<template><td>yo</td></template>',
43            '<select><template><option></option></template></select>'
44        ];
45
46    var parsing1 = JsDomParser.parseDocument(docHtml1)
47
48        .done(function (document1) {
49            var actual = serializer.serialize(document1);
50
51            assert.strictEqual(asyncAssertionCount, 5);
52            assert.ok(actual === docHtml1, TestUtils.getStringDiffMsg(actual, docHtml1));
53            done();
54        })
55
56        .handleScripts(function () {
57            parsing1.suspend();
58
59            setTimeout(function () {
60                fragments.forEach(function (fragment) {
61                    var actual = serializer.serialize(JsDomParser.parseInnerHtml(fragment));
62                    assert.ok(actual === fragment, TestUtils.getStringDiffMsg(actual, fragment));
63                    asyncAssertionCount++;
64                });
65
66                var parsing2 = JsDomParser.parseDocument(docHtml2);
67
68                parsing2.done(function (document2) {
69                    var actual = serializer.serialize(document2);
70
71                    assert.ok(actual === docHtml2, TestUtils.getStringDiffMsg(actual, docHtml2));
72                    asyncAssertionCount++;
73                    parsing1.resume();
74                });
75            });
76        });
77};
78
79
80TestUtils.generateTestsForEachTreeAdapter(module.exports, function (_test, treeAdapter) {
81    function getFullTestName(test) {
82        return ['JsDomParser - ', test.idx, '.', test.setName, ' - ', test.input].join('');
83    }
84
85    function parseDocument(input, callback) {
86        var parsing = JsDomParser.parseDocument(input, treeAdapter)
87
88            .done(callback)
89
90            .handleScripts(function (document, scriptElement) {
91                parsing.suspend();
92
93                //NOTE: test parser suspension in different modes (sync and async).
94                //If we have script then execute it and resume parser asynchronously.
95                //Otherwise - resume synchronously.
96                var scriptTextNode = treeAdapter.getChildNodes(scriptElement)[0],
97                    script = scriptTextNode && treeAdapter.getTextNodeContent(scriptTextNode);
98
99                //NOTE: don't pollute test runner output by console.log() calls from test scripts
100                if (script && script.trim() && script.indexOf('console.log') === -1) {
101                    setTimeout(function () {
102                        //NOTE: mock document for script evaluation
103                        document.write = function (html) {
104                            parsing.documentWrite(html);
105                        };
106
107                        try {
108                            eval(script);
109                        } catch (err) {
110                            //NOTE: ignore broken scripts from test data
111                        }
112
113                        parsing.resume();
114                    });
115                }
116
117                else
118                    parsing.resume();
119            });
120    }
121
122    //Here we go..
123    TestUtils.loadTreeConstructionTestData([
124        path.join(__dirname, '../data/tree_construction'),
125        path.join(__dirname, '../data/tree_construction_jsdom')
126    ], treeAdapter).forEach(function (test) {
127        _test[getFullTestName(test)] = function (done) {
128            function assertResult(result) {
129                var actual = TestUtils.serializeToTestDataFormat(result, treeAdapter),
130                    msg = TestUtils.prettyPrintParserAssertionArgs(actual, test.expected);
131
132                assert.strictEqual(actual, test.expected, msg);
133
134                done();
135            }
136
137            if (test.fragmentContext) {
138                var result = JsDomParser.parseInnerHtml(test.input, test.fragmentContext, treeAdapter);
139                assertResult(result);
140            }
141
142            else
143                parseDocument(test.input, assertResult);
144        };
145    });
146});
147
148
149