Skip to content

Commit

Permalink
v2.3.22
Browse files Browse the repository at this point in the history
More memory optimization, ~50% less than v2.3.20
Added more comments
Revisión to tests and helpers
Updated .eslintrc.yml to use 1tbs block style
  • Loading branch information
amarcruz committed Jan 20, 2016
1 parent 783521c commit d2843fe
Show file tree
Hide file tree
Showing 13 changed files with 395 additions and 305 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ rules:
# STYLISTIC ISSUES
#
array-bracket-spacing: 2 # disallow spaces inside of brackets (Default: [0, "never"])
brace-style: [1, "stroustrup", { "allowSingleLine": true }]
brace-style: [1, "1tbs", { "allowSingleLine": true }]
# require Stroustrup style: `else` must be on its own line (Default: [0, '1tbs'])
camelcase: [2, { "properties": "always" }]
# require camel case names (Default: 0)
Expand All @@ -130,7 +130,7 @@ rules:
# (Default: [0, { "beforeColon": false, "afterColon": true }])
linebreak-style: [2, "unix"]
max-depth: [1, 5] # specify the maximum depth blocks can be nested
max-len: [1, 92, 4, { "ignoreUrls": true, "ignorePattern": "=\\s+/.+/|_regEx\\(" }]
max-len: [1, 92, 4, { "ignoreUrls": true, "ignorePattern": "=\\s+/.+/|_regEx\\(|RegExp\\(" }]
# maximum line length, except for regexes
max-nested-callbacks: [2, 4] # specify the maximum depth callbacks can be nested (Default: [0, 2])
new-cap: 2 # require a capital letter for constructors (Default: 0)
Expand Down
131 changes: 66 additions & 65 deletions dist/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ var parsers = (function () {
function _try (name, req) {

function fn (r) {
try { return require(r) }
catch (_) { }
try { return require(r) } catch (_) { }
return null
}

Expand Down Expand Up @@ -258,8 +257,7 @@ var brackets = (function () {
function unescapeStr (s) {
if (isexpr) {
parts.push(s && s.replace(_bp[5], '$1'))
}
else {
} else {
parts.push(s)
}
}
Expand Down Expand Up @@ -388,17 +386,15 @@ function parseAttrs (str, pcex) {

if (!v) {
list.push(k)
}
else {
} else {

if (v[0] !== DQ) {
v = DQ + (v[0] === "'" ? v.slice(1, -1) : v) + DQ
}

if (k === 'type' && SPEC_TYPES.test(v)) {
t = v
}
else {
} else {
if (/\u0001\d/.test(v)) {

if (k === 'value') e = 1
Expand All @@ -425,16 +421,14 @@ function splitHtml (html, opts, pcex) {
var
jsfn = opts.expr && (opts.parser || opts.type) ? _compileJS : 0,
list = brackets.split(html, 0, _bp),
expr
expr, israw

for (var i = 1; i < list.length; i += 2) {
expr = list[i]
if (expr[0] === '^') {
expr = expr.slice(1)
}
else if (jsfn) {
var israw = expr[0] === '='

} else if (jsfn) {
israw = expr[0] === '='
expr = jsfn(israw ? expr.slice(1) : expr, opts).trim()
if (expr.slice(-1) === ';') expr = expr.slice(0, -1)
if (israw) expr = '=' + expr
Expand Down Expand Up @@ -523,8 +517,7 @@ function compileHTML (html, opts, pcex) {
if (Array.isArray(opts)) {
pcex = opts
opts = {}
}
else {
} else {
if (!pcex) pcex = []
if (!opts) opts = {}
}
Expand All @@ -547,13 +540,14 @@ function riotjs (js) {
parts = [],
match,
toes5,
pos, name, rm
pos, name, rm, R

JS_COMMS.lastIndex = 0
while (rm = JS_COMMS.exec(js)) {
R = JS_COMMS
R.lastIndex = 0
while (rm = R.exec(js)) {
if (rm[0][0] === '/' && !rm[1] && !rm[2]) {
js = RegExp.leftContext + ' ' + RegExp.rightContext
JS_COMMS.lastIndex = rm[3] + 1
R.lastIndex = rm[3] + 1
}
}

Expand All @@ -562,13 +556,14 @@ function riotjs (js) {
parts.push(RegExp.leftContext)
js = RegExp.rightContext

JS_ES6END.lastIndex = 0
R = JS_ES6END
R.lastIndex = 0
pos = 1
while (pos && (rm = JS_ES6END.exec(js))) {
while (pos && (rm = R.exec(js))) {
if (rm[0] === '{') ++pos
else if (rm[0] === '}') --pos
}
pos = pos ? js.length : JS_ES6END.lastIndex
pos = pos ? js.length : R.lastIndex

name = match[1]
toes5 = !/^(?:if|while|for|switch|catch|function)$/.test(name)
Expand Down Expand Up @@ -610,21 +605,25 @@ function compileJS (js, opts, type, extra) {
return _compileJS(js, opts, type, extra.parserOptions, extra.url)
}

var CSS_SELECTOR = _regEx('(}|{|^)[ ;]*([^@ ;{}][^{}]*)(?={)|' + S_LINESTR, 'g')
var CSS_SELECTOR = _regEx('([{}]|^)[ ;]*([^@ ;{}][^{}]*)(?={)|' + S_LINESTR, 'g')

function scopedCSS (tag, style) {
function scopedCSS (tag, css) {
var scope = ':scope'

return style.replace(CSS_SELECTOR, function (m, p1, p2) {
return css.replace(CSS_SELECTOR, function (m, p1, p2) {

if (!p2) return m

p2 = p2.replace(/[^,]+/g, function (sel) {
var s = sel.trim()

if (s && s !== 'from' && s !== 'to' && s.slice(-1) !== '%') {
if (!s || s === 'from' || s === 'to' || s.slice(-1) === '%') {
return sel
}

if (s.indexOf(scope) < 0) s = scope + ' ' + s
if (s.indexOf(scope) < 0) {
s = tag + ' ' + s + ',[riot-tag="' + tag + '"] ' + s
} else {
s = s.replace(scope, tag) + ',' +
s.replace(scope, '[riot-tag="' + tag + '"]')
}
Expand All @@ -635,65 +634,65 @@ function scopedCSS (tag, style) {
})
}

function _compileCSS (style, tag, type, opts) {
function _compileCSS (css, tag, type, opts) {
var scoped = (opts || (opts = {})).scoped

if (type) {
if (type === 'scoped-css') {
scoped = true
}
else if (parsers.css[type]) {
style = parsers.css[type](tag, style, opts.parserOpts || {}, opts.url)
}
else if (type !== 'css') {
} else if (parsers.css[type]) {
css = parsers.css[type](tag, css, opts.parserOpts || {}, opts.url)
} else if (type !== 'css') {
throw new Error('CSS parser not found: "' + type + '"')
}
}

style = style.replace(brackets.R_MLCOMMS, '').replace(/\s+/g, ' ').trim()
css = css.replace(brackets.R_MLCOMMS, '').replace(/\s+/g, ' ').trim()

if (scoped) {
// istanbul ignore next
if (!tag) {
throw new Error('Can not parse scoped CSS without a tagName')
}
style = scopedCSS(tag, style)
css = scopedCSS(tag, css)
}
return style
return css
}

// istanbul ignore next
function compileCSS (style, parser, opts) {
if (parser && typeof parser === 'object') {
opts = parser
parser = ''
function compileCSS (css, type, opts) {
if (type && typeof type === 'object') {
opts = type
type = ''
}
return _compileCSS(style, opts.tagName, parser, opts)
return _compileCSS(css, opts.tagName, type, opts)
}

var

TYPE_ATTR = /\stype\s*=\s*(?:(['"])(.+?)\1|(\S+))/i,
MISC_ATTR = '\\s*=\\s*(' + S_STRINGS + '|{[^}]+}|\\S+)'

function getType (str) {
MISC_ATTR = '\\s*=\\s*(' + S_STRINGS + '|{[^}]+}|\\S+)'

if (str) {
var match = str.match(TYPE_ATTR)
function getType (attribs) {
if (attribs) {
var match = attribs.match(TYPE_ATTR)

str = match && (match[2] || match[3])
match = match && (match[2] || match[3])
if (match) {
return match.replace('text/', '')
}
}
return str ? str.replace('text/', '') : ''
return ''
}

function getAttr (str, name) {
if (str) {
var
re = _regEx('\\s' + name + MISC_ATTR, 'i'),
match = str.match(re)
function getAttr (attribs, name) {
if (attribs) {
var match = attribs.match(_regEx('\\s' + name + MISC_ATTR, 'i'))

str = match && match[1]
if (str) {
return (/^['"]/).test(str) ? str.slice(1, -1) : str
match = match && match[1]
if (match) {
return (/^['"]/).test(match) ? match.slice(1, -1) : match
}
}
return ''
Expand All @@ -702,8 +701,7 @@ function getAttr (str, name) {
function getParserOptions (attrs) {
var opts = getAttr(attrs, 'options')

if (opts) opts = JSON.parse(opts)
return opts
return opts ? JSON.parse(opts) : null
}

function getCode (code, opts, attrs, url) {
Expand Down Expand Up @@ -731,20 +729,22 @@ function cssCode (code, opts, attrs, url, tag) {
return _compileCSS(code, tag, getType(attrs) || opts.style, extraOpts)
}

var END_TAGS = /\/>\n|^<(?:\/[-\w]+\s*|[-\w]+(?:\s+(?:[-\w:\xA0-\xFF][\S\s]*?)?)?)>\n/
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 (/<[-\w]/.test(str)) {
k = str.lastIndexOf('<')
var
m,
k = str.lastIndexOf('<'),
n = str.length

while (~k) {
m = str.slice(k).match(END_TAGS)
m = str.slice(k, n).match(END_TAGS)
if (m) {
k += m.index + m[0].length
return [str.slice(0, k), str.slice(k)]
}
n = k
k = str.lastIndexOf('<', k - 1)
}
}
Expand All @@ -761,8 +761,10 @@ function compileTemplate (html, url, lang, opts) {
}

var

CUST_TAG = _regEx(/^([ \t]*)<([-\w]+)(?:\s+([^'"\/>]+(?:(?:@|\/[^>])[^'"\/>]*)*)|\s*)?(?:\/>|>[ \t]*\n?([\S\s]*)^\1<\/\2\s*>|>(.*)<\/\2\s*>)/
.source.replace('@', S_STRINGS), 'gim'),

SCRIPTS = /<script(\s+[^>]*)?>\n?([\S\s]*?)<\/script\s*>/gi,
STYLES = /<style(\s+[^>]*)?>\n?([\S\s]*?)<\/style\s*>/gi

Expand Down Expand Up @@ -805,8 +807,7 @@ function compile (src, opts, url) {
if (body2) {
/* istanbul ignore next */
html = included('html') ? _compileHTML(body2, opts, pcex) : ''
}
else {
} else {

var blocks = splitBlocks(
body.replace(_regEx('^' + indent, 'gm'), '').replace(TRIM_TRAIL, '')
Expand Down

0 comments on commit d2843fe

Please sign in to comment.