Giter VIP home page Giter VIP logo

css-validator's Introduction

css-validator Build status

Validate CSS via W3C's service

This was created to validate CSS inside of the json2css test suite.

Getting Started

Install the module with: npm install css-validator

var validateCss = require('css-validator');
validateCss({text: 'a { color: blue; }'}, function (err, data) {
  assert.strictEqual(data.validity, true);
  assert.deepEqual(data.errors, []);
  assert.deepEqual(data.warnings, []);
});

Documentation

css-validator returns a single function as its module.exports

validateCss(options, cb)

Validate CSS against W3C's Jigsaw validation service

  • options String|Object - If options is a String, it will be treated as options.text
    • w3cUrl String - URL to validate against. Default is http://jigsaw.w3.org/css-validator/validator
    • The following options from the validator itself
    • uri null|String - URL of document to validate. CSS and HTML documents are allowed
    • text null|String - CSS to validate
    • usermedium String - Medium where the CSS will be used (e.g. screen, print, braille). Default is all.
    • profile String - CSS profile to use for validation. Default is css3.
      • Possible values are css1, css2, css21, css3, svg, svgbasic, svgtiny, mobile, atsc-tv, tv, none
    • lang String - Language to use in response. Default is en
      • Possible values are en, fr, it, ko, ja, es, zh-cn, nl, de, it, pl
    • warning Number|String - Warning level to set. Default is 2
      • If set to no, no warnings will be returned
      • If set to 0, less warnings will be returned
      • If set to 1 or 2, more warnings will be returned
  • cb null|Function - Error first callback with function (err, data) {} signature
    • err null|Error - If there was a connetivity error, this will be it
    • data null|Object - Container for response from jigsaw
      • validity Boolean - If there were no errors, this will be true. Otherwise, it is false.
      • errors Object[] - Array of errors
        • These are dynamically parsed and not guaranteed to exist. The service only guarantees line, level, and message.
        • line Number - Line where error occurred
        • errortype String
        • context String
        • errorsubtype String
        • skippedstring String - Content where error occurred
        • message String - Human readable information about the error and why it occurred
      • warnings Object[] - Array of warnings
        • line Number - Line where error occurred
        • level Number - Intensity of the warning. See options.warning for more info
        • message String - Human readable information about the warning and why it occurred

If cb is not provided, a DuplexStream will be returned to you.

If you have not provided options.uri or options.text, you can .write + .end OR .pipe to the stream CSS to validate.

Additionally, you can use .read and .pipe to get the data returned by cb.

The stream will emit the following events:

  • error Error - Error occurring during connection or parsing of response
  • data Object - Same as data sent to cb. Emitted once.
  • end - Emitted when we have finished parsing the input and outputting events
  • validity Boolean - Event for data.validity with data.validity as its data
  • validation-error Object - Event for a new data.errors object with the error as its argument
  • validation-warning Object - Event for a new data.warnings object with the warning as its argument

Examples

var cssValidate = require('css-validate');
var css = [
  "body {",
  "  background: url(ab'cd');",
  "  -moz-box-sizing: content-box;",
  "}",
].join('\n');

cssValidate(css, function (err, data) {
  console.log(data);
  /*
  { validity: false,
  errors:
   [ { line: '2',
       errortype: 'parse-error',
       context: ' body ',
       errorsubtype: '\n                                exp\n                            ',
       skippedstring: '\n                                url(ab \'cd\')\n                            ',
       message: '\n        \n                                Value Error :  background (nullcolors.html#propdef-background)\n        \n                                url(ab \'cd\') is not a background-color value : \n                            ',
       error: '\n                        ' } ],
  warnings:
   [ { line: '3',
       level: '0',
       message: 'Property -moz-box-sizing is an unknown vendor extension',
       warning: '\n                        ' } ] }
  */
});

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via grunt and test via npm test.

Donating

Support this project and others by twolfson via gittip.

Support via Gittip

Unlicense

As of Nov 27 2013, Todd Wolfson has released this repository and its contents to the public domain.

It has been released under the UNLICENSE.

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.