-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
541 lines (495 loc) · 18 KB
/
index.js
File metadata and controls
541 lines (495 loc) · 18 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
var htmlParser = require("htmlparser2"),
cssSelector = require("css-select"),
fs = require("fs"),
deepcopy = require("deepcopy"),
traverse = require('babel-traverse').default,
t = require("babel-types"),
path = require("path")
function error(msg,node, path){
var loc = node && (node.loc || node._loc)
var err = new SyntaxError(msg)
var errorVisitor = {
enter(path, state) {
var loc = path.node.loc
if (loc) {
state.loc = loc
path.stop()
}
}
}
if (loc) {
err.loc = loc.start;
} else if(path) {
traverse(node, errorVisitor, path.scope, err)
err.message += " (This is an error on an internal node. Probably an internal error"
if (err.loc) { err.message += ". Location has been estimated." }
err.message += ")"
}
throw err
}
function isJSXAttribute(name) {
if (name === undefined) {
return function(attr) { return attr.type === 'JSXAttribute' }
} else {
return function(attr) { return (attr.type === 'JSXAttribute' && attr.name.name === name) }
}
}
function isJSXSpreadAttribute(attr) {
return attr.type === 'JSXSpreadAttribute'
}
/**
* Check if a JSX node is empty (only whitespace)
*/
function isNodeEmpty(node) {
return !node.children.some(function(child) {
return child.type !== 'JSXText' || child.value.trim() !== ''
})
}
function isZReservedAttr(attr) {
const reserved = ["tag","sel","if","replace"]
return reserved.some(function(x) { return isJSXAttribute(x)(attr) })
}
function isJSXZReservedAttr(attr) {
const reserved = ["tag","sel","in"]
return reserved.some(function(x) { return isJSXAttribute(x)(attr) })
}
function parseJSXsSpec(path,options,callback){
var ast = path.node
var opentag = ast.openingElement
var htmlPathAttr = opentag.attributes.filter(isJSXAttribute("in"))[0]
if(!htmlPathAttr)
error("jsxZ attribute 'in' necessary",ast.openingElement,path)
if(htmlPathAttr.value.type !== 'StringLiteral')
error("jsxZ 'in' must be an hardcoded string",htmlPathAttr.value,path)
var htmlPath = htmlPathAttr.value.value
var selectorAttr = opentag.attributes.filter(isJSXAttribute("sel"))[0]
if(selectorAttr && selectorAttr.value.type !== 'StringLiteral')
error("jsxZ 'sel' must be an hardcoded CSS selector",selectorAttr.value,path)
var rootSelector = selectorAttr && selectorAttr.value.value
transfos = ast.children
.filter(function(c){return c.type==='JSXElement'})
.map(function(c){
if(c.openingElement.name.name !== "Z")
return null
var selectorAttr = c.openingElement.attributes.filter(isJSXAttribute("sel"))[0]
if(!selectorAttr || selectorAttr.value.type !== 'StringLiteral')
error("Z 'sel' attribute is mandatory and must be a hardcoded CSS selector",selectorAttr && selectorAttr.value || c.openingElement,path)
var tagAttr = c.openingElement.attributes.filter(isJSXAttribute("tag"))[0]
var tag = tagAttr && tagAttr.value.value
var replaceAttr = c.openingElement.attributes.filter(isJSXAttribute("replace"))[0]
var replace = replaceAttr && replaceAttr.value.value === "true"
var ifAttr = c.openingElement.attributes.filter(isJSXAttribute("if"))[0]
var ifExpr = ifAttr && ifAttr.value.expression
var otherAttrs = c.openingElement.attributes
.filter(function(attr){ return !isZReservedAttr(attr) })
return {selector: selectorAttr.value.value, tag: tag, attrs: otherAttrs, node: c,selNode: selectorAttr.value, replace: replace, ifExpr: ifExpr}
})
.filter(function(c){return c !== null})
var tagAttr = ast.openingElement.attributes.filter(isJSXAttribute("tag"))[0]
var tag = tagAttr && tagAttr.value.value
var otherAttrs = ast.openingElement.attributes
.filter(function(attr){ return !isJSXZReservedAttr(attr) })
var rootTransfoNode = null
if (transfos.length === 0 && !isNodeEmpty(path.node)){
rootTransfoNode = path.node
}
const rootTransfo = {
tag: tag,
attrs: otherAttrs,
node: rootTransfoNode
}
transfos.push(rootTransfo)
if (htmlPath.indexOf(".html", htmlPath.length - 5) === -1){
htmlPath = htmlPath + ".html"
}
if (options.templatesDir && htmlPath[0] !== "/"){
htmlPath = options.templatesDir + "/" + htmlPath
}
try{
var data = fs.readFileSync(htmlPath)
}catch(e){
error("Impossible to read html file "+htmlPath,htmlPathAttr.value, path)
}
return {htmlFile: data.toString(), htmlPath: htmlPath, rootSelector: rootSelector, transfos: transfos, node: ast, selNode: selectorAttr.value}
}
function parseDom(jsxZ,callback){
var parser = new htmlParser.Parser(
new htmlParser.DomHandler(function (err, dom) {
if (err) err("Too much malformed HTML "+jsxZ.htmlPath,jsxZ.node)
if (jsxZ.rootSelector){
var dom = cssSelector.selectOne(jsxZ.rootSelector,dom)
if (!dom) error("selector "+jsxZ.rootSelector+" does not match any node in "+ jsxZ.htmlPath,jsxZ.selNode)
}
callback(dom)
}))
parser.write(jsxZ.htmlFile)
parser.done()
}
function syncForEach(leftOrRight,list,then,end){
var next = function(){
if(elem = (leftOrRight == "left" ? list.shift() : list.pop())){
then(elem,next)
}else{
end()
}
};next()
}
function extractJsxzPaths(sourceAst){
var jsxZPaths = []
traverse(sourceAst,{
JSXElement(path){
if(path.node.openingElement.name.name === "JSXZ") jsxZPaths.push(path)
}
})
return jsxZPaths
}
function domStyleToJSX(style){
var styleObj = stylesHTML2Obj(style)
return t.jSXExpressionContainer(
t.objectExpression(Object.keys(styleObj).map(function(key){
return t.objectProperty(t.identifier(hyphenToCamelCase(key)),toJSXValue(styleObj[key]))
}))
)
}
var ATTRIBUTE_MAPPING = {
'for': 'htmlFor','class': 'className',
'accept-charset': 'acceptCharset',
'accesskey': 'accessKey',
'allowfullscreen': 'allowFullScreen',
'allowtransparency': 'allowTransparency',
'autocomplete': 'autoComplete',
'autofocus': 'autoFocus',
'autoplay': 'autoPlay',
'cellpadding': 'cellPadding',
'cellspacing': 'cellSpacing',
'charset': 'charSet',
'classid': 'classID',
'colspan': 'colSpan',
'contenteditable': 'contentEditable',
'contextmenu': 'contextMenu',
'crossorigin': 'crossOrigin',
'datetime': 'dateTime',
'enctype': 'encType',
'formaction': 'formAction',
'formenctype': 'formEncType',
'formmethod': 'formMethod',
'formnovalidate': 'formNoValidate',
'formtarget': 'formTarget',
'frameborder': 'frameBorder',
'hreflang': 'hrefLang',
'http-equiv': 'httpEquiv',
'inputmode': 'inputMode',
'keyparams': 'keyParams',
'keytype': 'keyType',
'marginheight': 'marginHeight',
'marginwidth': 'marginWidth',
'maxlength': 'maxLength',
'mediagroup': 'mediaGroup',
'minlength': 'minLength',
'novalidate': 'noValidate',
'radiogroup': 'radioGroup',
'readonly': 'readOnly',
'rowspan': 'rowSpan',
'spellcheck': 'spellCheck',
'srcdoc': 'srcDoc',
'srclang': 'srcLang',
'srcset': 'srcSet',
'tabindex': 'tabIndex',
'usemap': 'useMap',
'viewbox': 'viewBox',
'preserveaspectratio': 'preserveAspectRatio',
'clip-rule': 'clipRule',
'fill-rule': 'fillRule',
'stroke-dasharray': 'strokeDasharray',
'stroke-dashoffset': 'strokeDashoffset',
'stroke-linecap': 'strokeLinecap',
'stroke-linejoin': 'strokeLinejoin',
'stroke-miterlimit': 'strokeMiterlimit',
'stroke-opacity': 'strokeOpacity',
'stroke-width': 'strokeWidth',
'text-anchor': 'textAnchor',
'text-decoration': 'textDecoration',
'text-rendering': 'textRendering',
}
var ELEMENT_ATTRIBUTE_MAPPING = {input: {checked: 'defaultChecked',value: 'defaultValue'}}
function domAttrToJSX(tag,attrNameCase,attrValue){
var attrName = attrNameCase.toLowerCase()
var astAttrName = (ELEMENT_ATTRIBUTE_MAPPING[tag] && ELEMENT_ATTRIBUTE_MAPPING[tag][attrName])
|| ATTRIBUTE_MAPPING[attrName] || attrName
if (astAttrName === 'style'){
var astAttrValue = domStyleToJSX(attrValue)
}else if(isNumeric(attrValue)){
var astAttrValue = t.jSXExpressionContainer(
t.numericLiteral(parseInt(attrValue, 10)))
}else{
var astAttrValue = t.stringLiteral(attrValue)
}
return t.jSXAttribute(t.jSXIdentifier(astAttrName),astAttrValue)
}
function domToAst(dom,tagIndex){
if (dom.type==='tag'){
var astTag = dom.name.toLowerCase()
var astAttribs = Object.keys(dom.attribs).map(function(attrName){
return domAttrToJSX(astTag,attrName,dom.attribs[attrName])
})
var ast = t.jSXElement(
t.jSXOpeningElement(t.jSXIdentifier(astTag),astAttribs),
t.jSXClosingElement(t.jSXIdentifier(astTag)),
dom.children.filter(
function(child){return child.type === 'text' || child.type === 'tag'}
).map(function(child){
var ast = domToAst(child,tagIndex)
tagIndex=ast.tagIndex
return ast
}))
}else if(dom.type==='text'){
var ast = t.jSXText(dom.data)
}
ast.tagIndex = tagIndex + 1 // associate tag ast node to dom to map selector to ast transfos
dom.tagIndex = ast.tagIndex // use string index to use object as hash map
return ast
}
/**
*
* @param {*} jsxZ the jsxZ spec build with the parseJSXsSpec function
* @param {*} rootdom the root dom of the html file
* @returns a mapping between the tagIndex of dom nodes and the transfos to apply
*/
function searchTransfosByTagIndex(jsxZ,rootdom){
var map = {}
jsxZ.transfos.map(function(transfo){
var matchingNodes = transfo.selector && cssSelector(transfo.selector,rootdom) || [rootdom]
if (matchingNodes.length == 0){
error("Transfo "+transfo.selector+" does not match anything in "+jsxZ.htmlPath,transfo.selNode)
return []
}
matchingNodes.map(function(subdom,i){
var cloned_transfo = deepcopy(transfo)
cloned_transfo.attrs = cloned_transfo.attrs.map(t.cloneDeep)
map[subdom.tagIndex] = {i: i,transfo: cloned_transfo}
})
})
return map
}
function attributesMap(attrs,nameFun,valueFun){
map = {}
attrs.forEach(function(attr){
// Only ignore {...props} for now, if React add something more it'll crash at that time
if (isJSXSpreadAttribute(attr)) { return }
map[nameFun(attr.name.name)] = valueFun(attr.value)
})
return map
}
function removeOverwrittenAttrs(attrsPath,newAttrs){
var newAttrsSet = attributesMap(newAttrs,function(name){return name},function(_){return true})
attrsPath.forEach(function(attr) {
if (isJSXSpreadAttribute(attr)) { return }
if (newAttrsSet[attr.node.name.name]) { attr.remove() }
})
}
function genSwapMap(attrs,nodeIndex){
var swapMap = attributesMap(attrs,function(name){return name+'Z'},function(value){return value})
swapMap["indexZ"] = t.numericLiteral(nodeIndex)
return swapMap
}
function alterAttributes(path,transfo,swapMap){
var attrsPath = path.get("attributes")
removeOverwrittenAttrs(attrsPath,transfo.attrs)
transfo.attrs.filter(function(attr){
if (isJSXAttribute()(attr)) {
// We don't want to keep attributes that have the the expression `undefined` inside.
// Why does it matter though ?
if (attr.value.type == "JSXExpressionContainer" && attr.value.expression.type == "Identifier" && attr.value.expression.name == "undefined") {
return false
}
}
// We want to keep the spread attributes though. And other stuffs eventually.
return true
}).map(function(attr){
path.pushContainer("attributes",attr)
})
path.traverse({
Identifier(path){
if(swapMap[path.node.name])
path.replaceWith(swapMap[path.node.name])
}
})
}
function alterTag(path,transfo,swapMap){
if(transfo.tag){
path.get("openingElement.name").replaceWith(t.jSXIdentifier(transfo.tag))
if(path.node.closingElement)
path.get("closingElement.name").replaceWith(t.jSXIdentifier(transfo.tag))
}
}
/**
* Handle ChildrenZ
* @param {*} path a path in the source HTML AST, selected by a Z or JSXZ element
* @param {*} transfo the transfo to apply. transfo.node if the Z element AST node. transfo.node is undefined for the JSXZ element itself
* @param {*} swapMap
*/
function alterChildren(path,transfo,swapMap){
if(transfo.node){ // no children alteration if no "node" transfo attribute, this is the case for the transfo of the JSXZ element itself
var childrenz = path.node.children.map(t.cloneDeep)
path.get('children').map(function(childz){ childz.remove() })
transfo.node.children.map(function(zchild){ path.pushContainer('children', t.cloneDeep(zchild)) })
var do_transform_path = function(elemPath){
if(elemPath.node.openingElement.name.name == "ChildrenZ"){
var children = childrenz.map(t.cloneDeep)
if(elemPath.parentPath.node.type ==='JSXElement'){
var prev_children = elemPath.parentPath.node.children.map(t.cloneDeep)
elemPath.parentPath.get('children').map(function(child){ child.remove() })
prev_children.map(function(prev_child){
if(prev_child.type =='JSXElement' && prev_child.openingElement.name.name == "ChildrenZ"){
children.map(function(new_child){
elemPath.parentPath.pushContainer('children',new_child)
})
}else{
elemPath.parentPath.pushContainer('children',prev_child)
}
})
elemPath.parentPath.traverse({
Identifier: function(path) {
if(swapMap[path.node.name])
path.replaceWith(swapMap[path.node.name])
},
JSXElement: do_transform_path
})
}else{
var children_without_text = children.map(function(child){
return (child.type == "JSXText") ? t.stringLiteral(child.value) : child
})
elemPath.replaceWith(t.arrayExpression(children_without_text))
}
}
}
do_transform_path(path)
path.traverse({
Identifier: function(path) {
if(swapMap[path.node.name])
path.replaceWith(swapMap[path.node.name])
},
JSXElement: do_transform_path
})
}
}
function applyIfExpr(path, transfo) {
if (transfo.ifExpr) {
const ifExpr = t.cloneDeep(transfo.ifExpr)
ifExpr.consequent = path.node
// This build into `{ifExpr ? path.node : null}`
const expr = t.jSXExpressionContainer(
t.conditionalExpression(ifExpr, path.node, t.nullLiteral())
)
path.replaceWith(expr)
}
}
function applyReplace(path, transfo) {
if (transfo.replace) {
if (transfo.attrs.length > 0 || transfo.tag || transfo.ifExpr) {
console.warn("Warning: your are using the replace feature with other transformations on sel", transfo.selector)
}
path.replaceInline(path.node.children)
}
}
/**
* @param {*} jsxzPath the HTML selected by a JSXZ element, as a JSXElement Babel path.
* @param {*} jsxZ the jsxZ parsed spec, as returned by the parseJSXsSpec function. Contains the transformations to apply.
* @param {*} dom the dom parsed spec, as returned by the htmlparser2 parseDOM function.
*/
function domAstZTransfo(jsxzPath,jsxZ,dom){
var transfosByTagIndex = searchTransfosByTagIndex(jsxZ,dom)
var do_transform_path = function(subpath){
if (transfoIndexed=transfosByTagIndex[subpath.node.tagIndex]){
// Deleting the tagIndex property to avoid applying the same transformation twice,
// in case the subpath is moved deeper in the AST (this happen with the if feature
// which add a conditional expression around the subpath)
delete subpath.node.tagIndex
var transfo = transfoIndexed.transfo
var swapMap = genSwapMap(subpath.node.openingElement.attributes,transfoIndexed.i)
alterAttributes(subpath.get("openingElement"),transfo,swapMap)
alterTag(subpath,transfo,swapMap)
alterChildren(subpath,transfo,swapMap)
applyIfExpr(subpath,transfo)
applyReplace(subpath,transfo)
}
}
// First, apply the Z transformations on the JSXZ element itself.
do_transform_path(jsxzPath)
// Then, apply the Z transformations on the children of the JSXZ element.
jsxzPath.traverse({JSXElement: { exit: do_transform_path }})
}
module.exports.default = function() {
return {
inherits: require("babel-plugin-syntax-jsx"),
pre(state) { this.currentTagIndex = 0 },
visitor: {
Program(path,state){
// On program start, do an explicit traversal up front for your plugin.
var options = state.opts
var self = this
path.traverse({
JSXElement: {exit(path){
if(path.node.openingElement.name.name === "JSXZ"){
var jsxzPath = path, jsxZ = parseJSXsSpec(path,options || {})
parseDom(jsxZ,function(dom){
var domAst = domToAst(dom,self.currentTagIndex)
self.currentTagIndex = domAst.tagIndex
jsxzPath.replaceWith(domAst)
domAstZTransfo(jsxzPath,jsxZ,dom)
})
}
}}
})
}
}
}
}
function trimEnd(haystack, needle) {
return (haystack.indexOf(needle,haystack.length-needle.length) !== -1) ? haystack.slice(0, -needle.length) : haystack
}
function hyphenToCamelCase(string) {
return string.replace(/-(.)/g, function(match, chr) {
return chr.toUpperCase()
})
}
function toJSXValue(value) {
if (isNumeric(value)) {
return t.numericLiteral(parseInt(value, 10))
} else if (isConvertiblePixelValue(value)) {
return t.numericLiteral(parseInt(trimEnd(value, 'px'), 10))
} else {
return t.stringLiteral(value)
}
}
function isEmpty(string) {
return !/[^\s]/.test(string)
}
function isConvertiblePixelValue(value) {
return /^\d+px$/.test(value)
}
function isNumeric(input) {
return input !== undefined
&& input !== null
&& (typeof input === 'number' || parseInt(input, 10) == input)
}
function escapeSpecialChars(value) {
return (String(value)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>'))
}
function stylesHTML2Obj(rawStyle){
var styles = {}
rawStyle.split(';').forEach(function(style) {
style = style.trim()
var firstColon = style.indexOf(':')
var key = style.substr(0, firstColon)
var value = style.substr(firstColon + 1).trim()
if (key !== '')
styles[key] = value
})
return styles
}