parse5 0.6.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.npmignore +3 -1
- package/LICENSE +1 -1
- package/README.md +166 -77
- package/index.js +7 -0
- package/lib/{html.js → const/html.js} +0 -0
- package/lib/{unicode.js → const/unicode.js} +0 -0
- package/lib/{named_entity_trie.js → tokenization_stage/named_entity_trie.js} +1 -1
- package/lib/tokenization_stage/parser_simulator.js +2 -0
- package/lib/{preprocessor.js → tokenization_stage/preprocessor.js} +5 -5
- package/lib/{tokenizer.js → tokenization_stage/tokenizer.js} +37 -37
- package/lib/{default_tree_adapter.js → tree_adapters/default.js} +47 -0
- package/lib/tree_adapters/htmlparser2.js +267 -0
- package/lib/{formatting_element_list.js → tree_construction_stage/formatting_element_list.js} +1 -1
- package/lib/{open_element_stack.js → tree_construction_stage/open_element_stack.js} +2 -2
- package/lib/{parser.js → tree_construction_stage/parser.js} +8 -8
- package/lib/tree_construction_stage/tree_serializer.js +165 -0
- package/package.json +33 -26
- package/test/data/tokenizer/contentModelFlags.test +0 -75
- package/test/data/tokenizer/domjs.test +0 -90
- package/test/data/tokenizer/entities.test +0 -283
- package/test/data/tokenizer/escapeFlag.test +0 -33
- package/test/data/tokenizer/namedEntities.test +0 -44189
- package/test/data/tokenizer/numericEntities.test +0 -1313
- package/test/data/tokenizer/pendingSpecChanges.test +0 -7
- package/test/data/tokenizer/test1.test +0 -196
- package/test/data/tokenizer/test2.test +0 -179
- package/test/data/tokenizer/test3.test +0 -6047
- package/test/data/tokenizer/test4.test +0 -344
- package/test/data/tokenizer/unicodeChars.test +0 -1295
- package/test/data/tokenizer/unicodeCharsProblematic.test +0 -27
- package/test/data/tree-construction/adoption01.dat +0 -275
- package/test/data/tree-construction/adoption02.dat +0 -31
- package/test/data/tree-construction/comments01.dat +0 -146
- package/test/data/tree-construction/doctype01.dat +0 -370
- package/test/data/tree-construction/domjs-unsafe.dat +0 -0
- package/test/data/tree-construction/entities01.dat +0 -603
- package/test/data/tree-construction/entities02.dat +0 -249
- package/test/data/tree-construction/html5test-com.dat +0 -246
- package/test/data/tree-construction/inbody01.dat +0 -43
- package/test/data/tree-construction/isindex.dat +0 -40
- package/test/data/tree-construction/main-element.dat +0 -29
- package/test/data/tree-construction/pending-spec-changes-plain-text-unsafe.dat +0 -0
- package/test/data/tree-construction/pending-spec-changes.dat +0 -52
- package/test/data/tree-construction/plain-text-unsafe.dat +0 -0
- package/test/data/tree-construction/scriptdata01.dat +0 -308
- package/test/data/tree-construction/tables01.dat +0 -212
- package/test/data/tree-construction/tests1.dat +0 -1952
- package/test/data/tree-construction/tests10.dat +0 -799
- package/test/data/tree-construction/tests11.dat +0 -482
- package/test/data/tree-construction/tests12.dat +0 -62
- package/test/data/tree-construction/tests14.dat +0 -74
- package/test/data/tree-construction/tests15.dat +0 -208
- package/test/data/tree-construction/tests16.dat +0 -2299
- package/test/data/tree-construction/tests17.dat +0 -153
- package/test/data/tree-construction/tests18.dat +0 -269
- package/test/data/tree-construction/tests19.dat +0 -1249
- package/test/data/tree-construction/tests2.dat +0 -763
- package/test/data/tree-construction/tests20.dat +0 -455
- package/test/data/tree-construction/tests21.dat +0 -257
- package/test/data/tree-construction/tests22.dat +0 -157
- package/test/data/tree-construction/tests23.dat +0 -155
- package/test/data/tree-construction/tests24.dat +0 -79
- package/test/data/tree-construction/tests25.dat +0 -219
- package/test/data/tree-construction/tests26.dat +0 -313
- package/test/data/tree-construction/tests3.dat +0 -305
- package/test/data/tree-construction/tests4.dat +0 -59
- package/test/data/tree-construction/tests5.dat +0 -191
- package/test/data/tree-construction/tests6.dat +0 -663
- package/test/data/tree-construction/tests7.dat +0 -390
- package/test/data/tree-construction/tests8.dat +0 -148
- package/test/data/tree-construction/tests9.dat +0 -457
- package/test/data/tree-construction/tests_innerHTML_1.dat +0 -741
- package/test/data/tree-construction/tricky01.dat +0 -261
- package/test/data/tree-construction/webkit01.dat +0 -594
- package/test/data/tree-construction/webkit02.dat +0 -94
- package/test/fixtures/formatting_element_list_test.js +0 -267
- package/test/fixtures/open_element_stack_test.js +0 -495
- package/test/fixtures/parser_test.js +0 -160
- package/test/fixtures/tokenizer_test.js +0 -178
- package/test/run_tests.js +0 -10
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
var fs = require('fs'),
|
|
2
|
-
path = require('path'),
|
|
3
|
-
util = require('util'),
|
|
4
|
-
Tokenizer = require('../../lib/tokenizer').Tokenizer;
|
|
5
|
-
|
|
6
|
-
function tokenize(html, initialState, lastStartTag) {
|
|
7
|
-
var tokenizer = new Tokenizer(html),
|
|
8
|
-
nextToken = null,
|
|
9
|
-
out = [];
|
|
10
|
-
|
|
11
|
-
tokenizer.state = initialState;
|
|
12
|
-
|
|
13
|
-
if (lastStartTag)
|
|
14
|
-
tokenizer.lastStartTagName = lastStartTag;
|
|
15
|
-
|
|
16
|
-
do {
|
|
17
|
-
nextToken = tokenizer.getNextToken();
|
|
18
|
-
|
|
19
|
-
//NOTE: append current token to the output sequence in html5lib test suite compatible format
|
|
20
|
-
switch (nextToken.type) {
|
|
21
|
-
case Tokenizer.CHARACTER_TOKEN:
|
|
22
|
-
case Tokenizer.NULL_CHARACTER_TOKEN:
|
|
23
|
-
case Tokenizer.WHITESPACE_CHARACTER_TOKEN:
|
|
24
|
-
out.push(['Character', nextToken.chars]);
|
|
25
|
-
break;
|
|
26
|
-
|
|
27
|
-
case Tokenizer.START_TAG_TOKEN:
|
|
28
|
-
var reformatedAttrs = {};
|
|
29
|
-
|
|
30
|
-
nextToken.attrs.forEach(function (attr) {
|
|
31
|
-
reformatedAttrs[attr.name] = attr.value;
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
var startTagEntry = [
|
|
35
|
-
'StartTag',
|
|
36
|
-
nextToken.tagName,
|
|
37
|
-
reformatedAttrs
|
|
38
|
-
];
|
|
39
|
-
|
|
40
|
-
if (nextToken.selfClosing)
|
|
41
|
-
startTagEntry.push(true);
|
|
42
|
-
|
|
43
|
-
out.push(startTagEntry);
|
|
44
|
-
break;
|
|
45
|
-
|
|
46
|
-
case Tokenizer.END_TAG_TOKEN:
|
|
47
|
-
out.push(['EndTag', nextToken.tagName]);
|
|
48
|
-
break;
|
|
49
|
-
|
|
50
|
-
case Tokenizer.COMMENT_TOKEN:
|
|
51
|
-
out.push(['Comment', nextToken.data]);
|
|
52
|
-
break;
|
|
53
|
-
|
|
54
|
-
case Tokenizer.DOCTYPE_TOKEN:
|
|
55
|
-
out.push([
|
|
56
|
-
'DOCTYPE',
|
|
57
|
-
nextToken.name,
|
|
58
|
-
nextToken.publicId,
|
|
59
|
-
nextToken.systemId,
|
|
60
|
-
!nextToken.forceQuirks
|
|
61
|
-
]);
|
|
62
|
-
break;
|
|
63
|
-
}
|
|
64
|
-
} while (nextToken.type !== Tokenizer.EOF_TOKEN);
|
|
65
|
-
|
|
66
|
-
return concatCharacterTokens(out)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function unicodeUnescape(str) {
|
|
70
|
-
return str.replace(/\\u([\d\w]{4})/gi, function (match, chCodeStr) {
|
|
71
|
-
return String.fromCharCode(parseInt(chCodeStr, 16));
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function unescapeDescrIO(testDescr) {
|
|
76
|
-
testDescr.input = unicodeUnescape(testDescr.input);
|
|
77
|
-
|
|
78
|
-
testDescr.output.forEach(function (tokenEntry) {
|
|
79
|
-
if (tokenEntry === 'ParseError')
|
|
80
|
-
return;
|
|
81
|
-
|
|
82
|
-
//NOTE: unescape token tagName (for StartTag and EndTag tokens), comment data (for Comment token),
|
|
83
|
-
//character token data (for Character token).
|
|
84
|
-
tokenEntry[1] = unicodeUnescape(tokenEntry[1]);
|
|
85
|
-
|
|
86
|
-
//NOTE: unescape token attributes(if we have them).
|
|
87
|
-
if (tokenEntry.length > 2) {
|
|
88
|
-
Object.keys(tokenEntry).forEach(function (attrName) {
|
|
89
|
-
var attrVal = tokenEntry[attrName];
|
|
90
|
-
|
|
91
|
-
delete tokenEntry[attrName];
|
|
92
|
-
tokenEntry[unicodeUnescape(attrName)] = unicodeUnescape(attrVal);
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function concatCharacterTokens(tokenEntries) {
|
|
99
|
-
var result = [];
|
|
100
|
-
|
|
101
|
-
tokenEntries.forEach(function (tokenEntry) {
|
|
102
|
-
if (tokenEntry[0] === 'Character') {
|
|
103
|
-
var lastEntry = result[result.length - 1];
|
|
104
|
-
|
|
105
|
-
if (lastEntry && lastEntry[0] === 'Character') {
|
|
106
|
-
lastEntry[1] += tokenEntry[1];
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
result.push(tokenEntry);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
return result;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function getTokenizerSuitableStateName(testDataStateName) {
|
|
118
|
-
return testDataStateName.toUpperCase().replace(/\s/g, '_');
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function loadTests() {
|
|
122
|
-
var dataDirPath = path.join(__dirname, '../data/tokenizer'),
|
|
123
|
-
testSetFileNames = fs.readdirSync(dataDirPath),
|
|
124
|
-
testIdx = 0,
|
|
125
|
-
tests = [];
|
|
126
|
-
|
|
127
|
-
testSetFileNames.forEach(function (fileName) {
|
|
128
|
-
var filePath = path.join(dataDirPath, fileName),
|
|
129
|
-
testSetJson = fs.readFileSync(filePath).toString(),
|
|
130
|
-
testSet = JSON.parse(testSetJson),
|
|
131
|
-
testDescrs = testSet.tests,
|
|
132
|
-
setName = fileName.replace('.test', '');
|
|
133
|
-
|
|
134
|
-
testDescrs.forEach(function (descr) {
|
|
135
|
-
if (!descr.initialStates)
|
|
136
|
-
descr.initialStates = ['Data state'];
|
|
137
|
-
|
|
138
|
-
if (descr.doubleEscaped)
|
|
139
|
-
unescapeDescrIO(descr);
|
|
140
|
-
|
|
141
|
-
var expected = [];
|
|
142
|
-
|
|
143
|
-
descr.output.forEach(function (tokenEntry) {
|
|
144
|
-
if (tokenEntry !== 'ParseError')
|
|
145
|
-
expected.push(tokenEntry);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
descr.initialStates.forEach(function (initialState) {
|
|
149
|
-
tests.push({
|
|
150
|
-
idx: ++testIdx,
|
|
151
|
-
setName: setName,
|
|
152
|
-
name: descr.description,
|
|
153
|
-
input: descr.input,
|
|
154
|
-
expected: concatCharacterTokens(expected),
|
|
155
|
-
initialState: getTokenizerSuitableStateName(initialState),
|
|
156
|
-
lastStartTag: descr.lastStartTag
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
return tests;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function getFullTestName(test) {
|
|
166
|
-
return [test.idx, '.', test.setName, ' - ', test.name, ' - Initial state: ', test.initialState].join('');
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
//Here we go..
|
|
170
|
-
loadTests().forEach(function (test) {
|
|
171
|
-
exports[getFullTestName(test)] = function (t) {
|
|
172
|
-
var out = tokenize(test.input, test.initialState, test.lastStartTag);
|
|
173
|
-
|
|
174
|
-
t.deepEqual(out, test.expected);
|
|
175
|
-
|
|
176
|
-
t.done();
|
|
177
|
-
};
|
|
178
|
-
});
|
package/test/run_tests.js
DELETED