Skip to content

Commit

Permalink
v2.3.0-beta.5
Browse files Browse the repository at this point in the history
Updated dependencies in package.json
Support for one-line tags
  • Loading branch information
aMarCruz committed Oct 28, 2015
1 parent ba9052c commit 1be9f97
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 73 deletions.
46 changes: 26 additions & 20 deletions dist/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,7 @@
}

var

CUST_TAG = /^<([-\w]+)(?:\s+([^'"\/>]+(?:(?:"[^"]*"|'[^']*'|\/[^>])[^'"\/>]*)*)|\s*)?(?:\/>|>[ \t]*\n?([\s\S]*)^<\/\1\s*>)/gim,
CUST_TAG = /^<([-\w]+)(?:\s+([^'"\/>]+(?:(?:"[^"]*"|'[^']*'|\/[^>])[^'"\/>]*)*)|\s*)?(?:\/>|>[ \t]*\n?([\s\S]*)^<\/\1\s*>|>(.*)<\/\1\s*>)/gim,
STYLE = /<style(\s+[^>]*)?>\n?([^<]*(?:<(?!\/style\s*>)[^<]*)*)<\/style\s*>/gi,
SCRIPT = _regEx(STYLE.source.replace(/tyle/g, 'cript'), 'gi')

Expand All @@ -436,7 +435,7 @@

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

var
jscode = '',
Expand All @@ -449,29 +448,36 @@
if (attribs)
attribs = restoreExpr(parseAttrs(splitHtml(attribs, opts, pcex)), pcex)

if (body2) body = body2

if (body && (body = body.replace(HTML_COMMENT, '')) && /\S/.test(body)) {

body = body.replace(STYLE, function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs)
styles += (styles ? ' ' : '') +
compileCSS(_style, tagName, getType(_attrs), scoped)
return ''
})
if (body2)
html = compileHTML(body2, opts, pcex, 1)
else {

body = body.replace(SCRIPT, function (_, _attrs, _script) {
jscode += (jscode ? '\n' : '') + getCode(_script, opts, _attrs)
return ''
})
body = body.replace(STYLE, function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs)
styles += (styles ? ' ' : '') +
compileCSS(_style, tagName, getType(_attrs), scoped)
return ''
})

var blocks = splitBlocks(body.replace(TRIM_TRAIL, ''))
body = body.replace(SCRIPT, function (_, _attrs, _script) {
jscode += (jscode ? '\n' : '') + getCode(_script, opts, _attrs)
return ''
})

body = blocks[0]
if (body)
html = compileHTML(body, opts, pcex, 1)
var blocks = splitBlocks(body.replace(TRIM_TRAIL, ''))

body = blocks[1]
if (/\S/.test(body))
jscode += (jscode ? '\n' : '') + compileJS(body, opts)
body = blocks[0]
if (body)
html = compileHTML(body, opts, pcex, 1)

body = blocks[1]
if (/\S/.test(body))
jscode += (jscode ? '\n' : '') + compileJS(body, opts)
}
}

return mktag(tagName, html, styles, attribs, jscode, pcex)
Expand Down
46 changes: 26 additions & 20 deletions dist/riot.compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,7 @@ var compile = (function () {
}

var

CUST_TAG = /^<([-\w]+)(?:\s+([^'"\/>]+(?:(?:"[^"]*"|'[^']*'|\/[^>])[^'"\/>]*)*)|\s*)?(?:\/>|>[ \t]*\n?([\s\S]*)^<\/\1\s*>)/gim,
CUST_TAG = /^<([-\w]+)(?:\s+([^'"\/>]+(?:(?:"[^"]*"|'[^']*'|\/[^>])[^'"\/>]*)*)|\s*)?(?:\/>|>[ \t]*\n?([\s\S]*)^<\/\1\s*>|>(.*)<\/\1\s*>)/gim,
STYLE = /<style(\s+[^>]*)?>\n?([^<]*(?:<(?!\/style\s*>)[^<]*)*)<\/style\s*>/gi,
SCRIPT = _regEx(STYLE.source.replace(/tyle/g, 'cript'), 'gi')

Expand All @@ -407,7 +406,7 @@ var compile = (function () {

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

var
jscode = '',
Expand All @@ -420,29 +419,36 @@ var compile = (function () {
if (attribs)
attribs = restoreExpr(parseAttrs(splitHtml(attribs, opts, pcex)), pcex)

if (body2) body = body2

if (body && (body = body.replace(HTML_COMMENT, '')) && /\S/.test(body)) {

body = body.replace(STYLE, function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs)
styles += (styles ? ' ' : '') +
compileCSS(_style, tagName, getType(_attrs), scoped)
return ''
})
if (body2)
html = compileHTML(body2, opts, pcex, 1)
else {

body = body.replace(SCRIPT, function (_, _attrs, _script) {
jscode += (jscode ? '\n' : '') + getCode(_script, opts, _attrs)
return ''
})
body = body.replace(STYLE, function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs)
styles += (styles ? ' ' : '') +
compileCSS(_style, tagName, getType(_attrs), scoped)
return ''
})

var blocks = splitBlocks(body.replace(TRIM_TRAIL, ''))
body = body.replace(SCRIPT, function (_, _attrs, _script) {
jscode += (jscode ? '\n' : '') + getCode(_script, opts, _attrs)
return ''
})

body = blocks[0]
if (body)
html = compileHTML(body, opts, pcex, 1)
var blocks = splitBlocks(body.replace(TRIM_TRAIL, ''))

body = blocks[1]
if (/\S/.test(body))
jscode += (jscode ? '\n' : '') + compileJS(body, opts)
body = blocks[0]
if (body)
html = compileHTML(body, opts, pcex, 1)

body = blocks[1]
if (/\S/.test(body))
jscode += (jscode ? '\n' : '') + compileJS(body, opts)
}
}

return mktag(tagName, html, styles, attribs, jscode, pcex)
Expand Down
64 changes: 35 additions & 29 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,16 +471,16 @@ var compile = (function () {
return parser(html)
}

// CUST_TAG regex don't allow unquoted expressions containing the `>` operator.
// STYLE and SCRIPT disallows the operator `>` at all.
/*
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*([^'"\/>]*(?:(?:\/[^>]|"[^"]*"|'[^']*')[^'"\/>]*)*)(?:\/|>\n?([^<]*(?:<(?!\/\1\s*>[ \t]*$)[^<]*)*)<\/\1\s*)>[ \t]*$/gim,
CUST_TAG = /^<([-\w]+)(?:\s+([^'"\/>]+(?:(?:"[^"]*"|'[^']*'|\/[^>])[^'"\/>]*)*)|\s*)?(?:\/>|>[ \t]*\n?([\s\S]*)^<\/\1\s*>)/gim,
CUST_TAG = /^<([-\w]+)(?:\s+([^'"\/>]+(?:(?:"[^"]*"|'[^']*'|\/[^>])[^'"\/>]*)*)|\s*)?(?:\/>|>[ \t]*\n?([\s\S]*)^<\/\1\s*>|>(.*)<\/\1\s*>)/gim,
STYLE = /<style(\s+[^>]*)?>\n?([^<]*(?:<(?!\/style\s*>)[^<]*)*)<\/style\s*>/gi,
SCRIPT = _regEx(STYLE.source.replace(/tyle/g, 'cript'), 'gi')

Expand Down Expand Up @@ -518,7 +518,7 @@ var compile = (function () {
// normalize eols and start processing the tags
return label + src
.replace(/\r\n?/g, '\n')
.replace(CUST_TAG, function (_, tagName, attribs, body) {
.replace(CUST_TAG, function (_, 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.
Expand All @@ -534,33 +534,39 @@ var compile = (function () {
if (attribs)
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)) {

// get and process the style blocks
body = body.replace(STYLE, function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs)
styles += (styles ? ' ' : '') +
compileCSS(_style, tagName, getType(_attrs), scoped)
return ''
})

// now the script blocks
body = body.replace(SCRIPT, function (_, _attrs, _script) {
jscode += (jscode ? '\n' : '') + getCode(_script, opts, _attrs)
return ''
})

// separate the untagged javascript block from the html markup
var blocks = splitBlocks(body.replace(TRIM_TRAIL, ''))

body = blocks[0]
if (body)
html = compileHTML(body, opts, pcex, 1)

body = blocks[1]
if (/\S/.test(body))
jscode += (jscode ? '\n' : '') + compileJS(body, opts)
if (body2)
html = compileHTML(body2, opts, pcex, 1)
else {
// get and process the style blocks
body = body.replace(STYLE, function (_, _attrs, _style) {
var scoped = _attrs && /\sscoped(\s|=|$)/i.test(_attrs)
styles += (styles ? ' ' : '') +
compileCSS(_style, tagName, getType(_attrs), scoped)
return ''
})

// now the script blocks
body = body.replace(SCRIPT, function (_, _attrs, _script) {
jscode += (jscode ? '\n' : '') + getCode(_script, opts, _attrs)
return ''
})

// separate the untagged javascript block from the html markup
var blocks = splitBlocks(body.replace(TRIM_TRAIL, ''))

body = blocks[0]
if (body)
html = compileHTML(body, opts, pcex, 1)

body = blocks[1]
if (/\S/.test(body))
jscode += (jscode ? '\n' : '') + compileJS(body, opts)
}
}

// replace the tag with a call to the riot.tag2 function and we are done
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"devDependencies": {
"coveralls": "^2.11.4",
"eslint": "^1.7.2",
"eslint": "^1.7.3",
"expect.js": "^0.3.1",
"istanbul": "^0.4.0",
"jspreproc": "^0.2.3",
Expand Down
2 changes: 1 addition & 1 deletion test/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var

var
basedir = path.join(__dirname, 'specs', 'fixtures'),
tags = ['box', 'input-last', 'mixed-js', 'same', 'scoped', 'timetable', 'treeview'],
tags = ['box', 'empty', 'input-last', 'mixed-js', 'same', 'scoped', 'timetable', 'treeview', 'oneline'],
data = { num: 1, str: 'string', date: new Date(), bool: true, item: null }

var files = tags.map(function (f) {
Expand Down
5 changes: 4 additions & 1 deletion test/specs/expect/empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ riot.tag2('empty1', '', '', '', function(opts) {
});

riot.tag2('empty2', '', '', '', function(opts) {
});
});

riot.tag2('empty3', '', '', '', function(opts) {
});
3 changes: 3 additions & 0 deletions test/specs/expect/oneline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//src: oneline.tag
riot.tag2('oneline', '<p>one line</p>', '', '', function(opts) {
});
4 changes: 3 additions & 1 deletion test/specs/fixtures/empty.tag
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<empty1>
</empty1>

<empty2/>
<empty2/>

<empty3></empty3>
1 change: 1 addition & 0 deletions test/specs/fixtures/oneline.tag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<oneline><p>one line</p></oneline>
4 changes: 4 additions & 0 deletions test/specs/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ describe('Compile tags', function() {
testFile('unclosed-es6')
})

it('Compatibility with one line tags', function () {
testFile('oneline')
})

it('With attributes in the root', function () {
var
src = cat('fixtures', 'root-attribs.tag'),
Expand Down

0 comments on commit 1be9f97

Please sign in to comment.