Comparing version 2.0.7 to 2.0.8
;(function(is_node) { | ||
(function(is_node) { | ||
var BOOL_ATTR = ('allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,default,'+ | ||
@@ -26,6 +25,6 @@ 'defaultchecked,defaultmuted,defaultselected,defer,disabled,draggable,enabled,formnovalidate,hidden,'+ | ||
// (tagname) (html) (javascript) endtag | ||
var CUSTOM_TAG = /^<([\w\-]+)>([^\x00]*[\w\/]>$)?([^\x00]*?)^<\/\1>/gim, | ||
var CUSTOM_TAG = /^<([\w\-]+)>([^\x00]*[\w\-\/]>$)?([^\x00]*?)^<\/\1>/gim, | ||
SCRIPT = /<script(\s+type=['"]?([^>'"]+)['"]?)?>([^\x00]*?)<\/script>/gm, | ||
HTML_COMMENT = /<!--.*?-->/g, | ||
CLOSED_TAG = /<([\w\-]+)([^\/]*)\/\s*>/g, | ||
CLOSED_TAG = /<([\w\-]+)([^>]*)\/\s*>/g, | ||
LINE_COMMENT = /^\s*\/\/.*$/gm, | ||
@@ -44,3 +43,3 @@ JS_COMMENT = /\/\*[^\x00]*?\*\//gm | ||
// foo={ bar } --> foo="{ bar }" | ||
html = html.replace(/=(\{[^\}]+\})([\s\>])/g, '="$1"$2') | ||
html = html.replace(/=(\{[^\}]+\})([\s\/\>])/g, '="$1"$2') | ||
@@ -128,3 +127,3 @@ // IE8 looses boolean attr values: `checked={ expr }` --> `__checked={ expr }` | ||
if (line.slice(0, es6_ident.length + 1) == es6_ident + '}') { | ||
lines[i] += '.bind(this);' | ||
lines[i] = es6_ident + es6_ident + 'this.update()\n' + es6_ident + '}.bind(this);' | ||
es6_ident = '' | ||
@@ -228,3 +227,2 @@ } | ||
function compileTag(source) { | ||
script.parentNode.removeChild(script) | ||
globalEval(source) | ||
@@ -244,9 +242,22 @@ | ||
function browserCompile(arg, skip_eval) { | ||
// string -> compile a new tag | ||
riot.compile = function(arg, fn) { | ||
// string | ||
if (typeof arg == 'string') { | ||
var js = unindent(compile(arg)) | ||
if (!skip_eval) globalEval(js) | ||
return js | ||
// compile & return | ||
if (arg.trim()[0] == '<') { | ||
var js = unindent(compile(arg)) | ||
if (!fn) globalEval(js) | ||
return js | ||
// URL | ||
} else { | ||
return GET(arg, function(str) { | ||
var js = unindent(compile(str)) | ||
globalEval(js) | ||
fn && fn(js, str) | ||
}) | ||
} | ||
} | ||
@@ -277,13 +288,13 @@ | ||
riot.mount = function(a, b) { | ||
browserCompile(function() { mount(a, b) }) | ||
var ret | ||
riot.compile(function() { ret = mount(a, b) }) | ||
return ret | ||
} | ||
riot.mountTo = function(a, b, c) { | ||
browserCompile(function() { mountTo(a, b, c) }) | ||
var ret | ||
riot.compile(function() { ret = mountTo(a, b, c) }) | ||
return ret | ||
} | ||
riot._compile = function(str) { | ||
return browserCompile(str, true) | ||
} | ||
})(!this.top) |
@@ -1,1 +0,1 @@ | ||
(function(e){var t=("allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,default,"+"defaultchecked,defaultmuted,defaultselected,defer,disabled,draggable,enabled,formnovalidate,hidden,"+"indeterminate,inert,ismap,itemscope,loop,multiple,muted,nohref,noresize,noshade,novalidate,nowrap,open,"+"pauseonexit,readonly,required,reversed,scoped,seamless,selected,sortable,spellcheck,translate,truespeed,"+"typemustmatch,visible").split(",");var r="area,base,br,col,command,embed,hr,img,input,keygen,link,meta,param,source,track,wbr".split(",");var n={jade:v};var i={coffeescript:p,none:g,cs:p,es6:d,typescript:m};var o=/^<([\w\-]+)>([^\x00]*[\w\/]>$)?([^\x00]*?)^<\/\1>/gim,a=/<script(\s+type=['"]?([^>'"]+)['"]?)?>([^\x00]*?)<\/script>/gm,c=/<!--.*?-->/g,u=/<([\w\-]+)([^\/]*)\/\s*>/g,f=/^\s*\/\/.*$/gm,s=/\/\*[^\x00]*?\*\//gm;function l(e,n,i){e=e.replace(/\s+/g," ");e=e.trim().replace(c,"");e=e.replace(/=(\{[^\}]+\})([\s\>])/g,'="$1"$2');e=e.replace(/([\w\-]+)=["'](\{[^\}]+\})["']/g,function(e,r,n){if(t.indexOf(r.toLowerCase())>=0)r="__"+r;return r+'="'+n+'"'});if(n.expr){e=e.replace(/\{\s*([^\}]+)\s*\}/g,function(e,t){return"{"+w(t,n,i).trim()+"}"})}e=e.replace(u,function(e,t,n){var i="<"+t+(n?" "+n.trim():"")+">";if(r.indexOf(t.toLowerCase())==-1)i+="</"+t+">";return i});e=e.replace(/'/g,"\\'");e=e.replace(/\\[{}]/g,"\\$&");if(n.compact)e=e.replace(/> </g,"><");return e}function p(e){return require("coffee-script").compile(e,{bare:true})}function d(e){return require("6to5").transform(e).code}function m(e){return require("typescript-simple")(e)}function g(e){return e}function v(e){return require("jade").render(e,{pretty:true})}function h(e){e=e.replace(f,"").replace(s,"");var t=e.split("\n"),r="";t.forEach(function(e,n){var i=e.trim();if(i[0]!="}"&&i.indexOf("(")>0&&i.slice(-1)=="{"&&i.indexOf("function")==-1){var o=/(\s+)([\w]+)\s*\(([\w,\s]*)\)\s*\{/.exec(e);if(o&&!/^(if|while|switch|for)$/.test(o[2])){t[n]=o[1]+"this."+o[2]+" = function("+o[3]+") {";r=o[1]}}if(e.slice(0,r.length+1)==r+"}"){t[n]+=".bind(this);";r=""}});return t.join("\n")}function w(e,t,r){var n=t.parser||(r?i[r]:h);if(!n)throw new Error('Parser not found "'+r+'"');return n(e,t)}function y(e,t){var r=n[e];if(!r)throw new Error('Template parser not found "'+e+'"');return r(t)}function x(e,t){t=t||{};if(t.template)e=y(t.template,e);return e.replace(o,function(e,r,n,i){n=n||"";var o=t.type;if(!i.trim()){n=n.replace(a,function(e,t,r,n){if(r)o=r.replace("text/","");i=n;return""})}return"riot.tag('"+r+"', '"+l(n,t,o)+"', function(opts) {"+w(i,t,o)+"\n});"})}if(e){return module.exports={html:l,compile:x}}var b=document,q,E;function k(e,t){var r=new XMLHttpRequest;r.onreadystatechange=function(){if(r.readyState==4&&r.status==200)t(r.responseText)};r.open("GET",e,true);r.send("")}function T(e){var t=/[ \t]+/.exec(e);if(t)e=e.replace(new RegExp("^"+t[0],"gm"),"");return e}function $(e){var t=b.createElement("script"),r=b.documentElement;t.text=x(e);r.appendChild(t);r.removeChild(t)}function C(e){var t=b.querySelectorAll('script[type="riot/tag"]');[].map.call(t,function(r,n){var i=r.getAttribute("src");function o(i){r.parentNode.removeChild(r);$(i);if(n+1==t.length){q.trigger("ready");E=true;e&&e()}}return i?k(i,o):o(T(r.innerHTML))})}function L(e,t){if(typeof e=="string"){var r=T(x(e));if(!t)$(r);return r}if(typeof e!="function")e=undefined;if(E)return e&&e();if(q){e&&q.on("ready",e)}else{q=riot.observable();C(e)}}var O=riot.mount,j=riot.mountTo;riot.mount=function(e,t){L(function(){O(e,t)})};riot.mountTo=function(e,t,r){L(function(){j(e,t,r)})};riot._compile=function(e){return L(e,true)}})(!this.top); | ||
(function(e){var r=("allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,default,"+"defaultchecked,defaultmuted,defaultselected,defer,disabled,draggable,enabled,formnovalidate,hidden,"+"indeterminate,inert,ismap,itemscope,loop,multiple,muted,nohref,noresize,noshade,novalidate,nowrap,open,"+"pauseonexit,readonly,required,reversed,scoped,seamless,selected,sortable,spellcheck,translate,truespeed,"+"typemustmatch,visible").split(",");var t="area,base,br,col,command,embed,hr,img,input,keygen,link,meta,param,source,track,wbr".split(",");var n={jade:g};var i={coffeescript:p,none:v,cs:p,es6:d,typescript:m};var o=/^<([\w\-]+)>([^\x00]*[\w\-\/]>$)?([^\x00]*?)^<\/\1>/gim,a=/<script(\s+type=['"]?([^>'"]+)['"]?)?>([^\x00]*?)<\/script>/gm,c=/<!--.*?-->/g,u=/<([\w\-]+)([^>]*)\/\s*>/g,f=/^\s*\/\/.*$/gm,s=/\/\*[^\x00]*?\*\//gm;function l(e,n,i){e=e.replace(/\s+/g," ");e=e.trim().replace(c,"");e=e.replace(/=(\{[^\}]+\})([\s\/\>])/g,'="$1"$2');e=e.replace(/([\w\-]+)=["'](\{[^\}]+\})["']/g,function(e,t,n){if(r.indexOf(t.toLowerCase())>=0)t="__"+t;return t+'="'+n+'"'});if(n.expr){e=e.replace(/\{\s*([^\}]+)\s*\}/g,function(e,r){return"{"+w(r,n,i).trim()+"}"})}e=e.replace(u,function(e,r,n){var i="<"+r+(n?" "+n.trim():"")+">";if(t.indexOf(r.toLowerCase())==-1)i+="</"+r+">";return i});e=e.replace(/'/g,"\\'");e=e.replace(/\\[{}]/g,"\\$&");if(n.compact)e=e.replace(/> </g,"><");return e}function p(e){return require("coffee-script").compile(e,{bare:true})}function d(e){return require("6to5").transform(e).code}function m(e){return require("typescript-simple")(e)}function v(e){return e}function g(e){return require("jade").render(e,{pretty:true})}function h(e){e=e.replace(f,"").replace(s,"");var r=e.split("\n"),t="";r.forEach(function(e,n){var i=e.trim();if(i[0]!="}"&&i.indexOf("(")>0&&i.slice(-1)=="{"&&i.indexOf("function")==-1){var o=/(\s+)([\w]+)\s*\(([\w,\s]*)\)\s*\{/.exec(e);if(o&&!/^(if|while|switch|for)$/.test(o[2])){r[n]=o[1]+"this."+o[2]+" = function("+o[3]+") {";t=o[1]}}if(e.slice(0,t.length+1)==t+"}"){r[n]=t+t+"this.update()\n"+t+"}.bind(this);";t=""}});return r.join("\n")}function w(e,r,t){var n=r.parser||(t?i[t]:h);if(!n)throw new Error('Parser not found "'+t+'"');return n(e,r)}function y(e,r){var t=n[e];if(!t)throw new Error('Template parser not found "'+e+'"');return t(r)}function x(e,r){r=r||{};if(r.template)e=y(r.template,e);return e.replace(o,function(e,t,n,i){n=n||"";var o=r.type;if(!i.trim()){n=n.replace(a,function(e,r,t,n){if(t)o=t.replace("text/","");i=n;return""})}return"riot.tag('"+t+"', '"+l(n,r,o)+"', function(opts) {"+w(i,r,o)+"\n});"})}if(e){return module.exports={html:l,compile:x}}var b=document,q,E;function k(e,r){var t=new XMLHttpRequest;t.onreadystatechange=function(){if(t.readyState==4&&t.status==200)r(t.responseText)};t.open("GET",e,true);t.send("")}function T(e){var r=/[ \t]+/.exec(e);if(r)e=e.replace(new RegExp("^"+r[0],"gm"),"");return e}function $(e){var r=b.createElement("script"),t=b.documentElement;r.text=x(e);t.appendChild(r);t.removeChild(r)}function C(e){var r=b.querySelectorAll('script[type="riot/tag"]');[].map.call(r,function(t,n){var i=t.getAttribute("src");function o(t){$(t);if(n+1==r.length){q.trigger("ready");E=true;e&&e()}}return i?k(i,o):o(T(t.innerHTML))})}riot.compile=function(e,r){if(typeof e=="string"){if(e.trim()[0]=="<"){var t=T(x(e));if(!r)$(t);return t}else{return k(e,function(e){var t=T(x(e));$(t);r&&r(t,e)})}}if(typeof e!="function")e=undefined;if(E)return e&&e();if(q){e&&q.on("ready",e)}else{q=riot.observable();C(e)}};var L=riot.mount,O=riot.mountTo;riot.mount=function(e,r){var t;riot.compile(function(){t=L(e,r)});return t};riot.mountTo=function(e,r,t){var n;riot.compile(function(){n=O(e,r,t)});return n}})(!this.top); |
208
lib/cli.js
@@ -14,41 +14,2 @@ #!/usr/bin/env node | ||
function help() { | ||
log([ | ||
'', | ||
'Builds .tag files to .js', | ||
'', | ||
'Options:', | ||
'', | ||
' -h, --help You\'re reading it', | ||
' -w, --watch Watch for changes', | ||
' -c, --compact Minify </p> <p> to </p><p>', | ||
' -t, --type JavaScript pre-processor. Build-in support for: es6, coffeescript, typescript, none', | ||
' --template HTML pre-processor. Build-in suupport for: jade', | ||
' --expr Run expressions trough parser defined with --type', | ||
'', | ||
'Build a single .tag file:', | ||
'', | ||
' riot foo.tag To a same named file (foo.js)', | ||
' riot foo.tag bar.js To a different named file (bar.js)', | ||
' riot foo.tag bar To a different dir (bar/foo.js)', | ||
'', | ||
'Build all .tag files in a directory:', | ||
'', | ||
' riot foo/bar To a same directory (foo/**/*.js)', | ||
' riot foo/bar baz To a different directory (baz/**/*.js)', | ||
' riot foo/bar baz.js To a single concatenated file (baz.js)', | ||
'', | ||
'Examples for options:', | ||
'', | ||
' riot foo bar', | ||
' riot --w foo bar', | ||
' riot --watch foo bar', | ||
' riot --compact foo bar', | ||
' riot foo bar --compact', | ||
' riot test.tag --type coffeescript --expr', | ||
'' | ||
].join('\n')) | ||
} | ||
var ph = require('path'), | ||
@@ -60,38 +21,50 @@ sh = require('shelljs'), | ||
function init(opt) { | ||
var methods = { | ||
// Run only once | ||
help: function() { | ||
log([ | ||
'', | ||
'Builds .tag files to .js', | ||
'', | ||
'Options:', | ||
'', | ||
' -h, --help You\'re reading it', | ||
' -v, --version Print Riot\'s version', | ||
' -w, --watch Watch for changes', | ||
' -c, --compact Minify </p> <p> to </p><p>', | ||
' -t, --type JavaScript pre-processor. Built-in support for: es6, coffeescript, typescript, none', | ||
' --template HTML pre-processor. Built-in suupport for: jade', | ||
' --expr Run expressions trough parser defined with --type', | ||
' --ext Change tag file extension. Defaults to .tag', | ||
'', | ||
'Build a single .tag file:', | ||
'', | ||
' riot foo.tag To a same named file (foo.js)', | ||
' riot foo.tag bar.js To a different named file (bar.js)', | ||
' riot foo.tag bar To a different dir (bar/foo.js)', | ||
'', | ||
'Build all .tag files in a directory:', | ||
'', | ||
' riot foo/bar To a same directory (foo/**/*.js)', | ||
' riot foo/bar baz To a different directory (baz/**/*.js)', | ||
' riot foo/bar baz.js To a single concatenated file (baz.js)', | ||
'', | ||
'Examples for options:', | ||
'', | ||
' riot foo bar', | ||
' riot --w foo bar', | ||
' riot --watch foo bar', | ||
' riot --compact foo bar', | ||
' riot foo bar --compact', | ||
' riot test.tag --type coffeescript --expr', | ||
'' | ||
].join('\n')) | ||
}, | ||
if (init.called) return | ||
init.called = true | ||
// If no target dir, default to source dir | ||
version: function() { | ||
log(require('../package.json').version) | ||
}, | ||
if (!opt.to) opt.to = /\.tag$/.test(opt.from) ? ph.dirname(opt.from) : opt.from | ||
// Resolve to absolute paths | ||
opt.from = ph.resolve(opt.from) | ||
opt.to = ph.resolve(opt.to) | ||
// Throw if source path doesn't exist | ||
if (!sh.test('-e', opt.from)) err('Source path does not exist') | ||
// Determine the input/output types | ||
opt.flow = (/\.tag$/.test(opt.from) ? 'f' : 'd') + (/\.js$/.test(opt.to) ? 'f' : 'd') | ||
} | ||
function toRelative(path) { | ||
return path.replace(sh.pwd() + '/', '') | ||
} | ||
// Exported methods | ||
var self = { | ||
make: function(opt) { | ||
@@ -102,5 +75,6 @@ init(opt) | ||
function find(from) { return sh.find(from).filter(function(f) { return /\.tag$/.test(f) }) } | ||
var tmp = /\/[^.~][^~/]+$/ // skip temporary files (created by editors), e.g. /.name.tag, /~name.tag, /name~.tag | ||
function find(from) { return sh.find(from).filter(function(f) { return ext.test(f) && tmp.test(f) }) } | ||
function remap(from, to, base) { return from.map(function(from) { | ||
return ph.join(to, ph.relative(base, from).replace(/\.tag$/, '.js')) | ||
return ph.join(to, ph.relative(base, from).replace(ext, '.js')) | ||
}) } | ||
@@ -120,3 +94,3 @@ | ||
function parse(from) { return compiler.compile(sh.cat(from), opt.compile_opts) } | ||
function parse(from) { return compiler.compile(sh.cat(from).replace(/^\uFEFF/g/* strips BOM */, ''), opt.compiler) } | ||
function toFile(from, to) { from.map(parse).join('\n').to(to[0]) } | ||
@@ -138,30 +112,45 @@ function toDir(from, to) { from.map(function(from, i) { parse(from).to(to[i]) }) } | ||
self.make(opt) | ||
methods.make(opt) | ||
var glob = opt.flow[0] == 'f' ? opt.from : ph.join(opt.from, '**/*.tag') | ||
var glob = opt.flow[0] == 'f' ? opt.from : ph.join(opt.from, '**/*.'+opt.ext) | ||
chokidar.watch(glob, { ignoreInitial: true }) | ||
.on('ready', function() { log('Watching ' + toRelative(glob)) }) | ||
.on('all', function(e, path) { self.make(opt) }) | ||
.on('all', function(e, path) { methods.make(opt) }) | ||
} | ||
} | ||
// Logging | ||
var ext | ||
function log(msg) { | ||
if (log.print) console.log(' ' + msg) | ||
} | ||
function init(opt) { | ||
function err(msg) { | ||
msg += '\n' | ||
if (log.print) log(msg) || process.exit(1) | ||
else throw msg | ||
} | ||
// Run only once | ||
if (init.called) return | ||
init.called = true | ||
// When used from CLI | ||
if (!opt.ext) opt.ext = 'tag' | ||
ext = RegExp('\\.' + opt.ext + '$') | ||
// If no target dir, default to source dir | ||
if (!opt.to) opt.to = ext.test(opt.from) ? ph.dirname(opt.from) : opt.from | ||
// Resolve to absolute paths | ||
opt.from = ph.resolve(opt.from) | ||
opt.to = ph.resolve(opt.to) | ||
// Throw if source path doesn't exist | ||
if (!sh.test('-e', opt.from)) err('Source path does not exist') | ||
// Determine the input/output types | ||
opt.flow = (ext.test(opt.from) ? 'f' : 'd') + (/\.js$/.test(opt.to) ? 'f' : 'd') | ||
} | ||
function cli() { | ||
@@ -172,4 +161,4 @@ | ||
var args = require('minimist')(process.argv.slice(2), { | ||
boolean: ['watch', 'compact', 'help'], | ||
alias: { w: 'watch', c: 'compact', h: 'help', t: 'type' } | ||
boolean: ['watch', 'compact', 'help', 'version'], | ||
alias: { w: 'watch', c: 'compact', h: 'help', v: 'version', t: 'type' } | ||
}) | ||
@@ -180,10 +169,9 @@ | ||
var opts = { | ||
help: args.help, | ||
method: args.watch ? 'watch' : 'make', | ||
compile_opts: { | ||
compiler: { | ||
compact: args.compact, | ||
template: args.template, | ||
type: args.type, | ||
expr: args.expr, | ||
template: args.template | ||
expr: args.expr | ||
}, | ||
ext: args.ext, | ||
from: args._.shift(), | ||
@@ -193,18 +181,32 @@ to: args._.shift() | ||
// Enable text output | ||
// Call matching method | ||
log.print = true | ||
var method = Object.keys(methods).filter(function(v) { return args[v] })[0] | ||
|| ( opts.from ? 'make' : 'help' ) | ||
// Print help or run the builder | ||
methods[method](opts) | ||
opts.help || !opts.from | ||
? help() | ||
: self[opts.method](opts) | ||
} | ||
function toRelative(path) { | ||
return path.replace(sh.pwd() + '/', '') | ||
} | ||
function log(msg) { | ||
if (!log.silent) console.log(msg) | ||
} | ||
// Allow to require as a Node module too | ||
function err(msg) { | ||
msg += '\n' | ||
if (!log.silent) log(msg) || process.exit(1) | ||
else throw msg | ||
} | ||
module.exports = self | ||
if (!module.parent) cli() | ||
// Run from CLI or as Node module | ||
if (module.parent) { | ||
module.exports = methods | ||
log.silent = true | ||
} else cli() |
;(function(is_node) { | ||
(function(is_node) { | ||
var BOOL_ATTR = ('allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,default,'+ | ||
@@ -26,6 +25,6 @@ 'defaultchecked,defaultmuted,defaultselected,defer,disabled,draggable,enabled,formnovalidate,hidden,'+ | ||
// (tagname) (html) (javascript) endtag | ||
var CUSTOM_TAG = /^<([\w\-]+)>([^\x00]*[\w\/]>$)?([^\x00]*?)^<\/\1>/gim, | ||
var CUSTOM_TAG = /^<([\w\-]+)>([^\x00]*[\w\-\/]>$)?([^\x00]*?)^<\/\1>/gim, | ||
SCRIPT = /<script(\s+type=['"]?([^>'"]+)['"]?)?>([^\x00]*?)<\/script>/gm, | ||
HTML_COMMENT = /<!--.*?-->/g, | ||
CLOSED_TAG = /<([\w\-]+)([^\/]*)\/\s*>/g, | ||
CLOSED_TAG = /<([\w\-]+)([^>]*)\/\s*>/g, | ||
LINE_COMMENT = /^\s*\/\/.*$/gm, | ||
@@ -44,3 +43,3 @@ JS_COMMENT = /\/\*[^\x00]*?\*\//gm | ||
// foo={ bar } --> foo="{ bar }" | ||
html = html.replace(/=(\{[^\}]+\})([\s\>])/g, '="$1"$2') | ||
html = html.replace(/=(\{[^\}]+\})([\s\/\>])/g, '="$1"$2') | ||
@@ -128,3 +127,3 @@ // IE8 looses boolean attr values: `checked={ expr }` --> `__checked={ expr }` | ||
if (line.slice(0, es6_ident.length + 1) == es6_ident + '}') { | ||
lines[i] += '.bind(this);' | ||
lines[i] = es6_ident + es6_ident + 'this.update()\n' + es6_ident + '}.bind(this);' | ||
es6_ident = '' | ||
@@ -228,3 +227,2 @@ } | ||
function compileTag(source) { | ||
script.parentNode.removeChild(script) | ||
globalEval(source) | ||
@@ -244,9 +242,22 @@ | ||
function browserCompile(arg, skip_eval) { | ||
// string -> compile a new tag | ||
riot.compile = function(arg, fn) { | ||
// string | ||
if (typeof arg == 'string') { | ||
var js = unindent(compile(arg)) | ||
if (!skip_eval) globalEval(js) | ||
return js | ||
// compile & return | ||
if (arg.trim()[0] == '<') { | ||
var js = unindent(compile(arg)) | ||
if (!fn) globalEval(js) | ||
return js | ||
// URL | ||
} else { | ||
return GET(arg, function(str) { | ||
var js = unindent(compile(str)) | ||
globalEval(js) | ||
fn && fn(js, str) | ||
}) | ||
} | ||
} | ||
@@ -277,13 +288,13 @@ | ||
riot.mount = function(a, b) { | ||
browserCompile(function() { mount(a, b) }) | ||
var ret | ||
riot.compile(function() { ret = mount(a, b) }) | ||
return ret | ||
} | ||
riot.mountTo = function(a, b, c) { | ||
browserCompile(function() { mountTo(a, b, c) }) | ||
var ret | ||
riot.compile(function() { ret = mountTo(a, b, c) }) | ||
return ret | ||
} | ||
riot._compile = function(str) { | ||
return browserCompile(str, true) | ||
} | ||
})(!this.top) |
@@ -37,21 +37,39 @@ /* | ||
// Customizable brackets | ||
riot.settings.brackets = '[ ]' | ||
riot.settings.brackets = '<% %>' | ||
*/ | ||
riot._tmpl = (function() { | ||
var tmpl = (function() { | ||
var cache = {}, | ||
// find variable names | ||
re_vars = /("|').+?[^\\]\1|\.\w*|\w*:|\b(?:this|true|false|null|undefined|new|typeof|Number|String|Object|Array|Math|Date|JSON)\b|([a-z_]\w*)/gi | ||
// [ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] | ||
brackets, | ||
re_expr, | ||
re_vars = /("|').+?[^\\]\1|\.\w*|\w*:|\b(?:(?:new|typeof|in|instanceof) |(?:this|true|false|null|undefined)\b|function *\()|([a-z_]\w*)/gi | ||
// [ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] | ||
// find variable names: | ||
// 1. skip quoted strings: "a b", 'a b', 'a \'b\'' | ||
// 2. skip object properties: .name | ||
// 3. skip object literals: name: | ||
// 4. skip reserved words | ||
// 4. skip javascript keywords | ||
// 5. match var name | ||
// build a template (or get it from cache), render with data | ||
return function(str, data) { | ||
return function(str, data) { | ||
return str && (cache[str] = cache[str] || tmpl(str))(data) | ||
// make sure we use current brackets setting | ||
var b = riot.settings.brackets || '{ }' | ||
if(b != brackets){ | ||
brackets = b.split(' ') | ||
re_expr = re(/({[\s\S]*?})/) | ||
} | ||
// build a template (or get it from cache), render with data | ||
// (or just test if string has expression when called w/o data) | ||
return data | ||
? str && (cache[str] = cache[str] || tmpl(str))(data) | ||
: re_expr.test(str) | ||
} | ||
@@ -63,10 +81,12 @@ | ||
function tmpl(s, p) { | ||
p = (s || '{}') | ||
// default template string to {} | ||
p = (s || brackets.join('')) | ||
// temporarily convert \{ and \} to a non-character | ||
.replace(/\\{/g, '\uFFF0') | ||
.replace(/\\}/g, '\uFFF1') | ||
.replace(re(/\\{/), '\uFFF0') | ||
.replace(re(/\\}/), '\uFFF1') | ||
// split string to expression and non-expresion parts | ||
.split(/({[\s\S]*?})/) | ||
.split(re_expr) | ||
@@ -76,3 +96,3 @@ return new Function('d', 'return ' + ( | ||
// is it a single expression or a template? i.e. {x} or <b>{x}</b> | ||
!p[0] && !p[2] | ||
!p[0] && !p[2] && !p[3] | ||
@@ -106,4 +126,4 @@ // if expression, evaluate it | ||
// bring escaped { and } back | ||
.replace(/\uFFF0/g, '{') | ||
.replace(/\uFFF1/g, '}') | ||
.replace(/\uFFF0/g, brackets[0]) | ||
.replace(/\uFFF1/g, brackets[1]) | ||
@@ -124,3 +144,3 @@ ) | ||
// trim whitespace, curly brackets, strip comments | ||
.replace(/^[{ ]+|[ }]+$|\/\*.+?\*\//g, '') | ||
.replace(re(/^[{ ]+|[ }]+$|\/\*.+?\*\//g), '') | ||
@@ -151,3 +171,3 @@ // is it an object literal? i.e. { key : value } | ||
// prefix vars (name => data.name) | ||
+ (s.replace(re_vars, function(s, _, v) { return v ? 'd.' + v : s }) | ||
+ (s.replace(re_vars, function(s, _, v) { return v ? '(d.'+v+'===undefined?window.'+v+':d.'+v+')' : s }) | ||
@@ -165,2 +185,12 @@ // break the expression if its empty (resulting in undefined value) | ||
// change regexp to use custom brackets | ||
function re(r) { | ||
return RegExp(r.source | ||
.split('{').join('\\'+brackets[0]) | ||
.split('}').join('\\'+brackets[1]), | ||
r.global ? 'g' : '') | ||
} | ||
})() |
{ | ||
"name": "riot", | ||
"version": "2.0.7", | ||
"version": "2.0.8", | ||
"description": "A React- like, 2.5K user interface library", | ||
@@ -5,0 +5,0 @@ "homepage": "https://muut.com/riotjs/", |
@@ -6,3 +6,3 @@ | ||
### Custom tags • Minimal syntax • Less DOM reflows • Full stack • IE8 | ||
### Custom tags • Minimal syntax • Virtual DOM • Full stack • IE8 | ||
@@ -55,7 +55,7 @@ Riot brings custom tags to all browsers starting from IE8. Think React + Polymer, but squeezed into 5.7KB (2.5KB when gzipped). | ||
### Intelligent DOM manipulation | ||
### Virtual DOM | ||
- Absolutely the smallest possible amount of DOM updates and reflows. | ||
- One way data flow: updates and unmounts are propagated downwards from parent to children. | ||
- Expressions are pre-compiled and cached for high performance. | ||
- No extra HTML root elements or `data-` attributes. | ||
- Lifecycle events for more special needs. | ||
- Lifecycle events for more control. | ||
@@ -67,2 +67,3 @@ | ||
- The rendered DOM can be freely manipulated with other tools. | ||
- No extra HTML root elements or `data-` attributes. | ||
- Plays well with jQuery. | ||
@@ -98,2 +99,4 @@ | ||
### Resources | ||
- [Riot + Angular](https://github.com/lucasbrigida/angular-riot) | ||
- [Module loader for WebPack](https://www.npmjs.com/package/riotjs-loader) | ||
- [Riot module for AngularJS](https://github.com/lucasbrigida/angular-riot) | ||
@@ -104,4 +107,3 @@ - [Riot + Meteor]( https://atmospherejs.com/xaiki/riotjs) | ||
https://muut.com/riotjs/ | ||
719
riot.js
@@ -1,6 +0,6 @@ | ||
/* Riot 2.0.7, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
/* Riot v2.0.8, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
;(function() { | ||
var riot = { version: 'v2.0.7' } | ||
var riot = { version: 'v2.0.8', settings: {} } | ||
@@ -152,21 +152,39 @@ 'use strict' | ||
// Customizable brackets | ||
riot.settings.brackets = '[ ]' | ||
riot.settings.brackets = '<% %>' | ||
*/ | ||
riot._tmpl = (function() { | ||
var tmpl = (function() { | ||
var cache = {}, | ||
// find variable names | ||
re_vars = /("|').+?[^\\]\1|\.\w*|\w*:|\b(?:this|true|false|null|undefined|new|typeof|Number|String|Object|Array|Math|Date|JSON)\b|([a-z_]\w*)/gi | ||
// [ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] | ||
brackets, | ||
re_expr, | ||
re_vars = /("|').+?[^\\]\1|\.\w*|\w*:|\b(?:(?:new|typeof|in|instanceof) |(?:this|true|false|null|undefined)\b|function *\()|([a-z_]\w*)/gi | ||
// [ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] | ||
// find variable names: | ||
// 1. skip quoted strings: "a b", 'a b', 'a \'b\'' | ||
// 2. skip object properties: .name | ||
// 3. skip object literals: name: | ||
// 4. skip reserved words | ||
// 4. skip javascript keywords | ||
// 5. match var name | ||
// build a template (or get it from cache), render with data | ||
return function(str, data) { | ||
return function(str, data) { | ||
return str && (cache[str] = cache[str] || tmpl(str))(data) | ||
// make sure we use current brackets setting | ||
var b = riot.settings.brackets || '{ }' | ||
if(b != brackets){ | ||
brackets = b.split(' ') | ||
re_expr = re(/({[\s\S]*?})/) | ||
} | ||
// build a template (or get it from cache), render with data | ||
// (or just test if string has expression when called w/o data) | ||
return data | ||
? str && (cache[str] = cache[str] || tmpl(str))(data) | ||
: re_expr.test(str) | ||
} | ||
@@ -178,10 +196,12 @@ | ||
function tmpl(s, p) { | ||
p = (s || '{}') | ||
// default template string to {} | ||
p = (s || brackets.join('')) | ||
// temporarily convert \{ and \} to a non-character | ||
.replace(/\\{/g, '\uFFF0') | ||
.replace(/\\}/g, '\uFFF1') | ||
.replace(re(/\\{/), '\uFFF0') | ||
.replace(re(/\\}/), '\uFFF1') | ||
// split string to expression and non-expresion parts | ||
.split(/({[\s\S]*?})/) | ||
.split(re_expr) | ||
@@ -191,3 +211,3 @@ return new Function('d', 'return ' + ( | ||
// is it a single expression or a template? i.e. {x} or <b>{x}</b> | ||
!p[0] && !p[2] | ||
!p[0] && !p[2] && !p[3] | ||
@@ -221,4 +241,4 @@ // if expression, evaluate it | ||
// bring escaped { and } back | ||
.replace(/\uFFF0/g, '{') | ||
.replace(/\uFFF1/g, '}') | ||
.replace(/\uFFF0/g, brackets[0]) | ||
.replace(/\uFFF1/g, brackets[1]) | ||
@@ -239,3 +259,3 @@ ) | ||
// trim whitespace, curly brackets, strip comments | ||
.replace(/^[{ ]+|[ }]+$|\/\*.+?\*\//g, '') | ||
.replace(re(/^[{ ]+|[ }]+$|\/\*.+?\*\//g), '') | ||
@@ -266,3 +286,3 @@ // is it an object literal? i.e. { key : value } | ||
// prefix vars (name => data.name) | ||
+ (s.replace(re_vars, function(s, _, v) { return v ? 'd.' + v : s }) | ||
+ (s.replace(re_vars, function(s, _, v) { return v ? '(d.'+v+'===undefined?window.'+v+':d.'+v+')' : s }) | ||
@@ -280,422 +300,485 @@ // break the expression if its empty (resulting in undefined value) | ||
})() | ||
;(function(riot, is_browser) { | ||
if (!is_browser) return | ||
// change regexp to use custom brackets | ||
var tmpl = riot._tmpl, | ||
all_tags = [], | ||
tag_impl = {}, | ||
doc = document | ||
function each(nodes, fn) { | ||
for (var i = 0; i < (nodes || []).length; i++) { | ||
if (fn(nodes[i], i) === false) i-- | ||
} | ||
function re(r) { | ||
return RegExp(r.source | ||
.split('{').join('\\'+brackets[0]) | ||
.split('}').join('\\'+brackets[1]), | ||
r.global ? 'g' : '') | ||
} | ||
function extend(obj, from) { | ||
from && Object.keys(from).map(function(key) { | ||
obj[key] = from[key] | ||
}) | ||
return obj | ||
} | ||
})() | ||
// { key, i in items} -> { key, i, items } | ||
function loopKeys(expr) { | ||
var ret = { val: expr }, | ||
els = expr.split(/\s+in\s+/) | ||
function diff(arr1, arr2) { | ||
return arr1.filter(function(el) { | ||
return arr2.indexOf(el) < 0 | ||
}) | ||
if (els[1]) { | ||
ret.val = '{ ' + els[1] | ||
els = els[0].slice(1).trim().split(/,\s*/) | ||
ret.key = els[0] | ||
ret.pos = els[1] | ||
} | ||
return ret | ||
} | ||
function walk(dom, fn) { | ||
dom = fn(dom) === false ? dom.nextSibling : dom.firstChild | ||
function _each(dom, parent, expr) { | ||
while (dom) { | ||
walk(dom, fn) | ||
dom = dom.nextSibling | ||
} | ||
} | ||
remAttr(dom, 'each') | ||
var template = dom.outerHTML, | ||
prev = dom.previousSibling, | ||
root = dom.parentNode, | ||
rendered = [], | ||
tags = [], | ||
checksum | ||
function mkdom(tmpl) { | ||
var tag_name = tmpl.trim().slice(1, 3).toLowerCase(), | ||
root_tag = /td|th/.test(tag_name) ? 'tr' : tag_name == 'tr' ? 'tbody' : 'div' | ||
el = doc.createElement(root_tag) | ||
expr = loopKeys(expr) | ||
el.innerHTML = tmpl | ||
return el | ||
} | ||
// clean template code after update (and let walk finish it's parse) | ||
parent.one('update', function() { | ||
root.removeChild(dom) | ||
}).one('mount', function() { | ||
if (!hasParent(root)) root = parent.root | ||
function update(expressions, instance) { | ||
}).on('update', function() { | ||
// allow recalculation of context data | ||
instance.trigger('update') | ||
var items = tmpl(expr.val, parent) | ||
if (!items) return | ||
each(expressions, function(expr) { | ||
var tag = expr.tag, | ||
dom = expr.dom | ||
// object loop. any changes cause full redraw | ||
if (!Array.isArray(items)) { | ||
var testsum = JSON.stringify(items) | ||
if (testsum == checksum) return | ||
checksum = testsum | ||
function remAttr(name) { | ||
dom.removeAttribute(name) | ||
} | ||
// clear old items | ||
tags.map(function(tag) { | ||
tag.unmount() | ||
}) | ||
// loops first: TODO remove from expressions arr | ||
if (expr.loop) { | ||
remAttr('each') | ||
return loop(expr, instance) | ||
} | ||
tags = rendered = [] | ||
// custom tag | ||
if (tag) return tag.update ? tag.update() : | ||
expr.tag = createTag({ tmpl: tag[0], fn: tag[1], root: dom, parent: instance }) | ||
items = Object.keys(items).map(function(key, i) { | ||
var obj = {} | ||
obj[expr.key] = key | ||
obj[expr.pos] = items[key] | ||
return obj | ||
}) | ||
} | ||
var attr_name = expr.attr, | ||
value = tmpl(expr.expr, instance) | ||
// unmount redundant | ||
arrDiff(rendered, items).map(function(item) { | ||
var pos = rendered.indexOf(item), | ||
tag = tags[pos] | ||
if (value == null) value = '' | ||
if (tag) { | ||
tag.unmount() | ||
rendered.splice(pos, 1) | ||
tags.splice(pos, 1) | ||
} | ||
}) | ||
// no change | ||
if (expr.value === value) return | ||
expr.value = value | ||
// mount new | ||
var nodes = root.childNodes, | ||
prev_index = Array.prototype.indexOf.call(nodes, prev) | ||
arrDiff(items, rendered).map(function(item, i) { | ||
// text node | ||
if (!attr_name) return dom.nodeValue = value | ||
var pos = items.indexOf(item) | ||
// attribute | ||
if (!value && expr.bool || /obj|func/.test(typeof value)) remAttr(attr_name) | ||
if (!checksum && expr.key) { | ||
var obj = {} | ||
obj[expr.key] = item | ||
obj[expr.pos] = pos | ||
item = obj | ||
} | ||
// event handler | ||
if (typeof value == 'function') { | ||
dom[attr_name] = function(e) { | ||
var tag = new Tag({ tmpl: template }, { | ||
before: nodes[prev_index + 1 + pos], | ||
parent: parent, | ||
root: root, | ||
loop: true, | ||
item: item | ||
}) | ||
// cross browser event fix | ||
e = e || window.event | ||
e.which = e.which || e.charCode || e.keyCode | ||
e.target = e.target || e.srcElement | ||
e.currentTarget = dom | ||
tags.splice(pos, 0, tag) | ||
// currently looped item | ||
e.item = instance.__item || instance | ||
}) | ||
// prevent default behaviour (by default) | ||
if (value.call(instance, e) !== true) { | ||
e.preventDefault && e.preventDefault() | ||
e.returnValue = false | ||
} | ||
rendered = items.slice() | ||
instance.update() | ||
} | ||
}) | ||
// show / hide / if | ||
} else if (/^(show|hide|if)$/.test(attr_name)) { | ||
remAttr(attr_name) | ||
if (attr_name == 'hide') value = !value | ||
dom.style.display = value ? '' : 'none' | ||
} | ||
function parseNamedElements(root, tag, expressions) { | ||
walk(root, function(dom) { | ||
if (dom.nodeType != 1) return | ||
// normal attribute | ||
} else { | ||
if (expr.bool) { | ||
dom[attr_name] = value | ||
if (!value) return | ||
value = attr_name | ||
} | ||
dom.setAttribute(attr_name, value) | ||
} | ||
each(dom.attributes, function(attr) { | ||
if (/^(name|id)$/.test(attr.name)) tag[attr.value] = dom | ||
}) | ||
}) | ||
} | ||
instance.trigger('updated') | ||
function parseLayout(root, tag, expressions) { | ||
function addExpr(dom, value, data) { | ||
if (tmpl(value) || data) { | ||
var expr = { dom: dom, expr: value } | ||
expressions.push(extend(expr, data || {})) | ||
} | ||
} | ||
function parse(root) { | ||
walk(root, function(dom) { | ||
var named_elements = {}, | ||
expressions = [] | ||
var type = dom.nodeType | ||
walk(root, function(dom) { | ||
// text node | ||
if (type == 3 && dom.parentNode.tagName != 'STYLE') addExpr(dom, dom.nodeValue) | ||
if (type != 1) return | ||
var type = dom.nodeType, | ||
value = dom.nodeValue | ||
/* element */ | ||
// text node | ||
if (type == 3 && dom.parentNode.tagName != 'STYLE') { | ||
addExpr(dom, value) | ||
// loop | ||
var attr = dom.getAttribute('each') | ||
if (attr) { _each(dom, tag, attr); return false } | ||
// element | ||
} else if (type == 1) { | ||
// child tag | ||
var impl = tag_impl[dom.tagName.toLowerCase()] | ||
if (impl) { | ||
impl = new Tag(impl, { root: dom, parent: tag }) | ||
return false | ||
} | ||
// loop? | ||
value = dom.getAttribute('each') | ||
// attributes | ||
each(dom.attributes, function(attr) { | ||
var name = attr.name, | ||
value = attr.value | ||
if (value) { | ||
addExpr(dom, value, { loop: 1 }) | ||
return false | ||
} | ||
// expressions | ||
var bool = name.split('__')[1] | ||
addExpr(dom, value, { attr: bool || name, bool: bool }) | ||
// custom tag? | ||
var tag = tag_impl[dom.tagName.toLowerCase()] | ||
if (bool) { | ||
remAttr(dom, name) | ||
return false | ||
} | ||
// attributes | ||
each(dom.attributes, function(attr) { | ||
var name = attr.name, | ||
value = attr.value | ||
}) | ||
// named elements | ||
if (/^(name|id)$/.test(name)) named_elements[value] = dom | ||
}) | ||
// expressions | ||
if (!tag) { | ||
var bool = name.split('__')[1] | ||
addExpr(dom, value, { attr: bool || name, bool: bool }) | ||
if (bool) { | ||
dom.removeAttribute(name) | ||
return false | ||
} | ||
} | ||
} | ||
function Tag(impl, conf) { | ||
}) | ||
var self = riot.observable(this), | ||
expressions = [], | ||
attributes = {}, | ||
parent = conf.parent, | ||
is_loop = conf.loop, | ||
root = conf.root, | ||
opts = conf.opts, | ||
item = conf.item | ||
if (tag) addExpr(dom, 0, { tag: tag }) | ||
// cannot initialize twice on the same root element | ||
if (!is_loop && root.riot) return | ||
root.riot = 1 | ||
} | ||
opts = opts || {} | ||
}) | ||
extend(this, { parent: parent, root: root, opts: opts, children: [] }) | ||
extend(this, item) | ||
return { expr: expressions, elem: named_elements } | ||
function addExpr(dom, value, data) { | ||
if (value ? value.indexOf('{') >= 0 : data) { | ||
var expr = { dom: dom, expr: value } | ||
expressions.push(extend(expr, data || {})) | ||
} | ||
// attributes | ||
each(root.attributes, function(attr) { | ||
var name = attr.name, | ||
val = attr.value | ||
attributes[name] = val | ||
// remove dynamic attributes from node | ||
if (val.indexOf('{') >= 0) { | ||
remAttr(root, name) | ||
return false | ||
} | ||
}) | ||
// options | ||
function updateOpts() { | ||
Object.keys(attributes).map(function(name) { | ||
opts[name] = tmpl(attributes[name], parent || self) | ||
}) | ||
} | ||
updateOpts() | ||
// child | ||
parent && parent.children.push(this) | ||
// create new custom tag (component) | ||
function createTag(conf) { | ||
var dom = mkdom(impl.tmpl), | ||
loop_dom | ||
var opts = conf.opts || {}, | ||
dom = mkdom(conf.tmpl), | ||
mountNode = conf.root, | ||
parent = conf.parent, | ||
ast = parse(dom), | ||
tag = { root: mountNode, opts: opts, parent: parent, __item: conf.item }, | ||
attributes = {} | ||
// named elements | ||
parseNamedElements(dom, this) | ||
// named elements | ||
extend(tag, ast.elem) | ||
this.update = function(data, init) { | ||
extend(self, data) | ||
extend(self, item) | ||
self.trigger('update') | ||
updateOpts() | ||
update(expressions, self, item) | ||
self.trigger('updated') | ||
} | ||
// attributes | ||
each(mountNode.attributes, function(attr) { | ||
attributes[attr.name] = attr.value | ||
}) | ||
this.unmount = function() { | ||
function updateOpts() { | ||
Object.keys(attributes).map(function(name) { | ||
var val = opts[name] = tmpl(attributes[name], parent || tag) | ||
if (typeof val == 'object') mountNode.removeAttribute(name) | ||
}) | ||
if (is_loop) { | ||
root.removeChild(loop_dom) | ||
} else { | ||
var p = root.parentNode | ||
p && p.removeChild(root) | ||
} | ||
updateOpts() | ||
if (!tag.on) { | ||
riot.observable(tag) | ||
delete tag.off // off method not needed | ||
// splice from parent.children[] | ||
if (parent) { | ||
var els = parent.children | ||
els.splice(els.indexOf(self), 1) | ||
} | ||
if (conf.fn) conf.fn.call(tag, opts) | ||
self.trigger('unmount') | ||
// cleanup | ||
parent && parent.off('update', self.update) | ||
mounted = false | ||
} | ||
tag.update = function(data, _system) { | ||
function mount() { | ||
while (dom.firstChild) { | ||
if (is_loop) { | ||
loop_dom = dom.firstChild | ||
root.insertBefore(dom.firstChild, conf.before || null) // null needed for IE8 | ||
/* | ||
If loop is defined on the root of the HTML template | ||
the original parent is a temporary <div/> by mkdom() | ||
*/ | ||
if (parent && dom && !dom.firstChild) { | ||
mountNode = parent.root | ||
dom = null | ||
} else { | ||
root.appendChild(dom.firstChild) | ||
} | ||
} | ||
if (_system || doc.body.contains(mountNode)) { | ||
extend(tag, data) | ||
extend(tag, tag.__item) | ||
updateOpts() | ||
update(ast.expr, tag) | ||
if (!hasParent(root)) self.root = root = parent.root | ||
// update parent | ||
!_system && tag.__item && parent.update() | ||
return true | ||
self.trigger('mount') | ||
} else { | ||
tag.trigger('unmount') | ||
} | ||
// one way data flow: propagate updates and unmounts downwards from parent to children | ||
parent && parent.on('update', self.update).one('unmount', self.unmount) | ||
} | ||
} | ||
tag.update(0, true) | ||
// initialize | ||
if (impl.fn) impl.fn.call(this, opts) | ||
// append to root | ||
while (dom.firstChild) { | ||
if (conf.before) mountNode.insertBefore(dom.firstChild, conf.before) | ||
else mountNode.appendChild(dom.firstChild) | ||
} | ||
// layout | ||
parseLayout(dom, this, expressions) | ||
this.update() | ||
mount() | ||
tag.trigger('mount') | ||
} | ||
all_tags.push(tag) | ||
return tag | ||
function setEventHandler(name, handler, dom, tag, item) { | ||
dom[name] = function(e) { | ||
// cross browser event fix | ||
e = e || window.event | ||
e.which = e.which || e.charCode || e.keyCode | ||
e.target = e.target || e.srcElement | ||
e.currentTarget = dom | ||
e.item = item | ||
// prevent default behaviour (by default) | ||
if (handler.call(tag, e) !== true) { | ||
e.preventDefault && e.preventDefault() | ||
e.returnValue = false | ||
} | ||
tag.update() | ||
} | ||
} | ||
function loop(expr, instance) { | ||
function insertTo(root, node, before) { | ||
if (root) { | ||
root.insertBefore(before, node) | ||
root.removeChild(node) | ||
} | ||
} | ||
// initialize once | ||
if (expr.done) return | ||
expr.done = true | ||
// item = currently looped item | ||
function update(expressions, tag, item) { | ||
each(expressions, function(expr) { | ||
var dom = expr.dom, | ||
prev = dom.previousSibling, | ||
root = dom.parentNode, | ||
template = dom.outerHTML, | ||
val = expr.expr, | ||
els = val.split(/\s+in\s+/), | ||
rendered = [], | ||
checksum, | ||
keys | ||
attr_name = expr.attr, | ||
value = tmpl(expr.expr, tag) | ||
if (value == null) value = '' | ||
if (els[1]) { | ||
val = '{ ' + els[1] | ||
keys = els[0].slice(1).trim().split(/,\s*/) | ||
} | ||
// no change | ||
if (expr.value === value) return | ||
expr.value = value | ||
// clean template code | ||
instance.one('mount', function() { | ||
var p = dom.parentNode | ||
if (p) { | ||
root = p | ||
root.removeChild(dom) | ||
} | ||
}) | ||
// text node | ||
if (!attr_name) return dom.nodeValue = value | ||
function startPos() { | ||
return Array.prototype.indexOf.call(root.childNodes, prev) + 1 | ||
} | ||
// remove attribute | ||
if (!value && expr.bool || /obj|func/.test(typeof value)) remAttr(dom, attr_name) | ||
instance.on('updated', function() { | ||
// event handler | ||
if (typeof value == 'function') { | ||
setEventHandler(attr_name, value, dom, tag, item) | ||
var items = tmpl(val, instance) | ||
is_array = Array.isArray(items) | ||
// if- conditional | ||
} else if (attr_name == 'if') { | ||
if (is_array) items = items.slice(0) | ||
remAttr(dom, attr_name) | ||
else { | ||
var stub = expr.stub | ||
if (!items) return // some IE8 issue | ||
// add to DOM | ||
if (value) { | ||
stub && insertTo(stub.parentNode, stub, dom) | ||
// detect Object changes | ||
var testsum = JSON.stringify(items) | ||
if (testsum == checksum) return | ||
checksum = testsum | ||
// remove from DOM | ||
} else { | ||
stub = expr.stub = stub || document.createTextNode('') | ||
insertTo(dom.parentNode, dom, stub) | ||
} | ||
items = Object.keys(items).map(function(key, i) { | ||
var item = {} | ||
item[keys[0]] = key | ||
item[keys[1]] = items[key] | ||
return item | ||
}) | ||
// show / hide | ||
} else if (/^(show|hide)$/.test(attr_name)) { | ||
remAttr(dom, attr_name) | ||
if (attr_name == 'hide') value = !value | ||
dom.style.display = value ? '' : 'none' | ||
// normal attribute | ||
} else { | ||
if (expr.bool) { | ||
dom[attr_name] = value | ||
if (!value) return | ||
value = attr_name | ||
} | ||
// remove redundant | ||
diff(rendered, items).map(function(item) { | ||
var pos = rendered.indexOf(item) | ||
root.removeChild(root.childNodes[startPos() + pos]) | ||
rendered.splice(pos, 1) | ||
}) | ||
dom.setAttribute(attr_name, value) | ||
} | ||
// add new | ||
diff(items, rendered).map(function(item, i) { | ||
var pos = items.indexOf(item) | ||
}) | ||
// string array | ||
if (keys && !checksum) { | ||
var obj = {} | ||
obj[keys[0]] = item | ||
obj[keys[1]] = pos | ||
item = obj | ||
} | ||
} | ||
function each(nodes, fn) { | ||
for (var i = 0; i < (nodes || []).length; i++) { | ||
if (fn(nodes[i], i) === false) i-- | ||
} | ||
} | ||
var tag = createTag({ | ||
before: root.childNodes[startPos() + pos], | ||
parent: instance, | ||
tmpl: template, | ||
item: item, | ||
root: root | ||
}) | ||
function remAttr(dom, name) { | ||
dom.removeAttribute(name) | ||
} | ||
instance.on('update', function() { | ||
tag.update(0, true) | ||
}) | ||
function extend(obj, from) { | ||
from && Object.keys(from).map(function(key) { | ||
obj[key] = from[key] | ||
}) | ||
return obj | ||
} | ||
}) | ||
function mkdom(template) { | ||
var tag_name = template.trim().slice(1, 3).toLowerCase(), | ||
root_tag = /td|th/.test(tag_name) ? 'tr' : tag_name == 'tr' ? 'tbody' : 'div' | ||
el = document.createElement(root_tag) | ||
// assign rendered | ||
rendered = items | ||
el.stub = true | ||
el.innerHTML = template | ||
return el | ||
} | ||
}) | ||
function walk(dom, fn) { | ||
dom = fn(dom) === false ? dom.nextSibling : dom.firstChild | ||
while (dom) { | ||
walk(dom, fn) | ||
dom = dom.nextSibling | ||
} | ||
} | ||
riot.tag = function(name, tmpl, fn) { | ||
fn = fn || noop, | ||
tag_impl[name] = [tmpl, fn] | ||
} | ||
function arrDiff(arr1, arr2) { | ||
return arr1.filter(function(el) { | ||
return arr2.indexOf(el) < 0 | ||
}) | ||
} | ||
riot.mountTo = function(node, tagName, opts) { | ||
var tag = tag_impl[tagName] | ||
return tag && createTag({ tmpl: tag[0], fn: tag[1], root: node, opts: opts }) | ||
} | ||
// HTMLDocument == IE8 thing | ||
function hasParent(el) { | ||
var p = el.parentNode, | ||
doc = window.HTMLDocument | ||
riot.mount = function(selector, opts) { | ||
if (selector == '*') selector = Object.keys(tag_impl).join(', ') | ||
return p && !(doc && p instanceof doc) | ||
} | ||
var instances = [] | ||
/* | ||
Virtual dom is an array of custom tags on the document. | ||
Each tag stores an array of child tags. | ||
Updates and unmounts propagate downwards from parent to children. | ||
*/ | ||
each(doc.querySelectorAll(selector), function(node) { | ||
if (node.riot) return | ||
var virtual_dom = [], | ||
tag_impl = {} | ||
var tagName = node.tagName.toLowerCase(), | ||
instance = riot.mountTo(node, tagName, opts) | ||
riot.tag = function(name, html, fn) { | ||
tag_impl[name] = { name: name, tmpl: html, fn: fn } | ||
} | ||
if (instance) { | ||
instances.push(instance) | ||
node.riot = 1 | ||
} | ||
}) | ||
var mountTo = riot.mountTo = function(root, tagName, opts) { | ||
var impl = tag_impl[tagName], tag | ||
return instances | ||
} | ||
if (impl) tag = new Tag(impl, { root: root, opts: opts }) | ||
// update everything | ||
riot.update = function() { | ||
return all_tags = all_tags.filter(function(tag) { | ||
return !!tag.update() | ||
if (tag) { | ||
virtual_dom.push(tag) | ||
return tag.on('unmount', function() { | ||
virtual_dom.splice(virtual_dom.indexOf(tag), 1) | ||
}) | ||
} | ||
} | ||
})(riot, this.top) | ||
riot.mount = function(selector, opts) { | ||
if (selector == '*') selector = Object.keys(tag_impl).join(', ') | ||
var tags = [] | ||
each(document.querySelectorAll(selector), function(root) { | ||
var tagName = root.tagName.toLowerCase(), | ||
tag = mountTo(root, tagName, opts) | ||
if (tag) tags.push(tag) | ||
}) | ||
return tags | ||
} | ||
// update everything | ||
riot.update = function() { | ||
virtual_dom.map(function(tag) { | ||
tag.update() | ||
}) | ||
return virtual_dom | ||
} | ||
// support CommonJS | ||
@@ -713,2 +796,2 @@ if (typeof exports === 'object') | ||
})(); | ||
})(); |
@@ -1,2 +0,2 @@ | ||
/* Riot 2.0.7, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
(function(){var e={version:"v2.0.7"};"use strict";e.observable=function(e){e=e||{};var t={};e.on=function(n,r){if(typeof r=="function"){n.replace(/\S+/g,function(e,n){(t[e]=t[e]||[]).push(r);r.typed=n>0})}return e};e.off=function(n,r){if(n=="*")t={};else if(r){var i=t[n];for(var o=0,u;u=i&&i[o];++o){if(u==r){i.splice(o,1);o--}}}else{n.replace(/\S+/g,function(e){t[e]=[]})}return e};e.one=function(t,n){if(n)n.one=1;return e.on(t,n)};e.trigger=function(n){var r=[].slice.call(arguments,1),i=t[n]||[];for(var o=0,u;u=i[o];++o){if(!u.busy){u.busy=1;u.apply(e,u.typed?[n].concat(r):r);if(u.one){i.splice(o,1);o--}else if(i[o]!==u){o--}u.busy=0}}return e};return e};(function(e,t){if(!this.top)return;var n=location,r=e.observable(),i=u(),o=window;function u(){return n.hash.slice(1)}function f(e){return e.split("/")}function a(e){if(e.type)e=u();if(e!=i){r.trigger.apply(null,["H"].concat(f(e)));i=e}}var l=e.route=function(e){if(e[0]){n.hash=e;a(e)}else{r.on("H",e)}};l.exec=function(e){e.apply(null,f(u()))};l.parser=function(e){f=e};o.addEventListener?o.addEventListener(t,a,false):o.attachEvent("on"+t,a)})(e,"hashchange");e._tmpl=function(){var e={},t=/("|').+?[^\\]\1|\.\w*|\w*:|\b(?:this|true|false|null|undefined|new|typeof|Number|String|Object|Array|Math|Date|JSON)\b|([a-z_]\w*)/gi;return function(t,r){return t&&(e[t]=e[t]||n(t))(r)};function n(e,t){t=(e||"{}").replace(/\\{/g,"").replace(/\\}/g,"").split(/({[\s\S]*?})/);return new Function("d","return "+(!t[0]&&!t[2]?r(t[1]):"["+t.map(function(e,t){return t%2?r(e,1):'"'+e.replace(/\n/g,"\\n").replace(/"/g,'\\"')+'"'}).join(",")+'].join("")').replace(/\uFFF0/g,"{").replace(/\uFFF1/g,"}"))}function r(e,t){e=e.replace(/\n/g," ").replace(/^[{ ]+|[ }]+$|\/\*.+?\*\//g,"");return/^\s*[\w-"']+ *:/.test(e)?"["+e.replace(/\W*([\w-]+)\W*:([^,]+)/g,function(e,n,r){return r.replace(/\w[^,|& ]*/g,function(e){return i(e,t)})+'?"'+n+'":"",'})+'].join(" ")':i(e,t)}function i(e,n){return"(function(v){try{v="+(e.replace(t,function(e,t,n){return n?"d."+n:e})||"x")+"}finally{return "+(n?'!v&&v!==0?"":v':"v")+"}}).call(d)"}}();(function(e,t){if(!t)return;var n=e._tmpl,r=[],i={},o=document;function u(e,t){for(var n=0;n<(e||[]).length;n++){if(t(e[n],n)===false)n--}}function f(e,t){t&&Object.keys(t).map(function(n){e[n]=t[n]});return e}function a(e,t){return e.filter(function(e){return t.indexOf(e)<0})}function l(e,t){e=t(e)===false?e.nextSibling:e.firstChild;while(e){l(e,t);e=e.nextSibling}}function c(e){var t=e.trim().slice(1,3).toLowerCase(),n=/td|th/.test(t)?"tr":t=="tr"?"tbody":"div";el=o.createElement(n);el.innerHTML=e;return el}function s(e,t){t.trigger("update");u(e,function(e){var r=e.tag,i=e.dom;function o(e){i.removeAttribute(e)}if(e.loop){o("each");return v(e,t)}if(r)return r.update?r.update():e.tag=d({tmpl:r[0],fn:r[1],root:i,parent:t});var u=e.attr,f=n(e.expr,t);if(f==null)f="";if(e.value===f)return;e.value=f;if(!u)return i.nodeValue=f;if(!f&&e.bool||/obj|func/.test(typeof f))o(u);if(typeof f=="function"){i[u]=function(e){e=e||window.event;e.which=e.which||e.charCode||e.keyCode;e.target=e.target||e.srcElement;e.currentTarget=i;e.item=t.__item||t;if(f.call(t,e)!==true){e.preventDefault&&e.preventDefault();e.returnValue=false}t.update()}}else if(/^(show|hide|if)$/.test(u)){o(u);if(u=="hide")f=!f;i.style.display=f?"":"none"}else{if(e.bool){i[u]=f;if(!f)return;f=u}i.setAttribute(u,f)}});t.trigger("updated")}function p(e){var t={},n=[];l(e,function(e){var n=e.nodeType,o=e.nodeValue;if(n==3&&e.parentNode.tagName!="STYLE"){r(e,o)}else if(n==1){o=e.getAttribute("each");if(o){r(e,o,{loop:1});return false}var f=i[e.tagName.toLowerCase()];u(e.attributes,function(n){var i=n.name,o=n.value;if(/^(name|id)$/.test(i))t[o]=e;if(!f){var u=i.split("__")[1];r(e,o,{attr:u||i,bool:u});if(u){e.removeAttribute(i);return false}}});if(f)r(e,0,{tag:f})}});return{expr:n,elem:t};function r(e,t,r){if(t?t.indexOf("{")>=0:r){var i={dom:e,expr:t};n.push(f(i,r||{}))}}}function d(t){var i=t.opts||{},a=c(t.tmpl),l=t.root,d=t.parent,v=p(a),m={root:l,opts:i,parent:d,__item:t.item},g={};f(m,v.elem);u(l.attributes,function(e){g[e.name]=e.value});function h(){Object.keys(g).map(function(e){var t=i[e]=n(g[e],d||m);if(typeof t=="object")l.removeAttribute(e)})}h();if(!m.on){e.observable(m);delete m.off}if(t.fn)t.fn.call(m,i);m.update=function(e,t){if(d&&a&&!a.firstChild){l=d.root;a=null}if(t||o.body.contains(l)){f(m,e);f(m,m.__item);h();s(v.expr,m);!t&&m.__item&&d.update();return true}else{m.trigger("unmount")}};m.update(0,true);while(a.firstChild){if(t.before)l.insertBefore(a.firstChild,t.before);else l.appendChild(a.firstChild)}m.trigger("mount");r.push(m);return m}function v(e,t){if(e.done)return;e.done=true;var r=e.dom,i=r.previousSibling,o=r.parentNode,u=r.outerHTML,f=e.expr,l=f.split(/\s+in\s+/),c=[],s,p;if(l[1]){f="{ "+l[1];p=l[0].slice(1).trim().split(/,\s*/)}t.one("mount",function(){var e=r.parentNode;if(e){o=e;o.removeChild(r)}});function v(){return Array.prototype.indexOf.call(o.childNodes,i)+1}t.on("updated",function(){var e=n(f,t);is_array=Array.isArray(e);if(is_array)e=e.slice(0);else{if(!e)return;var r=JSON.stringify(e);if(r==s)return;s=r;e=Object.keys(e).map(function(t,n){var r={};r[p[0]]=t;r[p[1]]=e[t];return r})}a(c,e).map(function(e){var t=c.indexOf(e);o.removeChild(o.childNodes[v()+t]);c.splice(t,1)});a(e,c).map(function(n,r){var i=e.indexOf(n);if(p&&!s){var f={};f[p[0]]=n;f[p[1]]=i;n=f}var a=d({before:o.childNodes[v()+i],parent:t,tmpl:u,item:n,root:o});t.on("update",function(){a.update(0,true)})});c=e})}e.tag=function(e,t,n){n=n||noop,i[e]=[t,n]};e.mountTo=function(e,t,n){var r=i[t];return r&&d({tmpl:r[0],fn:r[1],root:e,opts:n})};e.mount=function(t,n){if(t=="*")t=Object.keys(i).join(", ");var r=[];u(o.querySelectorAll(t),function(t){if(t.riot)return;var i=t.tagName.toLowerCase(),o=e.mountTo(t,i,n);if(o){r.push(o);t.riot=1}});return r};e.update=function(){return r=r.filter(function(e){return!!e.update()})}})(e,this.top);if(typeof exports==="object")module.exports=e;else if(typeof define==="function"&&define.amd)define(function(){return e});else this.riot=e})(); | ||
/* Riot v2.0.8, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
(function(){var e={version:"v2.0.8",settings:{}};"use strict";e.observable=function(e){e=e||{};var n={};e.on=function(t,i){if(typeof i=="function"){t.replace(/\S+/g,function(e,t){(n[e]=n[e]||[]).push(i);i.typed=t>0})}return e};e.off=function(t,i){if(t=="*")n={};else if(i){var r=n[t];for(var o=0,u;u=r&&r[o];++o){if(u==i){r.splice(o,1);o--}}}else{t.replace(/\S+/g,function(e){n[e]=[]})}return e};e.one=function(n,t){if(t)t.one=1;return e.on(n,t)};e.trigger=function(t){var i=[].slice.call(arguments,1),r=n[t]||[];for(var o=0,u;u=r[o];++o){if(!u.busy){u.busy=1;u.apply(e,u.typed?[t].concat(i):i);if(u.one){r.splice(o,1);o--}else if(r[o]!==u){o--}u.busy=0}}return e};return e};(function(e,n){if(!this.top)return;var t=location,i=e.observable(),r=u(),o=window;function u(){return t.hash.slice(1)}function f(e){return e.split("/")}function a(e){if(e.type)e=u();if(e!=r){i.trigger.apply(null,["H"].concat(f(e)));r=e}}var c=e.route=function(e){if(e[0]){t.hash=e;a(e)}else{i.on("H",e)}};c.exec=function(e){e.apply(null,f(u()))};c.parser=function(e){f=e};o.addEventListener?o.addEventListener(n,a,false):o.attachEvent("on"+n,a)})(e,"hashchange");var n=function(){var n={},t,i,r=/("|').+?[^\\]\1|\.\w*|\w*:|\b(?:(?:new|typeof|in|instanceof) |(?:this|true|false|null|undefined)\b|function *\()|([a-z_]\w*)/gi;return function(r,u){var f=e.settings.brackets||"{ }";if(f!=t){t=f.split(" ");i=a(/({[\s\S]*?})/)}return u?r&&(n[r]=n[r]||o(r))(u):i.test(r)};function o(e,n){n=(e||t.join("")).replace(a(/\\{/),"").replace(a(/\\}/),"").split(i);return new Function("d","return "+(!n[0]&&!n[2]&&!n[3]?u(n[1]):"["+n.map(function(e,n){return n%2?u(e,1):'"'+e.replace(/\n/g,"\\n").replace(/"/g,'\\"')+'"'}).join(",")+'].join("")').replace(/\uFFF0/g,t[0]).replace(/\uFFF1/g,t[1]))}function u(e,n){e=e.replace(/\n/g," ").replace(a(/^[{ ]+|[ }]+$|\/\*.+?\*\//g),"");return/^\s*[\w-"']+ *:/.test(e)?"["+e.replace(/\W*([\w-]+)\W*:([^,]+)/g,function(e,t,i){return i.replace(/\w[^,|& ]*/g,function(e){return f(e,n)})+'?"'+t+'":"",'})+'].join(" ")':f(e,n)}function f(e,n){return"(function(v){try{v="+(e.replace(r,function(e,n,t){return t?"(d."+t+"===undefined?window."+t+":d."+t+")":e})||"x")+"}finally{return "+(n?'!v&&v!==0?"":v':"v")+"}}).call(d)"}function a(e){return RegExp(e.source.split("{").join("\\"+t[0]).split("}").join("\\"+t[1]),e.global?"g":"")}}();function t(e){var n={val:e},t=e.split(/\s+in\s+/);if(t[1]){n.val="{ "+t[1];t=t[0].slice(1).trim().split(/,\s*/);n.key=t[0];n.pos=t[1]}return n}function i(e,i,r){s(e,"each");var o=e.outerHTML,f=e.previousSibling,a=e.parentNode,c=[],l=[],p;r=t(r);i.one("update",function(){a.removeChild(e)}).one("mount",function(){if(!m(a))a=i.root}).on("update",function(){var e=n(r.val,i);if(!e)return;if(!Array.isArray(e)){var t=JSON.stringify(e);if(t==p)return;p=t;l.map(function(e){e.unmount()});l=c=[];e=Object.keys(e).map(function(n,t){var i={};i[r.key]=n;i[r.pos]=e[n];return i})}h(c,e).map(function(e){var n=c.indexOf(e),t=l[n];if(t){t.unmount();c.splice(n,1);l.splice(n,1)}});var s=a.childNodes,d=Array.prototype.indexOf.call(s,f);h(e,c).map(function(n,t){var f=e.indexOf(n);if(!p&&r.key){var c={};c[r.key]=n;c[r.pos]=f;n=c}var v=new u({tmpl:o},{before:s[d+1+f],parent:i,root:a,loop:true,item:n});l.splice(f,0,v)});c=e.slice()})}function r(e,n,t){v(e,function(e){if(e.nodeType!=1)return;l(e.attributes,function(t){if(/^(name|id)$/.test(t.name))n[t.value]=e})})}function o(e,t,r){function o(e,t,i){if(n(t)||i){var o={dom:e,expr:t};r.push(p(o,i||{}))}}v(e,function(e){var n=e.nodeType;if(n==3&&e.parentNode.tagName!="STYLE")o(e,e.nodeValue);if(n!=1)return;var r=e.getAttribute("each");if(r){i(e,t,r);return false}var f=b[e.tagName.toLowerCase()];if(f){f=new u(f,{root:e,parent:t});return false}l(e.attributes,function(n){var t=n.name,i=n.value;var r=t.split("__")[1];o(e,i,{attr:r||t,bool:r});if(r){s(e,t);return false}})})}function u(t,i){var u=e.observable(this),f=[],a={},v=i.parent,h=i.loop,g=i.root,b=i.opts,y=i.item;if(!h&&g.riot)return;g.riot=1;b=b||{};p(this,{parent:v,root:g,opts:b,children:[]});p(this,y);l(g.attributes,function(e){var n=e.name,t=e.value;a[n]=t;if(t.indexOf("{")>=0){s(g,n);return false}});function w(){Object.keys(a).map(function(e){b[e]=n(a[e],v||u)})}w();v&&v.children.push(this);var x=d(t.tmpl),C;r(x,this);this.update=function(e,n){p(u,e);p(u,y);u.trigger("update");w();c(f,u,y);u.trigger("updated")};this.unmount=function(){if(h){g.removeChild(C)}else{var e=g.parentNode;e&&e.removeChild(g)}if(v){var n=v.children;n.splice(n.indexOf(u),1)}u.trigger("unmount");v&&v.off("update",u.update);mounted=false};function j(){while(x.firstChild){if(h){C=x.firstChild;g.insertBefore(x.firstChild,i.before||null)}else{g.appendChild(x.firstChild)}}if(!m(g))u.root=g=v.root;u.trigger("mount");v&&v.on("update",u.update).one("unmount",u.unmount)}if(t.fn)t.fn.call(this,b);o(x,this,f);this.update();j()}function f(e,n,t,i,r){t[e]=function(e){e=e||window.event;e.which=e.which||e.charCode||e.keyCode;e.target=e.target||e.srcElement;e.currentTarget=t;e.item=r;if(n.call(i,e)!==true){e.preventDefault&&e.preventDefault();e.returnValue=false}i.update()}}function a(e,n,t){if(e){e.insertBefore(t,n);e.removeChild(n)}}function c(e,t,i){l(e,function(e){var r=e.dom,o=e.attr,u=n(e.expr,t);if(u==null)u="";if(e.value===u)return;e.value=u;if(!o)return r.nodeValue=u;if(!u&&e.bool||/obj|func/.test(typeof u))s(r,o);if(typeof u=="function"){f(o,u,r,t,i)}else if(o=="if"){s(r,o);var c=e.stub;if(u){c&&a(c.parentNode,c,r)}else{c=e.stub=c||document.createTextNode("");a(r.parentNode,r,c)}}else if(/^(show|hide)$/.test(o)){s(r,o);if(o=="hide")u=!u;r.style.display=u?"":"none"}else{if(e.bool){r[o]=u;if(!u)return;u=o}r.setAttribute(o,u)}})}function l(e,n){for(var t=0;t<(e||[]).length;t++){if(n(e[t],t)===false)t--}}function s(e,n){e.removeAttribute(n)}function p(e,n){n&&Object.keys(n).map(function(t){e[t]=n[t]});return e}function d(e){var n=e.trim().slice(1,3).toLowerCase(),t=/td|th/.test(n)?"tr":n=="tr"?"tbody":"div";el=document.createElement(t);el.stub=true;el.innerHTML=e;return el}function v(e,n){e=n(e)===false?e.nextSibling:e.firstChild;while(e){v(e,n);e=e.nextSibling}}function h(e,n){return e.filter(function(e){return n.indexOf(e)<0})}function m(e){var n=e.parentNode,t=window.HTMLDocument;return n&&!(t&&n instanceof t)}var g=[],b={};e.tag=function(e,n,t){b[e]={name:e,tmpl:n,fn:t}};var y=e.mountTo=function(e,n,t){var i=b[n],r;if(i)r=new u(i,{root:e,opts:t});if(r){g.push(r);return r.on("unmount",function(){g.splice(g.indexOf(r),1)})}};e.mount=function(e,n){if(e=="*")e=Object.keys(b).join(", ");var t=[];l(document.querySelectorAll(e),function(e){var i=e.tagName.toLowerCase(),r=y(e,i,n);if(r)t.push(r)});return t};e.update=function(){g.map(function(e){e.update()});return g};if(typeof exports==="object")module.exports=e;else if(typeof define==="function"&&define.amd)define(function(){return e});else this.riot=e})(); |
@@ -1,6 +0,6 @@ | ||
/* Riot 2.0.7, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
/* Riot v2.0.8, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
;(function() { | ||
var riot = { version: 'v2.0.7' } | ||
var riot = { version: 'v2.0.8', settings: {} } | ||
@@ -152,21 +152,39 @@ 'use strict' | ||
// Customizable brackets | ||
riot.settings.brackets = '[ ]' | ||
riot.settings.brackets = '<% %>' | ||
*/ | ||
riot._tmpl = (function() { | ||
var tmpl = (function() { | ||
var cache = {}, | ||
// find variable names | ||
re_vars = /("|').+?[^\\]\1|\.\w*|\w*:|\b(?:this|true|false|null|undefined|new|typeof|Number|String|Object|Array|Math|Date|JSON)\b|([a-z_]\w*)/gi | ||
// [ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] | ||
brackets, | ||
re_expr, | ||
re_vars = /("|').+?[^\\]\1|\.\w*|\w*:|\b(?:(?:new|typeof|in|instanceof) |(?:this|true|false|null|undefined)\b|function *\()|([a-z_]\w*)/gi | ||
// [ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] | ||
// find variable names: | ||
// 1. skip quoted strings: "a b", 'a b', 'a \'b\'' | ||
// 2. skip object properties: .name | ||
// 3. skip object literals: name: | ||
// 4. skip reserved words | ||
// 4. skip javascript keywords | ||
// 5. match var name | ||
// build a template (or get it from cache), render with data | ||
return function(str, data) { | ||
return function(str, data) { | ||
return str && (cache[str] = cache[str] || tmpl(str))(data) | ||
// make sure we use current brackets setting | ||
var b = riot.settings.brackets || '{ }' | ||
if(b != brackets){ | ||
brackets = b.split(' ') | ||
re_expr = re(/({[\s\S]*?})/) | ||
} | ||
// build a template (or get it from cache), render with data | ||
// (or just test if string has expression when called w/o data) | ||
return data | ||
? str && (cache[str] = cache[str] || tmpl(str))(data) | ||
: re_expr.test(str) | ||
} | ||
@@ -178,10 +196,12 @@ | ||
function tmpl(s, p) { | ||
p = (s || '{}') | ||
// default template string to {} | ||
p = (s || brackets.join('')) | ||
// temporarily convert \{ and \} to a non-character | ||
.replace(/\\{/g, '\uFFF0') | ||
.replace(/\\}/g, '\uFFF1') | ||
.replace(re(/\\{/), '\uFFF0') | ||
.replace(re(/\\}/), '\uFFF1') | ||
// split string to expression and non-expresion parts | ||
.split(/({[\s\S]*?})/) | ||
.split(re_expr) | ||
@@ -191,3 +211,3 @@ return new Function('d', 'return ' + ( | ||
// is it a single expression or a template? i.e. {x} or <b>{x}</b> | ||
!p[0] && !p[2] | ||
!p[0] && !p[2] && !p[3] | ||
@@ -221,4 +241,4 @@ // if expression, evaluate it | ||
// bring escaped { and } back | ||
.replace(/\uFFF0/g, '{') | ||
.replace(/\uFFF1/g, '}') | ||
.replace(/\uFFF0/g, brackets[0]) | ||
.replace(/\uFFF1/g, brackets[1]) | ||
@@ -239,3 +259,3 @@ ) | ||
// trim whitespace, curly brackets, strip comments | ||
.replace(/^[{ ]+|[ }]+$|\/\*.+?\*\//g, '') | ||
.replace(re(/^[{ ]+|[ }]+$|\/\*.+?\*\//g), '') | ||
@@ -266,3 +286,3 @@ // is it an object literal? i.e. { key : value } | ||
// prefix vars (name => data.name) | ||
+ (s.replace(re_vars, function(s, _, v) { return v ? 'd.' + v : s }) | ||
+ (s.replace(re_vars, function(s, _, v) { return v ? '(d.'+v+'===undefined?window.'+v+':d.'+v+')' : s }) | ||
@@ -280,425 +300,487 @@ // break the expression if its empty (resulting in undefined value) | ||
})() | ||
;(function(riot, is_browser) { | ||
if (!is_browser) return | ||
// change regexp to use custom brackets | ||
var tmpl = riot._tmpl, | ||
all_tags = [], | ||
tag_impl = {}, | ||
doc = document | ||
function each(nodes, fn) { | ||
for (var i = 0; i < (nodes || []).length; i++) { | ||
if (fn(nodes[i], i) === false) i-- | ||
} | ||
function re(r) { | ||
return RegExp(r.source | ||
.split('{').join('\\'+brackets[0]) | ||
.split('}').join('\\'+brackets[1]), | ||
r.global ? 'g' : '') | ||
} | ||
function extend(obj, from) { | ||
from && Object.keys(from).map(function(key) { | ||
obj[key] = from[key] | ||
}) | ||
return obj | ||
} | ||
})() | ||
// { key, i in items} -> { key, i, items } | ||
function loopKeys(expr) { | ||
var ret = { val: expr }, | ||
els = expr.split(/\s+in\s+/) | ||
function diff(arr1, arr2) { | ||
return arr1.filter(function(el) { | ||
return arr2.indexOf(el) < 0 | ||
}) | ||
if (els[1]) { | ||
ret.val = '{ ' + els[1] | ||
els = els[0].slice(1).trim().split(/,\s*/) | ||
ret.key = els[0] | ||
ret.pos = els[1] | ||
} | ||
return ret | ||
} | ||
function walk(dom, fn) { | ||
dom = fn(dom) === false ? dom.nextSibling : dom.firstChild | ||
function _each(dom, parent, expr) { | ||
while (dom) { | ||
walk(dom, fn) | ||
dom = dom.nextSibling | ||
} | ||
} | ||
remAttr(dom, 'each') | ||
var template = dom.outerHTML, | ||
prev = dom.previousSibling, | ||
root = dom.parentNode, | ||
rendered = [], | ||
tags = [], | ||
checksum | ||
function mkdom(tmpl) { | ||
var tag_name = tmpl.trim().slice(1, 3).toLowerCase(), | ||
root_tag = /td|th/.test(tag_name) ? 'tr' : tag_name == 'tr' ? 'tbody' : 'div' | ||
el = doc.createElement(root_tag) | ||
expr = loopKeys(expr) | ||
el.innerHTML = tmpl | ||
return el | ||
} | ||
// clean template code after update (and let walk finish it's parse) | ||
parent.one('update', function() { | ||
root.removeChild(dom) | ||
}).one('mount', function() { | ||
if (!hasParent(root)) root = parent.root | ||
function update(expressions, instance) { | ||
}).on('update', function() { | ||
// allow recalculation of context data | ||
instance.trigger('update') | ||
var items = tmpl(expr.val, parent) | ||
if (!items) return | ||
each(expressions, function(expr) { | ||
var tag = expr.tag, | ||
dom = expr.dom | ||
// object loop. any changes cause full redraw | ||
if (!Array.isArray(items)) { | ||
var testsum = JSON.stringify(items) | ||
if (testsum == checksum) return | ||
checksum = testsum | ||
function remAttr(name) { | ||
dom.removeAttribute(name) | ||
} | ||
// clear old items | ||
tags.map(function(tag) { | ||
tag.unmount() | ||
}) | ||
// loops first: TODO remove from expressions arr | ||
if (expr.loop) { | ||
remAttr('each') | ||
return loop(expr, instance) | ||
} | ||
tags = rendered = [] | ||
// custom tag | ||
if (tag) return tag.update ? tag.update() : | ||
expr.tag = createTag({ tmpl: tag[0], fn: tag[1], root: dom, parent: instance }) | ||
items = Object.keys(items).map(function(key, i) { | ||
var obj = {} | ||
obj[expr.key] = key | ||
obj[expr.pos] = items[key] | ||
return obj | ||
}) | ||
} | ||
var attr_name = expr.attr, | ||
value = tmpl(expr.expr, instance) | ||
// unmount redundant | ||
arrDiff(rendered, items).map(function(item) { | ||
var pos = rendered.indexOf(item), | ||
tag = tags[pos] | ||
if (value == null) value = '' | ||
if (tag) { | ||
tag.unmount() | ||
rendered.splice(pos, 1) | ||
tags.splice(pos, 1) | ||
} | ||
}) | ||
// no change | ||
if (expr.value === value) return | ||
expr.value = value | ||
// mount new | ||
var nodes = root.childNodes, | ||
prev_index = Array.prototype.indexOf.call(nodes, prev) | ||
arrDiff(items, rendered).map(function(item, i) { | ||
// text node | ||
if (!attr_name) return dom.nodeValue = value | ||
var pos = items.indexOf(item) | ||
// attribute | ||
if (!value && expr.bool || /obj|func/.test(typeof value)) remAttr(attr_name) | ||
if (!checksum && expr.key) { | ||
var obj = {} | ||
obj[expr.key] = item | ||
obj[expr.pos] = pos | ||
item = obj | ||
} | ||
// event handler | ||
if (typeof value == 'function') { | ||
dom[attr_name] = function(e) { | ||
var tag = new Tag({ tmpl: template }, { | ||
before: nodes[prev_index + 1 + pos], | ||
parent: parent, | ||
root: root, | ||
loop: true, | ||
item: item | ||
}) | ||
// cross browser event fix | ||
e = e || window.event | ||
e.which = e.which || e.charCode || e.keyCode | ||
e.target = e.target || e.srcElement | ||
e.currentTarget = dom | ||
tags.splice(pos, 0, tag) | ||
// currently looped item | ||
e.item = instance.__item || instance | ||
}) | ||
// prevent default behaviour (by default) | ||
if (value.call(instance, e) !== true) { | ||
e.preventDefault && e.preventDefault() | ||
e.returnValue = false | ||
} | ||
rendered = items.slice() | ||
instance.update() | ||
} | ||
}) | ||
// show / hide / if | ||
} else if (/^(show|hide|if)$/.test(attr_name)) { | ||
remAttr(attr_name) | ||
if (attr_name == 'hide') value = !value | ||
dom.style.display = value ? '' : 'none' | ||
} | ||
function parseNamedElements(root, tag, expressions) { | ||
walk(root, function(dom) { | ||
if (dom.nodeType != 1) return | ||
// normal attribute | ||
} else { | ||
if (expr.bool) { | ||
dom[attr_name] = value | ||
if (!value) return | ||
value = attr_name | ||
} | ||
dom.setAttribute(attr_name, value) | ||
} | ||
each(dom.attributes, function(attr) { | ||
if (/^(name|id)$/.test(attr.name)) tag[attr.value] = dom | ||
}) | ||
}) | ||
} | ||
instance.trigger('updated') | ||
function parseLayout(root, tag, expressions) { | ||
function addExpr(dom, value, data) { | ||
if (tmpl(value) || data) { | ||
var expr = { dom: dom, expr: value } | ||
expressions.push(extend(expr, data || {})) | ||
} | ||
} | ||
function parse(root) { | ||
walk(root, function(dom) { | ||
var named_elements = {}, | ||
expressions = [] | ||
var type = dom.nodeType | ||
walk(root, function(dom) { | ||
// text node | ||
if (type == 3 && dom.parentNode.tagName != 'STYLE') addExpr(dom, dom.nodeValue) | ||
if (type != 1) return | ||
var type = dom.nodeType, | ||
value = dom.nodeValue | ||
/* element */ | ||
// text node | ||
if (type == 3 && dom.parentNode.tagName != 'STYLE') { | ||
addExpr(dom, value) | ||
// loop | ||
var attr = dom.getAttribute('each') | ||
if (attr) { _each(dom, tag, attr); return false } | ||
// element | ||
} else if (type == 1) { | ||
// child tag | ||
var impl = tag_impl[dom.tagName.toLowerCase()] | ||
if (impl) { | ||
impl = new Tag(impl, { root: dom, parent: tag }) | ||
return false | ||
} | ||
// loop? | ||
value = dom.getAttribute('each') | ||
// attributes | ||
each(dom.attributes, function(attr) { | ||
var name = attr.name, | ||
value = attr.value | ||
if (value) { | ||
addExpr(dom, value, { loop: 1 }) | ||
return false | ||
} | ||
// expressions | ||
var bool = name.split('__')[1] | ||
addExpr(dom, value, { attr: bool || name, bool: bool }) | ||
// custom tag? | ||
var tag = tag_impl[dom.tagName.toLowerCase()] | ||
if (bool) { | ||
remAttr(dom, name) | ||
return false | ||
} | ||
// attributes | ||
each(dom.attributes, function(attr) { | ||
var name = attr.name, | ||
value = attr.value | ||
}) | ||
// named elements | ||
if (/^(name|id)$/.test(name)) named_elements[value] = dom | ||
}) | ||
// expressions | ||
if (!tag) { | ||
var bool = name.split('__')[1] | ||
addExpr(dom, value, { attr: bool || name, bool: bool }) | ||
if (bool) { | ||
dom.removeAttribute(name) | ||
return false | ||
} | ||
} | ||
} | ||
function Tag(impl, conf) { | ||
}) | ||
var self = riot.observable(this), | ||
expressions = [], | ||
attributes = {}, | ||
parent = conf.parent, | ||
is_loop = conf.loop, | ||
root = conf.root, | ||
opts = conf.opts, | ||
item = conf.item | ||
if (tag) addExpr(dom, 0, { tag: tag }) | ||
// cannot initialize twice on the same root element | ||
if (!is_loop && root.riot) return | ||
root.riot = 1 | ||
} | ||
opts = opts || {} | ||
}) | ||
extend(this, { parent: parent, root: root, opts: opts, children: [] }) | ||
extend(this, item) | ||
return { expr: expressions, elem: named_elements } | ||
function addExpr(dom, value, data) { | ||
if (value ? value.indexOf('{') >= 0 : data) { | ||
var expr = { dom: dom, expr: value } | ||
expressions.push(extend(expr, data || {})) | ||
} | ||
// attributes | ||
each(root.attributes, function(attr) { | ||
var name = attr.name, | ||
val = attr.value | ||
attributes[name] = val | ||
// remove dynamic attributes from node | ||
if (val.indexOf('{') >= 0) { | ||
remAttr(root, name) | ||
return false | ||
} | ||
}) | ||
// options | ||
function updateOpts() { | ||
Object.keys(attributes).map(function(name) { | ||
opts[name] = tmpl(attributes[name], parent || self) | ||
}) | ||
} | ||
updateOpts() | ||
// child | ||
parent && parent.children.push(this) | ||
// create new custom tag (component) | ||
function createTag(conf) { | ||
var dom = mkdom(impl.tmpl), | ||
loop_dom | ||
var opts = conf.opts || {}, | ||
dom = mkdom(conf.tmpl), | ||
mountNode = conf.root, | ||
parent = conf.parent, | ||
ast = parse(dom), | ||
tag = { root: mountNode, opts: opts, parent: parent, __item: conf.item }, | ||
attributes = {} | ||
// named elements | ||
parseNamedElements(dom, this) | ||
// named elements | ||
extend(tag, ast.elem) | ||
this.update = function(data, init) { | ||
extend(self, data) | ||
extend(self, item) | ||
self.trigger('update') | ||
updateOpts() | ||
update(expressions, self, item) | ||
self.trigger('updated') | ||
} | ||
// attributes | ||
each(mountNode.attributes, function(attr) { | ||
attributes[attr.name] = attr.value | ||
}) | ||
this.unmount = function() { | ||
function updateOpts() { | ||
Object.keys(attributes).map(function(name) { | ||
var val = opts[name] = tmpl(attributes[name], parent || tag) | ||
if (typeof val == 'object') mountNode.removeAttribute(name) | ||
}) | ||
if (is_loop) { | ||
root.removeChild(loop_dom) | ||
} else { | ||
var p = root.parentNode | ||
p && p.removeChild(root) | ||
} | ||
updateOpts() | ||
if (!tag.on) { | ||
riot.observable(tag) | ||
delete tag.off // off method not needed | ||
// splice from parent.children[] | ||
if (parent) { | ||
var els = parent.children | ||
els.splice(els.indexOf(self), 1) | ||
} | ||
if (conf.fn) conf.fn.call(tag, opts) | ||
self.trigger('unmount') | ||
// cleanup | ||
parent && parent.off('update', self.update) | ||
mounted = false | ||
} | ||
tag.update = function(data, _system) { | ||
function mount() { | ||
while (dom.firstChild) { | ||
if (is_loop) { | ||
loop_dom = dom.firstChild | ||
root.insertBefore(dom.firstChild, conf.before || null) // null needed for IE8 | ||
/* | ||
If loop is defined on the root of the HTML template | ||
the original parent is a temporary <div/> by mkdom() | ||
*/ | ||
if (parent && dom && !dom.firstChild) { | ||
mountNode = parent.root | ||
dom = null | ||
} else { | ||
root.appendChild(dom.firstChild) | ||
} | ||
} | ||
if (_system || doc.body.contains(mountNode)) { | ||
extend(tag, data) | ||
extend(tag, tag.__item) | ||
updateOpts() | ||
update(ast.expr, tag) | ||
if (!hasParent(root)) self.root = root = parent.root | ||
// update parent | ||
!_system && tag.__item && parent.update() | ||
return true | ||
self.trigger('mount') | ||
} else { | ||
tag.trigger('unmount') | ||
} | ||
// one way data flow: propagate updates and unmounts downwards from parent to children | ||
parent && parent.on('update', self.update).one('unmount', self.unmount) | ||
} | ||
} | ||
tag.update(0, true) | ||
// initialize | ||
if (impl.fn) impl.fn.call(this, opts) | ||
// append to root | ||
while (dom.firstChild) { | ||
if (conf.before) mountNode.insertBefore(dom.firstChild, conf.before) | ||
else mountNode.appendChild(dom.firstChild) | ||
} | ||
// layout | ||
parseLayout(dom, this, expressions) | ||
this.update() | ||
mount() | ||
tag.trigger('mount') | ||
} | ||
all_tags.push(tag) | ||
return tag | ||
function setEventHandler(name, handler, dom, tag, item) { | ||
dom[name] = function(e) { | ||
// cross browser event fix | ||
e = e || window.event | ||
e.which = e.which || e.charCode || e.keyCode | ||
e.target = e.target || e.srcElement | ||
e.currentTarget = dom | ||
e.item = item | ||
// prevent default behaviour (by default) | ||
if (handler.call(tag, e) !== true) { | ||
e.preventDefault && e.preventDefault() | ||
e.returnValue = false | ||
} | ||
tag.update() | ||
} | ||
} | ||
function loop(expr, instance) { | ||
function insertTo(root, node, before) { | ||
if (root) { | ||
root.insertBefore(before, node) | ||
root.removeChild(node) | ||
} | ||
} | ||
// initialize once | ||
if (expr.done) return | ||
expr.done = true | ||
// item = currently looped item | ||
function update(expressions, tag, item) { | ||
each(expressions, function(expr) { | ||
var dom = expr.dom, | ||
prev = dom.previousSibling, | ||
root = dom.parentNode, | ||
template = dom.outerHTML, | ||
val = expr.expr, | ||
els = val.split(/\s+in\s+/), | ||
rendered = [], | ||
checksum, | ||
keys | ||
attr_name = expr.attr, | ||
value = tmpl(expr.expr, tag) | ||
if (value == null) value = '' | ||
if (els[1]) { | ||
val = '{ ' + els[1] | ||
keys = els[0].slice(1).trim().split(/,\s*/) | ||
} | ||
// no change | ||
if (expr.value === value) return | ||
expr.value = value | ||
// clean template code | ||
instance.one('mount', function() { | ||
var p = dom.parentNode | ||
if (p) { | ||
root = p | ||
root.removeChild(dom) | ||
} | ||
}) | ||
// text node | ||
if (!attr_name) return dom.nodeValue = value | ||
function startPos() { | ||
return Array.prototype.indexOf.call(root.childNodes, prev) + 1 | ||
} | ||
// remove attribute | ||
if (!value && expr.bool || /obj|func/.test(typeof value)) remAttr(dom, attr_name) | ||
instance.on('updated', function() { | ||
// event handler | ||
if (typeof value == 'function') { | ||
setEventHandler(attr_name, value, dom, tag, item) | ||
var items = tmpl(val, instance) | ||
is_array = Array.isArray(items) | ||
// if- conditional | ||
} else if (attr_name == 'if') { | ||
if (is_array) items = items.slice(0) | ||
remAttr(dom, attr_name) | ||
else { | ||
var stub = expr.stub | ||
if (!items) return // some IE8 issue | ||
// add to DOM | ||
if (value) { | ||
stub && insertTo(stub.parentNode, stub, dom) | ||
// detect Object changes | ||
var testsum = JSON.stringify(items) | ||
if (testsum == checksum) return | ||
checksum = testsum | ||
// remove from DOM | ||
} else { | ||
stub = expr.stub = stub || document.createTextNode('') | ||
insertTo(dom.parentNode, dom, stub) | ||
} | ||
items = Object.keys(items).map(function(key, i) { | ||
var item = {} | ||
item[keys[0]] = key | ||
item[keys[1]] = items[key] | ||
return item | ||
}) | ||
// show / hide | ||
} else if (/^(show|hide)$/.test(attr_name)) { | ||
remAttr(dom, attr_name) | ||
if (attr_name == 'hide') value = !value | ||
dom.style.display = value ? '' : 'none' | ||
// normal attribute | ||
} else { | ||
if (expr.bool) { | ||
dom[attr_name] = value | ||
if (!value) return | ||
value = attr_name | ||
} | ||
// remove redundant | ||
diff(rendered, items).map(function(item) { | ||
var pos = rendered.indexOf(item) | ||
root.removeChild(root.childNodes[startPos() + pos]) | ||
rendered.splice(pos, 1) | ||
}) | ||
dom.setAttribute(attr_name, value) | ||
} | ||
// add new | ||
diff(items, rendered).map(function(item, i) { | ||
var pos = items.indexOf(item) | ||
}) | ||
// string array | ||
if (keys && !checksum) { | ||
var obj = {} | ||
obj[keys[0]] = item | ||
obj[keys[1]] = pos | ||
item = obj | ||
} | ||
} | ||
function each(nodes, fn) { | ||
for (var i = 0; i < (nodes || []).length; i++) { | ||
if (fn(nodes[i], i) === false) i-- | ||
} | ||
} | ||
var tag = createTag({ | ||
before: root.childNodes[startPos() + pos], | ||
parent: instance, | ||
tmpl: template, | ||
item: item, | ||
root: root | ||
}) | ||
function remAttr(dom, name) { | ||
dom.removeAttribute(name) | ||
} | ||
instance.on('update', function() { | ||
tag.update(0, true) | ||
}) | ||
function extend(obj, from) { | ||
from && Object.keys(from).map(function(key) { | ||
obj[key] = from[key] | ||
}) | ||
return obj | ||
} | ||
}) | ||
function mkdom(template) { | ||
var tag_name = template.trim().slice(1, 3).toLowerCase(), | ||
root_tag = /td|th/.test(tag_name) ? 'tr' : tag_name == 'tr' ? 'tbody' : 'div' | ||
el = document.createElement(root_tag) | ||
// assign rendered | ||
rendered = items | ||
el.stub = true | ||
el.innerHTML = template | ||
return el | ||
} | ||
}) | ||
function walk(dom, fn) { | ||
dom = fn(dom) === false ? dom.nextSibling : dom.firstChild | ||
while (dom) { | ||
walk(dom, fn) | ||
dom = dom.nextSibling | ||
} | ||
} | ||
riot.tag = function(name, tmpl, fn) { | ||
fn = fn || noop, | ||
tag_impl[name] = [tmpl, fn] | ||
} | ||
function arrDiff(arr1, arr2) { | ||
return arr1.filter(function(el) { | ||
return arr2.indexOf(el) < 0 | ||
}) | ||
} | ||
riot.mountTo = function(node, tagName, opts) { | ||
var tag = tag_impl[tagName] | ||
return tag && createTag({ tmpl: tag[0], fn: tag[1], root: node, opts: opts }) | ||
} | ||
// HTMLDocument == IE8 thing | ||
function hasParent(el) { | ||
var p = el.parentNode, | ||
doc = window.HTMLDocument | ||
riot.mount = function(selector, opts) { | ||
if (selector == '*') selector = Object.keys(tag_impl).join(', ') | ||
return p && !(doc && p instanceof doc) | ||
} | ||
var instances = [] | ||
/* | ||
Virtual dom is an array of custom tags on the document. | ||
Each tag stores an array of child tags. | ||
Updates and unmounts propagate downwards from parent to children. | ||
*/ | ||
each(doc.querySelectorAll(selector), function(node) { | ||
if (node.riot) return | ||
var virtual_dom = [], | ||
tag_impl = {} | ||
var tagName = node.tagName.toLowerCase(), | ||
instance = riot.mountTo(node, tagName, opts) | ||
riot.tag = function(name, html, fn) { | ||
tag_impl[name] = { name: name, tmpl: html, fn: fn } | ||
} | ||
if (instance) { | ||
instances.push(instance) | ||
node.riot = 1 | ||
} | ||
}) | ||
var mountTo = riot.mountTo = function(root, tagName, opts) { | ||
var impl = tag_impl[tagName], tag | ||
return instances | ||
} | ||
if (impl) tag = new Tag(impl, { root: root, opts: opts }) | ||
// update everything | ||
riot.update = function() { | ||
return all_tags = all_tags.filter(function(tag) { | ||
return !!tag.update() | ||
if (tag) { | ||
virtual_dom.push(tag) | ||
return tag.on('unmount', function() { | ||
virtual_dom.splice(virtual_dom.indexOf(tag), 1) | ||
}) | ||
} | ||
} | ||
})(riot, this.top) | ||
riot.mount = function(selector, opts) { | ||
if (selector == '*') selector = Object.keys(tag_impl).join(', ') | ||
var tags = [] | ||
each(document.querySelectorAll(selector), function(root) { | ||
(function(is_node) { | ||
var tagName = root.tagName.toLowerCase(), | ||
tag = mountTo(root, tagName, opts) | ||
if (tag) tags.push(tag) | ||
}) | ||
return tags | ||
} | ||
// update everything | ||
riot.update = function() { | ||
virtual_dom.map(function(tag) { | ||
tag.update() | ||
}) | ||
return virtual_dom | ||
} | ||
;(function(is_node) { | ||
var BOOL_ATTR = ('allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,default,'+ | ||
@@ -725,6 +807,6 @@ 'defaultchecked,defaultmuted,defaultselected,defer,disabled,draggable,enabled,formnovalidate,hidden,'+ | ||
// (tagname) (html) (javascript) endtag | ||
var CUSTOM_TAG = /^<([\w\-]+)>([^\x00]*[\w\/]>$)?([^\x00]*?)^<\/\1>/gim, | ||
var CUSTOM_TAG = /^<([\w\-]+)>([^\x00]*[\w\-\/]>$)?([^\x00]*?)^<\/\1>/gim, | ||
SCRIPT = /<script(\s+type=['"]?([^>'"]+)['"]?)?>([^\x00]*?)<\/script>/gm, | ||
HTML_COMMENT = /<!--.*?-->/g, | ||
CLOSED_TAG = /<([\w\-]+)([^\/]*)\/\s*>/g, | ||
CLOSED_TAG = /<([\w\-]+)([^>]*)\/\s*>/g, | ||
LINE_COMMENT = /^\s*\/\/.*$/gm, | ||
@@ -743,3 +825,3 @@ JS_COMMENT = /\/\*[^\x00]*?\*\//gm | ||
// foo={ bar } --> foo="{ bar }" | ||
html = html.replace(/=(\{[^\}]+\})([\s\>])/g, '="$1"$2') | ||
html = html.replace(/=(\{[^\}]+\})([\s\/\>])/g, '="$1"$2') | ||
@@ -827,3 +909,3 @@ // IE8 looses boolean attr values: `checked={ expr }` --> `__checked={ expr }` | ||
if (line.slice(0, es6_ident.length + 1) == es6_ident + '}') { | ||
lines[i] += '.bind(this);' | ||
lines[i] = es6_ident + es6_ident + 'this.update()\n' + es6_ident + '}.bind(this);' | ||
es6_ident = '' | ||
@@ -927,3 +1009,2 @@ } | ||
function compileTag(source) { | ||
script.parentNode.removeChild(script) | ||
globalEval(source) | ||
@@ -943,9 +1024,22 @@ | ||
function browserCompile(arg, skip_eval) { | ||
// string -> compile a new tag | ||
riot.compile = function(arg, fn) { | ||
// string | ||
if (typeof arg == 'string') { | ||
var js = unindent(compile(arg)) | ||
if (!skip_eval) globalEval(js) | ||
return js | ||
// compile & return | ||
if (arg.trim()[0] == '<') { | ||
var js = unindent(compile(arg)) | ||
if (!fn) globalEval(js) | ||
return js | ||
// URL | ||
} else { | ||
return GET(arg, function(str) { | ||
var js = unindent(compile(str)) | ||
globalEval(js) | ||
fn && fn(js, str) | ||
}) | ||
} | ||
} | ||
@@ -976,13 +1070,13 @@ | ||
riot.mount = function(a, b) { | ||
browserCompile(function() { mount(a, b) }) | ||
var ret | ||
riot.compile(function() { ret = mount(a, b) }) | ||
return ret | ||
} | ||
riot.mountTo = function(a, b, c) { | ||
browserCompile(function() { mountTo(a, b, c) }) | ||
var ret | ||
riot.compile(function() { ret = mountTo(a, b, c) }) | ||
return ret | ||
} | ||
riot._compile = function(str) { | ||
return browserCompile(str, true) | ||
} | ||
})(!this.top) | ||
@@ -1002,2 +1096,2 @@ | ||
})(); | ||
})(); |
@@ -1,2 +0,2 @@ | ||
/* Riot 2.0.7, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
(function(){var e={version:"v2.0.7"};"use strict";e.observable=function(e){e=e||{};var t={};e.on=function(r,n){if(typeof n=="function"){r.replace(/\S+/g,function(e,r){(t[e]=t[e]||[]).push(n);n.typed=r>0})}return e};e.off=function(r,n){if(r=="*")t={};else if(n){var i=t[r];for(var o=0,u;u=i&&i[o];++o){if(u==n){i.splice(o,1);o--}}}else{r.replace(/\S+/g,function(e){t[e]=[]})}return e};e.one=function(t,r){if(r)r.one=1;return e.on(t,r)};e.trigger=function(r){var n=[].slice.call(arguments,1),i=t[r]||[];for(var o=0,u;u=i[o];++o){if(!u.busy){u.busy=1;u.apply(e,u.typed?[r].concat(n):n);if(u.one){i.splice(o,1);o--}else if(i[o]!==u){o--}u.busy=0}}return e};return e};(function(e,t){if(!this.top)return;var r=location,n=e.observable(),i=u(),o=window;function u(){return r.hash.slice(1)}function a(e){return e.split("/")}function f(e){if(e.type)e=u();if(e!=i){n.trigger.apply(null,["H"].concat(a(e)));i=e}}var c=e.route=function(e){if(e[0]){r.hash=e;f(e)}else{n.on("H",e)}};c.exec=function(e){e.apply(null,a(u()))};c.parser=function(e){a=e};o.addEventListener?o.addEventListener(t,f,false):o.attachEvent("on"+t,f)})(e,"hashchange");e._tmpl=function(){var e={},t=/("|').+?[^\\]\1|\.\w*|\w*:|\b(?:this|true|false|null|undefined|new|typeof|Number|String|Object|Array|Math|Date|JSON)\b|([a-z_]\w*)/gi;return function(t,n){return t&&(e[t]=e[t]||r(t))(n)};function r(e,t){t=(e||"{}").replace(/\\{/g,"").replace(/\\}/g,"").split(/({[\s\S]*?})/);return new Function("d","return "+(!t[0]&&!t[2]?n(t[1]):"["+t.map(function(e,t){return t%2?n(e,1):'"'+e.replace(/\n/g,"\\n").replace(/"/g,'\\"')+'"'}).join(",")+'].join("")').replace(/\uFFF0/g,"{").replace(/\uFFF1/g,"}"))}function n(e,t){e=e.replace(/\n/g," ").replace(/^[{ ]+|[ }]+$|\/\*.+?\*\//g,"");return/^\s*[\w-"']+ *:/.test(e)?"["+e.replace(/\W*([\w-]+)\W*:([^,]+)/g,function(e,r,n){return n.replace(/\w[^,|& ]*/g,function(e){return i(e,t)})+'?"'+r+'":"",'})+'].join(" ")':i(e,t)}function i(e,r){return"(function(v){try{v="+(e.replace(t,function(e,t,r){return r?"d."+r:e})||"x")+"}finally{return "+(r?'!v&&v!==0?"":v':"v")+"}}).call(d)"}}();(function(e,t){if(!t)return;var r=e._tmpl,n=[],i={},o=document;function u(e,t){for(var r=0;r<(e||[]).length;r++){if(t(e[r],r)===false)r--}}function a(e,t){t&&Object.keys(t).map(function(r){e[r]=t[r]});return e}function f(e,t){return e.filter(function(e){return t.indexOf(e)<0})}function c(e,t){e=t(e)===false?e.nextSibling:e.firstChild;while(e){c(e,t);e=e.nextSibling}}function l(e){var t=e.trim().slice(1,3).toLowerCase(),r=/td|th/.test(t)?"tr":t=="tr"?"tbody":"div";el=o.createElement(r);el.innerHTML=e;return el}function s(e,t){t.trigger("update");u(e,function(e){var n=e.tag,i=e.dom;function o(e){i.removeAttribute(e)}if(e.loop){o("each");return m(e,t)}if(n)return n.update?n.update():e.tag=d({tmpl:n[0],fn:n[1],root:i,parent:t});var u=e.attr,a=r(e.expr,t);if(a==null)a="";if(e.value===a)return;e.value=a;if(!u)return i.nodeValue=a;if(!a&&e.bool||/obj|func/.test(typeof a))o(u);if(typeof a=="function"){i[u]=function(e){e=e||window.event;e.which=e.which||e.charCode||e.keyCode;e.target=e.target||e.srcElement;e.currentTarget=i;e.item=t.__item||t;if(a.call(t,e)!==true){e.preventDefault&&e.preventDefault();e.returnValue=false}t.update()}}else if(/^(show|hide|if)$/.test(u)){o(u);if(u=="hide")a=!a;i.style.display=a?"":"none"}else{if(e.bool){i[u]=a;if(!a)return;a=u}i.setAttribute(u,a)}});t.trigger("updated")}function p(e){var t={},r=[];c(e,function(e){var r=e.nodeType,o=e.nodeValue;if(r==3&&e.parentNode.tagName!="STYLE"){n(e,o)}else if(r==1){o=e.getAttribute("each");if(o){n(e,o,{loop:1});return false}var a=i[e.tagName.toLowerCase()];u(e.attributes,function(r){var i=r.name,o=r.value;if(/^(name|id)$/.test(i))t[o]=e;if(!a){var u=i.split("__")[1];n(e,o,{attr:u||i,bool:u});if(u){e.removeAttribute(i);return false}}});if(a)n(e,0,{tag:a})}});return{expr:r,elem:t};function n(e,t,n){if(t?t.indexOf("{")>=0:n){var i={dom:e,expr:t};r.push(a(i,n||{}))}}}function d(t){var i=t.opts||{},f=l(t.tmpl),c=t.root,d=t.parent,m=p(f),v={root:c,opts:i,parent:d,__item:t.item},g={};a(v,m.elem);u(c.attributes,function(e){g[e.name]=e.value});function h(){Object.keys(g).map(function(e){var t=i[e]=r(g[e],d||v);if(typeof t=="object")c.removeAttribute(e)})}h();if(!v.on){e.observable(v);delete v.off}if(t.fn)t.fn.call(v,i);v.update=function(e,t){if(d&&f&&!f.firstChild){c=d.root;f=null}if(t||o.body.contains(c)){a(v,e);a(v,v.__item);h();s(m.expr,v);!t&&v.__item&&d.update();return true}else{v.trigger("unmount")}};v.update(0,true);while(f.firstChild){if(t.before)c.insertBefore(f.firstChild,t.before);else c.appendChild(f.firstChild)}v.trigger("mount");n.push(v);return v}function m(e,t){if(e.done)return;e.done=true;var n=e.dom,i=n.previousSibling,o=n.parentNode,u=n.outerHTML,a=e.expr,c=a.split(/\s+in\s+/),l=[],s,p;if(c[1]){a="{ "+c[1];p=c[0].slice(1).trim().split(/,\s*/)}t.one("mount",function(){var e=n.parentNode;if(e){o=e;o.removeChild(n)}});function m(){return Array.prototype.indexOf.call(o.childNodes,i)+1}t.on("updated",function(){var e=r(a,t);is_array=Array.isArray(e);if(is_array)e=e.slice(0);else{if(!e)return;var n=JSON.stringify(e);if(n==s)return;s=n;e=Object.keys(e).map(function(t,r){var n={};n[p[0]]=t;n[p[1]]=e[t];return n})}f(l,e).map(function(e){var t=l.indexOf(e);o.removeChild(o.childNodes[m()+t]);l.splice(t,1)});f(e,l).map(function(r,n){var i=e.indexOf(r);if(p&&!s){var a={};a[p[0]]=r;a[p[1]]=i;r=a}var f=d({before:o.childNodes[m()+i],parent:t,tmpl:u,item:r,root:o});t.on("update",function(){f.update(0,true)})});l=e})}e.tag=function(e,t,r){r=r||noop,i[e]=[t,r]};e.mountTo=function(e,t,r){var n=i[t];return n&&d({tmpl:n[0],fn:n[1],root:e,opts:r})};e.mount=function(t,r){if(t=="*")t=Object.keys(i).join(", ");var n=[];u(o.querySelectorAll(t),function(t){if(t.riot)return;var i=t.tagName.toLowerCase(),o=e.mountTo(t,i,r);if(o){n.push(o);t.riot=1}});return n};e.update=function(){return n=n.filter(function(e){return!!e.update()})}})(e,this.top)(function(t){var r=("allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,default,"+"defaultchecked,defaultmuted,defaultselected,defer,disabled,draggable,enabled,formnovalidate,hidden,"+"indeterminate,inert,ismap,itemscope,loop,multiple,muted,nohref,noresize,noshade,novalidate,nowrap,open,"+"pauseonexit,readonly,required,reversed,scoped,seamless,selected,sortable,spellcheck,translate,truespeed,"+"typemustmatch,visible").split(",");var n="area,base,br,col,command,embed,hr,img,input,keygen,link,meta,param,source,track,wbr".split(",");var i={jade:h};var o={coffeescript:d,none:g,cs:d,es6:m,typescript:v};var u=/^<([\w\-]+)>([^\x00]*[\w\/]>$)?([^\x00]*?)^<\/\1>/gim,a=/<script(\s+type=['"]?([^>'"]+)['"]?)?>([^\x00]*?)<\/script>/gm,f=/<!--.*?-->/g,c=/<([\w\-]+)([^\/]*)\/\s*>/g,l=/^\s*\/\/.*$/gm,s=/\/\*[^\x00]*?\*\//gm;function p(e,t,i){e=e.replace(/\s+/g," ");e=e.trim().replace(f,"");e=e.replace(/=(\{[^\}]+\})([\s\>])/g,'="$1"$2');e=e.replace(/([\w\-]+)=["'](\{[^\}]+\})["']/g,function(e,t,n){if(r.indexOf(t.toLowerCase())>=0)t="__"+t;return t+'="'+n+'"'});if(t.expr){e=e.replace(/\{\s*([^\}]+)\s*\}/g,function(e,r){return"{"+b(r,t,i).trim()+"}"})}e=e.replace(c,function(e,t,r){var i="<"+t+(r?" "+r.trim():"")+">";if(n.indexOf(t.toLowerCase())==-1)i+="</"+t+">";return i});e=e.replace(/'/g,"\\'");e=e.replace(/\\[{}]/g,"\\$&");if(t.compact)e=e.replace(/> </g,"><");return e}function d(e){return require("coffee-script").compile(e,{bare:true})}function m(e){return require("6to5").transform(e).code}function v(e){return require("typescript-simple")(e)}function g(e){return e}function h(e){return require("jade").render(e,{pretty:true})}function y(e){e=e.replace(l,"").replace(s,"");var t=e.split("\n"),r="";t.forEach(function(e,n){var i=e.trim();if(i[0]!="}"&&i.indexOf("(")>0&&i.slice(-1)=="{"&&i.indexOf("function")==-1){var o=/(\s+)([\w]+)\s*\(([\w,\s]*)\)\s*\{/.exec(e);if(o&&!/^(if|while|switch|for)$/.test(o[2])){t[n]=o[1]+"this."+o[2]+" = function("+o[3]+") {";r=o[1]}}if(e.slice(0,r.length+1)==r+"}"){t[n]+=".bind(this);";r=""}});return t.join("\n")}function b(e,t,r){var n=t.parser||(r?o[r]:y);if(!n)throw new Error('Parser not found "'+r+'"');return n(e,t)}function w(e,t){var r=i[e];if(!r)throw new Error('Template parser not found "'+e+'"');return r(t)}function x(e,t){t=t||{};if(t.template)e=w(t.template,e);return e.replace(u,function(e,r,n,i){n=n||"";var o=t.type;if(!i.trim()){n=n.replace(a,function(e,t,r,n){if(r)o=r.replace("text/","");i=n;return""})}return"riot.tag('"+r+"', '"+p(n,t,o)+"', function(opts) {"+b(i,t,o)+"\n});"})}if(t){return module.exports={html:p,compile:x}}var C=document,_,O;function j(e,t){var r=new XMLHttpRequest;r.onreadystatechange=function(){if(r.readyState==4&&r.status==200)t(r.responseText)};r.open("GET",e,true);r.send("")}function E(e){var t=/[ \t]+/.exec(e);if(t)e=e.replace(new RegExp("^"+t[0],"gm"),"");return e}function N(e){var t=C.createElement("script"),r=C.documentElement;t.text=x(e);r.appendChild(t);r.removeChild(t)}function S(e){var t=C.querySelectorAll('script[type="riot/tag"]');[].map.call(t,function(r,n){var i=r.getAttribute("src");function o(i){r.parentNode.removeChild(r);N(i);if(n+1==t.length){_.trigger("ready");O=true;e&&e()}}return i?j(i,o):o(E(r.innerHTML))})}function T(t,r){if(typeof t=="string"){var n=E(x(t));if(!r)N(n);return n}if(typeof t!="function")t=undefined;if(O)return t&&t();if(_){t&&_.on("ready",t)}else{_=e.observable();S(t)}}var A=e.mount,L=e.mountTo;e.mount=function(e,t){T(function(){A(e,t)})};e.mountTo=function(e,t,r){T(function(){L(e,t,r)})};e._compile=function(e){return T(e,true)}})(!this.top);if(typeof exports==="object")module.exports=e;else if(typeof define==="function"&&define.amd)define(function(){return e});else this.riot=e})(); | ||
/* Riot v2.0.8, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
(function(){var e={version:"v2.0.8",settings:{}};"use strict";e.observable=function(e){e=e||{};var t={};e.on=function(n,r){if(typeof r=="function"){n.replace(/\S+/g,function(e,n){(t[e]=t[e]||[]).push(r);r.typed=n>0})}return e};e.off=function(n,r){if(n=="*")t={};else if(r){var i=t[n];for(var o=0,u;u=i&&i[o];++o){if(u==r){i.splice(o,1);o--}}}else{n.replace(/\S+/g,function(e){t[e]=[]})}return e};e.one=function(t,n){if(n)n.one=1;return e.on(t,n)};e.trigger=function(n){var r=[].slice.call(arguments,1),i=t[n]||[];for(var o=0,u;u=i[o];++o){if(!u.busy){u.busy=1;u.apply(e,u.typed?[n].concat(r):r);if(u.one){i.splice(o,1);o--}else if(i[o]!==u){o--}u.busy=0}}return e};return e};(function(e,t){if(!this.top)return;var n=location,r=e.observable(),i=u(),o=window;function u(){return n.hash.slice(1)}function a(e){return e.split("/")}function f(e){if(e.type)e=u();if(e!=i){r.trigger.apply(null,["H"].concat(a(e)));i=e}}var c=e.route=function(e){if(e[0]){n.hash=e;f(e)}else{r.on("H",e)}};c.exec=function(e){e.apply(null,a(u()))};c.parser=function(e){a=e};o.addEventListener?o.addEventListener(t,f,false):o.attachEvent("on"+t,f)})(e,"hashchange");var t=function(){var t={},n,r,i=/("|').+?[^\\]\1|\.\w*|\w*:|\b(?:(?:new|typeof|in|instanceof) |(?:this|true|false|null|undefined)\b|function *\()|([a-z_]\w*)/gi;return function(i,u){var a=e.settings.brackets||"{ }";if(a!=n){n=a.split(" ");r=f(/({[\s\S]*?})/)}return u?i&&(t[i]=t[i]||o(i))(u):r.test(i)};function o(e,t){t=(e||n.join("")).replace(f(/\\{/),"").replace(f(/\\}/),"").split(r);return new Function("d","return "+(!t[0]&&!t[2]&&!t[3]?u(t[1]):"["+t.map(function(e,t){return t%2?u(e,1):'"'+e.replace(/\n/g,"\\n").replace(/"/g,'\\"')+'"'}).join(",")+'].join("")').replace(/\uFFF0/g,n[0]).replace(/\uFFF1/g,n[1]))}function u(e,t){e=e.replace(/\n/g," ").replace(f(/^[{ ]+|[ }]+$|\/\*.+?\*\//g),"");return/^\s*[\w-"']+ *:/.test(e)?"["+e.replace(/\W*([\w-]+)\W*:([^,]+)/g,function(e,n,r){return r.replace(/\w[^,|& ]*/g,function(e){return a(e,t)})+'?"'+n+'":"",'})+'].join(" ")':a(e,t)}function a(e,t){return"(function(v){try{v="+(e.replace(i,function(e,t,n){return n?"(d."+n+"===undefined?window."+n+":d."+n+")":e})||"x")+"}finally{return "+(t?'!v&&v!==0?"":v':"v")+"}}).call(d)"}function f(e){return RegExp(e.source.split("{").join("\\"+n[0]).split("}").join("\\"+n[1]),e.global?"g":"")}}();function n(e){var t={val:e},n=e.split(/\s+in\s+/);if(n[1]){t.val="{ "+n[1];n=n[0].slice(1).trim().split(/,\s*/);t.key=n[0];t.pos=n[1]}return t}function r(e,r,i){s(e,"each");var o=e.outerHTML,a=e.previousSibling,f=e.parentNode,c=[],l=[],p;i=n(i);r.one("update",function(){f.removeChild(e)}).one("mount",function(){if(!h(f))f=r.root}).on("update",function(){var e=t(i.val,r);if(!e)return;if(!Array.isArray(e)){var n=JSON.stringify(e);if(n==p)return;p=n;l.map(function(e){e.unmount()});l=c=[];e=Object.keys(e).map(function(t,n){var r={};r[i.key]=t;r[i.pos]=e[t];return r})}v(c,e).map(function(e){var t=c.indexOf(e),n=l[t];if(n){n.unmount();c.splice(t,1);l.splice(t,1)}});var s=f.childNodes,d=Array.prototype.indexOf.call(s,a);v(e,c).map(function(t,n){var a=e.indexOf(t);if(!p&&i.key){var c={};c[i.key]=t;c[i.pos]=a;t=c}var m=new u({tmpl:o},{before:s[d+1+a],parent:r,root:f,loop:true,item:t});l.splice(a,0,m)});c=e.slice()})}function i(e,t,n){m(e,function(e){if(e.nodeType!=1)return;l(e.attributes,function(n){if(/^(name|id)$/.test(n.name))t[n.value]=e})})}function o(e,n,i){function o(e,n,r){if(t(n)||r){var o={dom:e,expr:n};i.push(p(o,r||{}))}}m(e,function(e){var t=e.nodeType;if(t==3&&e.parentNode.tagName!="STYLE")o(e,e.nodeValue);if(t!=1)return;var i=e.getAttribute("each");if(i){r(e,n,i);return false}var a=y[e.tagName.toLowerCase()];if(a){a=new u(a,{root:e,parent:n});return false}l(e.attributes,function(t){var n=t.name,r=t.value;var i=n.split("__")[1];o(e,r,{attr:i||n,bool:i});if(i){s(e,n);return false}})})}function u(n,r){var u=e.observable(this),a=[],f={},m=r.parent,v=r.loop,g=r.root,y=r.opts,b=r.item;if(!v&&g.riot)return;g.riot=1;y=y||{};p(this,{parent:m,root:g,opts:y,children:[]});p(this,b);l(g.attributes,function(e){var t=e.name,n=e.value;f[t]=n;if(n.indexOf("{")>=0){s(g,t);return false}});function w(){Object.keys(f).map(function(e){y[e]=t(f[e],m||u)})}w();m&&m.children.push(this);var x=d(n.tmpl),C;i(x,this);this.update=function(e,t){p(u,e);p(u,b);u.trigger("update");w();c(a,u,b);u.trigger("updated")};this.unmount=function(){if(v){g.removeChild(C)}else{var e=g.parentNode;e&&e.removeChild(g)}if(m){var t=m.children;t.splice(t.indexOf(u),1)}u.trigger("unmount");m&&m.off("update",u.update);mounted=false};function j(){while(x.firstChild){if(v){C=x.firstChild;g.insertBefore(x.firstChild,r.before||null)}else{g.appendChild(x.firstChild)}}if(!h(g))u.root=g=m.root;u.trigger("mount");m&&m.on("update",u.update).one("unmount",u.unmount)}if(n.fn)n.fn.call(this,y);o(x,this,a);this.update();j()}function a(e,t,n,r,i){n[e]=function(e){e=e||window.event;e.which=e.which||e.charCode||e.keyCode;e.target=e.target||e.srcElement;e.currentTarget=n;e.item=i;if(t.call(r,e)!==true){e.preventDefault&&e.preventDefault();e.returnValue=false}r.update()}}function f(e,t,n){if(e){e.insertBefore(n,t);e.removeChild(t)}}function c(e,n,r){l(e,function(e){var i=e.dom,o=e.attr,u=t(e.expr,n);if(u==null)u="";if(e.value===u)return;e.value=u;if(!o)return i.nodeValue=u;if(!u&&e.bool||/obj|func/.test(typeof u))s(i,o);if(typeof u=="function"){a(o,u,i,n,r)}else if(o=="if"){s(i,o);var c=e.stub;if(u){c&&f(c.parentNode,c,i)}else{c=e.stub=c||document.createTextNode("");f(i.parentNode,i,c)}}else if(/^(show|hide)$/.test(o)){s(i,o);if(o=="hide")u=!u;i.style.display=u?"":"none"}else{if(e.bool){i[o]=u;if(!u)return;u=o}i.setAttribute(o,u)}})}function l(e,t){for(var n=0;n<(e||[]).length;n++){if(t(e[n],n)===false)n--}}function s(e,t){e.removeAttribute(t)}function p(e,t){t&&Object.keys(t).map(function(n){e[n]=t[n]});return e}function d(e){var t=e.trim().slice(1,3).toLowerCase(),n=/td|th/.test(t)?"tr":t=="tr"?"tbody":"div";el=document.createElement(n);el.stub=true;el.innerHTML=e;return el}function m(e,t){e=t(e)===false?e.nextSibling:e.firstChild;while(e){m(e,t);e=e.nextSibling}}function v(e,t){return e.filter(function(e){return t.indexOf(e)<0})}function h(e){var t=e.parentNode,n=window.HTMLDocument;return t&&!(n&&t instanceof n)}var g=[],y={};e.tag=function(e,t,n){y[e]={name:e,tmpl:t,fn:n}};var b=e.mountTo=function(e,t,n){var r=y[t],i;if(r)i=new u(r,{root:e,opts:n});if(i){g.push(i);return i.on("unmount",function(){g.splice(g.indexOf(i),1)})}};e.mount=function(e,t){if(e=="*")e=Object.keys(y).join(", ");var n=[];l(document.querySelectorAll(e),function(e){var r=e.tagName.toLowerCase(),i=b(e,r,t);if(i)n.push(i)});return n};e.update=function(){g.map(function(e){e.update()});return g};(function(t){var n=("allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,default,"+"defaultchecked,defaultmuted,defaultselected,defer,disabled,draggable,enabled,formnovalidate,hidden,"+"indeterminate,inert,ismap,itemscope,loop,multiple,muted,nohref,noresize,noshade,novalidate,nowrap,open,"+"pauseonexit,readonly,required,reversed,scoped,seamless,selected,sortable,spellcheck,translate,truespeed,"+"typemustmatch,visible").split(",");var r="area,base,br,col,command,embed,hr,img,input,keygen,link,meta,param,source,track,wbr".split(",");var i={jade:g};var o={coffeescript:d,none:h,cs:d,es6:m,typescript:v};var u=/^<([\w\-]+)>([^\x00]*[\w\-\/]>$)?([^\x00]*?)^<\/\1>/gim,a=/<script(\s+type=['"]?([^>'"]+)['"]?)?>([^\x00]*?)<\/script>/gm,f=/<!--.*?-->/g,c=/<([\w\-]+)([^>]*)\/\s*>/g,l=/^\s*\/\/.*$/gm,s=/\/\*[^\x00]*?\*\//gm;function p(e,t,i){e=e.replace(/\s+/g," ");e=e.trim().replace(f,"");e=e.replace(/=(\{[^\}]+\})([\s\/\>])/g,'="$1"$2');e=e.replace(/([\w\-]+)=["'](\{[^\}]+\})["']/g,function(e,t,r){if(n.indexOf(t.toLowerCase())>=0)t="__"+t;return t+'="'+r+'"'});if(t.expr){e=e.replace(/\{\s*([^\}]+)\s*\}/g,function(e,n){return"{"+b(n,t,i).trim()+"}"})}e=e.replace(c,function(e,t,n){var i="<"+t+(n?" "+n.trim():"")+">";if(r.indexOf(t.toLowerCase())==-1)i+="</"+t+">";return i});e=e.replace(/'/g,"\\'");e=e.replace(/\\[{}]/g,"\\$&");if(t.compact)e=e.replace(/> </g,"><");return e}function d(e){return require("coffee-script").compile(e,{bare:true})}function m(e){return require("6to5").transform(e).code}function v(e){return require("typescript-simple")(e)}function h(e){return e}function g(e){return require("jade").render(e,{pretty:true})}function y(e){e=e.replace(l,"").replace(s,"");var t=e.split("\n"),n="";t.forEach(function(e,r){var i=e.trim();if(i[0]!="}"&&i.indexOf("(")>0&&i.slice(-1)=="{"&&i.indexOf("function")==-1){var o=/(\s+)([\w]+)\s*\(([\w,\s]*)\)\s*\{/.exec(e);if(o&&!/^(if|while|switch|for)$/.test(o[2])){t[r]=o[1]+"this."+o[2]+" = function("+o[3]+") {";n=o[1]}}if(e.slice(0,n.length+1)==n+"}"){t[r]=n+n+"this.update()\n"+n+"}.bind(this);";n=""}});return t.join("\n")}function b(e,t,n){var r=t.parser||(n?o[n]:y);if(!r)throw new Error('Parser not found "'+n+'"');return r(e,t)}function w(e,t){var n=i[e];if(!n)throw new Error('Template parser not found "'+e+'"');return n(t)}function x(e,t){t=t||{};if(t.template)e=w(t.template,e);return e.replace(u,function(e,n,r,i){r=r||"";var o=t.type;if(!i.trim()){r=r.replace(a,function(e,t,n,r){if(n)o=n.replace("text/","");i=r;return""})}return"riot.tag('"+n+"', '"+p(r,t,o)+"', function(opts) {"+b(i,t,o)+"\n});"})}if(t){return module.exports={html:p,compile:x}}var C=document,j,k;function O(e,t){var n=new XMLHttpRequest;n.onreadystatechange=function(){if(n.readyState==4&&n.status==200)t(n.responseText)};n.open("GET",e,true);n.send("")}function T(e){var t=/[ \t]+/.exec(e);if(t)e=e.replace(new RegExp("^"+t[0],"gm"),"");return e}function E(e){var t=C.createElement("script"),n=C.documentElement;t.text=x(e);n.appendChild(t);n.removeChild(t)}function L(e){var t=C.querySelectorAll('script[type="riot/tag"]');[].map.call(t,function(n,r){var i=n.getAttribute("src");function o(n){E(n);if(r+1==t.length){j.trigger("ready");k=true;e&&e()}}return i?O(i,o):o(T(n.innerHTML))})}e.compile=function(t,n){if(typeof t=="string"){if(t.trim()[0]=="<"){var r=T(x(t));if(!n)E(r);return r}else{return O(t,function(e){var t=T(x(e));E(t);n&&n(t,e)})}}if(typeof t!="function")t=undefined;if(k)return t&&t();if(j){t&&j.on("ready",t)}else{j=e.observable();L(t)}};var N=e.mount,S=e.mountTo;e.mount=function(t,n){var r;e.compile(function(){r=N(t,n)});return r};e.mountTo=function(t,n,r){var i;e.compile(function(){i=S(t,n,r)});return i}})(!this.top);if(typeof exports==="object")module.exports=e;else if(typeof define==="function"&&define.amd)define(function(){return e});else this.riot=e})(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
104506
21
2580
105