• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const assert = require('assert');
4const SAXParser = require('../lib');
5const loadSAXParserTestData = require('../../../test/utils/load-sax-parser-test-data');
6const { writeChunkedToStream } = require('../../../test/utils/common');
7
8exports['Location info (SAX)'] = function() {
9    loadSAXParserTestData().forEach(test => {
10        //NOTE: we've already tested the correctness of the location info with the Tokenizer tests.
11        //So here we just check that SAXParser provides this info in the handlers.
12        const parser = new SAXParser({ sourceCodeLocationInfo: true });
13
14        const handler = ({ sourceCodeLocation }) => {
15            assert.strictEqual(typeof sourceCodeLocation.startLine, 'number');
16            assert.strictEqual(typeof sourceCodeLocation.startCol, 'number');
17            assert.strictEqual(typeof sourceCodeLocation.startOffset, 'number');
18            assert.strictEqual(typeof sourceCodeLocation.endOffset, 'number');
19            assert.ok(sourceCodeLocation.startOffset < sourceCodeLocation.endOffset);
20        };
21
22        parser.on('startTag', handler);
23        parser.on('endTag', handler);
24        parser.on('doctype', handler);
25        parser.on('comment', handler);
26        parser.on('text', handler);
27
28        writeChunkedToStream(test.src, parser);
29    });
30};
31
32exports['Regression - location info for text (GH-153, GH-266)'] = function() {
33    const html = '<!DOCTYPE html><html><head><title>Here is a title</title></html>';
34    const parser = new SAXParser({ sourceCodeLocationInfo: true });
35
36    parser.on('text', ({ sourceCodeLocation }) => {
37        assert.deepStrictEqual(sourceCodeLocation, {
38            startLine: 1,
39            startCol: 35,
40            startOffset: 34,
41            endLine: 1,
42            endCol: 50,
43            endOffset: 49
44        });
45    });
46
47    parser.end(html);
48};
49