weex-styler
Advanced tools
Comparing version 0.0.17 to 0.1.0
63
index.js
@@ -14,13 +14,3 @@ 'use strict' | ||
// ast: syntax error | ||
// ast: rule type | ||
// ast: selector format | ||
// content: prop name | ||
// content: prop value | ||
var SUPPORTED_AST_TYPE = ['stylesheet', 'rule', 'declaration'] | ||
var IGNORED_AST_TYPE = ['comment', 'charset'] | ||
var UNSUPPORTED_AST_TYPE = ['custom', 'document', 'font', 'host', 'import', 'keyframes', 'keyframe', 'media', 'namespace', 'page', 'supports'] | ||
/** | ||
@@ -65,4 +55,2 @@ * Parse `<style>` code to a JSON Object and log errors & warnings | ||
if (subType !== 'declaration') { | ||
// catch unsupported rules | ||
// console.log('sub type', subType) | ||
return | ||
@@ -90,4 +78,17 @@ } | ||
rule.selectors.forEach(function (selector) { | ||
if (selector.match(/^\.[A-Za-z0-9_\-]+$/)) { | ||
if (selector.match(/^\.[A-Za-z0-9_\-:]+$/)) { | ||
var className = selector.substr(1) | ||
// handle pseudo class | ||
var pseudoIndex = className.indexOf(':') | ||
if (pseudoIndex > -1) { | ||
var pseudoCls = className.slice(pseudoIndex) | ||
className = className.slice(0, pseudoIndex) | ||
var pseudoRuleResult = {} | ||
Object.keys(ruleResult).forEach(function (prop) { | ||
pseudoRuleResult[prop + pseudoCls] = ruleResult[prop] | ||
}) | ||
ruleResult = pseudoRuleResult | ||
} | ||
if (!jsonStyle[className]) { | ||
@@ -111,22 +112,24 @@ jsonStyle[className] = ruleResult | ||
} | ||
/* istanbul ignore else */ | ||
else if (IGNORED_AST_TYPE.indexOf(type) >= 0) { | ||
// catch unsupported rules | ||
// console.log('ignored', type) | ||
else if (type === 'font-face') { | ||
if (rule.declarations && rule.declarations.length) { | ||
rule.declarations.forEach(function (declaration) { | ||
/* istanbul ignore if */ | ||
if (declaration.type !== 'declaration') { | ||
return | ||
} | ||
var name = util.hyphenedToCamelCase(declaration.property) | ||
var value = declaration.value | ||
if (name === 'fontFamily' && '\"\''.indexOf(value[0]) > -1) { // FIXME: delete leading and trailing quotes | ||
value = value.slice(1, value.length - 1) | ||
} | ||
ruleResult[name] = value | ||
}) | ||
if (!jsonStyle['@FONT-FACE']) { | ||
jsonStyle['@FONT-FACE'] = [] | ||
} | ||
jsonStyle['@FONT-FACE'].push(ruleResult) | ||
} | ||
} | ||
/* istanbul ignore else */ | ||
else if (UNSUPPORTED_AST_TYPE.indexOf(type) >= 0) { | ||
// catch unsupported rules | ||
// console.log('unsupported', type) | ||
} | ||
else { | ||
// catch unsupported rules | ||
// console.log('unknown', type || 'rule') | ||
} | ||
}) | ||
} | ||
else { | ||
// catch unsupported rules | ||
// console.log(ast) | ||
} | ||
done(err, {jsonStyle: jsonStyle, log: log}) | ||
@@ -133,0 +136,0 @@ } |
@@ -174,2 +174,5 @@ var util = require('./util') | ||
var LENGTH_REGEXP = /^[-+]?[0-9]*\.?[0-9]+(.*)$/ | ||
var SUPPORT_CSS_UNIT = ['pt'] | ||
/** | ||
@@ -187,12 +190,18 @@ * the values above is valid | ||
v = (v || '').toString() | ||
var match = v.match(LENGTH_REGEXP) | ||
if (v.match(/^[-+]?[0-9]*\.?[0-9]+$/)) { | ||
return {value: parseFloat(v)} | ||
} | ||
if (v.match(/^[-+]?[0-9]*\.?[0-9]+px$/)) { | ||
return { | ||
value: parseFloat(v), | ||
reason: function reason(k, v, result) { | ||
return 'NOTE: property value `' + v + '` is autofixed to `' + result + '`' | ||
if (match) { | ||
var unit = match[1] | ||
if (!unit) { | ||
return {value: parseFloat(v)} | ||
} | ||
else if (SUPPORT_CSS_UNIT.indexOf(unit) > -1) { | ||
return {value: v} | ||
} | ||
else { | ||
return { | ||
value: parseFloat(v), | ||
reason: function reason(k, v, result) { | ||
return 'NOTE: unit `' + unit + '` is not supported and property value `' + v + '` is autofixed to `' + result + '`' | ||
} | ||
} | ||
@@ -287,4 +296,5 @@ } | ||
v = (v || '').toString() | ||
var match = v.match(LENGTH_REGEXP) | ||
if (v.match(/^[-+]?[0-9]*\.?[0-9]+$/)) { | ||
if (match && !match[1]) { | ||
return {value: parseFloat(v)} | ||
@@ -466,4 +476,7 @@ } | ||
// ensure number type, no `px` | ||
if (typeof value !== 'function' && value.match(/^[-+]?[0-9]*\.?[0-9]+(px)?$/)) { | ||
value = parseFloat(value) | ||
if (typeof value !== 'function') { | ||
var match = value.match(LENGTH_REGEXP) | ||
if (match && (!match[1] || SUPPORT_CSS_UNIT.indexOf(match[1]) === -1)) { | ||
value = parseFloat(value) | ||
} | ||
} | ||
@@ -470,0 +483,0 @@ result = {value: value} |
{ | ||
"name": "weex-styler", | ||
"version": "0.0.17", | ||
"version": "0.1.0", | ||
"description": "Weex <style> transformer", | ||
@@ -12,3 +12,3 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "https://github.com/alibaba/weex_transformer.git" | ||
"url": "https://github.com/weexteam/weex-styler.git" | ||
}, | ||
@@ -19,3 +19,3 @@ "keywords": [ | ||
"author": "songsiqi <songsiqi2006@126.com>", | ||
"license": "Apache", | ||
"license": "GPL-3.0", | ||
"dependencies": { | ||
@@ -22,0 +22,0 @@ "css": "~2.2.1" |
@@ -12,7 +12,7 @@ var chai = require('chai') | ||
it('parse normal style code', function (done) { | ||
var code = 'html {color: #000000;}\n\n.foo {color: red; background-color: rgba(255,255,255,0.6); -webkit-transform: rotate(90deg); width: 200px; left: 0; right: 0px}' | ||
var code = 'html {color: #000000;}\n\n.foo {color: red; background-color: rgba(255,255,255,0.6); -webkit-transform: rotate(90deg); width: 200px; left: 0; right: 0px; borderWidth: 1pt;}' | ||
styler.parse(code, function (err, data) { | ||
expect(err).is.undefined | ||
expect(data).is.an.object | ||
expect(data.jsonStyle).eql({foo: {color: '#FF0000', backgroundColor: 'rgba(255,255,255,0.6)', WebkitTransform: 'rotate(90deg)', width: 200, left: 0, right: 0}}) | ||
expect(data.jsonStyle).eql({foo: {color: '#FF0000', backgroundColor: 'rgba(255,255,255,0.6)', WebkitTransform: 'rotate(90deg)', width: 200, left: 0, right: 0, borderWidth: '1pt'}}) | ||
expect(data.log).eql([ | ||
@@ -22,4 +22,4 @@ {line: 1, column: 1, reason: 'ERROR: Selector `html` is not supported. Weex only support single-classname selector'}, | ||
{line: 3, column: 60, reason: 'WARNING: `-webkit-transform` is not a standard property name'}, | ||
{line: 3, column: 94, reason: 'NOTE: property value `200px` is autofixed to `200`'}, | ||
{line: 3, column: 117, reason: 'NOTE: property value `0px` is autofixed to `0`'} | ||
{line: 3, column: 94, reason: 'NOTE: unit `px` is not supported and property value `200px` is autofixed to `200`'}, | ||
{line: 3, column: 117, reason: 'NOTE: unit `px` is not supported and property value `0px` is autofixed to `0`'} | ||
]) | ||
@@ -64,2 +64,32 @@ done() | ||
it('handle pseudo class', function (done) { | ||
var code = '.class-a {color: #0000ff;} .class-a:last-child:focus {color: #ff0000;}' | ||
styler.parse(code, function (err, data) { | ||
expect(err).is.undefined | ||
expect(data).is.an.object | ||
expect(data.jsonStyle).eql({ | ||
'class-a': { | ||
color: '#0000ff', | ||
'color:last-child:focus': '#ff0000' | ||
} | ||
}) | ||
done() | ||
}) | ||
}) | ||
it('handle iconfont', function (done) { | ||
var code = '@font-face {font-family: "font-family-name-1"; src: url("font file url 1-1") format("truetype");} @font-face {font-family: "font-family-name-2"; src: url("font file url 2-1") format("truetype"), url("font file url 2-2") format("woff");}' | ||
styler.parse(code, function (err, data) { | ||
expect(err).is.undefined | ||
expect(data).is.an.object | ||
expect(data.jsonStyle).eql({ | ||
'@FONT-FACE': [ | ||
{fontFamily: 'font-family-name-1', src: 'url("font file url 1-1") format("truetype")'}, | ||
{fontFamily: 'font-family-name-2', src: 'url("font file url 2-1") format("truetype"), url("font file url 2-2") format("woff")'} | ||
] | ||
}) | ||
done() | ||
}) | ||
}) | ||
it('handle syntax error', function (done) { | ||
@@ -66,0 +96,0 @@ var code = 'asdf' |
@@ -34,3 +34,4 @@ var chai = require('chai') | ||
paddingLeft: '300', | ||
margin: '10.5px', | ||
margin: '10.5em', | ||
borderWidth: '1pt', | ||
left: '0', | ||
@@ -48,2 +49,3 @@ right: '0px', | ||
margin: 10.5, | ||
borderWidth: '1pt', | ||
left: 0, | ||
@@ -53,5 +55,5 @@ right: 0 | ||
expect(data.log).eql([ | ||
{reason: 'NOTE: property value `200px` is autofixed to `200`'}, | ||
{reason: 'NOTE: property value `10.5px` is autofixed to `10.5`'}, | ||
{reason: 'NOTE: property value `0px` is autofixed to `0`'}, | ||
{reason: 'NOTE: unit `px` is not supported and property value `200px` is autofixed to `200`'}, | ||
{reason: 'NOTE: unit `em` is not supported and property value `10.5em` is autofixed to `10.5`'}, | ||
{reason: 'NOTE: unit `px` is not supported and property value `0px` is autofixed to `0`'}, | ||
{reason: 'ERROR: property value `asdf` is not supported for `margin-right` (only number and pixel values are supported)'} | ||
@@ -253,2 +255,4 @@ ]) | ||
abc: '123', | ||
def: '456px', | ||
ghi: '789pt', | ||
AbcDef: '456', | ||
@@ -264,2 +268,4 @@ abcDef: 'abc' | ||
abc: 123, | ||
def: 456, | ||
ghi: '789pt', | ||
AbcDef: 456, | ||
@@ -271,2 +277,4 @@ abcDef: 'abc' | ||
{reason: 'WARNING: `abc` is not a standard property name'}, | ||
{reason: 'WARNING: `def` is not a standard property name'}, | ||
{reason: 'WARNING: `ghi` is not a standard property name'}, | ||
{reason: 'WARNING: `-abc-def` is not a standard property name'}, | ||
@@ -294,3 +302,3 @@ {reason: 'WARNING: `abc-def` is not a standard property name'} | ||
{reason: 'WARNING: `-webkit-transform` is not a standard property name'}, | ||
{reason: 'NOTE: property value `200px` is autofixed to `200`'} | ||
{reason: 'NOTE: unit `px` is not supported and property value `200px` is autofixed to `200`'} | ||
]) | ||
@@ -297,0 +305,0 @@ done() |
Sorry, the diff of this file is not supported yet
Copyleft License
License(Experimental) Copyleft license information was found
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found
Found 1 instance in 1 package
71660
13
1069
4
70