Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

css-tree

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

css-tree - npm Package Compare versions

Comparing version 1.0.0-alpha15 to 1.0.0-alpha16

docs/readme.md

20

docs/parsing.md

@@ -15,3 +15,3 @@ # Parsing CSS into AST

var ast = csstree.parse('.foo.bar', {
context: 'simpleSelector',
context: 'selector',
positions: true

@@ -23,2 +23,4 @@ });

<!-- MarkdownTOC -->
- [context](#context)

@@ -29,2 +31,3 @@ - [atrule](#atrule)

- [filename](#filename)
- [offset](#offset)
- [line](#line)

@@ -37,2 +40,4 @@ - [column](#column)

<!-- /MarkdownTOC -->
### context

@@ -54,3 +59,3 @@

- `block` – block with curly braces (`{ color: red; border: 1px solid black; }` for rule example)
- `declarationList` – block content w/o curly braces (`color: red; border: 1px solid black;` for rule example), useful to parse HTML `style` attribute value
- `declarationList` – block content w/o curly braces (`color: red; border: 1px solid black;` for rule example), useful for parsing HTML `style` attribute value
- `declaration` – declaration (`color: red` or `border: 1px solid black` for rule example)

@@ -87,2 +92,9 @@ - `value` – declaration value (`red` or `1px solid black` for rule example)

### offset
Type: `number`
Default: `0`
Start offset. Useful when parsing fragment of CSS to store correct positions in node's `loc` property.
### line

@@ -93,3 +105,3 @@

Initial line number. Useful when parse fragment of CSS to store correct positions in node's `loc` property.
Start line number. Useful when parsing fragment of CSS to store correct positions in node's `loc` property.

@@ -101,3 +113,3 @@ ### column

Initial column number. Useful when parse fragment of CSS to store correct positions in node's `loc` property.
Start column number. Useful when parsing fragment of CSS to store correct positions in node's `loc` property.

@@ -104,0 +116,0 @@ ### parseAtruleExpression

4

docs/Tokenizer.md

@@ -28,3 +28,3 @@ # Tokenizer

- [Methods](#methods)
- [setSource\(source, initLine, initColumn\)](#setsourcesource-initline-initcolumn)
- [setSource\(source, startOffset, startLine, startColumn\)](#setsourcesource-startoffset-startline-startcolumn)
- [lookupType\(offset\)](#lookuptypeoffset)

@@ -98,3 +98,3 @@ - [lookupNonWSType\(offset\)](#lookupnonwstypeoffset)

### setSource(source, initLine, initColumn)
### setSource(source, startOffset, startLine, startColumn)

@@ -101,0 +101,0 @@ ### lookupType(offset)

@@ -0,1 +1,9 @@

## 1.0.0-alpha16 (February 12, 2017)
- 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
## 1.0.0-alpha15 (February 8, 2017)

@@ -2,0 +10,0 @@

@@ -9,2 +9,3 @@ 'use strict';

Tokenizer: require('./tokenizer'),
Parser: require('./parser/Parser'),
Lexer: require('./lexer/Lexer'),

@@ -11,0 +12,0 @@

@@ -88,3 +88,3 @@ 'use strict';

'matches': sequence.selectorList,
'has': sequence.relativeSelectorList,
'has': sequence.selectorList,
'nth-child': sequence.nthWithOfClause,

@@ -150,3 +150,6 @@ 'nth-last-child': sequence.nthWithOfClause,

readSC: readSC,
readSelectorSequence: sequence.selector,
readSelectorSequenceFallback: null,
readSequence: sequence.default,
readSequenceFallback: null,

@@ -167,4 +170,4 @@ getLocation: function(start, end) {

return this.scanner.getLocationRange(
list.head !== null ? list.first().loc.start.offset : this.scanner.tokenStart,
list.head !== null ? list.last().loc.end.offset : this.scanner.tokenStart,
list.head !== null ? list.first().loc.start.offset - this.scanner.startOffset : this.scanner.tokenStart,
list.head !== null ? list.last().loc.end.offset - this.scanner.startOffset : this.scanner.tokenStart,
this.filename

@@ -183,3 +186,3 @@ );

this.scanner.setSource(source, options.line, options.column);
this.scanner.setSource(source, options.offset, options.line, options.column);
this.filename = options.filename || '<unknown>';

@@ -186,0 +189,0 @@ this.needPositions = Boolean(options.positions);

@@ -19,2 +19,7 @@ var List = require('../utils/list');

var PERCENTSIGN = TYPE.PercentSign;
var FULLSTOP = TYPE.FullStop;
var COLON = TYPE.Colon;
var GREATERTHANSIGN = TYPE.GreaterThanSign;
var VERTICALLINE = TYPE.VerticalLine;
var TILDE = TYPE.Tilde;
var U = 117; // 'u'.charCodeAt(0)

@@ -37,8 +42,2 @@

function relativeSelectorList() {
return new List().appendData(
this.SelectorList()
);
}
function compoundSelector() {

@@ -62,8 +61,8 @@ return new List().appendData(

function defaultSequence(scope) {
function selectorSequence() {
var children = new List();
var space = null;
var nonWSOperator = false;
var prevNonWSOperator = false;
var child;
var child = null;
var ignoreWSAfter = false;
var ignoreWS = false;

@@ -75,10 +74,115 @@ this.readSC();

switch (this.scanner.tokenType) {
case COMMENT:
this.scanner.next();
continue;
case WHITESPACE:
space = this.WhiteSpace();
if (ignoreWS) {
this.scanner.next();
} else {
space = this.WhiteSpace();
}
continue;
case COMMENT: // ignore comments
case PLUSSIGN:
case GREATERTHANSIGN:
case TILDE:
space = null;
ignoreWSAfter = true;
child = this.Combinator();
break;
case SOLIDUS: // /deep/
child = this.Combinator();
break;
case FULLSTOP:
child = this.ClassSelector();
break;
case LEFTSQUAREBRACKET:
child = this.AttributeSelector();
break;
case NUMBERSIGN:
child = this.IdSelector();
break;
case COLON:
if (this.scanner.lookupType(1) === COLON) {
child = this.PseudoElementSelector();
} else {
child = this.PseudoClassSelector();
}
break;
case IDENTIFIER:
case ASTERISK:
case VERTICALLINE:
child = this.TypeSelector();
break;
case NUMBER:
child = this.Percentage();
break;
default:
if (typeof this.readSelectorSequenceFallback === 'function') {
child = this.readSelectorSequenceFallback();
if (!child) {
break scan;
}
} else {
break scan;
}
}
if (space !== null) {
children.appendData(space);
space = null;
}
children.appendData(child);
if (ignoreWSAfter) {
ignoreWSAfter = false;
ignoreWS = true;
} else {
ignoreWS = false;
}
}
// nothing were consumed
if (child === null) {
this.scanner.error('Selector is expected');
}
return children;
}
function defaultSequence(scope) {
var children = new List();
var space = null;
var child = null;
var ignoreWSAfter = false;
var ignoreWS = false;
this.readSC();
scan:
while (!this.scanner.eof) {
switch (this.scanner.tokenType) {
case COMMENT:
this.scanner.next();
continue;
case WHITESPACE:
if (ignoreWS) {
this.scanner.next();
} else {
space = this.WhiteSpace();
}
continue;
case NUMBERSIGN:

@@ -90,3 +194,3 @@ child = this.HexColor();

space = null;
nonWSOperator = true;
ignoreWSAfter = true;
child = this.Operator();

@@ -148,11 +252,14 @@ break;

default:
break scan;
if (typeof this.readSequenceFallback === 'function') {
child = this.readSequenceFallback();
if (!child) {
break scan;
}
} else {
break scan;
}
}
if (space !== null) {
// ignore spaces around operator
if (!nonWSOperator && !prevNonWSOperator) {
children.appendData(space);
}
children.appendData(space);
space = null;

@@ -162,4 +269,9 @@ }

children.appendData(child);
prevNonWSOperator = nonWSOperator;
nonWSOperator = false;
if (ignoreWSAfter) {
ignoreWSAfter = false;
ignoreWS = true;
} else {
ignoreWS = false;
}
}

@@ -173,7 +285,7 @@

selectorList: selectorList,
relativeSelectorList: relativeSelectorList,
compoundSelector: compoundSelector,
nth: nth,
nthWithOfClause: nthWithOfClause,
selector: selectorSequence,
default: defaultSequence
};

@@ -10,4 +10,6 @@ var cmpChar = require('../../tokenizer').cmpChar;

var N = 110; // 'n'.charCodeAt(0)
var DISALLOW_SIGN = true;
var ALLOW_SIGN = false;
function checkTokenIsInteger(scanner) {
function checkTokenIsInteger(scanner, disallowSign) {
var pos = scanner.tokenStart;

@@ -17,2 +19,5 @@

scanner.source.charCodeAt(pos) === HYPHENMINUS) {
if (disallowSign) {
scanner.error();
}
pos++;

@@ -36,5 +41,5 @@ }

if (this.scanner.tokenType === PLUSSIGN ||
this.scanner.tokenType === NUMBER) {
checkTokenIsInteger(this.scanner);
if (this.scanner.tokenType === NUMBER ||
this.scanner.tokenType === PLUSSIGN) {
checkTokenIsInteger(this.scanner, ALLOW_SIGN);
prefix = this.scanner.getTokenValue();

@@ -61,3 +66,4 @@ this.scanner.next();

a = prefix === '' || prefix === '+' ? '1' :
a = prefix === '' ? '1' :
prefix === '+' ? '+1' :
prefix === '-' ? '-1' :

@@ -73,28 +79,15 @@ prefix;

this.scanner.tokenStart = bStart + 1;
// ..n-{number}..
if (len > 2) {
for (var i = bStart + 2; i < this.scanner.tokenEnd; i++) {
if (!isNumber(this.scanner.source.charCodeAt(i))) {
this.scanner.error('Unexpected input', i);
}
}
this.scanner.next();
b = '-' + this.scanner.substrToCursor(bStart + 2);
// ..n-{number}..
this.scanner.tokenStart = bStart + 2;
} else {
// ..n- {number}
this.scanner.next();
this.readSC();
}
if (this.scanner.tokenType !== NUMBER ||
cmpChar(this.scanner.source, this.scanner.tokenStart, PLUSSIGN) ||
cmpChar(this.scanner.source, this.scanner.tokenStart, HYPHENMINUS)) {
this.scanner.error();
}
b = '-' + this.scanner.getTokenValue();
this.scanner.next();
end = this.scanner.tokenStart;
}
checkTokenIsInteger(this.scanner, DISALLOW_SIGN);
b = '-' + this.scanner.getTokenValue();
this.scanner.next();
end = this.scanner.tokenStart;
} else {

@@ -110,3 +103,2 @@ prefix = '';

this.scanner.next();
end = this.scanner.tokenStart;
this.readSC();

@@ -116,11 +108,5 @@ }

if (this.scanner.tokenType === NUMBER) {
checkTokenIsInteger(this.scanner);
checkTokenIsInteger(this.scanner, prefix !== '');
if (cmpChar(this.scanner.source, this.scanner.tokenStart, PLUSSIGN) ||
cmpChar(this.scanner.source, this.scanner.tokenStart, HYPHENMINUS)) {
// prefix or sign should be specified but not both
if (prefix !== '') {
this.scanner.error();
}
if (!isNumber(this.scanner.source.charCodeAt(this.scanner.tokenStart))) {
prefix = this.scanner.source.charAt(this.scanner.tokenStart);

@@ -142,7 +128,17 @@ this.scanner.tokenStart++;

end = this.scanner.tokenStart;
} else {
if (prefix) {
this.scanner.eat(NUMBER);
}
}
}
} else {
if (prefix === '' || prefix === '-' || prefix === '+') { // no number
this.scanner.error('Number or identifier is expected');
if (prefix === '' || prefix === '+') { // no number
this.scanner.error(
'Number or identifier is expected',
this.scanner.tokenStart + (
this.scanner.tokenType === PLUSSIGN ||
this.scanner.tokenType === HYPHENMINUS
)
);
}

@@ -149,0 +145,0 @@

@@ -1,108 +0,4 @@

var List = require('../../utils/list');
var TYPE = require('../../tokenizer').TYPE;
var WHITESPACE = TYPE.Whitespace;
var IDENTIFIER = TYPE.Identifier;
var NUMBER = TYPE.Number;
var COMMENT = TYPE.Comment;
var NUMBERSIGN = TYPE.NumberSign;
var ASTERISK = TYPE.Asterisk;
var PLUSSIGN = TYPE.PlusSign;
var FULLSTOP = TYPE.FullStop;
var SOLIDUS = TYPE.Solidus;
var COLON = TYPE.Colon;
var GREATERTHANSIGN = TYPE.GreaterThanSign;
var LEFTSQUAREBRACKET = TYPE.LeftSquareBracket;
var VERTICALLINE = TYPE.VerticalLine;
var TILDE = TYPE.Tilde;
module.exports = function Selector() {
var children = new List();
var space = null;
var child = null;
var ignoreWSAfter = false;
var ignoreWS = false;
var children = this.readSelectorSequence();
this.readSC();
scan:
while (!this.scanner.eof) {
switch (this.scanner.tokenType) {
case COMMENT:
this.scanner.next();
continue;
case WHITESPACE:
if (ignoreWS) {
this.scanner.next();
} else {
space = this.WhiteSpace();
}
continue;
case PLUSSIGN:
case GREATERTHANSIGN:
case TILDE:
space = null;
ignoreWSAfter = true;
child = this.Combinator();
break;
case SOLIDUS: // /deep/
child = this.Combinator();
break;
case FULLSTOP:
child = this.ClassSelector();
break;
case LEFTSQUAREBRACKET:
child = this.AttributeSelector();
break;
case NUMBERSIGN:
child = this.IdSelector();
break;
case COLON:
if (this.scanner.lookupType(1) === COLON) {
child = this.PseudoElementSelector();
} else {
child = this.PseudoClassSelector();
}
break;
case IDENTIFIER:
case ASTERISK:
case VERTICALLINE:
child = this.TypeSelector();
break;
case NUMBER:
child = this.Percentage();
break;
default:
break scan;
}
if (space !== null) {
children.appendData(space);
space = null;
}
children.appendData(child);
if (ignoreWSAfter) {
ignoreWSAfter = false;
ignoreWS = true;
}
}
// nothing were consumed
if (child === null) {
this.scanner.error('Selector is expected');
}
return {

@@ -109,0 +5,0 @@ type: 'Selector',

@@ -11,4 +11,2 @@ var List = require('../../utils/list');

this.readSC();
while (!this.scanner.eof) {

@@ -15,0 +13,0 @@ children.appendData(this.parseSelector

@@ -14,3 +14,3 @@ var endsWith = require('../../tokenizer').endsWith;

// special parser for filter property since it can contains non-standart syntax for old IE
if (property !== null && endsWith(property, 'filter') && checkProgid.call(this)) {
if (property !== null && endsWith(property, 'filter') && checkProgid(this.scanner)) {
this.readSC();

@@ -30,4 +30,7 @@ return this.Raw(BALANCED, SEMICOLON, EXCLAMATIONMARK);

function findNonSCOffset(offset) {
for (var type; type = this.scanner.lookupType(offset); offset++) {
// 'progid:' ws* 'DXImageTransform.Microsoft.' ident ws* '(' .* ')'
function checkProgid(scanner) {
var offset = 0;
for (var type; type = scanner.lookupType(offset); offset++) {
if (type !== WHITESPACE && type !== COMMENT) {

@@ -38,19 +41,11 @@ break;

return offset;
}
// 'progid:' ws* 'DXImageTransform.Microsoft.' ident ws* '(' .* ')'
function checkProgid() {
var startOffset = findNonSCOffset.call(this, 0);
var offset = startOffset;
if (this.scanner.lookupValue(offset, 'alpha') ||
this.scanner.lookupValue(offset, 'dropshadow')) {
if (this.scanner.lookupType(offset + 1) !== LEFTPARENTHESIS) {
return false; // fail
if (scanner.lookupValue(offset, 'alpha') ||
scanner.lookupValue(offset, 'dropshadow')) {
if (scanner.lookupType(offset + 1) !== LEFTPARENTHESIS) {
return false;
}
} else {
if (this.scanner.lookupValue(offset, 'progid') === false ||
this.scanner.lookupType(offset + 1) !== COLON) {
return false; // fail
if (scanner.lookupValue(offset, 'progid') === false ||
scanner.lookupType(offset + 1) !== COLON) {
return false;
}

@@ -57,0 +52,0 @@ }

@@ -48,5 +48,7 @@ 'use strict';

LeftSquareBracket: 91, // [
Backslash: 92, // \
RightSquareBracket: 93, // ]
CircumflexAccent: 94, // ^
LowLine: 95, // _
GraveAccent: 96, // `
LeftCurlyBracket: 123, // {

@@ -73,31 +75,34 @@ VerticalLine: 124, // |

[
TYPE.ExclamationMark, // '!'
TYPE.QuotationMark, // '"'
TYPE.NumberSign, // '#'
TYPE.DollarSign, // '$'
TYPE.PercentSign, // '%'
TYPE.Ampersand, // '&'
TYPE.Apostrophe, // '\''
TYPE.LeftParenthesis, // '('
TYPE.RightParenthesis, // ')'
TYPE.Asterisk, // '*'
TYPE.PlusSign, // '+'
TYPE.Comma, // ','
TYPE.HyphenMinus, // '-'
TYPE.FullStop, // '.'
TYPE.Solidus, // '/'
TYPE.Colon, // ':'
TYPE.Semicolon, // ';'
TYPE.LessThanSign, // '<'
TYPE.EqualsSign, // '='
TYPE.GreaterThanSign, // '>'
TYPE.QuestionMark, // '?'
TYPE.CommercialAt, // '@'
TYPE.LeftSquareBracket, // '['
TYPE.RightSquareBracket, // ']'
TYPE.CircumflexAccent, // '^'
TYPE.LeftCurlyBracket, // '{'
TYPE.VerticalLine, // '|'
TYPE.RightCurlyBracket, // '}'
TYPE.Tilde // '~'
TYPE.ExclamationMark, // !
TYPE.QuotationMark, // "
TYPE.NumberSign, // #
TYPE.DollarSign, // $
TYPE.PercentSign, // %
TYPE.Ampersand, // &
TYPE.Apostrophe, // '
TYPE.LeftParenthesis, // (
TYPE.RightParenthesis, // )
TYPE.Asterisk, // *
TYPE.PlusSign, // +
TYPE.Comma, // ,
TYPE.HyphenMinus, // -
TYPE.FullStop, // .
TYPE.Solidus, // /
TYPE.Colon, // :
TYPE.Semicolon, // ;
TYPE.LessThanSign, // <
TYPE.EqualsSign, // =
TYPE.GreaterThanSign, // >
TYPE.QuestionMark, // ?
TYPE.CommercialAt, // @
TYPE.LeftSquareBracket, // [
// TYPE.Backslash, // \
TYPE.RightSquareBracket, // ]
TYPE.CircumflexAccent, // ^
// TYPE.LowLine, // _
TYPE.GraveAccent, // `
TYPE.LeftCurlyBracket, // {
TYPE.VerticalLine, // |
TYPE.RightCurlyBracket, // }
TYPE.Tilde // ~
].forEach(function(key) {

@@ -104,0 +109,0 @@ SYMBOL_TYPE[Number(key)] = PUNCTUATOR;

@@ -33,4 +33,4 @@ 'use strict';

var R = 13;
var STAR = 42;
var SLASH = 47;
var STAR = TYPE.Asterisk;
var SLASH = TYPE.Solidus;
var FULLSTOP = TYPE.FullStop;

@@ -49,5 +49,5 @@ var PLUSSIGN = TYPE.PlusSign;

var lines = tokenizer.lines;
var line = tokenizer.initLine;
var line = tokenizer.startLine;
var columns = tokenizer.columns;
var column = tokenizer.initColumn;
var column = tokenizer.startColumn;

@@ -166,3 +166,3 @@ if (lines === null || lines.length < sourceLength + 1) {

var Tokenizer = function(source, initLine, initColumn) {
var Tokenizer = function(source, startOffset, startLine, startColumn) {
this.offsetAndType = null;

@@ -172,12 +172,13 @@ this.lines = null;

this.setSource(source || '', initLine, initColumn);
this.setSource(source || '', startOffset, startLine, startColumn);
};
Tokenizer.prototype = {
setSource: function(source, initLine, initColumn) {
setSource: function(source, startOffset, startLine, startColumn) {
var start = firstCharOffset(source);
this.source = source;
this.initLine = typeof initLine === 'undefined' ? 1 : initLine;
this.initColumn = typeof initColumn === 'undefined' ? 1 : initColumn;
this.startOffset = typeof startOffset === 'undefined' ? 0 : startOffset;
this.startLine = typeof startLine === 'undefined' ? 1 : startLine;
this.startColumn = typeof startColumn === 'undefined' ? 1 : startColumn;
this.linesAnsColumnsComputed = false;

@@ -326,3 +327,3 @@

source: filename,
offset: offset,
offset: this.startOffset + offset,
line: this.lines[offset],

@@ -341,3 +342,3 @@ column: this.columns[offset]

start: {
offset: start,
offset: this.startOffset + start,
line: this.lines[start],

@@ -347,3 +348,3 @@ column: this.columns[start]

end: {
offset: end,
offset: this.startOffset + end,
line: this.lines[end],

@@ -350,0 +351,0 @@ column: this.columns[end]

@@ -63,22 +63,2 @@ 'use strict';

function translateSelector(list) {
var cursor = list.head;
var result = '';
if (cursor === null) {
return result;
}
if (cursor === list.tail) {
return translate(list.head.data);
}
while (cursor !== null) {
result += translate(cursor.data);
cursor = cursor.next;
}
return result;
}
function translateBlock(list) {

@@ -122,3 +102,3 @@ var cursor = list.head;

case 'Selector':
return translateSelector(node.children);
return each(node.children);

@@ -125,0 +105,0 @@ case 'Block':

{
"name": "css-tree",
"version": "1.0.0-alpha15",
"version": "1.0.0-alpha16",
"description": "Fast detailed CSS parser",

@@ -5,0 +5,0 @@ "keywords": [

@@ -58,3 +58,3 @@ <img align="right" width="111" height="111"

csstree.walk(ast, function(node) {
if (node.type === 'Class' && node.name === 'example') {
if (node.type === 'ClassSelector' && node.name === 'example') {
node.name = 'hello';

@@ -61,0 +61,0 @@ }

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc