weex-templater
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -14,10 +14,2 @@ var exp = require('./exp') | ||
events: COMMON_EVENTS, | ||
// attr: { | ||
// value: function (v) { | ||
// return { | ||
// value: v, | ||
// reason: 'NOTE: `value` could be written as text content in <text>' | ||
// } | ||
// } | ||
// }, | ||
textContent: true | ||
@@ -92,4 +84,2 @@ }, | ||
/** | ||
@@ -126,6 +116,31 @@ * tag name checking | ||
} | ||
result.type = tagName | ||
if (tagName === 'component') { | ||
var indexOfIs = -1 | ||
if (node.attrs) { | ||
node.attrs.forEach(function (attr, index) { | ||
if (attr.name === 'is') { | ||
indexOfIs = index | ||
result.type = tagName = exp(attr.value) | ||
} | ||
}) | ||
} | ||
if (indexOfIs > -1) { | ||
node.attrs.splice(indexOfIs, 1) // delete `is` | ||
} | ||
else { | ||
result.type = tagName = 'container' | ||
log.push({ | ||
line: location.line || 1, | ||
column: location.col || 1, | ||
reason: 'WARNING: tag `component` should have an `is` attribute, otherwise it will be regarded as a `container`' | ||
}) | ||
} | ||
} | ||
else { | ||
result.type = tagName | ||
} | ||
// deps | ||
if (deps.indexOf(tagName) < 0) { | ||
if (deps.indexOf(tagName) < 0 && typeof tagName === 'string') { // FIXME: improve `require` to bundle dynamic binding components | ||
deps.push(tagName) | ||
@@ -142,3 +157,3 @@ } | ||
column: location.col || 1, | ||
reason: 'ERROR: tag name `' + tagName + '` should not have children' | ||
reason: 'ERROR: tag `' + tagName + '` should not have children' | ||
}) | ||
@@ -163,3 +178,4 @@ } | ||
result.attr[attr] = TAG_DEFAULT_ATTR_MAP[tagName][attr] | ||
} else { | ||
} | ||
else { | ||
result[attr] = TAG_DEFAULT_ATTR_MAP[tagName][attr] | ||
@@ -204,3 +220,4 @@ } | ||
expStart = index | ||
} else if (expStart !== -1 && subName.indexOf('}}') > -1) { | ||
} | ||
else if (expStart !== -1 && subName.indexOf('}}') > -1) { | ||
expEnd = index | ||
@@ -210,3 +227,4 @@ classList.push(tempClassList.slice(expStart, expEnd + 1).join('')) | ||
expEnd = -1 | ||
} else if ((expStart === -1 && expEnd === -1) || (subName.indexOf('{{') > -1 && subName.indexOf('}}') > -1)) { | ||
} | ||
else if ((expStart === -1 && expEnd === -1) || (subName.indexOf('{{') > -1 && subName.indexOf('}}') > -1)) { | ||
classList.push(subName) | ||
@@ -254,16 +272,14 @@ } | ||
v = exp(v) | ||
if (typeof v !== 'function') { | ||
vResult = styler.validateItem(k, v) | ||
v = vResult.value | ||
if (vResult.log) { | ||
// FIXME: in order to guarantee order of keys of a log item | ||
var ret = {} | ||
ret.line = locationInfo.line | ||
ret.column = locationInfo.column | ||
ret.reason = vResult.log.reason | ||
log.push(ret) | ||
// vResult.log.line = locationInfo.line | ||
// vResult.log.column = locationInfo.column | ||
// log.push(vResult.log) | ||
} | ||
vResult = styler.validateItem(k, v) | ||
v = vResult.value | ||
if (vResult.log) { | ||
// FIXME: in order to guarantee order of keys of a log item | ||
var ret = {} | ||
ret.line = locationInfo.line | ||
ret.column = locationInfo.column | ||
ret.reason = vResult.log.reason | ||
log.push(ret) | ||
// vResult.log.line = locationInfo.line | ||
// vResult.log.column = locationInfo.column | ||
// log.push(vResult.log) | ||
} | ||
@@ -286,3 +302,2 @@ if (typeof v === 'number' || typeof v === 'string' || typeof v === 'function') { | ||
if (!exp.isExpr(value)) { | ||
// output.log | ||
value = '{{' + value + '}}' | ||
@@ -311,9 +326,30 @@ } | ||
function checkRepeat(value, output) { | ||
if (!exp.isExpr(value)) { | ||
// output.log | ||
if (value) { | ||
if (exp.isExpr(value)) { | ||
value = value.substr(2, value.length - 4) | ||
} | ||
var key | ||
var val | ||
var inMatch = value.match(/(.*) (?:in) (.*)/) | ||
if (inMatch) { | ||
var itMatch = inMatch[1].match(/\((.*),(.*)\)/) | ||
if (itMatch) { | ||
key = itMatch[1].trim() | ||
val = itMatch[2].trim() | ||
} | ||
else { | ||
val = inMatch[1].trim() | ||
} | ||
value = inMatch[2] | ||
} | ||
value = '{{' + value + '}}' | ||
var repeat = {expression: exp(value)} | ||
if (key) { | ||
repeat.key = key | ||
} | ||
if (val) { | ||
repeat.value = val | ||
} | ||
output.result.repeat = repeat | ||
} | ||
if (value) { | ||
output.result.repeat = exp(value) | ||
} | ||
} | ||
@@ -340,5 +376,19 @@ | ||
if (exp.isExpr(value)) { | ||
// output.log | ||
value = value.substr(2, value.length - 4) | ||
} | ||
var paramsMatch = value.match(/(.*)\((.*)\)/) | ||
if (paramsMatch) { | ||
var funcName = paramsMatch[1] | ||
var params = paramsMatch[2] | ||
if (params) { | ||
params = params.split(',') | ||
if (params[params.length - 1].trim() !== 'EVENT') { | ||
params[params.length] = 'EVENT' | ||
} | ||
} else { | ||
params = ['EVENT'] | ||
} | ||
value = '{{' + funcName + '(' + params.join(',') + ')}}' | ||
value = eval('(function (EVENT) {' + exp(value, false).replace('this.EVENT', 'EVENT') + '})') | ||
} | ||
output.result.events = output.result.events || {} | ||
@@ -345,0 +395,0 @@ output.result.events[eventName] = value |
{ | ||
"name": "weex-templater", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "Weex <template> transformer", | ||
@@ -18,3 +18,3 @@ "main": "index.js", | ||
"author": "songsiqi <songsiqi2006@126.com>", | ||
"license": "Apache", | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
@@ -21,0 +21,0 @@ "weex-styler": "~0.0.16", |
# Weex `<template>` Transformer | ||
[![NPM version][npm-image]][npm-url] | ||
[![Build status][circle-image]][circle-url] | ||
[![Downloads][downloads-image]][downloads-url] | ||
[npm-image]: https://img.shields.io/npm/v/weex-templater.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/weex-templater | ||
[circle-image]: https://circleci.com/gh/alibaba/weex_toolchain.svg?style=svg | ||
[circle-url]: https://circleci.com/gh/alibaba/weex_toolchain/tree/master | ||
[downloads-image]: https://img.shields.io/npm/dm/weex-templater.svg?style=flat-square | ||
[downloads-url]: https://npmjs.org/package/weex-templater | ||
## Features | ||
@@ -4,0 +15,0 @@ |
@@ -206,3 +206,4 @@ var chai = require('chai') | ||
{line: 1, column: 109, reason: 'WARNING: `x` is not a standard property name'}, | ||
{line: 1, column: 109, reason: 'WARNING: `y` is not a standard property name'} | ||
{line: 1, column: 109, reason: 'WARNING: `y` is not a standard property name'}, | ||
{line: 1, column: 109, reason: 'WARNING: `z` is not a standard property name'} | ||
] | ||
@@ -216,4 +217,4 @@ } | ||
it('parse if/repeat', function (done) { | ||
var code = '<container><text if="a"></text><text if="{{a}}"></text><text if="{{a()}}"></text><text repeat="a"></text></container>' | ||
it('parse if', function (done) { | ||
var code = '<container><text if="a"></text><text if="{{a}}"></text><text if="{{a()}}"></text></container>' | ||
var expected = { | ||
@@ -225,4 +226,3 @@ jsonTemplate: { | ||
{type: 'text', shown: function () {return this.a}}, | ||
{type: 'text', shown: function () {return this.a()}}, | ||
{type: 'text', repeat: function () {return this.a}} | ||
{type: 'text', shown: function () {return this.a()}} | ||
] | ||
@@ -239,2 +239,43 @@ }, | ||
it('parse repeat', function (done) { | ||
var code = '<container><text repeat="a"></text><text repeat="{{a}}"></text><text repeat="{{a()}}"></text></container>' | ||
var expected = { | ||
jsonTemplate: { | ||
type: 'container', | ||
children: [ | ||
{type: 'text', repeat: {expression: function () {return this.a}}}, | ||
{type: 'text', repeat: {expression: function () {return this.a}}}, | ||
{type: 'text', repeat: {expression: function () {return this.a()}}} | ||
] | ||
}, | ||
deps: ['container', 'text'], | ||
log: [] | ||
} | ||
templater.parse(code, function (err, result) { | ||
expect(stringify(result)).eql(stringify(expected)) | ||
done() | ||
}) | ||
}) | ||
it('parse repeat key/value', function (done) { | ||
var code = '<container><text repeat="v in listOrMap"></text><text repeat="{{v in listOrMap}}"></text><text repeat="(k, v) in listOrMap"></text><text repeat="{{(k, v) in listOrMap}}"></text></container>' | ||
var expected = { | ||
jsonTemplate: { | ||
type: 'container', | ||
children: [ | ||
{type: 'text', repeat: {expression: function () {return this.listOrMap}, value: 'v'}}, | ||
{type: 'text', repeat: {expression: function () {return this.listOrMap}, value: 'v'}}, | ||
{type: 'text', repeat: {expression: function () {return this.listOrMap}, key: 'k', value: 'v'}}, | ||
{type: 'text', repeat: {expression: function () {return this.listOrMap}, key: 'k', value: 'v'}} | ||
] | ||
}, | ||
deps: ['container', 'text'], | ||
log: [] | ||
} | ||
templater.parse(code, function (err, result) { | ||
expect(stringify(result)).eql(stringify(expected)) | ||
done() | ||
}) | ||
}) | ||
it('parse else with if', function (done) { | ||
@@ -298,3 +339,3 @@ var code = '<container><text if="a && b"></text><text else></text></container>' | ||
it('parse events', function (done) { | ||
var code = '<container><text onclick="a"></text><text onclick="{{a}}"></text></container>' | ||
var code = '<container><text onclick="a" onappear="{{a()}}"></text><text onclick="{{a}}" onappear="{{a(x, 1, \'2\', EVENT)}}"></text></container>' | ||
var expected = { | ||
@@ -304,4 +345,4 @@ jsonTemplate: { | ||
children: [ | ||
{type: 'text', events: {click: 'a'}}, | ||
{type: 'text', events: {click: 'a'}} | ||
{type: 'text', events: {click: 'a', appear: function (EVENT) {this.a(EVENT)}}}, | ||
{type: 'text', events: {click: 'a', appear: function (EVENT) {this.a(this.x,1,'2',EVENT)}}} | ||
] | ||
@@ -411,5 +452,3 @@ }, | ||
deps: ['container', 'text'], | ||
log: [ | ||
{line: 1, column: 12, reason: 'WARNING: `-webkit-transform` is not a standard property name'} | ||
] | ||
log: [{line: 1, column: 12, reason: 'WARNING: `-webkit-transform` is not a standard property name'}] | ||
} | ||
@@ -453,3 +492,3 @@ templater.parse(code, function (err, result) { | ||
deps: ['container', 'tabheader', 'text'], | ||
log: [{line: 1, column: 13, reason: 'ERROR: tag name `tabheader` should not have children'}] | ||
log: [{line: 1, column: 13, reason: 'ERROR: tag `tabheader` should not have children'}] | ||
} | ||
@@ -461,2 +500,22 @@ templater.parse(code, function (err, result) { | ||
}) | ||
it('parse component', function (done) { | ||
var code = '<container><component></component><component is="foo"></component><component is="{{foo}}"></component></container>' | ||
var expected = { | ||
jsonTemplate: { | ||
type: 'container', | ||
children: [ | ||
{type: 'container'}, | ||
{type: 'foo'}, | ||
{type: function () {return this.foo}} | ||
] | ||
}, | ||
deps: ['container', 'foo'], | ||
log: [{line: 1, column: 12, reason: 'WARNING: tag `component` should have an `is` attribute, otherwise it will be regarded as a `container`'}] | ||
} | ||
templater.parse(code, function (err, result) { | ||
expect(stringify(result)).eql(stringify(expected)) | ||
done() | ||
}) | ||
}) | ||
}) |
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
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
56402
0
1280
146
3