Skip to content

Commit 10f19a0

Browse files
committed
Initial commit
0 parents  commit 10f19a0

File tree

8 files changed

+985
-0
lines changed

8 files changed

+985
-0
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
*.log
3+
.nyc_output/
4+
coverage/
5+
node_modules/
6+
hast-util-raw.js
7+
hast-util-raw.min.js

.travis.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
language: node_js
2+
node_js:
3+
- '4.0'
4+
- '5.0'
5+
- '6.0'
6+
after_script: bash <(curl -s https://codecov.io/bash)
7+
deploy:
8+
- provider: npm
9+
10+
api_key:
11+
secure: I5aQ2u0h+b97z5NAzf7Mav/gQ6ZUGx/FwICdTZ8d98S44k4Rf+b575OzpFidfPQeqjWcPqJ4ZAAg1GJg5tYEapH9z+SY2R7VkrrW6bTmGNH87GuENfqLkKqoD5gIeTcuuJ282dNHJlqKSlktdBaBN+lRKnYBJvlQM3X8x/WmD90uFLaGmsw+rWHU/OsgSuj5qpbyUzQAFQrVANYPYCxH6L1r9zWjK7AA9k0WKTglM58nW709AR87ZHnF1qlMbij4WNpigRzgJ8apYi9Lja+ceWTqu+nqO6ixoHXZguMoAcFf+iid21GE0xB2gktWZ5ivU8ZTjwU6GyUH+vzjH6wA0Y3Uk2CZkHPV7yLMxwFUzN5Pb0YYVn0MbcdvNwr0ONafbhICpD4C+8q3r5lJrjsAJfv/CAe4sLolDbEPdCj4H9sRHB7cIisH6AstbLWN/f/pdauSnoP+yA4QPA6neExnGJpuyaci9CCqc0M2qzpl/pjVVeWZCxH19OG/HNrpKrxtCxMB0XLlgfLXQT55gW7nTYYh3cm5o2MJTAunDaKPBPvVuwMOrJmUuNf5Kd2QcNdCgsjHQAxQAWZsTl7ERBit6saexwh6ZJt8K3zdZ1Cu2Gv+Xdm9xE2Y/ud0YkLT1/RT+xU2FKjCkbwv3FcU26XgQWK8Hbcq2WB25UHzb5nkjF4=
12+
on:
13+
tags: true
14+
node: '5.0'
15+
- provider: releases
16+
api_key:
17+
secure: mIhrrh340n/dBSEJC+JhPlQPKSz0d18nRj9vPlcU1eQGypTvvl6Liiy7BdyWYkv+cNiuF0gvkkBAvVhWUmtkKM+RcNGzDQVZ3dn4+lhEyAzgBajlsdPyQtwB6dw4Blx4w1O7O5m5Ely3yAW2MjKbG2UKra9fZ9DVLnpQLQ5R50qscWx0pYkq4vSNDdOMBjDaMJ7gD5svzwtGGRJjSXYyYmv/PVbZo9lrTBmRQD4yJqMwljPiCA5FMjR5WU1TdvciFsvHdO8IyBvjkjKqNfRuBWnlMLx5OteUNFytsGLd/ba3+K7vmXe+vyO4BrzZKI5YtdXhtBvlxh2oqUGS1QGGJqok4Pg1BMmH1W7BQvhGaGJDySRX8Kfap6lEJSWHuokuusdp2s53JEwY6dzEDZi6vHF0/lroIjzfdCewI2n2Nn/hgnZvXloq5iMtxk+rBPssxlvWFIbvVXw2s+CsIg9wFBswUxaKREj+Qcf5g0lg2ZQXXhtPRFGJ8WzSmfX2c230YGlb0SgLUfXrzEc7F4okMatZE6dkiVN27jD/1draPP3gjQBIeafPISWcT6AhST9ZOXKayyfX9n9zjrP7tLZuxPdcgQqeJPSn2bInhqOzw40EtUQBStpYQp7ICr7MDlOFuZqlC0wV7lGWYi4B4s0yOuYuBvNhfK4kNL/Opmdm0UQ=
18+
file:
19+
- "hast-util-raw.js"
20+
- "hast-util-raw.min.js"
21+
on:
22+
tags: true
23+
node: '6.0'

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2016 Titus Wormer <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

index.js

+264
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
'use strict';
2+
3+
/* Dependencies. */
4+
var Parser = require('parse5/lib/parser');
5+
var pos = require('unist-util-position');
6+
var fromParse5 = require('hast-util-from-parse5');
7+
var toParse5 = require('hast-util-to-parse5');
8+
var voids = require('html-void-elements');
9+
var ns = require('web-namespaces');
10+
var zwitch = require('zwitch');
11+
12+
/* Expose. */
13+
module.exports = wrap;
14+
15+
/* Constants. */
16+
var IN_TEMPLATE_MODE = 'IN_TEMPLATE_MODE';
17+
var CHARACTER_TOKEN = 'CHARACTER_TOKEN';
18+
var START_TAG_TOKEN = 'START_TAG_TOKEN';
19+
var END_TAG_TOKEN = 'END_TAG_TOKEN';
20+
var HIBERNATION_TOKEN = 'HIBERNATION_TOKEN';
21+
var COMMENT_TOKEN = 'COMMENT_TOKEN';
22+
var DOCTYPE_TOKEN = 'DOCTYPE_TOKEN';
23+
var DOCUMENT = 'document';
24+
var FRAGMENT = 'fragment';
25+
26+
function wrap(tree, file) {
27+
var parser = new Parser({locationInfo: true});
28+
var one = zwitch('type');
29+
var mode = inferMode(tree);
30+
var preprocessor;
31+
var result;
32+
33+
one.handlers.root = root;
34+
one.handlers.element = element;
35+
one.handlers.text = text;
36+
one.handlers.comment = comment;
37+
one.handlers.doctype = doctype;
38+
one.handlers.raw = raw;
39+
one.unknown = unknown;
40+
41+
result = fromParse5(mode === FRAGMENT ? fragment() : document(), file);
42+
43+
/* Unpack if possible and when not given a `root`. */
44+
if (tree.type !== 'root' && result.children.length === 1) {
45+
return result.children[0];
46+
}
47+
48+
return result;
49+
50+
function fragment() {
51+
var context;
52+
var mock;
53+
var doc;
54+
55+
context = {
56+
nodeName: 'template',
57+
tagName: 'template',
58+
attrs: [],
59+
namespaceURI: ns.html,
60+
childNodes: []
61+
};
62+
63+
mock = {
64+
nodeName: 'documentmock',
65+
tagName: 'documentmock',
66+
attrs: [],
67+
namespaceURI: ns.html,
68+
childNodes: []
69+
};
70+
71+
doc = {
72+
nodeName: '#document-fragment',
73+
childNodes: []
74+
};
75+
76+
parser._bootstrap(mock, context);
77+
parser._pushTmplInsertionMode(IN_TEMPLATE_MODE);
78+
parser._initTokenizerForFragmentParsing();
79+
parser._insertFakeRootElement();
80+
parser._resetInsertionMode();
81+
parser._findFormInFragmentContext();
82+
83+
preprocessor = parser.tokenizer.preprocessor;
84+
85+
one(tree);
86+
87+
parser._adoptNodes(mock.childNodes[0], doc);
88+
89+
return doc;
90+
}
91+
92+
function document() {
93+
var doc = parser.treeAdapter.createDocument();
94+
95+
parser._bootstrap(doc, null);
96+
97+
one(tree);
98+
99+
return doc;
100+
}
101+
102+
function all(nodes) {
103+
var length = nodes.length;
104+
var index = -1;
105+
106+
while (++index < length) {
107+
one(nodes[index]);
108+
}
109+
}
110+
111+
function root(node) {
112+
all(node.children);
113+
}
114+
115+
function element(node) {
116+
var empty = voids.indexOf(node.tagName) !== -1;
117+
118+
parser._processToken(startTag(node, empty), ns.html);
119+
120+
all(node.children);
121+
122+
if (!empty) {
123+
parser._processToken(endTag(node));
124+
}
125+
}
126+
127+
function text(node) {
128+
parser._processToken({
129+
type: CHARACTER_TOKEN,
130+
chars: node.value,
131+
location: {
132+
line: pos.start(node).line,
133+
col: pos.start(node).column,
134+
startOffset: pos.start(node).offset,
135+
endOffset: pos.end(node).offset
136+
}
137+
});
138+
}
139+
140+
function doctype(node) {
141+
var p5 = toParse5(node);
142+
parser._processToken({
143+
type: DOCTYPE_TOKEN,
144+
name: p5.name,
145+
forceQuirks: false,
146+
publicId: p5.publicId,
147+
systemId: p5.systemId
148+
});
149+
}
150+
151+
function comment(node) {
152+
parser._processToken({
153+
type: COMMENT_TOKEN,
154+
data: node.value,
155+
location: {
156+
line: pos.start(node).line,
157+
col: pos.start(node).column,
158+
startOffset: pos.start(node).offset,
159+
endOffset: pos.end(node).offset
160+
}
161+
});
162+
}
163+
164+
function raw(node) {
165+
var start = pos.start(node).offset;
166+
167+
preprocessor.html = null;
168+
preprocessor.lastCharPos = preprocessor.pos = -1;
169+
170+
if (start !== null) {
171+
preprocessor.droppedBufferSize = start;
172+
}
173+
174+
parser.tokenizer.write(node.value);
175+
176+
run(parser);
177+
}
178+
}
179+
180+
function run(p) {
181+
var tokenizer = p.tokenizer;
182+
var token;
183+
184+
while (!p.stopped) {
185+
p._setupTokenizerCDATAMode();
186+
187+
token = tokenizer.getNextToken();
188+
189+
if (token.type === HIBERNATION_TOKEN) {
190+
token = tokenizer.currentCharacterToken || tokenizer.currentToken;
191+
192+
if (token) {
193+
p._processInputToken(token);
194+
}
195+
196+
tokenizer.currentCharacterToken = tokenizer.currentToken = null;
197+
198+
break;
199+
}
200+
201+
p._processInputToken(token);
202+
}
203+
}
204+
205+
function startTag(node, empty) {
206+
var start = pos.start(node);
207+
208+
return {
209+
type: START_TAG_TOKEN,
210+
tagName: node.tagName,
211+
selfClosing: false,
212+
attrs: attributes(node),
213+
location: {
214+
line: start.line,
215+
col: start.column,
216+
startOffset: start.offset,
217+
endOffset: start.offset,
218+
attrs: {},
219+
startTag: {
220+
line: start.line,
221+
col: start.column,
222+
startOffset: start.offset,
223+
endOffset: empty ? pos.end(node).offset : start.offset
224+
}
225+
}
226+
};
227+
}
228+
229+
function attributes(node) {
230+
return toParse5({
231+
type: 'element',
232+
properties: node.properties
233+
}).attrs;
234+
}
235+
236+
function endTag(node) {
237+
var end = pos.end(node);
238+
239+
return {
240+
type: END_TAG_TOKEN,
241+
tagName: node.tagName,
242+
attrs: [],
243+
location: {
244+
line: end.line,
245+
col: end.column,
246+
startOffset: end.offset,
247+
endOffset: end.offset
248+
}
249+
};
250+
}
251+
252+
function unknown(node) {
253+
throw new Error('Cannot compile `' + node.type + '` node');
254+
}
255+
256+
function inferMode(node) {
257+
var head = node.type === 'root' ? node.children[0] : node;
258+
259+
if (head && (head.type === 'doctype' || head.tagName === 'html')) {
260+
return DOCUMENT;
261+
}
262+
263+
return FRAGMENT;
264+
}

0 commit comments

Comments
 (0)