Socket
Socket
Sign inDemoInstall

riot-compiler

Package Overview
Dependencies
0
Maintainers
4
Versions
50
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.3.18 to 2.3.19

lib/brackets.js

5

CHANGELOG.md
# Compiler Changes
### v2.3.19
- Fixing issues with double quotes.
- Removed dependency on riot-tmpl for the node build, now we are using a local version of `brackets`.
### v2.3.18

@@ -8,3 +12,2 @@

- Fix: avoid changing the global brackets when the compiler is called with other brackets (requires riot-tmpl v2.3.15).
- Preparation for recognize the raw-html flag `=` (can change in the final implementation).
- A new property `version` (string) is included in the compiler set.

@@ -11,0 +14,0 @@ - Fixes to travis CI and the bump routine

314

dist/compiler.js

@@ -1,2 +0,2 @@

/* riot-compiler v2.3.18, @license MIT, (c) 2015 Muut Inc. + contributors */
/* riot-compiler v2.3.19, @license MIT, (c) 2015 Muut Inc. + contributors */
'use strict' // eslint-disable-line

@@ -8,4 +8,24 @@

var parsers = (function () {
var _mods = {}
var _mods = {
none: function (js) {
return js
}
}
_mods.javascript = _mods.none
var _modnames = {
es6: 'babel',
babel: 'babel-core',
javascript: 'none',
typescript: 'typescript-simple',
coffee: 'coffee-script',
coffeescript: 'coffee-script',
scss: 'node-sass',
sass: 'node-sass'
}
function _modname(name) {
return _modnames[name] || name
}
function _try(name, req) { //eslint-disable-line complexity

@@ -24,29 +44,8 @@ var parser

switch (name) {
case 'es6':
/* istanbul ignore next */
/* istanbul ignore next */
if (name === 'es6')
return fn('babel') || fn('babel-core')
case 'babel':
req = 'babel-core'
break
case 'none':
case 'javascript':
return _js.none
case 'typescript':
req = name + '-simple'
break
case 'coffee':
case 'coffeescript':
req = 'coffee-script'
break
case 'scss':
case 'sass':
req = 'node-sass'
break
default:
if (!req) req = name
break
}
parser = fn(req)
parser = fn(req || _modname(name))
return parser

@@ -81,5 +80,5 @@ }

scss: function(tag, css, opts, url) {
var sass = _req('sass')
var scss = _req('scss')
return sass.renderSync(extend({
return scss.renderSync(extend({
data: css,

@@ -115,5 +114,2 @@ indentedSyntax: false,

var _js = {
none: function (js, opts, url) {
return js
},
livescript: function (js, opts, url) {

@@ -131,5 +127,6 @@ return _req('livescript').compile(js, extend({bare: true, header: false}, opts))

babel: function (js, opts, url) {
// istanbul ignore next: url empty if comming from expression
return _req('babel').transform(js,
extend({
filename: url
filename: url || ''
}, opts)

@@ -140,3 +137,4 @@ ).code

return _req('coffee').compile(js, extend({bare: true}, opts))
}
},
none: _mods.none
}

@@ -147,8 +145,151 @@

return {html: _html, css: _css, js: _js, _req: _req}
return {
html: _html,
css: _css,
js: _js,
_modname: _modname,
_req: _req}
})()
var brackets = require('riot-tmpl').brackets
/**
* @module brackets
*
* `brackets ` Returns a string or regex based on its parameter
* `brackets.settings` Mirrors the `riot.settings` object (use brackets.set in new code)
* `brackets.set ` Change the current riot brackets
*/
var brackets = (function () {
var
REGLOB = 'g',
MLCOMMS = /\/\*[^*]*\*+(?:[^*\/][^*]*\*+)*\//g,
STRINGS = /"[^"\\]*(?:\\[\S\s][^"\\]*)*"|'[^'\\]*(?:\\[\S\s][^'\\]*)*'/g,
S_QBSRC = STRINGS.source + '|' +
/(?:\breturn\s+|(?:[$\w\)\]]|\+\+|--)\s*(\/)(?![*\/]))/.source + '|' +
/\/(?=[^*\/])[^[\/\\]*(?:(?:\[(?:\\.|[^\]\\]*)*\]|\\.)[^[\/\\]*)*?(\/)[gim]*/.source,
DEFAULT = '{ }',
FINDBRACES = {
'(': RegExp('([()])|' + S_QBSRC, REGLOB),
'[': RegExp('([[\\]])|' + S_QBSRC, REGLOB),
'{': RegExp('([{}])|' + S_QBSRC, REGLOB)
},
_pairs = [
'{', '}',
'{', '}',
/{[^}]*}/,
/\\({|})/g,
/(\\?)({)/g,
RegExp('(\\\\?)(?:([[({])|(}))|' + S_QBSRC, REGLOB),
DEFAULT
]
function _rewrite(re, bp) {
return RegExp(
re.source.replace(/{/g, bp[2]).replace(/}/g, bp[3]), re.global ? REGLOB : ''
)
}
var _brackets = {
R_STRINGS: STRINGS,
R_MLCOMMS: MLCOMMS,
S_QBLOCKS: S_QBSRC
}
_brackets.split = function split(str, tmpl, _bp) {
var
parts = [],
match,
isexpr,
start,
pos,
re = _bp[6]
isexpr = start = re.lastIndex = 0
while (match = re.exec(str)) {
pos = match.index
if (isexpr) {
if (match[2]) {
re.lastIndex = skipBraces(match[2], re.lastIndex)
continue
}
if (!match[3])
continue
}
if (!match[1]) {
unescapeStr(str.slice(start, pos))
start = re.lastIndex
re = _bp[6 + (isexpr ^= 1)]
re.lastIndex = start
}
}
if (str && start < str.length) {
unescapeStr(str.slice(start))
}
return parts
function unescapeStr(str) {
if (isexpr)
parts.push(str && str.replace(_bp[5], '$1'))
else
parts.push(str)
}
function skipBraces(ch, pos) {
var
match,
recch = FINDBRACES[ch],
level = 1
recch.lastIndex = pos
while (match = recch.exec(str)) {
// istanbul ignore next
if (match[1] &&
!(match[1] === ch ? ++level : --level)) break
}
// istanbul ignore next
return match ? recch.lastIndex : str.length
}
}
_brackets.array = function array(pair) {
if (!pair || pair === DEFAULT) return _pairs
var
arr = pair.split(' ')
// istanbul ignore next
if (arr.length !== 2 || /[\x00-\x1F<>a-zA-Z0-9'",;\\]/.test(pair)) {
throw new Error('Unsupported brackets "' + pair + '"')
}
arr = arr.concat(pair.replace(/(?=[[\]()*+?.^$|])/g, '\\').split(' '))
// istanbul ignore next
arr[4] = _rewrite(arr[1].length > 1 ? /{[\S\s]*?}/ : _pairs[4], arr)
arr[5] = _rewrite(/\\({|})/g, arr)
arr[6] = _rewrite(/(\\?)({)/g, arr)
arr[7] = RegExp('(\\\\?)(?:([[({])|(' + arr[3] + '))|' + S_QBSRC, REGLOB)
arr[8] = pair
return arr
}
return _brackets
})()
/**

@@ -158,7 +299,2 @@ * @module compiler

// istanbul ignore next
if (!brackets.version) {
throw new Error('This compiler version requires riot-tmpl v2.3.18 or above')
}
function _regEx(str, opt) { return new RegExp(str, opt) }

@@ -260,3 +396,3 @@

var
jsfn = opts.expr && (opts.parser || opts.type) ? compileJS : 0,
jsfn = opts.expr && (opts.parser || opts.type) ? _compileJS : 0,
list = brackets.split(html, 0, _bp),

@@ -294,3 +430,3 @@ expr

}
return pcex._bp[0] + expr
return pcex._bp[0] + expr.replace(/"/g, '\u2057')
})

@@ -308,10 +444,4 @@ }

function compileHTML(html, opts, pcex ) {
function _compileHTML(html, opts, pcex) {
var intf = (pcex || (pcex = []))._intflag
if (!intf)
html = html.replace(/\r\n?/g, '\n').replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
if (!pcex._bp) pcex._bp = brackets.array(opts.brackets)
html = splitHtml(html, opts, pcex)

@@ -347,2 +477,21 @@ .replace(HTML_TAGS, function (_, name, attr, ends) {

// istanbul ignore next
function compileHTML(html, opts, pcex) {
if (Array.isArray(opts)) {
pcex = opts
opts = {}
}
else {
if (!pcex) pcex = []
if (!opts) opts = {}
}
if (!pcex.__intflag)
html = html.replace(/\r\n?/g, '\n').replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
if (!pcex._bp) pcex._bp = brackets.array(opts.brackets)
return _compileHTML(html, opts, pcex)
}
var

@@ -392,3 +541,3 @@ JS_RMCOMMS = _regEx('(' + brackets.S_QBLOCKS + ')|' + brackets.R_MLCOMMS.source + '|//[^\r\n]*', 'g'),

function compileJS(js, opts, type, parserOpts, url) {
function _compileJS(js, opts, type, parserOpts, url) {
if (!js) return ''

@@ -404,2 +553,18 @@ if (!type) type = opts.type

// istanbul ignore next
function compileJS(js, opts, type, extra) {
if (typeof opts === 'string') {
extra = type
type = opts
opts = {}
}
if (typeof type === 'object') {
extra = type
type = ''
}
else if (!extra) extra = {}
return _compileJS(js, opts, type, extra.parserOptions, extra.url)
}
var CSS_SELECTOR = _regEx('(}|{|^)[ ;]*([^@ ;{}][^{}]*)(?={)|' + brackets.R_STRINGS.source, 'g')

@@ -430,3 +595,4 @@

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

@@ -438,3 +604,3 @@ if (type) {

else if (parsers.css[type]) {
style = parsers.css[type](tag, style, opts)
style = parsers.css[type](tag, style, opts.parserOpts, opts.url)
}

@@ -448,5 +614,21 @@ else if (type !== 'css') {

return scoped ? scopedCSS(tag, style) : style
if (scoped) {
// istanbul ignore next
if (!tag)
throw new Error('Can not parse scoped CSS without a tagName')
style = scopedCSS(tag, style)
}
return style
}
// istanbul ignore next
function compileCSS(style, parser, opts) {
if (typeof parser === 'object') {
opts = parser
parser = ''
}
else if (!opts) opts = {}
return _compileCSS(style, opts.tagName, parser, opts)
}
var

@@ -499,3 +681,3 @@ TYPE_ATTR = /\stype\s*=\s*(?:(['"])(.+?)\1|(\S+))/i,

}
return compileJS(code, opts, type, parserOpts, url)
return _compileJS(code, opts, type, parserOpts, url)
}

@@ -566,3 +748,3 @@

pcex._bp = _bp
pcex._intflag = 1
pcex.__intflag = 1

@@ -580,3 +762,3 @@ tagName = tagName.toLowerCase()

/* istanbul ignore next */
html = included('html') ? compileHTML(body2, opts, pcex, 1, url) : ''
html = included('html') ? _compileHTML(body2, opts, pcex) : ''
}

@@ -587,6 +769,9 @@ else {

body = body.replace(STYLE, included('css') ? function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs),
csstype = getType(_attrs) || opts.style
var extraOpts = {
scoped: _attrs && /\sscoped(\s|=|$)/i.test(_attrs),
url: url,
parserOpts: getParserOptions(_attrs)
}
styles += (styles ? ' ' : '') +
compileCSS(_style, tagName, csstype, scoped, getParserOptions(_attrs), url)
_compileCSS(_style, tagName, getType(_attrs) || opts.style, extraOpts)
return ''

@@ -605,3 +790,3 @@ } : '')

if (body)
html = compileHTML(body, opts, pcex, 1)
html = _compileHTML(body, opts, pcex)
}

@@ -612,3 +797,3 @@

if (/\S/.test(body))
jscode += (jscode ? '\n' : '') + compileJS(body, opts, null, null, url)
jscode += (jscode ? '\n' : '') + _compileJS(body, opts, null, null, url)
}

@@ -647,6 +832,7 @@ }

html: compileHTML,
style: compileCSS,
style: _compileCSS,
css: compileCSS,
js: compileJS,
parsers: parsers,
version: 'v2.3.18'
version: 'v2.3.19'
}
/**
* Compiler for riot custom tags
* @version v2.3.18
* @version v2.3.19
*/

@@ -12,3 +12,8 @@

var parsers = (function () {
var _mods = {}
var _mods = {
none: function (js) {
return js
}
}
_mods.javascript = _mods.none

@@ -23,4 +28,8 @@ function _try(name, req) { //eslint-disable-line complexity

case 'es6':
case 'babel':
req = 'babel'
break
case 'none':
case 'javascript':
return _mods.none
default:

@@ -34,2 +43,3 @@ if (!req) req = name

throw new Error(req + ' parser not found.')
_mods[name] = parser

@@ -78,5 +88,2 @@ return parser

var _js = {
none: function (js, opts, url) {
return js
},
livescript: function (js, opts, url) {

@@ -94,5 +101,6 @@ return _req('livescript').compile(js, extend({bare: true, header: false}, opts))

babel: function (js, opts, url) {
// istanbul ignore next: url empty if comming from expression
return _req('babel').transform(js,
extend({
filename: url
filename: url || ''
}, opts)

@@ -103,3 +111,4 @@ ).code

return _req('coffee').compile(js, extend({bare: true}, opts))
}
},
none: _mods.none
}

@@ -110,3 +119,7 @@

return {html: _html, css: _css, js: _js, _req: _req}
return {
html: _html,
css: _css,
js: _js,
_req: _req}

@@ -119,7 +132,2 @@ })()

// istanbul ignore next
if (!brackets.version) {
throw new Error('This compiler version requires riot-tmpl v2.3.18 or above')
}
function _regEx(str, opt) { return new RegExp(str, opt) }

@@ -219,3 +227,3 @@

var
jsfn = opts.expr && (opts.parser || opts.type) ? compileJS : 0,
jsfn = opts.expr && (opts.parser || opts.type) ? _compileJS : 0,
list = brackets.split(html, 0, _bp),

@@ -253,3 +261,3 @@ expr

}
return pcex._bp[0] + expr
return pcex._bp[0] + expr.replace(/"/g, '\u2057')
})

@@ -267,10 +275,4 @@ }

function compileHTML(html, opts, pcex ) {
function _compileHTML(html, opts, pcex) {
var intf = (pcex || (pcex = []))._intflag
if (!intf)
html = html.replace(/\r\n?/g, '\n').replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
if (!pcex._bp) pcex._bp = brackets.array(opts.brackets)
html = splitHtml(html, opts, pcex)

@@ -306,2 +308,21 @@ .replace(HTML_TAGS, function (_, name, attr, ends) {

// istanbul ignore next
function compileHTML(html, opts, pcex) {
if (Array.isArray(opts)) {
pcex = opts
opts = {}
}
else {
if (!pcex) pcex = []
if (!opts) opts = {}
}
if (!pcex.__intflag)
html = html.replace(/\r\n?/g, '\n').replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
if (!pcex._bp) pcex._bp = brackets.array(opts.brackets)
return _compileHTML(html, opts, pcex)
}
var

@@ -351,3 +372,3 @@ JS_RMCOMMS = _regEx('(' + brackets.S_QBLOCKS + ')|' + brackets.R_MLCOMMS.source + '|//[^\r\n]*', 'g'),

function compileJS(js, opts, type, parserOpts, url) {
function _compileJS(js, opts, type, parserOpts, url) {
if (!js) return ''

@@ -363,2 +384,18 @@ if (!type) type = opts.type

// istanbul ignore next
function compileJS(js, opts, type, extra) {
if (typeof opts === 'string') {
extra = type
type = opts
opts = {}
}
if (typeof type === 'object') {
extra = type
type = ''
}
else if (!extra) extra = {}
return _compileJS(js, opts, type, extra.parserOptions, extra.url)
}
var CSS_SELECTOR = _regEx('(}|{|^)[ ;]*([^@ ;{}][^{}]*)(?={)|' + brackets.R_STRINGS.source, 'g')

@@ -389,3 +426,4 @@

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

@@ -397,3 +435,3 @@ if (type) {

else if (parsers.css[type]) {
style = parsers.css[type](tag, style, opts)
style = parsers.css[type](tag, style, opts.parserOpts, opts.url)
}

@@ -407,5 +445,21 @@ else if (type !== 'css') {

return scoped ? scopedCSS(tag, style) : style
if (scoped) {
// istanbul ignore next
if (!tag)
throw new Error('Can not parse scoped CSS without a tagName')
style = scopedCSS(tag, style)
}
return style
}
// istanbul ignore next
function compileCSS(style, parser, opts) {
if (typeof parser === 'object') {
opts = parser
parser = ''
}
else if (!opts) opts = {}
return _compileCSS(style, opts.tagName, parser, opts)
}
var

@@ -448,3 +502,3 @@ TYPE_ATTR = /\stype\s*=\s*(?:(['"])(.+?)\1|(\S+))/i,

return compileJS(code, opts, type, parserOpts, url)
return _compileJS(code, opts, type, parserOpts, url)
}

@@ -513,3 +567,3 @@

pcex._bp = _bp
pcex._intflag = 1
pcex.__intflag = 1

@@ -527,3 +581,3 @@ tagName = tagName.toLowerCase()

/* istanbul ignore next */
html = included('html') ? compileHTML(body2, opts, pcex, 1, url) : ''
html = included('html') ? _compileHTML(body2, opts, pcex) : ''
}

@@ -534,6 +588,9 @@ else {

body = body.replace(STYLE, included('css') ? function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs),
csstype = getType(_attrs) || opts.style
var extraOpts = {
scoped: _attrs && /\sscoped(\s|=|$)/i.test(_attrs),
url: url,
parserOpts: getParserOptions(_attrs)
}
styles += (styles ? ' ' : '') +
compileCSS(_style, tagName, csstype, scoped, getParserOptions(_attrs), url)
_compileCSS(_style, tagName, getType(_attrs) || opts.style, extraOpts)
return ''

@@ -552,3 +609,3 @@ } : '')

if (body)
html = compileHTML(body, opts, pcex, 1)
html = _compileHTML(body, opts, pcex)
}

@@ -559,3 +616,3 @@

if (/\S/.test(body))
jscode += (jscode ? '\n' : '') + compileJS(body, opts, null, null, url)
jscode += (jscode ? '\n' : '') + _compileJS(body, opts, null, null, url)
}

@@ -586,3 +643,3 @@ }

var version = 'v2.3.18'
var version = 'v2.3.19'

@@ -589,0 +646,0 @@ export default {

@@ -6,3 +6,8 @@

var parsers = (function () {
var _mods = {}
var _mods = {
none: function (js) {
return js
}
}
_mods.javascript = _mods.none

@@ -17,4 +22,8 @@ function _try(name, req) { //eslint-disable-line complexity

case 'es6':
case 'babel':
req = 'babel'
break
case 'none':
case 'javascript':
return _mods.none
default:

@@ -28,2 +37,3 @@ if (!req) req = name

throw new Error(req + ' parser not found.')
_mods[name] = parser

@@ -72,5 +82,2 @@ return parser

var _js = {
none: function (js, opts, url) {
return js
},
livescript: function (js, opts, url) {

@@ -88,5 +95,6 @@ return _req('livescript').compile(js, extend({bare: true, header: false}, opts))

babel: function (js, opts, url) {
// istanbul ignore next: url empty if comming from expression
return _req('babel').transform(js,
extend({
filename: url
filename: url || ''
}, opts)

@@ -97,3 +105,4 @@ ).code

return _req('coffee').compile(js, extend({bare: true}, opts))
}
},
none: _mods.none
}

@@ -104,3 +113,7 @@

return {html: _html, css: _css, js: _js, _req: _req}
return {
html: _html,
css: _css,
js: _js,
_req: _req}

@@ -113,11 +126,6 @@ })()

* Compiler for riot custom tags
* @version v2.3.18
* @version v2.3.19
*/
var compile = (function () {
// istanbul ignore next
if (!brackets.version) {
throw new Error('This compiler version requires riot-tmpl v2.3.18 or above')
}
function _regEx(str, opt) { return new RegExp(str, opt) }

@@ -217,3 +225,3 @@

var
jsfn = opts.expr && (opts.parser || opts.type) ? compileJS : 0,
jsfn = opts.expr && (opts.parser || opts.type) ? _compileJS : 0,
list = brackets.split(html, 0, _bp),

@@ -251,3 +259,3 @@ expr

}
return pcex._bp[0] + expr
return pcex._bp[0] + expr.replace(/"/g, '\u2057')
})

@@ -265,10 +273,4 @@ }

function compileHTML(html, opts, pcex ) {
function _compileHTML(html, opts, pcex) {
var intf = (pcex || (pcex = []))._intflag
if (!intf)
html = html.replace(/\r\n?/g, '\n').replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
if (!pcex._bp) pcex._bp = brackets.array(opts.brackets)
html = splitHtml(html, opts, pcex)

@@ -304,2 +306,21 @@ .replace(HTML_TAGS, function (_, name, attr, ends) {

// istanbul ignore next
function compileHTML(html, opts, pcex) {
if (Array.isArray(opts)) {
pcex = opts
opts = {}
}
else {
if (!pcex) pcex = []
if (!opts) opts = {}
}
if (!pcex.__intflag)
html = html.replace(/\r\n?/g, '\n').replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
if (!pcex._bp) pcex._bp = brackets.array(opts.brackets)
return _compileHTML(html, opts, pcex)
}
var

@@ -349,3 +370,3 @@ JS_RMCOMMS = _regEx('(' + brackets.S_QBLOCKS + ')|' + brackets.R_MLCOMMS.source + '|//[^\r\n]*', 'g'),

function compileJS(js, opts, type, parserOpts, url) {
function _compileJS(js, opts, type, parserOpts, url) {
if (!js) return ''

@@ -361,2 +382,18 @@ if (!type) type = opts.type

// istanbul ignore next
function compileJS(js, opts, type, extra) {
if (typeof opts === 'string') {
extra = type
type = opts
opts = {}
}
if (typeof type === 'object') {
extra = type
type = ''
}
else if (!extra) extra = {}
return _compileJS(js, opts, type, extra.parserOptions, extra.url)
}
var CSS_SELECTOR = _regEx('(}|{|^)[ ;]*([^@ ;{}][^{}]*)(?={)|' + brackets.R_STRINGS.source, 'g')

@@ -387,3 +424,4 @@

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

@@ -395,3 +433,3 @@ if (type) {

else if (parsers.css[type]) {
style = parsers.css[type](tag, style, opts)
style = parsers.css[type](tag, style, opts.parserOpts, opts.url)
}

@@ -405,5 +443,21 @@ else if (type !== 'css') {

return scoped ? scopedCSS(tag, style) : style
if (scoped) {
// istanbul ignore next
if (!tag)
throw new Error('Can not parse scoped CSS without a tagName')
style = scopedCSS(tag, style)
}
return style
}
// istanbul ignore next
function compileCSS(style, parser, opts) {
if (typeof parser === 'object') {
opts = parser
parser = ''
}
else if (!opts) opts = {}
return _compileCSS(style, opts.tagName, parser, opts)
}
var

@@ -446,3 +500,3 @@ TYPE_ATTR = /\stype\s*=\s*(?:(['"])(.+?)\1|(\S+))/i,

return compileJS(code, opts, type, parserOpts, url)
return _compileJS(code, opts, type, parserOpts, url)
}

@@ -511,3 +565,3 @@

pcex._bp = _bp
pcex._intflag = 1
pcex.__intflag = 1

@@ -525,3 +579,3 @@ tagName = tagName.toLowerCase()

/* istanbul ignore next */
html = included('html') ? compileHTML(body2, opts, pcex, 1, url) : ''
html = included('html') ? _compileHTML(body2, opts, pcex) : ''
}

@@ -532,6 +586,9 @@ else {

body = body.replace(STYLE, included('css') ? function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs),
csstype = getType(_attrs) || opts.style
var extraOpts = {
scoped: _attrs && /\sscoped(\s|=|$)/i.test(_attrs),
url: url,
parserOpts: getParserOptions(_attrs)
}
styles += (styles ? ' ' : '') +
compileCSS(_style, tagName, csstype, scoped, getParserOptions(_attrs), url)
_compileCSS(_style, tagName, getType(_attrs) || opts.style, extraOpts)
return ''

@@ -550,3 +607,3 @@ } : '')

if (body)
html = compileHTML(body, opts, pcex, 1)
html = _compileHTML(body, opts, pcex)
}

@@ -557,3 +614,3 @@

if (/\S/.test(body))
jscode += (jscode ? '\n' : '') + compileJS(body, opts, null, null, url)
jscode += (jscode ? '\n' : '') + _compileJS(body, opts, null, null, url)
}

@@ -587,5 +644,5 @@ }

html: compileHTML,
style: compileCSS,
css: compileCSS,
js: compileJS,
version: 'v2.3.18'
version: 'v2.3.19'
}

@@ -592,0 +649,0 @@ return compile

@@ -33,3 +33,3 @@ # Compiler Guide (complement, WIP)

### One-line tags
### One-line Tags

@@ -49,3 +49,3 @@ Riot can handle one-line tag definitions like this:

### Untagged html content
### Untagged HTML Content

@@ -119,3 +119,3 @@ From the Riot Guide:

### Brackets and backslashes
### Brackets and Backslashes

@@ -143,3 +143,3 @@ From the perspective of the riot compiler, backslashes in the template are characters with no special meaning.

## Compilation options
## Compiler Options

@@ -160,3 +160,3 @@ The `compile` and `riot.compile` functions can take an additional parameter specifing various settings. This is a plain JavaScript object with one or more of following options as properties.

### The `entities` option
### The `entities` Option

@@ -194,3 +194,3 @@ This option, new in v2.3.13, causes the `compile` function return an array of objects with the parts of the tags.

## Parser options
## Parser Options

@@ -218,7 +218,7 @@ In addition to the `type` attribute, `script` and `style` tags can include additional configuration at tag level through the `options` attribute.

## The JavaScript
## JavaScript
Where the html ends? or where should I put my JavaScript?
### The untagged JS block
### The Untagged JavaScript Block

@@ -239,3 +239,3 @@ The first action taken by the compiler is send the received source to any html parser.

### Multiple JavaScript blocks
### Multiple JavaScript Blocks

@@ -245,9 +245,10 @@ Each JavaScript block in the tag can have different `type` attributes.

### Loading JavaScript from the file system (v2.3.13)
### Loading JavaScript from the File System (v2.3.13)
The `src` attribute of the `script` tags inside a riot tag, allows load source files from the file system.
The filename in `src` can be absolute or relative to the tag being compiled.
The filename in `src` can be absolute or relative.
It can be combined with the `charset` attribute. `charset` defaults to `utf8` and the JavaScript type defaults to the `type` option specified in the options passed to the compiler.
For this feature to work, you need to pass a third parameter to the `compile` function: the name of the file being compiled.
**Note**
If you pass a third parameter to the `compile` function with the full name of the file being compiled, relative paths will be resolved from this name, if not, these will be relative to the current working directory (as returned by `proccess.cwd()`).

@@ -289,4 +290,66 @@ Example:

### Scoped style
### Scoped Style
(WIP)
### Utility Functions
There are functions in the node.js build that allow you to compile certain sources:
#### html(source, compilerOptions, compiledExpressions)
| parameter | type | description
| --------- | ---- | -----------
| source | string | html markup, without styles nor JavaScript code
| compilerOptions | object | optional. Used properties: `brackets`, `withespace`, `compact`, `type`, `expr`
| compiledExpressions | Array | optional. See below.
`compiledExpressions` is interesting, on return it holds, in order of appearance, trimmed and without brackets, the expressions found in the html. If you set `type` and `expr` in the `compilerOptions` parameter, the expressions will be compiled.
#### css(source, parserName, extraOptions)
| parameter | type | description
| --------- | ---- | -----------
| source | string | styles
| parserName | string/function | optional, can be omited. If string, must be one of `parsers.js`
| extraOptions | Object | optional, can be omited. Used properties: `tagName`, `parserOpts`, `url`, and `scoped`
`tagName`, `parserOpts`, `url` are passed to the given parser.
`scoped` will compile the styles as Scoped CSS after run any parser, `scoped` can't be used without `tagName`.
Example:
```js
var opts = {url: filename, scoped: true, tagName: 'my-tag'},
css = compiler.css(styles, 'stylus', opts)
```
will run `parsers.css.stylus(opts.tagName, styles, opts.parserOpts, opts.url)`, then convert the result to Scoped CSS with `my-tag` as root.
#### js(source, compilerOptions, parserName, extraOptions)
| parameter | type | description
| --------- | ---- | -----------
| source | string | html markup, without styles nor JavaScript code
| compilerOptions | object | Optional, see note for omision. Used properties: `type`
| parserName | string | optional. If string, must be one of `parsers.js`
| extraOptions | Object | optional. Used properties: `parserOpts`, `url`
`parserOpts` and `url` are passed to the given parser.
Example:
```js
var opts = {url: url},
css = compiler.js(code, 'babel', opts)
```
will run `parsers.js.babel(code, opts.parserOpts, opts.url)` (inside the parser, the url will be passed as `{filename: url}` to babel).
**Note:**
If you omit `parserName` but include `extraOptions`, you **must** include `compilerOptions` as well:
```js
var js = compiler.js(source, {}, extraOptions)
```
Since the default JS parser does not make use of the extra options, this is the same:
```js
var js = compiler.js(source)
```
`compilerOptions` will be removed in a future version.

@@ -6,3 +6,3 @@ /* riot-compiler WIP, @license MIT, (c) 2015 Muut Inc. + contributors */

var brackets = require('riot-tmpl').brackets
//#include_once brackets

@@ -17,3 +17,4 @@ /**

html: compileHTML,
style: compileCSS,
style: _compileCSS,
css: compileCSS,
js: compileJS,

@@ -20,0 +21,0 @@ parsers: parsers,

@@ -20,3 +20,3 @@

html: compileHTML,
style: compileCSS,
css: compileCSS,
js: compileJS,

@@ -23,0 +23,0 @@ version: 'WIP'

// istanbul ignore next
if (!brackets.version) {
throw new Error('This compiler version requires riot-tmpl v2.3.18 or above')
}
function _regEx(str, opt) { return new RegExp(str, opt) }

@@ -153,3 +148,3 @@

var
jsfn = opts.expr && (opts.parser || opts.type) ? compileJS : 0,
jsfn = opts.expr && (opts.parser || opts.type) ? _compileJS : 0,
list = brackets.split(html, 0, _bp),

@@ -188,3 +183,3 @@ expr

}
return pcex._bp[0] + expr
return pcex._bp[0] + expr.replace(/"/g, '\u2057')
})

@@ -207,21 +202,4 @@ }

/**
* 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 /*, url*/) {
function _compileHTML(html, opts, pcex) {
var intf = (pcex || (pcex = []))._intflag
if (!intf)
html = html.replace(/\r\n?/g, '\n').replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
// `_bp` is undefined when `compileHTML` is not called by compile
if (!pcex._bp) pcex._bp = brackets.array(opts.brackets)
// separate the expressions, then parse the tags and their attributes

@@ -260,2 +238,32 @@ html = splitHtml(html, opts, pcex)

/**
* 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
* @returns {string} The parsed HTML text
* @see http://www.w3.org/TR/html5/syntax.html
*/
// istanbul ignore next
function compileHTML(html, opts, pcex) {
if (Array.isArray(opts)) {
pcex = opts
opts = {}
}
else {
if (!pcex) pcex = []
if (!opts) opts = {}
}
if (!pcex.__intflag)
html = html.replace(/\r\n?/g, '\n').replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
// `_bp` is undefined when `compileHTML` is not called by compile
if (!pcex._bp) pcex._bp = brackets.array(opts.brackets)
return _compileHTML(html, opts, pcex)
}
// JavaScript Compilation

@@ -318,13 +326,3 @@ // ----------------------

/**
* 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] - User options for the parser
* @param {string} [url] - url of the file being compiled
* @returns {string} The parsed JavaScript code
*/
function compileJS(js, opts, type, parserOpts, url) {
function _compileJS(js, opts, type, parserOpts, url) {
if (!js) return ''

@@ -340,3 +338,28 @@ if (!type) type = opts.type

/**
* Runs the parser for the JavaScript code, defaults to `riotjs`
*
* @param {string} js - Buffer with the javascript code
* @param {Object} [opts] - Compiler options, can include a custom parser function
* @param {string} [type] - Optional type for parser selection
* @param {Object} [extra] - User options for the parser
* @returns {string} The parsed JavaScript code
*/
// istanbul ignore next
function compileJS(js, opts, type, extra) {
if (typeof opts === 'string') {
extra = type
type = opts
opts = {}
}
if (typeof type === 'object') {
extra = type
type = ''
}
else if (!extra) extra = {}
return _compileJS(js, opts, type, extra.parserOptions, extra.url)
}
// CSS Compilation

@@ -377,14 +400,4 @@ // ----------------

/**
* 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) {
function _compileCSS(style, tag, type, opts) {
var scoped = (opts || (opts = {})).scoped

@@ -396,3 +409,3 @@ if (type) {

else if (parsers.css[type]) {
style = parsers.css[type](tag, style, opts)
style = parsers.css[type](tag, style, opts.parserOpts, opts.url)
}

@@ -408,5 +421,29 @@ else if (type !== 'css') {

// translate scoped rules if nedded
return scoped ? scopedCSS(tag, style) : style
if (scoped) {
// istanbul ignore next
if (!tag)
throw new Error('Can not parse scoped CSS without a tagName')
style = scopedCSS(tag, style)
}
return style
}
/**
* Runs the parser for style blocks.
* Simple API to the compileCSS function.
*
* @param {string} style - Raw style block
* @param {string} [parser] - Must be one of `parsers.css`, can be omited.
* @param {object} [opts] - passed to the given parser, can be omited.
* @returns {string} The processed style block
*/
// istanbul ignore next
function compileCSS(style, parser, opts) {
if (typeof parser === 'object') {
opts = parser
parser = ''
}
else if (!opts) opts = {}
return _compileCSS(style, opts.tagName, parser, opts)
}

@@ -471,3 +508,3 @@ // The main compiler

//#endif
return compileJS(code, opts, type, parserOpts, url)
return _compileJS(code, opts, type, parserOpts, url)
}

@@ -499,3 +536,11 @@

// Runs the external HTML parser for the entire tag file
/**
* Runs the external HTML parser for the entire tag file
*
* @param {string} html - Entire, untouched html received for the compiler
* @param {string} url - The source url or file name
* @param {string} lang - Name of the parser, one of `parsers.html`
* @param {object} opts - Extra option passed to the parser
* @returns {string} parsed html
*/
function compileTemplate(html, url, lang, opts) {

@@ -578,3 +623,3 @@ var parser = parsers.html[lang]

pcex._bp = _bp // local copy, in preparation for async compilation
pcex._intflag = 1
pcex.__intflag = 1

@@ -594,3 +639,3 @@ tagName = tagName.toLowerCase()

/* istanbul ignore next */
html = included('html') ? compileHTML(body2, opts, pcex, 1, url) : ''
html = included('html') ? _compileHTML(body2, opts, pcex) : ''
}

@@ -603,6 +648,9 @@ else {

body = body.replace(STYLE, included('css') ? function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs),
csstype = getType(_attrs) || opts.style
var extraOpts = {
scoped: _attrs && /\sscoped(\s|=|$)/i.test(_attrs),
url: url,
parserOpts: getParserOptions(_attrs)
}
styles += (styles ? ' ' : '') +
compileCSS(_style, tagName, csstype, scoped, getParserOptions(_attrs), url)
_compileCSS(_style, tagName, getType(_attrs) || opts.style, extraOpts)
return ''

@@ -625,3 +673,3 @@ } : '')

if (body)
html = compileHTML(body, opts, pcex, 1)
html = _compileHTML(body, opts, pcex)
}

@@ -632,3 +680,3 @@

if (/\S/.test(body))
jscode += (jscode ? '\n' : '') + compileJS(body, opts, null, null, url)
jscode += (jscode ? '\n' : '') + _compileJS(body, opts, null, null, url)
}

@@ -635,0 +683,0 @@ }

@@ -5,7 +5,36 @@ /**

var parsers = (function () {
var _mods = {} // cache of modules
var _mods = { // cache of required modules
none: function (js) {
return js
}
}
_mods.javascript = _mods.none
//#if NODE
var _modnames = { // for name-module convertion
es6: 'babel',
babel: 'babel-core',
javascript: 'none',
typescript: 'typescript-simple',
coffee: 'coffee-script',
coffeescript: 'coffee-script',
scss: 'node-sass',
sass: 'node-sass'
}
/**
* Returns the module name for the given parser name.
*
* @param {string} name - the _mods element, one of parsers.html/jss/css
* @returns {Function} parser function, or null if error
*/
function _modname(name) {
return _modnames[name] || name
}
//#endif
/**
* Loads a parser instance.
* On success, saves the function in _mods before return it to caller.
*
* @param {string} name - the _mods element, one of parsers.html/jss/css

@@ -29,28 +58,7 @@ * @param {string} [req] - name for require(), defaults to 'name'

switch (name) {
case 'es6':
/* istanbul ignore next */
/* istanbul ignore next */
if (name === 'es6')
return fn('babel') || fn('babel-core') // versions 5.8x
case 'babel':
req = 'babel-core'
break
case 'none':
case 'javascript':
return _js.none
case 'typescript':
req = name + '-simple'
break
case 'coffee':
case 'coffeescript':
req = 'coffee-script'
break
case 'scss':
case 'sass':
req = 'node-sass'
break
default:
if (!req) req = name
break
}
parser = fn(req)
parser = fn(req || _modname(name))
//#else

@@ -63,4 +71,8 @@

case 'es6':
case 'babel':
req = 'babel'
break
case 'none':
case 'javascript':
return _mods.none
default:

@@ -74,2 +86,3 @@ if (!req) req = name

throw new Error(req + ' parser not found.')
_mods[name] = parser
//#endif

@@ -111,5 +124,5 @@

scss: function(tag, css, opts, url) { // there's no standard sass for browsers
var sass = _req('sass')
var scss = _req('scss')
return sass.renderSync(extend({
return scss.renderSync(extend({
data: css,

@@ -146,5 +159,2 @@ indentedSyntax: false,

var _js = {
none: function (js, opts, url) {
return js
},
livescript: function (js, opts, url) {

@@ -162,5 +172,6 @@ return _req('livescript').compile(js, extend({bare: true, header: false}, opts))

babel: function (js, opts, url) {
// istanbul ignore next: url empty if comming from expression
return _req('babel').transform(js,
extend({
filename: url
filename: url || ''
}, opts)

@@ -171,3 +182,4 @@ ).code

return _req('coffee').compile(js, extend({bare: true}, opts))
}
},
none: _mods.none
}

@@ -178,4 +190,11 @@

return {html: _html, css: _css, js: _js, _req: _req}
return {
html: _html,
css: _css,
js: _js,
//#if NODE
_modname: _modname,
//#endif
_req: _req}
})()
{
"name": "riot-compiler",
"version": "2.3.18",
"version": "2.3.19",
"description": "Compiler for riot .tag files",

@@ -31,5 +31,2 @@ "main": "dist/compiler.js",

],
"dependencies": {
"riot-tmpl": "^2.3.18"
},
"devDependencies": {

@@ -36,0 +33,0 @@ "coveralls": "^2.11.4",

//src: test/specs/fixtures/raw-html.tag
riot.tag2('raw-html', '<p>{= [address1,address2].join(\'&lt;br&gt;\')}</p> <p onclick="{swap}"> {= \'&lt;\' + myElem + \' style="color: \' + myColor + \';"&gt;\\n Click\\n&lt;/\' + myElem + \'&gt;&lt;br&gt;\\n&lt;b&gt;me&lt;/b&gt;\'} </p>', '', '', function(opts) {
riot.tag2('raw-html', '<p>{= [address1,address2].join(\'&lt;br&gt;\')}</p> <p onclick="{swap}"> {= \'&lt;\' + myElem + \' style=⁗color: \' + myColor + \';⁗&gt;\\n Click\\n&lt;/\' + myElem + \'&gt;&lt;br&gt;\\n&lt;b&gt;me&lt;/b&gt;\'} </p>', '', '', function(opts) {

@@ -4,0 +4,0 @@ this.address1 = '1234 Peachtree'

//src: test/specs/fixtures/whitespace.tag
riot.tag2('my-tag', '<p></p>', 'p { display: none; }', 'style=" top:0; left:0" expr="{{ foo:"bar" }}"', function(opts) {
riot.tag2('my-tag', '<p></p>', 'p { display: none; }', 'style=" top:0; left:0" expr="{{ foo:⁗bar⁗ }}"', function(opts) {
this.click = function(e)
{}.bind(this)
}, '{ }');

@@ -72,6 +72,6 @@

it('nested double quotes are supported in expressions', function () {
testStr('<p x={ "a" } y="{2}">', '<p x="{"a"}" y="{2}">')
testStr('<p x="{"a"}" y="{2}">', '<p x="{"a"}" y="{2}">')
testStr('<p x=\'{"a"}\' y="{2}">', '<p x="{"a"}" y="{2}">')
testStr('<p x="{""}">', '<p x="{""}">')
testStr('<p x={ "a" } y="{2}">', '<p x="{\u2057a\u2057}" y="{2}">')
testStr('<p x="{"a"}" y="{2}">', '<p x="{\u2057a\u2057}" y="{2}">')
testStr('<p x=\'{"a"}\' y="{2}">', '<p x="{\u2057a\u2057}" y="{2}">')
testStr('<p x="{""}">', '<p x="{\u2057\u2057}">')
})

@@ -123,6 +123,6 @@

'<p>{= \'<\' + myElem + \' style="color: \' + myColor + \';">\\n Click me</\' + myElem + \'>\'}</p>',
'<p>{= \'&lt;\' + myElem + \' style="color: \' + myColor + \';"&gt;\\n Click me&lt;/\' + myElem + \'&gt;\'}</p>')
'<p>{= \'&lt;\' + myElem + \' style=\u2057color: \' + myColor + \';\u2057&gt;\\n Click me&lt;/\' + myElem + \'&gt;\'}</p>')
testStr(
'<ul><li>{= ["foo", "bar"].join(\'<br/>\') }</li></ul>',
'<ul><li>{= ["foo", "bar"].join(\'&lt;br/&gt;\')}</li></ul>')
'<ul><li>{= [\u2057foo\u2057, \u2057bar\u2057].join(\'&lt;br/&gt;\')}</li></ul>')
})

@@ -129,0 +129,0 @@

@@ -1,10 +0,10 @@

riot.tag2('raw-html', '<p>{=[address1,address2].join("&lt;br&gt;")}</p> <p onclick="{swap}"> {="&lt;" + myElem + " style=&quot;color: " + myColor + ";&quot;&gt;\\n Click\\n&lt;/" + myElem + "&gt;&lt;br&gt;\\n&lt;b&gt;me&lt;/b&gt;"} </p>', '', '', function(opts) {
riot.tag2('raw-html', '<p>{=[address1,address2].join(\'&lt;br&gt;\')}</p> <p onclick="{swap}"> {=\'&lt;\' + myElem + \' style=&quot;color: \' + myColor + \';&quot;&gt;\\n Click\\n&lt;/\' + myElem + \'&gt;&lt;br&gt;\\n&lt;b&gt;me&lt;/b&gt;\'} </p>', '', '', function(opts) {
this.address1 = "1234 Peachtree"
this.address2 = "Atlanta"
this.myElem = "span"
this.myColor = "red"
this.address1 = '1234 Peachtree'
this.address2 = 'Atlanta'
this.myElem = 'span'
this.myColor = 'red'
swap() {
this.myElem = this.myElem === "span" ? "p" : "span"
this.myElem = this.myElem === 'span' ? 'p' : 'span'
}
}, '{ }');

@@ -15,3 +15,3 @@ //

return true
console.error('\tnot installed locally: ' + (req || mod) + ' alias "' + mod + '"')
console.error('\tnot installed locally: ' + compiler.parsers._modname(req || mod) + ' alias "' + mod + '"')
return false

@@ -80,4 +80,4 @@ }

it('plays with quoted values', function () {
testStr('<a href={ "a" }>', '<a href="{@ "a"}">', opts)
testStr('<a>{"b"}</a>', '<a>{@"b"}</a>', opts)
testStr('<a href={ "a" }>', '<a href="{@ \u2057a\u2057}">', opts)
testStr('<a>{"b"}</a>', '<a>{@\u2057b\u2057}</a>', opts)
})

@@ -221,3 +221,3 @@

it('scss, indented 2, margin 0', function () {
if (have('scss', 'node-sass')) {
if (have('scss')) {
testParser('scss', {})

@@ -229,3 +229,3 @@ }

it('custom style options', function () {
if (have('sass', 'node-sass')) {
if (have('sass')) {
testParser('sass.options', {})

@@ -302,3 +302,3 @@ }

compiler.parsers.js.rawhtml = function(js) {
return js.replace(/"/g, '&quot;').replace(/'/g, '"')
return js.replace(/"/g, '&quot;')
}

@@ -305,0 +305,0 @@ testParser('raw', { type: 'rawhtml', expr: true })

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc