Skip to content

Commit

Permalink
Merge pull request #23 from riot/dev
Browse files Browse the repository at this point in the history
v2.3.12
  • Loading branch information
aMarCruz committed Nov 19, 2015
2 parents 2be37c1 + 329d291 commit ba356b2
Show file tree
Hide file tree
Showing 19 changed files with 424 additions and 151 deletions.
41 changes: 41 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# COMPILER CHANGES

## v2.3.12

- Gets rid of the zero-indentation restriction for custom tags, now you can indent these tags, but the opening and closing tag must have exactly the same indentation (length and type). All the tag will be unindented by this amount.
- Support for `src` and `charset` attributes in `<script>` tags for reading JavaScript sources from the file system - [riot#507](https://github.com/riot/riot/issues/507)
- The `compile` function can return separate parts by setting the new `entities` option. These parts has unescaped newlines.
- New attribute `options` for `script` and `style` tags will append/overwrite attributes in the default configuration object of the parser at tag level.
- Fix [riot#1261](https://github.com/riot/riot/issues/1261): `<pre>` tag does not preserve neither `\n` nor `\t`.
Now whitespace within `<pre>` tags is always preserved.
- Fix [riot#1358](https://github.com/riot/riot/issues/1358): Empty style in tag (scoped) breaks.

## v2.3.11

- New type="babel" supports babel-core v6.x. You must `npm install babel-preset-es2015` too, for this works.
Use type="es6" for babel and babel-core v5.8.x and bellow.
- Fix [riot#1306](https://github.com/riot/riot/issues/1306): Compiler preserves newlines in class objects, causing "Unterminated String Constant" errors.
- Fix [riot#1314](https://github.com/riot/riot/issues/1314): `settings.brackets` no longer works.

## v2.3.0

This is a complete rewrite and the first solo version of the compiler.

- Now the compiler generates a call to the new `riot.tag2` function, with the same parameters as `riot.tag` and an
additional one: the brackets used in the compilation. `riot.tag2` requires all parameters except the brackets,
so the compiler generates all but ignores the brackets if there are no generated expressions.
- Unlike previous versions, backslashes are removed from the expressions (before being sent to any parser).
Outside of expressions, all backslashes are preserved.
- Double quotes inside expressions are converted to `&quot;`, to avoid issues with HTML markup

- Fix [riot#1207](https://github.com/riot/riot/issues/1207): Riot compiler/parser breaks indentation.
- Fix [riot#1120](https://github.com/riot/riot/issues/1120): Double quotes break Riot attributes

Enhancements

- Multiple `<script>` blocks. These can have different types and are merged with the untagged script block, if any.
- More flexible formats in ES6 style method definitions.
- In the JavaScript, trailing whitespace are removed and multiple empty lines are combined into one.
- Better recognition of expressions. Now you can use almost any character, even in unquoted expressions (expressions containing the `>` operator needs to be enclosed in quotes) - [riot#744](https://github.com/riot/riot/issues/744)
- If the first character inside an expression is `^`, the expression is not passed to any parser. This is some sort of type=none at expression level - [riot#543](https://github.com/riot/riot/issues/543) and [riot#1090](https://github.com/riot/riot/issues/1090)
- Type es6 now supports babel-core - [riot#1039](https://github.com/riot/riot/issues/1039)
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ build: eslint

bump:
# Bump a new release
@ sed -i '' 's/WIP/v$(VERSION)/' $(DIST)*compiler.js
@ sed -i 's/WIP/v$(VERSION)/' $(DIST)*compiler.js

eslint:
# check code style
Expand Down
34 changes: 22 additions & 12 deletions dist/compiler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* riot-compiler v2.3.11, @license MIT, (c) 2015 Muut Inc. + contributors */
/* riot-compiler WIP, @license MIT, (c) 2015 Muut Inc. + contributors */
;(function (root, factory) {

/* istanbul ignore else */
Expand Down Expand Up @@ -180,8 +180,9 @@
var path = require('path')

function q(s) {

return "'" + (s ? s.replace(/\\/g, '\\\\').replace(/'/g, "\\'") : '') + "'"
return "'" + (s ? s
.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\n/g, '\\n').replace(/\r/g, '\\r') :
'') + "'"
}

function mktag(name, html, css, attrs, js, pcex) {
Expand All @@ -191,12 +192,13 @@

if (js && js.slice(-1) !== '\n') s = '\n' + s

return 'riot.tag2(' + q(name) + c + q(html) + c + q(css) + c + q(attrs) +
return 'riot.tag2(\'' + name + "'" + c + q(html) + c + q(css) + c + q(attrs) +
', function(opts) {\n' + js + s
}

function extend(obj, props) {
for (var prop in props) {
/* istanbul ignore next */
if (props.hasOwnProperty(prop)) {
obj[prop] = props[prop]
}
Expand Down Expand Up @@ -285,7 +287,7 @@

if (!intc) {
_bp = brackets.array(opts.brackets)
html = html.replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
html = html.replace(/\r\n?/g, '\n').replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
}
if (!pcex) pcex = []

Expand All @@ -301,8 +303,15 @@
return '<' + name + ends + '>'
})

html = opts.whitespace ?
html.replace(/\r\n?|\n/g, '\\n') : html.trim().replace(/\s+/g, ' ')
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] })
}

if (opts.compact) html = html.replace(/> <([-\w\/])/g, '><$1')

Expand Down Expand Up @@ -368,7 +377,7 @@
return parser(js, parserOpts).replace(TRIM_TRAIL, '')
}

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

function scopedCSS(tag, style) {
var scope = ':scope'
Expand All @@ -394,6 +403,7 @@
}

function compileCSS(style, tag, type, scoped, opts) {
if (!type) type = opts.style

if (type) {
if (type === 'scoped-css') {
Expand Down Expand Up @@ -509,7 +519,7 @@
With a greedy * operator, we have ~500 and 200bt, it is acceptable. So let's fix this.
*/
var
CUST_TAG = /^<([-\w]+)(?:\s+([^'"\/>]+(?:(?:"[^"]*"|'[^']*'|\/[^>])[^'"\/>]*)*)|\s*)?(?:\/>|>[ \t]*\n?([\s\S]*)^<\/\1\s*>|>(.*)<\/\1\s*>)/gim,
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')

Expand All @@ -530,7 +540,7 @@

src = label + src
.replace(/\r\n?/g, '\n')
.replace(CUST_TAG, function (_, tagName, attribs, body, body2) {
.replace(CUST_TAG, function (_, indent, tagName, attribs, body, body2) {

var
jscode = '',
Expand All @@ -540,8 +550,7 @@

tagName = tagName.toLowerCase()

attribs = !attribs ? '' :
restoreExpr(parseAttrs(splitHtml(attribs, opts, pcex)), pcex)
attribs = !attribs ? '' : restoreExpr(parseAttrs(splitHtml(attribs, opts, pcex)), pcex)

if (body2) body = body2

Expand All @@ -550,6 +559,7 @@
if (body2)
html = compileHTML(body2, opts, pcex, 1)
else {
body = body.replace(_regEx('^' + indent, 'gm'), '')

body = body.replace(STYLE, function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs)
Expand Down
34 changes: 22 additions & 12 deletions dist/riot.compiler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/**
* Compiler for riot custom tags
* @version v2.3.11
* @version WIP
*/

/**
Expand Down Expand Up @@ -111,8 +111,9 @@ var compile = (function () {
_bp = null

function q(s) {

return "'" + (s ? s.replace(/\\/g, '\\\\').replace(/'/g, "\\'") : '') + "'"
return "'" + (s ? s
.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\n/g, '\\n').replace(/\r/g, '\\r') :
'') + "'"
}

function mktag(name, html, css, attrs, js, pcex) {
Expand All @@ -122,12 +123,13 @@ var compile = (function () {

if (js && js.slice(-1) !== '\n') s = '\n' + s

return 'riot.tag2(' + q(name) + c + q(html) + c + q(css) + c + q(attrs) +
return 'riot.tag2(\'' + name + "'" + c + q(html) + c + q(css) + c + q(attrs) +
', function(opts) {\n' + js + s
}

function extend(obj, props) {
for (var prop in props) {
/* istanbul ignore next */
if (props.hasOwnProperty(prop)) {
obj[prop] = props[prop]
}
Expand Down Expand Up @@ -216,7 +218,7 @@ var compile = (function () {

if (!intc) {
_bp = brackets.array(opts.brackets)
html = html.replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
html = html.replace(/\r\n?/g, '\n').replace(HTML_COMMENT, '').replace(TRIM_TRAIL, '')
}
if (!pcex) pcex = []

Expand All @@ -232,8 +234,15 @@ var compile = (function () {
return '<' + name + ends + '>'
})

html = opts.whitespace ?
html.replace(/\r\n?|\n/g, '\\n') : html.trim().replace(/\s+/g, ' ')
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] })
}

if (opts.compact) html = html.replace(/> <([-\w\/])/g, '><$1')

Expand Down Expand Up @@ -299,7 +308,7 @@ var compile = (function () {
return parser(js, parserOpts).replace(TRIM_TRAIL, '')
}

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

function scopedCSS(tag, style) {
var scope = ':scope'
Expand All @@ -325,6 +334,7 @@ var compile = (function () {
}

function compileCSS(style, tag, type, scoped, opts) {
if (!type) type = opts.style

if (type) {
if (type === 'scoped-css') {
Expand Down Expand Up @@ -440,7 +450,7 @@ var compile = (function () {
With a greedy * operator, we have ~500 and 200bt, it is acceptable. So let's fix this.
*/
var
CUST_TAG = /^<([-\w]+)(?:\s+([^'"\/>]+(?:(?:"[^"]*"|'[^']*'|\/[^>])[^'"\/>]*)*)|\s*)?(?:\/>|>[ \t]*\n?([\s\S]*)^<\/\1\s*>|>(.*)<\/\1\s*>)/gim,
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')

Expand All @@ -458,7 +468,7 @@ var compile = (function () {

src = label + src
.replace(/\r\n?/g, '\n')
.replace(CUST_TAG, function (_, tagName, attribs, body, body2) {
.replace(CUST_TAG, function (_, indent, tagName, attribs, body, body2) {

var
jscode = '',
Expand All @@ -468,8 +478,7 @@ var compile = (function () {

tagName = tagName.toLowerCase()

attribs = !attribs ? '' :
restoreExpr(parseAttrs(splitHtml(attribs, opts, pcex)), pcex)
attribs = !attribs ? '' : restoreExpr(parseAttrs(splitHtml(attribs, opts, pcex)), pcex)

if (body2) body = body2

Expand All @@ -478,6 +487,7 @@ var compile = (function () {
if (body2)
html = compileHTML(body2, opts, pcex, 1)
else {
body = body.replace(_regEx('^' + indent, 'gm'), '')

body = body.replace(STYLE, function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs)
Expand Down

0 comments on commit ba356b2

Please sign in to comment.