Comparing version 0.0.10 to 0.0.11
@@ -720,2 +720,9 @@ const path = require('path'); | ||
} | ||
else if (attr.name === 'json') { | ||
// json encode the value, can be decoded client side using $elJson(el) | ||
attributes.push({ | ||
name: 'data--j', | ||
value: `$jsUrl(${attr.value})`, | ||
}); | ||
} | ||
else { | ||
@@ -722,0 +729,0 @@ let value = attr.value; |
205
global.js
@@ -40,24 +40,19 @@ function forEach(item, fn) { | ||
function $selectByClass(cls) { | ||
function $selectAllByClass(cls) { | ||
return document.getElementsByClassName(cls); | ||
} | ||
function $event(event, selector, func) { | ||
const list = typeof selector === 'string' ? $select(selector) : selector; | ||
forEach(list, (node) => { | ||
node.addEventListener(event, function (e) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
func.call(this, e); | ||
}); | ||
}); | ||
function $selectByClass(cls) { | ||
return $selectAllByClass(cls)[0]; | ||
} | ||
function $click(selector, func) { | ||
$event('click', selector, func); | ||
function _toArr(refs) { | ||
if (typeof refs === 'string') { | ||
return $select(refs); | ||
} | ||
return Array.isArray(refs) ? refs : [refs]; | ||
} | ||
function $action(refs, func) { | ||
refs = Array.isArray(refs) ? refs : [refs]; | ||
forEach(refs, (ref) => { | ||
forEach(_toArr(refs), (ref) => { | ||
if (!ref) return; | ||
@@ -69,2 +64,23 @@ if (!ref.length) func(ref); | ||
function $first(refs) { | ||
return _toArr(refs)[0]; | ||
} | ||
function $event(event, selector, func) { | ||
const passive = ['scroll', 'touchmove', 'touchstart', 'touchend'].includes(event); | ||
$action(selector, (el) => { | ||
el.addEventListener(event, function (e) { | ||
if (!passive) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
} | ||
func.call(this, e); | ||
}); | ||
}); | ||
} | ||
function $click(selector, func) { | ||
$event('click', selector, func); | ||
} | ||
function $addClass(refs, cls) { | ||
@@ -112,1 +128,162 @@ $action(refs, (el) => { | ||
} | ||
/** JSURL Decode */ | ||
(function(exports) { | ||
'use strict' | ||
var stringRE = /^[a-zA-Z]/ | ||
var numRE = /^[\d-]/ | ||
var dict = { | ||
T: true, | ||
F: false, | ||
N: null, | ||
U: undefined, | ||
n: NaN, | ||
I: Infinity, | ||
J: -Infinity, | ||
} | ||
var fromEscape = { | ||
'*': '*', | ||
_: '_', | ||
'-': '~', | ||
S: '$', | ||
P: '+', | ||
'"': "'", | ||
C: '(', // not necessary but we keep it for symmetry | ||
D: ')', | ||
L: '<', | ||
G: '>', // not necessary but we keep it for symmetry | ||
'.': '%', | ||
Q: '?', | ||
H: '#', | ||
A: '&', | ||
E: '=', | ||
B: '\\', | ||
N: '\n', | ||
R: '\r', | ||
U: '\u2028', | ||
Z: '\0', | ||
} | ||
function origChar(s) { | ||
if (s === '_') { | ||
return ' ' | ||
} | ||
var c = fromEscape[s.charAt(1)] | ||
if (!c) { | ||
throw new Error('Illegal escape code', s) | ||
} | ||
return c | ||
} | ||
var escapeRE = /(_|\*.)/g | ||
function unescape(s) { | ||
// oddly enough, testing first is faster | ||
return escapeRE.test(s) ? s.replace(escapeRE, origChar) : s | ||
} | ||
function eat(a) { | ||
var j, c | ||
for ( | ||
j = a.i; | ||
j < a.l && ((c = a.s.charAt(j)), c !== '~' && c !== ')'); | ||
j++ | ||
) {} | ||
var w = a.s.slice(a.i, j) | ||
if (c === '~') { | ||
j++ | ||
} | ||
a.i = j | ||
return w | ||
} | ||
function peek(a) { | ||
return a.s.charAt(a.i) | ||
} | ||
function eatOne(a) { | ||
a.i++ | ||
} | ||
var EOS = {} // unique symbol | ||
function decode(a) { | ||
var out, k, t | ||
var c = peek(a) | ||
if (!c) { | ||
return EOS | ||
} | ||
if (c === '(') { | ||
eatOne(a) | ||
out = {} | ||
while (((c = peek(a)), c && c !== ')')) { | ||
k = unescape(eat(a)) | ||
c = peek(a) | ||
if (c && c !== ')') { | ||
t = decode(a) | ||
} else { | ||
t = true | ||
} | ||
out[k] = t | ||
} | ||
if (c === ')') { | ||
eatOne(a) | ||
} | ||
} else if (c === '!') { | ||
eatOne(a) | ||
out = [] | ||
while (((c = peek(a)), c && c !== '~' && c !== ')')) { | ||
out.push(decode(a)) | ||
} | ||
if (c === '~') { | ||
eatOne(a) | ||
} | ||
} else if (c === '_') { | ||
eatOne(a) | ||
k = unescape(eat(a)) | ||
if (k.charAt(0) === 'D') { | ||
out = new Date(k.slice(1)) | ||
} else if (k in dict) { | ||
out = dict[k] | ||
} else { | ||
throw new Error('Unknown dict reference', k) | ||
} | ||
} else if (c === '*') { | ||
eatOne(a) | ||
out = unescape(eat(a)) | ||
} else if (c === '~') { | ||
eatOne(a) | ||
out = true | ||
} else if (numRE.test(c)) { | ||
out = Number(eat(a)) | ||
if (isNaN(out)) { | ||
throw new Error('Not a number', c) | ||
} | ||
} else if (stringRE.test(c)) { | ||
out = unescape(eat(a)) | ||
} else { | ||
throw new Error('Cannot decode part ' + [t].concat(a).join('~')) | ||
} | ||
return out | ||
} | ||
var JSONRE = /^({|\[|"|true$|false$|null$)/ | ||
exports.parse = function(s, options) { | ||
if (JSONRE.test(s)) return JSON.parse(s) | ||
var l = s.length | ||
var r = decode({s: s, i: 0, l: l}) | ||
return r === EOS ? true : r | ||
} | ||
exports.tryParse = function(s, def, options) { | ||
try { | ||
return exports.parse(s, options) | ||
} catch (ex) { | ||
return def | ||
} | ||
} | ||
})((window.JSURL = window.JSURL || {})); | ||
function $jsUrl(data, def) { | ||
return JSURL.tryParse(data, def); | ||
} | ||
function $elJson(ref, attr = 'data--j') { | ||
const el = $first(ref); | ||
if (!el) return; | ||
return $jsUrl(ref.getAttribute(attr)); | ||
} |
{ | ||
"name": "nixy", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "HTML templating framework focused on SSR and performance", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,3 +30,3 @@ const fs = require('fs'); | ||
console.assert(out === '<div><div data-pos="mw_product_3" class="sm-da" cla="height: 31.25vw"></div><div data-pos="mw_product_3" class="sm-da" cla="height: 31.25vw"></div><div class="box"><div class="header"><div class="title"><h3>A Box</h3></div><div class="actions"><a>View All →</a></div></div><a>Hello</a></div><div data-json="(a~b"></div></div>'); | ||
console.assert(out === '<div><div data-pos="mw_product_3" class="sm-da" cla="height: 31.25vw"></div><div data-pos="mw_product_3" class="sm-da" cla="height: 31.25vw"></div><div class="box"><div class="header"><div class="title"><h3>A Box</h3></div><div class="actions"><a>View All →</a></div></div><a>Hello</a></div><div data--j="(a~b"></div></div>'); | ||
@@ -33,0 +33,0 @@ console.time('render'); |
Sorry, the diff of this file is not supported yet
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
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
49531
1718