-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (76 loc) · 2.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Parser } from 'htmlparser2';
import Core from './lib/core.js';
import State from './lib/state.js';
import voidElements from './lib/void.js';
import _ from 'lodash';
var parxer = function(config, input, next) {
// Defaults
config.prefix = config.prefix || 'cx-';
config.rawSuffix = config.rawSuffix || '-raw';
var state = State.create(config);
var parser = new Parser({
onopentag: function(tagname, attribs) {
state.incrementTagCounter();
var selfClosing = false;
if(voidElements[tagname]) {
selfClosing = true;
state.setSkipClosingTag(tagname);
}
if(state.isBlock('if-false')) {
state.setSkipClosingTag(tagname);
return;
}
if(state.isBlock('inside-fragment')) {
state.setOutput(Core.createTag(tagname, attribs, selfClosing));
return;
}
var matched = !_.isEmpty(attribs) && Core.matchPlugin(config.plugins, tagname, attribs, config, state);
if(!matched) {
state.setOutput(Core.createTag(tagname, attribs, selfClosing));
}
},
onprocessinginstruction: function(name, data) {
if(state.isBlock('if-false')) { return; }
state.setOutput('<' + data + '>');
},
ontext:function(data) {
if(state.isBlock('if-false')) { return; }
state.setOutput(data);
},
oncomment: function(data) {
if(state.isBlock('if-false')) { return; }
state.setOutput('<!--' + data);
},
oncommentend: function() {
if(state.isBlock('if-false')) { return; }
state.setOutput('-->');
},
onclosetag: function(tagname) {
var writeEndTag = true;
if(state.isBlockCloseTag('if-false', tagname) || state.isSkipClosingTag(tagname)) {
writeEndTag = false;
}
state.clearSkipClosingTag(tagname);
state.clearBlocksForClosingTag(tagname);
state.decrementTagCounter();
if(writeEndTag) {
state.setOutput('</' + tagname + '>');
}
},
onend: function() {
state.waitFor(config, true, function(err) {
if(err) { return next(err); }
Core.processDeferredStack(config, state, function() {
state.waitFor(config, false, function (err, content) {
var clonedState = state._raw();
return next(err, clonedState.fragmentIndex, content);
});
});
});
},
recognizeSelfClosing: true
});
parser.end(input);
};
const render = Core.render;
export { parxer, render };