diff --git a/src/compiler/parse/read/css-tree-cq/node/comparison.ts b/src/compiler/parse/read/css-tree-cq/node/comparison.ts index 5ff3cdfa2f..4f40f59c24 100644 --- a/src/compiler/parse/read/css-tree-cq/node/comparison.ts +++ b/src/compiler/parse/read/css-tree-cq/node/comparison.ts @@ -3,46 +3,46 @@ import { Delim } from 'css-tree/tokenizer'; export const name = 'Comparison'; export const structure = { - value: String + value: String }; export function parse() { - const start = this.tokenStart; + const start = this.tokenStart; - const char1 = this.consume(Delim); + const char1 = this.consume(Delim); - // The first character in the comparison operator must match '<', '=', or '>'. - if (char1 !== '<' && char1 !== '>' && char1 !== '=') { - this.error('Malformed comparison operator'); - } + // The first character in the comparison operator must match '<', '=', or '>'. + if (char1 !== '<' && char1 !== '>' && char1 !== '=') { + this.error('Malformed comparison operator'); + } - let char2; + let char2; - if (this.tokenType === Delim) { - char2 = this.consume(Delim); + if (this.tokenType === Delim) { + char2 = this.consume(Delim); - // The second character in the comparison operator must match '='. - if (char2 !== '=') { - this.error('Malformed comparison operator'); - } - } + // The second character in the comparison operator must match '='. + if (char2 !== '=') { + this.error('Malformed comparison operator'); + } + } - // If the next token is also 'Delim' then it is malformed. - if (this.tokenType === Delim) { - this.error('Malformed comparison operator'); - } + // If the next token is also 'Delim' then it is malformed. + if (this.tokenType === Delim) { + this.error('Malformed comparison operator'); + } - const value = char2 ? `${char1}${char2}` : char1; + const value = char2 ? `${char1}${char2}` : char1; - return { - type: 'Comparison', - loc: this.getLocation(start, this.tokenStart), - value - }; + return { + type: 'Comparison', + loc: this.getLocation(start, this.tokenStart), + value + }; } export function generate(node) { - for (let index = 0; index < node.value.length; index++) { - this.token(Delim, node.value.charAt(index)); - } + for (let index = 0; index < node.value.length; index++) { + this.token(Delim, node.value.charAt(index)); + } } diff --git a/src/compiler/parse/read/css-tree-cq/node/container_feature.ts b/src/compiler/parse/read/css-tree-cq/node/container_feature.ts index 67d826b076..d84d5f5afd 100644 --- a/src/compiler/parse/read/css-tree-cq/node/container_feature.ts +++ b/src/compiler/parse/read/css-tree-cq/node/container_feature.ts @@ -1,82 +1,82 @@ // @ts-nocheck import { - Ident, - Number, - Dimension, - Function, - LeftParenthesis, - RightParenthesis, - Colon, - Delim + Ident, + Number, + Dimension, + Function, + LeftParenthesis, + RightParenthesis, + Colon, + Delim } from 'css-tree/tokenizer'; export const name = 'ContainerFeature'; export const structure = { - name: String, - value: ['Identifier', 'Number', 'Dimension', 'QueryCSSFunction', 'Ratio', null] + name: String, + value: ['Identifier', 'Number', 'Dimension', 'QueryCSSFunction', 'Ratio', null] }; export function parse() { - const start = this.tokenStart; - let value = null; - - this.eat(LeftParenthesis); - this.skipSC(); - - const name = this.consume(Ident); - this.skipSC(); - - if (this.tokenType !== RightParenthesis) { - this.eat(Colon); - this.skipSC(); - - switch (this.tokenType) { - case Number: - if (this.lookupNonWSType(1) === Delim) { - value = this.Ratio(); - } else { - value = this.Number(); - } - break; - - case Dimension: - value = this.Dimension(); - break; - - case Function: - value = this.QueryCSSFunction(); - break; - - case Ident: - value = this.Identifier(); - break; - - default: - this.error('Number, dimension, ratio, function, or identifier is expected'); - break; - } - - this.skipSC(); - } - - this.eat(RightParenthesis); - - return { - type: 'ContainerFeature', - loc: this.getLocation(start, this.tokenStart), - name, - value - }; + const start = this.tokenStart; + let value = null; + + this.eat(LeftParenthesis); + this.skipSC(); + + const name = this.consume(Ident); + this.skipSC(); + + if (this.tokenType !== RightParenthesis) { + this.eat(Colon); + this.skipSC(); + + switch (this.tokenType) { + case Number: + if (this.lookupNonWSType(1) === Delim) { + value = this.Ratio(); + } else { + value = this.Number(); + } + break; + + case Dimension: + value = this.Dimension(); + break; + + case Function: + value = this.QueryCSSFunction(); + break; + + case Ident: + value = this.Identifier(); + break; + + default: + this.error('Number, dimension, ratio, function, or identifier is expected'); + break; + } + + this.skipSC(); + } + + this.eat(RightParenthesis); + + return { + type: 'ContainerFeature', + loc: this.getLocation(start, this.tokenStart), + name, + value + }; } export function generate(node) { - this.token(LeftParenthesis, '('); - this.token(Ident, node.name); + this.token(LeftParenthesis, '('); + this.token(Ident, node.name); - if (node.value !== null) { - this.token(Colon, ':'); - this.node(node.value); - } + if (node.value !== null) { + this.token(Colon, ':'); + this.node(node.value); + } - this.token(RightParenthesis, ')'); + this.token(RightParenthesis, ')'); } diff --git a/src/compiler/parse/read/css-tree-cq/node/container_feature_range.ts b/src/compiler/parse/read/css-tree-cq/node/container_feature_range.ts index 62aa5b51d7..747fbe8da0 100644 --- a/src/compiler/parse/read/css-tree-cq/node/container_feature_range.ts +++ b/src/compiler/parse/read/css-tree-cq/node/container_feature_range.ts @@ -1,86 +1,86 @@ // @ts-nocheck import { - Ident, - Number, - Delim, - Dimension, - Function, - LeftParenthesis, - RightParenthesis, - WhiteSpace + Ident, + Number, + Delim, + Dimension, + Function, + LeftParenthesis, + RightParenthesis, + WhiteSpace } from 'css-tree/tokenizer'; export const name = 'ContainerFeatureRange'; export const structure = { - name: String, - value: ['Identifier', 'Number', 'Comparison', 'Dimension', 'QueryCSSFunction', 'Ratio', null] + name: String, + value: ['Identifier', 'Number', 'Comparison', 'Dimension', 'QueryCSSFunction', 'Ratio', null] }; -function lookupNonWSTypeAndValue(offset, type, referenceStr) { - let currentType; +function lookup_non_WS_type_and_value(offset, type, referenceStr) { + let current_type; do { - currentType = this.lookupType(offset++); - if (currentType !== WhiteSpace) { + current_type = this.lookupType(offset++); + if (current_type !== WhiteSpace) { break; } - } while (currentType !== 0); // NULL -> 0 + } while (current_type !== 0); // NULL -> 0 - return currentType === type ? this.lookupValue(offset - 1, referenceStr) : false; + return current_type === type ? this.lookupValue(offset - 1, referenceStr) : false; } export function parse() { - const children = this.createList(); - let child = null; - - this.eat(LeftParenthesis); - this.skipSC(); - - while (!this.eof && this.tokenType !== RightParenthesis) { - switch (this.tokenType) { - case Number: - if (lookupNonWSTypeAndValue.call(this, 1, Delim, '/')) { - child = this.Ratio(); - } else { - child = this.Number(); - } - break; - - case Delim: - child = this.Comparison(); - break; - - case Dimension: - child = this.Dimension(); - break; - - case Function: - child = this.QueryCSSFunction(); - break; - - case Ident: - child = this.Identifier(); - break; - - default: - this.error('Number, dimension, comparison, ratio, function, or identifier is expected'); - break; - } - - children.push(child); - - this.skipSC(); - } - - this.eat(RightParenthesis); - - return { - type: 'ContainerFeatureRange', - loc: this.getLocationFromList(children), - children - }; + const children = this.createList(); + let child = null; + + this.eat(LeftParenthesis); + this.skipSC(); + + while (!this.eof && this.tokenType !== RightParenthesis) { + switch (this.tokenType) { + case Number: + if (lookup_non_WS_type_and_value.call(this, 1, Delim, '/')) { + child = this.Ratio(); + } else { + child = this.Number(); + } + break; + + case Delim: + child = this.Comparison(); + break; + + case Dimension: + child = this.Dimension(); + break; + + case Function: + child = this.QueryCSSFunction(); + break; + + case Ident: + child = this.Identifier(); + break; + + default: + this.error('Number, dimension, comparison, ratio, function, or identifier is expected'); + break; + } + + children.push(child); + + this.skipSC(); + } + + this.eat(RightParenthesis); + + return { + type: 'ContainerFeatureRange', + loc: this.getLocationFromList(children), + children + }; } export function generate(node) { - this.children(node); + this.children(node); } diff --git a/src/compiler/parse/read/css-tree-cq/node/container_feature_style.ts b/src/compiler/parse/read/css-tree-cq/node/container_feature_style.ts index 50dc1049de..343b5d47ca 100644 --- a/src/compiler/parse/read/css-tree-cq/node/container_feature_style.ts +++ b/src/compiler/parse/read/css-tree-cq/node/container_feature_style.ts @@ -1,85 +1,85 @@ // @ts-nocheck import { - Function, - Ident, - Number, - Dimension, - RightParenthesis, - Colon, - Delim + Function, + Ident, + Number, + Dimension, + RightParenthesis, + Colon, + Delim } from 'css-tree/tokenizer'; export const name = 'ContainerFeatureStyle'; export const structure = { - name: String, - value: ['Function', 'Identifier', 'Number', 'Dimension', 'QueryCSSFunction', 'Ratio', null] + name: String, + value: ['Function', 'Identifier', 'Number', 'Dimension', 'QueryCSSFunction', 'Ratio', null] }; export function parse() { - const start = this.tokenStart; - let value = null; - - const functionName = this.consumeFunctionName(); - if (functionName !== 'style') { - this.error('Unknown container style query identifier; "style" is expected'); - } - - this.skipSC(); - - const name = this.consume(Ident); - this.skipSC(); - - if (this.tokenType !== RightParenthesis) { - this.eat(Colon); - this.skipSC(); - - switch (this.tokenType) { - case Number: - if (this.lookupNonWSType(1) === Delim) { - value = this.Ratio(); - } else { - value = this.Number(); - } - break; - - case Dimension: - value = this.Dimension(); - break; - - case Function: - value = this.QueryCSSFunction(); - break; - - case Ident: - value = this.Identifier(); - break; - - default: - this.error('Number, dimension, ratio, function or identifier is expected'); - break; - } - - this.skipSC(); - } - - this.eat(RightParenthesis); - - return { - type: 'ContainerFeatureStyle', - loc: this.getLocation(start, this.tokenStart), - name, - value - }; + const start = this.tokenStart; + let value = null; + + const function_name = this.consumeFunctionName(); + if (function_name !== 'style') { + this.error('Unknown container style query identifier; "style" is expected'); + } + + this.skipSC(); + + const name = this.consume(Ident); + this.skipSC(); + + if (this.tokenType !== RightParenthesis) { + this.eat(Colon); + this.skipSC(); + + switch (this.tokenType) { + case Number: + if (this.lookupNonWSType(1) === Delim) { + value = this.Ratio(); + } else { + value = this.Number(); + } + break; + + case Dimension: + value = this.Dimension(); + break; + + case Function: + value = this.QueryCSSFunction(); + break; + + case Ident: + value = this.Identifier(); + break; + + default: + this.error('Number, dimension, ratio, function or identifier is expected'); + break; + } + + this.skipSC(); + } + + this.eat(RightParenthesis); + + return { + type: 'ContainerFeatureStyle', + loc: this.getLocation(start, this.tokenStart), + name, + value + }; } export function generate(node) { - this.token(Function, 'style('); - this.token(Ident, node.name); + this.token(Function, 'style('); + this.token(Ident, node.name); - if (node.value !== null) { - this.token(Colon, ':'); - this.node(node.value); - } + if (node.value !== null) { + this.token(Colon, ':'); + this.node(node.value); + } - this.token(RightParenthesis, ')'); + this.token(RightParenthesis, ')'); } diff --git a/src/compiler/parse/read/css-tree-cq/node/container_query.ts b/src/compiler/parse/read/css-tree-cq/node/container_query.ts index 375aa25caf..c88b91f12d 100644 --- a/src/compiler/parse/read/css-tree-cq/node/container_query.ts +++ b/src/compiler/parse/read/css-tree-cq/node/container_query.ts @@ -1,132 +1,130 @@ // @ts-nocheck import { - EOF, - WhiteSpace, - Comment, - Delim, - Function, - Ident, - LeftParenthesis, - RightParenthesis, - LeftCurlyBracket, - Colon + EOF, + WhiteSpace, + Comment, + Delim, + Function, + Ident, + LeftParenthesis, + RightParenthesis, + LeftCurlyBracket, + Colon } from 'css-tree/tokenizer'; const CONTAINER_QUERY_KEYWORDS = new Set(['none', 'and', 'not', 'or']); export const name = 'ContainerQuery'; export const structure = { - name: 'Identifier', - children: [[ - 'Identifier', - 'ContainerFeature', - 'ContainerFeatureRange', - 'ContainerFeatureStyle', - 'WhiteSpace' - ]] + name: 'Identifier', + children: [[ + 'Identifier', + 'ContainerFeature', + 'ContainerFeatureRange', + 'ContainerFeatureStyle', + 'WhiteSpace' + ]] }; /** * Looks ahead to determine if query feature is a range query. This involves locating at least one delimiter and no * colon tokens. * - * @param context - - * * @returns {boolean} Is potential range query. */ -function lookaheadIsRange(context) { - let type; - let offset = 0; - - let count = 0; - let delimFound = false; - let noColon = true; - - // A range query has maximum 5 tokens when formatted as 'mf-range' / - // ' '. So only look ahead maximum of 6 non-whitespace tokens. - do { - type = context.lookupNonWSType(offset++); - if (type !== WhiteSpace) { - count++; - } - if (type === Delim) { - delimFound = true; - } - if (type === Colon) { - noColon = false; - } - if (type === LeftCurlyBracket || type === RightParenthesis) { - break; - } - } while (type !== EOF && count <= 6); - - return delimFound && noColon; +function lookahead_is_range() { + let type; + let offset = 0; + + let count = 0; + let delim_found = false; + let no_colon = true; + + // A range query has maximum 5 tokens when formatted as 'mf-range' / + // ' '. So only look ahead maximum of 6 non-whitespace tokens. + do { + type = this.lookupNonWSType(offset++); + if (type !== WhiteSpace) { + count++; + } + if (type === Delim) { + delim_found = true; + } + if (type === Colon) { + no_colon = false; + } + if (type === LeftCurlyBracket || type === RightParenthesis) { + break; + } + } while (type !== EOF && count <= 6); + + return delim_found && no_colon; } export function parse() { - const start = this.tokenStart; - const children = this.createList(); - let child = null; - let name = null; - - // Parse potential container name. - if (this.tokenType === Ident) { - const containerName = this.substring(this.tokenStart, this.tokenEnd); - - // Container name doesn't match a query keyword, so assign it as container name. - if (!CONTAINER_QUERY_KEYWORDS.has(containerName.toLowerCase())) { - name = containerName; - this.eatIdent(containerName); - } - } - - this.skipSC(); - - scan: - while (!this.eof) { - switch (this.tokenType) { - case Comment: - case WhiteSpace: - this.next(); - continue; - - case Ident: - child = this.Identifier(); - break; - - case Function: - child = this.ContainerFeatureStyle(); - break; - - case LeftParenthesis: - // Lookahead to determine if range feature. - child = lookaheadIsRange(this) ? this.ContainerFeatureRange() : this.ContainerFeature(); - break; - - default: - break scan; - } - - children.push(child); - } - - if (child === null) { - this.error('Identifier or parenthesis is expected'); - } - - return { - type: 'ContainerQuery', - loc: this.getLocation(start, this.tokenStart - 1), - name, - children - }; + const start = this.tokenStart; + const children = this.createList(); + let child = null; + let name = null; + + // Parse potential container name. + if (this.tokenType === Ident) { + const container_name = this.substring(this.tokenStart, this.tokenEnd); + + // Container name doesn't match a query keyword, so assign it as container name. + if (!CONTAINER_QUERY_KEYWORDS.has(container_name.toLowerCase())) { + name = container_name; + this.eatIdent(container_name); + } + } + + this.skipSC(); + + scan: + while (!this.eof) { + switch (this.tokenType) { + case Comment: + case WhiteSpace: + this.next(); + continue; + + case Ident: + child = this.Identifier(); + break; + + case Function: + child = this.ContainerFeatureStyle(); + break; + + case LeftParenthesis: + // Lookahead to determine if range feature. + child = lookahead_is_range.call(this) ? this.ContainerFeatureRange() : this.ContainerFeature(); + break; + + default: + break scan; + } + + children.push(child); + } + + if (child === null) { + this.error('Identifier or parenthesis is expected'); + } + + return { + type: 'ContainerQuery', + loc: this.getLocation(start, this.tokenStart - 1), + name, + children + }; } export function generate(node) { - if (typeof node.name === 'string') { - this.token(Ident, node.name); - } + if (typeof node.name === 'string') { + this.token(Ident, node.name); + } - this.children(node); + this.children(node); }