2017 © Pedro Peláez
 

library css-tokenizer

CSS tokenizer

image

yannickl88/css-tokenizer

CSS tokenizer

  • Friday, December 1, 2017
  • by yannickl88
  • Repository
  • 2 Watchers
  • 3 Stars
  • 6,064 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 11 Versions
  • 31 % Grown

The README.md

CSS Tokenizer

PHP implementation of a CSS tokenizer. This implementation was heavily inspired by the javascript variant that was developed by https://github.com/postcss/postcss. However, the result is slightly different., (*1)

Usage

Usage of the tokenizer is pretty straight forward:, (*2)

<?php
use Yannickl88\Component\CSS\Tokenizer;

$tokens = (new Tokenizer())->tokenize('span { padding: 2px; margin: 2px; }');

This is equivalent to:, (*3)

$tokens = [
    new Token(Token::T_WORD, 'span', 1, 1, 1, 4),
    new Token(Token::T_WHITESPACE, ' ', 1, 5),
    new Token(Token::T_OPENCURLY, '{', 1, 6),
    new Token(Token::T_WHITESPACE, ' ', 1, 7),
    new Token(Token::T_WORD, 'padding', 1, 8, 1, 14),
    new Token(Token::T_COLON, ':', 1, 15),
    new Token(Token::T_WHITESPACE, ' ', 1, 16),
    new Token(Token::T_WORD, '2px', 1, 17, 1, 19),
    new Token(Token::T_SEMICOLON, ';', 1, 20),
    new Token(Token::T_WHITESPACE, ' ', 1, 21),
    new Token(Token::T_WORD, 'margin', 1, 22, 1, 27),
    new Token(Token::T_COLON, ':', 1, 28),
    new Token(Token::T_WHITESPACE, ' ', 1, 29),
    new Token(Token::T_WORD, '2px', 1, 30, 1, 32),
    new Token(Token::T_SEMICOLON, ';', 1, 33),
    new Token(Token::T_WHITESPACE, ' ', 1, 34),
    new Token(Token::T_CLOSECURLY, '}', 1, 35),
];

What you do next is entirely up to you!, (*4)

Token types

If you have a token, you can access some public properties:, (*5)

$token->type; // for the type
$token->chars; // for the characters
$token->lines; // array containing start and (sometimes) end line
$token->offsets; // array containing start and (sometimes) end offset

Note: Only tokens which can span multiple lines will have a second value in the lines and offsets field. These are T_WORD, T_WHITESPACE, T_STRING, T_ADWORD, and T_COMMENT., (*6)

The following types are defined: * T_WHITESPACE, white space characters like , \t, \n, \r and \f. * T_WORD, all words, like color, padding-top or 12px. * T_COLON, colon :. * T_SEMICOLON, semi-colon ;. * T_COMMA, comma ,. * T_TILDE, tilde ~. * T_STRING, everything between ' or " quotes, like "/path/to/my/image.jpg". * T_OPENBRACKET, open bracket (. * T_CLOSEBRACKET, close bracket ). * T_OPENCURLY, open curly bracket {. * T_CLOSECURLY, close curly bracket } * T_OPENSQUARE, open square bracket [. * T_CLOSESQUARE, close square bracket ] * T_ATWORD, all words that start with a @, like @media or @font-face. * T_COMMENT, comments, like /* comment */ or // comment., (*7)

Note: There is also T_BRACKETS, however this is only in case of parse errors that you will see this token., (*8)

Installation

Installing is pretty easy, this package is available on packagist., (*9)

$ composer require yannickl88/css-tokenizer

Or you can add this to your composer.json manually:, (*10)

"require" : {
    "yannickl88/css-tokenizer" : "*@dev-master"
}

Note: You can use dev-master if you want the latest changes, but this is not recommended for production code!, (*11)

The Versions

01/12 2017

dev-master

9999999-dev

CSS tokenizer

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

01/12 2017

1.2.0

1.2.0.0

CSS tokenizer

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

08/10 2017

1.1.1

1.1.1.0

CSS tokenizer

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

05/10 2017

1.1.0

1.1.0.0

CSS tokenizer

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

04/09 2017

1.0.1

1.0.1.0

CSS tokenizer

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

03/08 2017

1.0.0

1.0.0.0

CSS tokenizer

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

03/08 2017

dev-feature/less

dev-feature/less

CSS tokenizer

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

07/05 2015

0.0.4

0.0.4.0

CSS tokenizer

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

07/05 2015

0.0.3

0.0.3.0

CSS tokenizer

  Sources   Download

MIT

The Requires

 

07/05 2015

0.0.2

0.0.2.0

CSS tokenizer

  Sources   Download

MIT

The Requires

 

07/05 2015

0.0.1

0.0.1.0

CSS tokenizer

  Sources   Download

MIT