-
Notifications
You must be signed in to change notification settings - Fork 29
/
core.js
644 lines (537 loc) · 20.9 KB
/
core.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
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/**
* @module compiler
*/
//#if RIOT
var compile = (function () {
var brackets = riot.util.brackets
//#endif
function _regEx(str, opt) { return new RegExp(str, opt) }
// Looks like, in [jsperf tests](http://jsperf.com/riot-regexp-test-vs-array-indexof)
// RegExp is faster in most browsers, except for very shorty arrays.
var
// Boolean attributes, prefixed with `__` in the riot tag definition.
// See ../doc/attributes.md
//
BOOL_ATTRS = _regEx(
'^(?:disabled|checked|readonly|required|allowfullscreen|auto(?:focus|play)|' +
'compact|controls|default|formnovalidate|hidden|ismap|itemscope|loop|' +
'multiple|muted|no(?:resize|shade|validate|wrap)?|open|reversed|seamless|' +
'selected|sortable|truespeed|typemustmatch)$'),
// The following attributes give error when parsed on browser with `{ exrp_value }`
// See ../doc/attributes.md
RIOT_ATTRS = ['style', 'src', 'd'],
// HTML5 void elements that cannot be auto-closed.
// See: http://www.w3.org/TR/html-markup/syntax.html#syntax-elements
// http://www.w3.org/TR/html5/syntax.html#void-elements
VOID_TAGS = /^(?:input|img|br|wbr|hr|area|base|col|embed|keygen|link|meta|param|source|track)$/,
// Matches attributes. Names can contain almost all iso-8859-1 character set.
HTML_ATTR = /\s*([-\w:\.\xA0-\xFF]+)\s*(?:=\s*('[^']+'|"[^"]+"|\S+))?/g,
TRIM_TRAIL = /[ \t]+$/gm,
_bp = null
//#if NODE
var path = require('path')
//#endif
//#set $_RIX_TEST = 4
//#set $_RIX_ESC = 5
//#set $_RIX_OPEN = 6
//#set $_RIX_CLOSE = 7
//#set $_RIX_PAIR = 8
//#ifndef $_RIX_TEST
var
$_RIX_TEST = 4, // DONT'T FORGET SYNC THE #set BLOCK!!!
$_RIX_ESC = 5,
$_RIX_OPEN = 6,
$_RIX_CLOSE = 7,
$_RIX_PAIR = 8
//#endif
// Escape backslashes and inner single quotes, and enclose s in single quotes
function q(s) {
return "'" + (s ? s
.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\n/g, '\\n').replace(/\r/g, '\\r') :
'') + "'"
}
// Generates the `riot.tag2` call with the processed parts.
function mktag(name, html, css, attrs, js, pcex) {
var
c = ', ',
s = '}' + (pcex.length ? ', ' + q(_bp[$_RIX_PAIR]) : '') + ');'
// give more consistency to the output
if (js && js.slice(-1) !== '\n') s = '\n' + s
return 'riot.tag2(\'' + name + "'" + c + q(html) + c + q(css) + c + q(attrs) +
', function(opts) {\n' + js + s
}
/**
* Merge two javascript object extending the properties of the first one with
* the second
* @param {object} obj - source object
* @param {object} props - extra properties
* @returns {object} source object containing the new properties
*/
function extend(obj, props) {
for (var prop in props) {
/* istanbul ignore next */
if (props.hasOwnProperty(prop)) {
obj[prop] = props[prop]
}
}
return obj
}
/**
* Parses and format attributes.
*
* @param {string} str - Attributes, with expressions replaced by their hash
* @returns {string} Formated attributes
*/
function parseAttrs(str) {
var
list = [],
match,
k, v,
DQ = '"'
HTML_ATTR.lastIndex = 0
str = str.replace(/\s+/g, ' ')
while (match = HTML_ATTR.exec(str)) {
// all attribute names are converted to lower case
k = match[1].toLowerCase()
v = match[2]
if (!v) {
list.push(k) // boolean attribute without explicit value
}
else {
// attribute values must be enclosed in double quotes
if (v[0] !== DQ)
v = DQ + (v[0] === "'" ? v.slice(1, -1) : v) + DQ
if (k === 'type' && v.toLowerCase() === '"number"') {
v = DQ + _bp[0] + "'number'" + _bp[1] + DQ // fix #827 by @rsbondi
}
else if (/\u0001\d/.test(v)) {
// renames special attributes with expressiones in their value.
if (BOOL_ATTRS.test(k)) {
k = '__' + k
}
else if (~RIOT_ATTRS.indexOf(k)) {
k = 'riot-' + k
}
}
// join the key-value pair, with no spaces between the parts
list.push(k + '=' + v)
}
}
return list.join(' ') // returns the attribute list
}
// Replaces expressions in the HTML with a marker, and runs expressions
// through the parser, except those beginning with `{^`.
function splitHtml(html, opts, pcex) {
// `brackets.split` is a heavy function, so don't call it if not necessary
if (html && _bp[$_RIX_TEST].test(html)) {
var
jsfn = opts.expr && (opts.parser || opts.type) ? compileJS : 0,
list = brackets.split(html),
expr
for (var i = 1; i < list.length; i += 2) {
expr = list[i]
if (expr[0] === '^')
expr = expr.slice(1)
else if (jsfn) {
expr = jsfn(expr, opts)
if (/;\s*$/.test(expr)) expr = expr.slice(0, expr.search(/;\s*$/))
}
list[i] = '\u0001' + (pcex.push(expr.replace(/[\r\n]+/g, ' ').trim()) - 1) + _bp[1]
}
html = list.join('')
}
return html
}
// Restores expressions hidden by splitHtml and escape literal internal brackets
function restoreExpr(html, pcex) {
if (pcex.length) {
html = html
.replace(/\u0001(\d+)/g, function (_, d) {
return _bp[0] + pcex[d]
})
}
return html
}
//## HTML Compilation
//-------------------
// `HTML_TAGS` matches only start and self-closing tags, not the content.
var
HTML_COMMENT = /<!--(?!>)[\S\s]*?-->/g,
HTML_TAGS = /<([-\w]+)\s*([^"'\/>]*(?:(?:"[^"]*"|'[^']*'|\/[^>])[^'"\/>]*)*)(\/?)>/g
/**
* Parses and formats the HTML text.
*
* @param {string} html - Can contain embedded HTML comments and literal whitespace
* @param {Object} opts - Collected user options. Includes the brackets array in `_bp`
* @param {Array} [pcex] - Keeps precompiled expressions
* @param {number} [intc] - 1 if internal call
* @returns {string} The parsed HTML text
* @see http://www.w3.org/TR/html5/syntax.html
*/
function compileHTML(html, opts, pcex, intc) {
// `_bp`is undefined when `compileHTML` is called from tests
if (!intc) {
_bp = brackets.array(opts.brackets)
html = html.replace(/\r\n?/g, '\n').replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
}
if (!pcex) pcex = []
// separate the expressions, then parse the tags and their attributes
html = splitHtml(html, opts, pcex)
.replace(HTML_TAGS, function (_, name, attr, ends) {
// force all tag names to lowercase
name = name.toLowerCase()
// close self-closing tag, except if this is a html5 void tag
ends = ends && !VOID_TAGS.test(name) ? '></' + name : ''
// format the attributes
if (attr) name += ' ' + parseAttrs(attr)
return '<' + name + ends + '>'
})
// tags parsed, now compact whitespace if `opts.whitespace` is not set
if (!opts.whitespace) {
var p = [],
pre = /<pre(?:\s+[^'">]+(?:(?:"[^"]*"|'[^']*')[^'">]*)*|\s*)>[\s\S]*<\/pre\s*>/gi
html = html.replace(pre, function (q) {
return '\u0002' + (p.push(q) - 1) + '~' }).trim().replace(/\s+/g, ' ')
if (p.length)
html = html.replace(/\u0002(\d+)~/g, function (q, n) { return p[n] })
}
// for `opts.compact`, remove whitespace between tags
if (opts.compact) html = html.replace(/> <([-\w\/])/g, '><$1')
return restoreExpr(html, pcex)
}
// JavaScript Compilation
// ----------------------
var
// Prepare regexp for remotion of multiline and single-line comments
JS_RMCOMMS = _regEx(
'(' + brackets.S_QBLOCKS + ')|' + brackets.R_MLCOMMS.source + '|//[^\r\n]*',
'g'),
// Matches es6 methods across multiple lines up to their first curly brace
JS_ES6SIGN = /^([ \t]*)([$_A-Za-z][$\w]*)\s*(\([^()]*\)\s*{)/m
// Default parser for JavaScript code
function riotjs(js) {
var
match,
toes5,
parts = [], // parsed code
pos
// remove comments
js = js.replace(JS_RMCOMMS, function (m, q) { return q ? m : ' ' })
// $1: indentation,
// $2: method name,
// $3: parameters
while (match = js.match(JS_ES6SIGN)) {
// save remaining part now -- IE9 changes `rightContext` in `RegExp.test`
parts.push(RegExp.leftContext)
js = RegExp.rightContext
pos = skipBlock(js) // find the closing bracket
// convert ES6 method signature to ES5 function
toes5 = !/^(?:if|while|for|switch|catch|function)$/.test(match[2])
if (toes5)
match[0] = match[1] + 'this.' + match[2] + ' = function' + match[3]
parts.push(match[0], js.slice(0, pos))
js = js.slice(pos)
if (toes5 && !/^\s*.\s*bind\b/.test(js)) parts.push('.bind(this)')
}
return parts.length ? parts.join('') + js : js
// Inner helper - find the position following the closing bracket for the current block
function skipBlock(str) {
var
re = _regEx('([{}])|' + brackets.S_QBLOCKS, 'g'),
level = 1,
match
while (level && (match = re.exec(str))) {
if (match[1])
match[1] === '{' ? ++level : --level
}
return level ? str.length : re.lastIndex
}
}
/**
* Runs the parser for the JavaScript code, defaults to `riotjs`
*
* @param {string} js - Buffer with the javascript code
* @param {Object} opts - Options, can include a custom parser function
* @param {string} type - Optional type for parser selection
* @param {Object} parserOpts - Optional parser options
* @returns {string} The parsed JavaScript code
*/
function compileJS(js, opts, type, parserOpts) {
if (!js) return ''
if (!type) type = opts.type
var parser = opts.parser || (type ? parsers.js[type] : riotjs)
if (!parser)
throw new Error('JS parser not found: "' + type + '"')
return parser(js, parserOpts).replace(TRIM_TRAIL, '')
}
// CSS Compilation
// ----------------
// See http://www.w3.org/TR/CSS21/
// Prepare regex to match CSS selectors, excluding those beginning with '@'.
var CSS_SELECTOR = _regEx('(}|{|^)[ ;]*([^@ ;{}][^{}]*)(?={)|' + brackets.R_STRINGS.source, 'g')
// Parses styles enclosed in a "scoped" tag (`scoped` is deprecated in HTML5).
// The "style" string is received without comments or surrounding spaces.
function scopedCSS(tag, style) {
var scope = ':scope'
return style.replace(CSS_SELECTOR, function (m, p1, p2) {
// skip quoted strings
if (!p2) return m
// we have a selector list, parse each individually
p2 = p2.replace(/[^,]+/g, function (sel) {
var s = sel.trim()
// skips the keywords and percents of css animations
if (s && s !== 'from' && s !== 'to' && s.slice(-1) !== '%') {
// replace the `:scope` pseudo-selector, where it is, with the root tag name;
// if `:scope` was not included, add the tag name as prefix, and mirror all
// to `[riot-tag]`
if (s.indexOf(scope) < 0) s = scope + ' ' + s
s = s.replace(scope, tag) + ',' +
s.replace(scope, '[riot-tag="' + tag + '"]')
}
return sel.slice(-1) === ' ' ? s + ' ' : s // respect (a little) the user style
})
// add the danling bracket char and return the processed selector list
return p1 ? p1 + ' ' + p2 : p2
})
}
/**
* Runs the parser for style blocks.
* For scoped styles, the `scopedCSS` function is called after any external parser.
*
* @param {string} style - Raw style block
* @param {string} tag - The root tag name to which the style belongs
* @param {string} [type] - One of `parsers.css`: jade, stylus, etc.
* @param {boolean} [scoped] - `true` for scoped styles
* @param {object} opts - get the custom parser options
* @returns {string} The processed style block
*/
function compileCSS(style, tag, type, scoped, opts) {
if (type) {
if (type === 'scoped-css') { // DEPRECATED
scoped = true
}
else if (parsers.css[type]) {
style = parsers.css[type](tag, style, opts)
}
else if (type !== 'css') {
throw new Error('CSS parser not found: "' + type + '"')
}
}
// remove comments, compact and trim whitespace
style = style.replace(brackets.R_MLCOMMS, '').replace(/\s+/g, ' ').trim()
// translate scoped rules if nedded
return scoped ? scopedCSS(tag, style) : style
}
// The main compiler
// -----------------
// Matches the 'type' attribute, for script and style tags
var
TYPE_ATTR = /\stype\s*=\s*(?:(['"])(.+?)\1|(\S+))/i, // don't handle escaped quotes :(
MISC_ATTR = /\s*=\s*("(?:\\[\S\s]|[^"\\]*)*"|'(?:\\[\S\s]|[^'\\]*)*'|\{[^}]+}|\S+)/.source
// Returns the value of the 'type' attribute, with the prefix "text/" removed.
function getType(str) {
if (str) {
var match = str.match(TYPE_ATTR)
str = match && (match[2] || match[3])
}
return str ? str.replace('text/', '') : ''
}
// Returns the value of any attribute, or the empty string for missing attribute.
function getAttr(str, name) {
if (str) {
var
re = _regEx('\\s' + name + MISC_ATTR, 'i'),
match = str.match(re)
str = match && match[1]
if (str)
return /^['"]/.test(str) ? str.slice(1, -1) : str
}
return ''
}
// get the parser options from the options attribute
function getParserOptions(attrs) {
var opts = getAttr(attrs, 'options')
// convert the string into a valid js object
if (opts) opts = JSON.parse(opts)
return opts
}
// Runs the custom or default parser on the received JavaScript code.
// The CLI version can read code from the file system (experimental)
function getCode(code, opts, attrs, url) {
var type = getType(attrs),
parserOpts = getParserOptions(attrs)
var src = getAttr(attrs, 'src')
if (src && url) {
var
charset = getAttr(attrs, 'charset'),
file = path.resolve(path.dirname(url), src)
code = require('fs').readFileSync(file, {encoding: charset || 'utf8'})
}
return compileJS(code, opts, type, parserOpts)
}
// Matches HTML tag ending a line. This regex still can be fooled by code as:
// ```js
// x <y && y >
// z
// ```
var END_TAGS = /\/>\n|^<(?:\/[\w\-]+\s*|[\w\-]+(?:\s+(?:[-\w:\xA0-\xFF][\S\s]*?)?)?)>\n/
function splitBlocks(str) {
var k, m
/* istanbul ignore next: this if() can't be true, but just in case... */
if (str[str.length - 1] === '>')
return [str, '']
k = str.lastIndexOf('<') // first probable open tag
while (~k) {
if (m = str.slice(k).match(END_TAGS)) {
k += m.index + m[0].length
return [str.slice(0, k), str.slice(k)]
}
k = str.lastIndexOf('<', k -1)
}
// TODO 2.4.0 make untagged content html, update the guide too
//return [str, ''] // no closing tag found, assume html text
return ['', str]
}
// Runs the external HTML parser for the entire tag file
function compileTemplate(lang, html, opts) {
var parser = parsers.html[lang]
if (!parser)
throw new Error('Template parser not found: "' + lang + '"')
return parser(html, opts)
}
/*
CUST_TAG regex don't allow unquoted expressions containing the `>` operator.
STYLE and SCRIPT disallows the operator `>` at all.
The beta.4 CUST_TAG regex is fast, with RegexBuddy I get 76 steps and 14 backtracks on
the test/specs/fixtures/treeview.tag :) but fails with nested tags of the same name :(
With a greedy * operator, we have ~500 and 200bt, it is acceptable. So let's fix this.
*/
var
CUST_TAG = /^([ \t]*)<([-\w]+)(?:\s+([^'"\/>]+(?:(?:"[^"]*"|'[^']*'|\/[^>])[^'"\/>]*)*)|\s*)?(?:\/>|>[ \t]*\n?([\s\S]*)^\1<\/\2\s*>|>(.*)<\/\2\s*>)/gim,
STYLE = /<style(\s+[^>]*)?>\n?([^<]*(?:<(?!\/style\s*>)[^<]*)*)<\/style\s*>/gi,
SCRIPT = _regEx(STYLE.source.replace(/tyle/g, 'cript'), 'gi')
/*
An alternate routine for finding custom tags, not depending in indentation.
Find openning tag, must be the first token in a line, after any whitespace.
if it is a self-close tag, i.e. ends with />. we are done.
Set counter to 1.
Find the next '<tag' or '</tag', if '<tag' found increment counter, else decrement.
When counter reach 0 we are done.
var re1 = _regEx('<' + tagName + '[\\s/>]', 'ig'), re2 = _regEx('</' + tagName + '[\\s>]', 'ig')
re1.lastIndex = TAG_START.lastIndex
while (match1 = re1.)
*/
/**
* The main compiler processes all custom tags, one by one.
*
* In .tag files, a custom tag can span multiple lines, but there should be no other
* external element sharing the starting and ending lines of the tag (HTML comments
* inclusive) and should not be indented.
* Custom tags in HTML files don't have this restriction.
*
* @param {string} src - String with zero or more custom riot tags.
* @param {Object} [opts] - User options.
* @param {string} [url] - Filename of the riot tag, prepended to the generated code.
* @returns {string} JavaScript code to build the tag later, through riot.tag2 function.
*/
function compile(src, opts, url) {
var label, exclude, parts = []
if (!opts) opts = {}
exclude = opts.exclude || false
function included(s) { return !exclude || exclude.indexOf(s) < 0 }
// get a static brackets array for use on the entire source
_bp = brackets.array(opts.brackets)
// run any custom html parser before the compilation
if (opts.template)
src = compileTemplate(opts.template, src, opts.templateOptions)
// riot #1070 -- isAbsolute does not exists in node 10.x
//#if NODE
/* istanbul ignore next */
label = !url ? '' : path.isAbsolute(url) ? path.relative('.', url) : url
if (label)
label = '//src: ' + label.replace(/\\/g, '/') + '\n'
//#else
label = url ? '//src: ' + url + '\n' : ''
//#endif
// normalize eols and start processing the tags
src = label + src
.replace(/\r\n?/g, '\n')
.replace(CUST_TAG, function (_, indent, tagName, attribs, body, body2) {
// content can have attributes first, then html markup with zero or more script or
// style tags of different types, and finish with an untagged block of javascript code.
var
jscode = '',
styles = '',
html = '',
pcex = []
tagName = tagName.toLowerCase()
// process the attributes, including their expressions
attribs = attribs && included('attribs') ?
restoreExpr(parseAttrs(splitHtml(attribs, opts, pcex)), pcex) : ''
if (body2) body = body2
// remove comments and trim trailing whitespace
if (body && (body = body.replace(HTML_COMMENT, '')) && /\S/.test(body)) {
if (body2) {
/* istanbul ignore next */
html = included('html') ? compileHTML(body2, opts, pcex, 1) : ''
}
else {
body = body.replace(_regEx('^' + indent, 'gm'), '')
// get and process the style blocks
if (included('css')) {
body = body.replace(STYLE, function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs),
csstype = getType(_attrs) || opts.style
styles += (styles ? ' ' : '') +
compileCSS(_style, tagName, csstype, scoped, getParserOptions(_attrs))
return ''
})
}
// now the script blocks
if (included('js')) {
body = body.replace(SCRIPT, function (_, _attrs, _script) {
jscode += (jscode ? '\n' : '') + getCode(_script, opts, _attrs, url)
return ''
})
}
// separate the untagged javascript block from the html markup
var blocks = splitBlocks(body.replace(TRIM_TRAIL, ''))
if (included('html')) {
body = blocks[0]
if (body)
html = compileHTML(body, opts, pcex, 1)
}
if (included('js')) {
body = blocks[1]
if (/\S/.test(body))
jscode += (jscode ? '\n' : '') + compileJS(body, opts)
}
}
}
// give more consistency to the output
jscode = /\S/.test(jscode) ? jscode.replace(/\n{3,}/g, '\n\n') : ''
// replace the tag with a call to the riot.tag2 function and we are done
if (opts.entities) {
parts.push({
tagName: tagName,
html: html,
css: styles,
attribs: attribs,
js: jscode
})
return ''
}
// replace the tag with a call to the riot.tag2 function and we are done
return mktag(tagName, html, styles, attribs, jscode, pcex)
})
return opts.entities ? parts : src
}
//#if RIOT
riot.util.compile = {
html: compileHTML,
style: compileCSS,
js: compileJS
}
return compile
})()
//#endif