Git Product home page Git Product logo

weex-styler's Introduction

Weex <style> Transformer

NPM version Build status Downloads

Features

  • convert a <style> element to JSON format
  • autofix common mistakes
  • friendly warnings

API

  • parse(code, done)
  • validate(json, done)
  • validateItem(name, value)

util api

  • util.hyphenedToCamelCase(value)
  • util.camelCaseToHyphened(value)
/**
 * Parse `<style>` code to a JSON Object and log errors & warnings
 * 
 * @param {string} code
 * @param {function} done
 */
function parse(code, done) {}

/**
 * Validate a JSON Object and log errors & warnings
 * 
 * @param {object} json
 * @param {function} done
 */
function validate(json, done) {}

/**
 * Result callback
 *
 * data
 * - jsonStyle{}: `classname.propname.value`-like object
 * - log[{line, column, reason}]
 * 
 * @param {Error} err
 * @param {object} data
 */
function done(err, data) {}

/**
 * Validate a single name-value pair
 * 
 * @param  {string} name  camel cased
 * @param  {string} value
 * @return {object}
 * - value
 * - log{reason}
 */
function validateItem(name, value) {}

Validation

  • rule check: only common rule type supported, othres will be ignored
  • selector check: only single-classname selector is supported, others will be ignored
  • prop name check: out-of-defined prop name will be warned but preserved
  • prop value check: common prop value mistakes will be autofixed or ignored
    • color type: keywords, #xxx -> warning: #xxxxxx
    • color type: transparent -> error: not supported
    • length type: 100px -> warning: 100

Demo

var styler = require('weex-styler')

var code = 'html {color: #000000;} .foo {color: red; -webkit-transform: rotate(90deg); width: 200px;}'

styler.parse(code, function (err, data) {
  // syntax error
  // format: {line, column, reason, ...}
  err
  // result
  // {foo: {color: '#ff0000', webkitTransform: 'rotate(90deg)', width: 200}}
  data.jsonStyle
  // format: {line, column, reason}
  // - Error: Selector `html` is not supported. Weex only support single-classname selector
  // - Warning: prop value `red` is autofixed to `#ff0000`
  // - Warning: prop name `-webkit-transform` is not supported
  // - Warning: prop value `200px` is autofixed to `200`
  data.log[]
})

var jsonStyle = {
  foo: {
    color: 'red',
    webkitTransform: 'rotate(90deg)',
    width: '200px'
  }
}

styler.validate(json, function (err, data) {
  // syntax error
  err
  // result
  // {foo: {color: '#ff0000', webkitTransform: 'rotate(90deg)', width: 200}}
  data.jsonStyle
  // format: {reason}
  // - Warning: prop value `red` is autofixed to `#ff0000`
  // - Warning: prop name `-webkit-transform` is not supported
  // - Warning: prop value `200px` is autofixed to `200`
  data.log[]
})

weex-styler's People

Contributors

songsiqi avatar jinjiang avatar hanks10100 avatar neeeo avatar

Watchers

James Cloos avatar Jan Salwowski avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.