csstree

A tool set for CSS including fast detailed parser, walker, generator and lexer based on W3C specs and browser implementations

MIT License

Downloads
126.8M
Stars
1.9K
Committers
24

Bot releases are visible (Hide)

csstree - 1.0.0-alpha16

Published by lahmatiy over 7 years ago

  • Exposed Parser class
  • Added startOffset option to Tokenizer (constructor and setSource() method)
  • Added fallback functions for default (readSequenceFallback) and selector (readSelectorSequenceFallback) sequence readers
  • Fixed edge cases for AnPlusB
  • Fixed wrong whitespace ignoring in Selector consumer
csstree - 1.0.0-alpha15

Published by lahmatiy over 7 years ago

  • Fixed broken atruleExpression context
  • Fixed vendor prefix detection in keyword() and property()
  • Fixed property() to not lowercase custom property names
  • Added variable boolean flag in property() result
  • Renamed scanner into tokenizer
  • Ranamed syntax into lexer
  • Moved docs/*.html files to csstree/docs repo
  • Added element() function for Value context (-moz-element() supported as well)
  • Merged Universal node type into Type
  • Renamed node types:
    • Id -> IdSelector
    • Class -> ClassSelector
    • Type -> TypeSelector
    • Attribute -> AttributeSelector
    • PseudoClass -> PseudoClassSelector
    • PseudoElement -> PseudoElementSelector
    • Hash -> HexColor
    • Space -> WhiteSpace
    • An+B -> AnPlusB
  • Removed Progid node type
  • Relaxed MediaQuery consumer to not validate syntax on parse and to include whitespaces in children sequence as is
  • Added WhiteSpace.value property to store whitespace sequence
  • Implemented parser options to specify what should be parsed in details (when option is false some part of CSS represents as balanced Raw):
    • parseAtruleExpression – to parse at-rule expressions (true by default)
    • parseSelector – to parse rule's selector (true by default)
    • parseValue – to parse declaration's value (true by default)
    • parseCustomProperty – to parse value and fallback of custom property (false by default)
  • Changed tokenization to stick leading hyphen minus to identifier token
  • Changed selector parsing:
    • Don't convert spaces into descendant combinator
    • Don't validate selector structure on parsing (selectors may be checked by lexer later)
  • Initial refactoring of docs
  • Various improvements and fixes
csstree - 1.0.0-alpha14

Published by lahmatiy over 7 years ago

  • Implemented DeclarationList, MediaQueryList, MediaQuery, MediaFeature and Ratio node types
  • Implemented declarationList context (useful to parse HTML style attribute content)
  • Implemented custom consumers for @import, @media, @page and @supports at-rules
  • Implemented atrule option for parse() config, is used for atruleExpession context to specify custom consumer for at-rule if any
  • Added Scanner#skipWS(), Scanner#eatNonWS(), Scanner#consume() and Scanner#consumeNonWS() helper methods
  • Added custom consumers for known functional-pseudos, consume unknown functional-pseudo content as balanced Raw
  • Allowed any PseudoElement to be a functional-pseudo (#33)
  • Improved walker implementations to reduce GC thrashing by reusing cursors
  • Changed Atrule.block to contain a Block node type only if any
  • Changed Block.loc positions to include curly brackets
  • Changed Atrule.expression to store a null if no expression
  • Changed parser to use StyleSheet node type only for top level node (when context is stylesheet, that's by default)
  • Changed Parentheses, Brackets and Function consumers to use passed sequence reader instead of its own
  • Changed Value and AtruleExpression consumers to use common sequence reader (that reader was used by Value consumer before)
  • Changed default sequence reader to exclude storage of spaces around Comma
  • Changed processing of custom properties:
    • Consume declaration value as balanced Raw
    • Consume var() fallback value as balanced Raw
    • Validate first argument of var() starts with double dash
    • Custom property's value and fallback includes spaces around
  • Fixed Nth to have a loc property
  • Fixed SelectorList.loc and Selector.loc positions to exclude spaces
  • Fixed issue Browserify build fail with default-syntax.json is not found error (#32, @philschatz)
  • Disallowed Type selector starting with dash (parser throws an error in this case now)
  • Disallowed empty selectors for Rule (not sure if it's correct but looks reasonable)
  • Removed >> combinator support until any browser support (no signals about that yet)
  • Removed PseudoElement.legacy property
  • Removed special case for :before, :after, :first-letter and :first-line to represent them as PseudoElement, now those pseudos are represented as PseudoClass nodes
  • Removed deprecated Syntax#match() method
  • Parser was splitted into modules and related changes, one step closer to an extensible parser
  • Various fixes and improvements, all changes have negligible impact on performance

Split parser into modules

The major change in this release is about splitting the parser. The earlier parser version was presented as a single module. Now it's a bunch of modules. It helped to simplify maintenance and readability. Moreover it leads to the parser extensibility, because splitting into modules requires another code structure.

The Parser class was implemented as context for the parsing. It hosts all node type consumers as its own methods and this makes an access from one consumer to another one quite simple. Furthermore it allows redefinition and extension of the consumer set. In addition some known at-rules, functional-pseudos and functions may have their own consumers for their content. Currently CSSTree has several such consumers for complex cases like @supports at-rule or :nth-child() pseudo.

That's the first step towards for parser's extensibility. But we are surely going in this direction. Some things left to be done before exposing Parser class in public API. However, this will happen in the future releases.

The good news is that changes almost didn't affect performance. Yep, some thing got more complicated and lower performance a little bit. But those changes are negligible. So parser became better and still as fast as before last changes.

One more thing

CSSTree was added to AST Explorer. You can explore AST with zero setup now. 🎉

csstree - 1.0.0-alpha13

Published by lahmatiy over 7 years ago

  • Changed location storing in SyntaxMatchError
    • Changed property to store mismatch offset to mismatchOffset
    • Changed offset property to store bad node offset in source CSS if any
    • Added loc property that stores bad node loc if any
csstree - 1.0.0-alpha12

Published by lahmatiy over 7 years ago

  • Fixed Syntax#matchProperty() method to always return a positive result for custom properties since syntax is never defined for them (#31)
  • Implemented fromPlainObject() and toPlainObject() to convert plain object to AST or AST to plain object (currently converts List <-> Array)
csstree - 1.0.0-alpha11

Published by lahmatiy almost 8 years ago

Big refactoring, new syntax support, changes in nth-pseudos format and the new way to store locations.

  • Added support for :matches(<selector-list>) (#28)
  • Added support for :has(<relative-selector-list>)
  • Added support for ::slotted(<compound-selector>)
  • Implemented Brackets node type
  • Implemented basic support for at-rule inside rule block (#24)
  • Renamed Selector node type to SelectorList
  • Renamed SimpleSelector node type to Selector
  • Renamed UnicodeRange.name property to UnicodeRange.value
  • Replaced Negation node type for regular PseudoClass
  • Unified name of node property to store nested nodes, it always children now:
    • StyleSheet.rules -> StyleSheet.children
    • SelectorList.selectors -> SelectorList.children
    • Block.declarations -> Block.children
    • *.sequence -> *.children
  • Fixed edge cases in parsing Hex and UnicodeRange when number not an integer

Changes in parsing of nth- pseudos

  • Implemented An+B node type to represent expressions like 2n + 1 or -3n
  • Fixed edge cases when a or b is not an integer
  • Changed odd and even keywords processing, keywords are storing as Identifier node type now
  • Changed Nth node type format to store a nth-query and an optional selector
  • Implemented of clause for nth- pseudos (a.e. :nth-child(2n + 1 of li, img))
  • Limited Nth parsing rules to :nth-child(), :nth-last-child(), :nth-of-type() and :nth-last-of-type() pseudos
var ast = csstree.parse(':nth-child(2n + 1 of li):nth-child(odd) {}');
console.log(JSON.stringify(ast.children[0].selector.children[0].children[0], null, 4));
// NOTE: loc properties are omitted to reduce output
// {
//     "type": "PseudoClass",
//     "name": "nth-child",
//     "children": [
//         {
//             "type": "Nth",
//             "nth": {
//                 "type": "An+B",
//                 "a": "2",
//                 "b": "1"
//             },
//             "selector": {
//                 "type": "SelectorList",
//                 "children": [
//                     {
//                         "type": "Selector",
//                         "children": [
//                             {
//                                 "type": "Type",
//                                 "name": "li"
//                             }
//                         ]
//                     }
//                 ]
//             }
//         }
//     ]
// }

console.log(JSON.stringify(ast.children[0].selector.children[0].children[1], null, 4));
// NOTE: loc properties are omitted to reduce output
// {
//     "type": "PseudoClass",
//     "name": "nth-child",
//     "children": [
//         {
//             "type": "Nth",
//             "nth": {
//                 "type": "Identifier",
//                 "name": "odd"
//             },
//             "selector": null
//         }
//     ]
// }

Changes in the way to store locations

  • Renamed info node property to loc
  • Changed format of loc to store start and end positions
var ast = csstree.parse('.foo {}', { positions: true });
console.log(JSON.stringify(ast, null, 4));
// {
//    "type": "StyleSheet",
//    "loc": {
//        "source": "<unknown>",
//        "start": {
//            "offset": 0,
//            "line": 1,
//            "column": 1
//        },
//        "end": {
//            "offset": 7,
//            "line": 1,
//            "column": 8
//        }
//    }
//    "children": [ ... ]
// }
csstree - 1.0.0-alpha10

Published by lahmatiy almost 8 years ago

  • Reworked Scanner to be a single point to its functionality
  • Exposed Scanner class to be useful for external projects
  • Changed walk() function behaviour to traverse AST nodes in natural order
  • Implemented walkUp() function to traverse AST nodes from deepest to parent (behaves as walk() before)
csstree - 1.0.0-alpha9

Published by lahmatiy almost 8 years ago

  • Fixed <angle> generic according to specs that allow a <number> equals to zero to be used as valid value (#30)
csstree - 1.0.0-alpha8

Published by lahmatiy almost 8 years ago

  • Fixed Scanner#skip() issue method when cursor is moving to the end of source
  • Simplified Progid node
  • Changed behaviour for bad selector processing, now parsing fails instead of selector ignoring
  • Fixed <id-selector> generic syntax
  • Added q unit for <length> generic syntax
  • Refactored syntax parser (performance)
  • Reduced startup time by implementing lazy syntax parsing (default syntax doesn't parse on module load)
  • Updated syntax dictionaries and used mdn/data instead of Template:CSSData
  • Renamed syntax.stringify() method to syntax.translate()
  • Simplified generic syntax functions, those functions receive a single AST node for checking and should return true or false
  • Added exception for values that contains var(), those values are always valid for now
  • Added more tests and increase code coverage to 98.5%
csstree - 1.0.0-alpha7

Published by lahmatiy about 8 years ago

  • Added support for explicit descendant combinator (>>)
  • Implemented Type and Universal type nodes to replace Identifier in selectors
  • Improved Number parsing by including sign and exponent (#26)
  • Parse before, after, first-letter and first-line pseudos with single colon as PseudoElement
  • Changed FunctionalPseudo node type to PseudoClass
  • Fixed attribute selector name parsing (namespace edge cases)
  • Fixed location calculation for specified offset when eof is reached
  • Added more non-standard colors (#25)
  • Removed obsolete Syntax#getAll() method
  • Fixed various edge cases, code clean up and performance improvements
csstree - 1.0.0 (alpha6)

Published by lahmatiy about 8 years ago

  • More accurate positions for syntax mismatch errors
  • Added apple specific font keywords (#20)
  • Changed Property node structure from object to string
  • Renamed Ruleset node type to Rule
  • Removed Argument node type
  • Fixed Dimension and Percentage position computation
  • Fixed bad selector parsing (temporary solution)
  • Fixed location computation for CSS with very long lines that may lead to really long parsing with positions:true (even freeze)
  • Fixed line and column computation for SyntaxMatch error
  • Improved performance of parsing and translation. Now CSSTree is under 10ms in PostCSS benchmark.

image