-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathindex.js
216 lines (180 loc) · 7.31 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// code pulled from https://www.npmjs.com/package/html2hyperscript
var Parser = require('htmlparser2').Parser;
var camel = require('to-camel-case');
var ent = require('ent');
var isEmpty = require('is-empty');
var thisIsSVGTag = require('./lib/svg-namespaces').thisIsSVGTag,
getSVGNamespace = require('./lib/svg-namespaces').getSVGNamespace,
getSVGAttributeNamespace = require('./lib/svg-namespaces').getSVGAttributeNamespace;
var elementStack = [];
function ItemList(parent) {
this.parent = parent;
this.content = '';
this.spacer = '';
this.indent = parent ? parent.indent : '';
this.isFirstItem = true;
}
ItemList.prototype.addSpace = function (space) {
this.spacer += space;
if (space.indexOf("\n") !== -1) {
// reset indent when there are new lines
this.indent = /[^\n]*$/.exec(space)[0];
} else {
// otherwise keep appending to current indent
this.indent += space;
}
}
ItemList.prototype.add = function (data, ignoreComma) {
if (!ignoreComma) {
if (!this.isFirstItem) {
this.content += this.spacer.length ? ',' : ', ';
}
this.isFirstItem = false;
}
this.content += this.spacer;
this.spacer = '';
this.content += data;
}
module.exports = function(html, cb) {
var currentItemList = new ItemList(null);
var parser = new Parser({
onopentag: function (name, attribs) {
currentItemList = new ItemList(currentItemList);
elementStack.unshift([ name, attribs ]);
},
ontext: function (text) {
currentItemList.add(JSON.stringify(ent.decode(text)));
/*var lines = text.split("\n");
var isFirst = true;
lines.forEach(function (line) {
var lineMatch = /^(\s*)(.*?)(\s*)$/.exec(line);
var preSpace = lineMatch[1],
mainText = lineMatch[2],
postSpace = lineMatch[3];
if (!isFirst) {
currentItemList.addSpace("\n");
}
currentItemList.addSpace(preSpace);
if (mainText.length > 0) {
currentItemList.add(JSON.stringify(mainText));
}
isFirst = false;
});*/
},
onclosetag: function (tagname) {
var element = elementStack.shift();
var elementContent = currentItemList.content + currentItemList.spacer;
currentItemList = currentItemList.parent;
var indent = currentItemList.indent;
var attribs = element[1];
var id = attribs['id'];
var idSuffix = id !== undefined ? '#' + id : '';
delete attribs['id'];
var classNames = attribs['class'];
var classSuffix;
if ( !thisIsSVGTag( element[ 0 ] ) ) {
classSuffix = (classNames !== undefined ? classNames : '').split(/\s+/g).filter(function(v) {
return v.length > 0;
}).map(function(cls) {
return '.' + cls;
}).join('');
delete attribs['class'];
} else {
classSuffix = '';
}
// Convert inline CSS style attribute to an object
if(attribs['style']){
var rules = attribs["style"].split(";");
attribs["style"] = {};
rules.forEach(function(rule){
var split = rule.split(":");
if(split.length == 2){
attribs["style"][split[0].trim()] = split[1].trim();
}
});
}
var style = attribs['style']
delete attribs['style']
var dataset = {};
var datasetKey;
Object.keys(attribs).forEach(function (k) {
if (k.slice(0, 5) === 'data-') {
datasetKey = camel(k.slice(5));
dataset[datasetKey] = attribs[k];
delete attribs[k];
}
});
var attrPairs = Object.keys( attribs ).map( function ( k ) {
return JSON.stringify( k ) + ': ' + JSON.stringify( attribs[ k ] )
} );
var datasetPairs = Object.keys( dataset ).map( function ( k ) {
return JSON.stringify( k ) + ': ' + JSON.stringify( dataset[ k ] )
} );
var objects = {}
if (attribs.value) {
objects.value = attribs.value;
delete attribs.value;
}
if ( !isEmpty( style ) ) objects.style = style
if ( !isEmpty( attribs ) ) objects.attributes = attribs
if ( !isEmpty( dataset ) ) objects.dataset = dataset
if ( thisIsSVGTag( element[ 0 ] ) ) {
objects.namespace = getSVGNamespace();
Object.keys(attribs).forEach(function (k) {
var namespace = getSVGAttributeNamespace(k);
if (namespace === void 0) { // not a svg attribute
return;
}
var value = objects.attributes[ k ];
if (typeof value !== 'string' &&
typeof value !== 'number' &&
typeof value !== 'boolean'
) {
return;
}
if (namespace !== null) { // namespaced attribute
objects[ k ] = 'SVGAttributeHook(\'' + namespace + '\',\'' + value + '\')';
}
});
}
var objectStr = !isEmpty(objects) ? JSON.stringify(objects) : ""
var item = 'h(' + JSON.stringify(element[0] + idSuffix + classSuffix) + (
(objectStr !== "") ? ", " + objectStr : ""
)
// attrPairs.length || datasetPairs.length
// ? ", { \"attributes\": { "
// : ''
// ) + (
// attrPairs.length
// ? attrPairs.join(",\n" + indent + ' ')
// : ''
// ) + (
// datasetPairs.length && attrPairs.length
// ? ",\n" + indent + ' '
// : ''
// ) + (
// datasetPairs.length
// ? "\"dataset\": { " + datasetPairs.join(",\n" + indent + ' ') + "}"
// : ''
// ) + (
// attrPairs.length || datasetPairs.length
// ? "}}"
// : ''
// )
+ (
elementContent.length
? ', [' + (elementContent[0] === "\n" ? '' : ' ') + elementContent + (elementContent.match(/\s$/) ? '' : ' ') + ']'
: ''
) + ')';
currentItemList.add(item);
},
oncomment: function (text) {
currentItemList.add('/*' + text + '*/', false); // @todo comment-safety
},
onend: function () {
cb(null, currentItemList.content);
}
}, {decodeEntities: true, xmlMode: true});
parser.write(html);
parser.end();
}