Code style / formatting.

pull/8275/head
typhonrt 4 years ago
parent 15fa8ad0cc
commit 8f5fd3d6fe

@ -3,46 +3,46 @@ import { Delim } from 'css-tree/tokenizer';
export const name = 'Comparison'; export const name = 'Comparison';
export const structure = { export const structure = {
value: String value: String
}; };
export function parse() { 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 '>'. // The first character in the comparison operator must match '<', '=', or '>'.
if (char1 !== '<' && char1 !== '>' && char1 !== '=') { if (char1 !== '<' && char1 !== '>' && char1 !== '=') {
this.error('Malformed comparison operator'); this.error('Malformed comparison operator');
} }
let char2; let char2;
if (this.tokenType === Delim) { if (this.tokenType === Delim) {
char2 = this.consume(Delim); char2 = this.consume(Delim);
// The second character in the comparison operator must match '='. // The second character in the comparison operator must match '='.
if (char2 !== '=') { if (char2 !== '=') {
this.error('Malformed comparison operator'); this.error('Malformed comparison operator');
} }
} }
// If the next token is also 'Delim' then it is malformed. // If the next token is also 'Delim' then it is malformed.
if (this.tokenType === Delim) { if (this.tokenType === Delim) {
this.error('Malformed comparison operator'); this.error('Malformed comparison operator');
} }
const value = char2 ? `${char1}${char2}` : char1; const value = char2 ? `${char1}${char2}` : char1;
return { return {
type: 'Comparison', type: 'Comparison',
loc: this.getLocation(start, this.tokenStart), loc: this.getLocation(start, this.tokenStart),
value value
}; };
} }
export function generate(node) { export function generate(node) {
for (let index = 0; index < node.value.length; index++) { for (let index = 0; index < node.value.length; index++) {
this.token(Delim, node.value.charAt(index)); this.token(Delim, node.value.charAt(index));
} }
} }

@ -1,82 +1,82 @@
// @ts-nocheck // @ts-nocheck
import { import {
Ident, Ident,
Number, Number,
Dimension, Dimension,
Function, Function,
LeftParenthesis, LeftParenthesis,
RightParenthesis, RightParenthesis,
Colon, Colon,
Delim Delim
} from 'css-tree/tokenizer'; } from 'css-tree/tokenizer';
export const name = 'ContainerFeature'; export const name = 'ContainerFeature';
export const structure = { export const structure = {
name: String, name: String,
value: ['Identifier', 'Number', 'Dimension', 'QueryCSSFunction', 'Ratio', null] value: ['Identifier', 'Number', 'Dimension', 'QueryCSSFunction', 'Ratio', null]
}; };
export function parse() { export function parse() {
const start = this.tokenStart; const start = this.tokenStart;
let value = null; let value = null;
this.eat(LeftParenthesis); this.eat(LeftParenthesis);
this.skipSC(); this.skipSC();
const name = this.consume(Ident); const name = this.consume(Ident);
this.skipSC(); this.skipSC();
if (this.tokenType !== RightParenthesis) { if (this.tokenType !== RightParenthesis) {
this.eat(Colon); this.eat(Colon);
this.skipSC(); this.skipSC();
switch (this.tokenType) { switch (this.tokenType) {
case Number: case Number:
if (this.lookupNonWSType(1) === Delim) { if (this.lookupNonWSType(1) === Delim) {
value = this.Ratio(); value = this.Ratio();
} else { } else {
value = this.Number(); value = this.Number();
} }
break; break;
case Dimension: case Dimension:
value = this.Dimension(); value = this.Dimension();
break; break;
case Function: case Function:
value = this.QueryCSSFunction(); value = this.QueryCSSFunction();
break; break;
case Ident: case Ident:
value = this.Identifier(); value = this.Identifier();
break; break;
default: default:
this.error('Number, dimension, ratio, function, or identifier is expected'); this.error('Number, dimension, ratio, function, or identifier is expected');
break; break;
} }
this.skipSC(); this.skipSC();
} }
this.eat(RightParenthesis); this.eat(RightParenthesis);
return { return {
type: 'ContainerFeature', type: 'ContainerFeature',
loc: this.getLocation(start, this.tokenStart), loc: this.getLocation(start, this.tokenStart),
name, name,
value value
}; };
} }
export function generate(node) { export function generate(node) {
this.token(LeftParenthesis, '('); this.token(LeftParenthesis, '(');
this.token(Ident, node.name); this.token(Ident, node.name);
if (node.value !== null) { if (node.value !== null) {
this.token(Colon, ':'); this.token(Colon, ':');
this.node(node.value); this.node(node.value);
} }
this.token(RightParenthesis, ')'); this.token(RightParenthesis, ')');
} }

@ -1,86 +1,86 @@
// @ts-nocheck // @ts-nocheck
import { import {
Ident, Ident,
Number, Number,
Delim, Delim,
Dimension, Dimension,
Function, Function,
LeftParenthesis, LeftParenthesis,
RightParenthesis, RightParenthesis,
WhiteSpace WhiteSpace
} from 'css-tree/tokenizer'; } from 'css-tree/tokenizer';
export const name = 'ContainerFeatureRange'; export const name = 'ContainerFeatureRange';
export const structure = { export const structure = {
name: String, name: String,
value: ['Identifier', 'Number', 'Comparison', 'Dimension', 'QueryCSSFunction', 'Ratio', null] value: ['Identifier', 'Number', 'Comparison', 'Dimension', 'QueryCSSFunction', 'Ratio', null]
}; };
function lookupNonWSTypeAndValue(offset, type, referenceStr) { function lookup_non_WS_type_and_value(offset, type, referenceStr) {
let currentType; let current_type;
do { do {
currentType = this.lookupType(offset++); current_type = this.lookupType(offset++);
if (currentType !== WhiteSpace) { if (current_type !== WhiteSpace) {
break; 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() { export function parse() {
const children = this.createList(); const children = this.createList();
let child = null; let child = null;
this.eat(LeftParenthesis); this.eat(LeftParenthesis);
this.skipSC(); this.skipSC();
while (!this.eof && this.tokenType !== RightParenthesis) { while (!this.eof && this.tokenType !== RightParenthesis) {
switch (this.tokenType) { switch (this.tokenType) {
case Number: case Number:
if (lookupNonWSTypeAndValue.call(this, 1, Delim, '/')) { if (lookup_non_WS_type_and_value.call(this, 1, Delim, '/')) {
child = this.Ratio(); child = this.Ratio();
} else { } else {
child = this.Number(); child = this.Number();
} }
break; break;
case Delim: case Delim:
child = this.Comparison(); child = this.Comparison();
break; break;
case Dimension: case Dimension:
child = this.Dimension(); child = this.Dimension();
break; break;
case Function: case Function:
child = this.QueryCSSFunction(); child = this.QueryCSSFunction();
break; break;
case Ident: case Ident:
child = this.Identifier(); child = this.Identifier();
break; break;
default: default:
this.error('Number, dimension, comparison, ratio, function, or identifier is expected'); this.error('Number, dimension, comparison, ratio, function, or identifier is expected');
break; break;
} }
children.push(child); children.push(child);
this.skipSC(); this.skipSC();
} }
this.eat(RightParenthesis); this.eat(RightParenthesis);
return { return {
type: 'ContainerFeatureRange', type: 'ContainerFeatureRange',
loc: this.getLocationFromList(children), loc: this.getLocationFromList(children),
children children
}; };
} }
export function generate(node) { export function generate(node) {
this.children(node); this.children(node);
} }

@ -1,85 +1,85 @@
// @ts-nocheck // @ts-nocheck
import { import {
Function, Function,
Ident, Ident,
Number, Number,
Dimension, Dimension,
RightParenthesis, RightParenthesis,
Colon, Colon,
Delim Delim
} from 'css-tree/tokenizer'; } from 'css-tree/tokenizer';
export const name = 'ContainerFeatureStyle'; export const name = 'ContainerFeatureStyle';
export const structure = { export const structure = {
name: String, name: String,
value: ['Function', 'Identifier', 'Number', 'Dimension', 'QueryCSSFunction', 'Ratio', null] value: ['Function', 'Identifier', 'Number', 'Dimension', 'QueryCSSFunction', 'Ratio', null]
}; };
export function parse() { export function parse() {
const start = this.tokenStart; const start = this.tokenStart;
let value = null; let value = null;
const functionName = this.consumeFunctionName(); const function_name = this.consumeFunctionName();
if (functionName !== 'style') { if (function_name !== 'style') {
this.error('Unknown container style query identifier; "style" is expected'); this.error('Unknown container style query identifier; "style" is expected');
} }
this.skipSC(); this.skipSC();
const name = this.consume(Ident); const name = this.consume(Ident);
this.skipSC(); this.skipSC();
if (this.tokenType !== RightParenthesis) { if (this.tokenType !== RightParenthesis) {
this.eat(Colon); this.eat(Colon);
this.skipSC(); this.skipSC();
switch (this.tokenType) { switch (this.tokenType) {
case Number: case Number:
if (this.lookupNonWSType(1) === Delim) { if (this.lookupNonWSType(1) === Delim) {
value = this.Ratio(); value = this.Ratio();
} else { } else {
value = this.Number(); value = this.Number();
} }
break; break;
case Dimension: case Dimension:
value = this.Dimension(); value = this.Dimension();
break; break;
case Function: case Function:
value = this.QueryCSSFunction(); value = this.QueryCSSFunction();
break; break;
case Ident: case Ident:
value = this.Identifier(); value = this.Identifier();
break; break;
default: default:
this.error('Number, dimension, ratio, function or identifier is expected'); this.error('Number, dimension, ratio, function or identifier is expected');
break; break;
} }
this.skipSC(); this.skipSC();
} }
this.eat(RightParenthesis); this.eat(RightParenthesis);
return { return {
type: 'ContainerFeatureStyle', type: 'ContainerFeatureStyle',
loc: this.getLocation(start, this.tokenStart), loc: this.getLocation(start, this.tokenStart),
name, name,
value value
}; };
} }
export function generate(node) { export function generate(node) {
this.token(Function, 'style('); this.token(Function, 'style(');
this.token(Ident, node.name); this.token(Ident, node.name);
if (node.value !== null) { if (node.value !== null) {
this.token(Colon, ':'); this.token(Colon, ':');
this.node(node.value); this.node(node.value);
} }
this.token(RightParenthesis, ')'); this.token(RightParenthesis, ')');
} }

@ -1,132 +1,130 @@
// @ts-nocheck // @ts-nocheck
import { import {
EOF, EOF,
WhiteSpace, WhiteSpace,
Comment, Comment,
Delim, Delim,
Function, Function,
Ident, Ident,
LeftParenthesis, LeftParenthesis,
RightParenthesis, RightParenthesis,
LeftCurlyBracket, LeftCurlyBracket,
Colon Colon
} from 'css-tree/tokenizer'; } from 'css-tree/tokenizer';
const CONTAINER_QUERY_KEYWORDS = new Set(['none', 'and', 'not', 'or']); const CONTAINER_QUERY_KEYWORDS = new Set(['none', 'and', 'not', 'or']);
export const name = 'ContainerQuery'; export const name = 'ContainerQuery';
export const structure = { export const structure = {
name: 'Identifier', name: 'Identifier',
children: [[ children: [[
'Identifier', 'Identifier',
'ContainerFeature', 'ContainerFeature',
'ContainerFeatureRange', 'ContainerFeatureRange',
'ContainerFeatureStyle', 'ContainerFeatureStyle',
'WhiteSpace' 'WhiteSpace'
]] ]]
}; };
/** /**
* Looks ahead to determine if query feature is a range query. This involves locating at least one delimiter and no * Looks ahead to determine if query feature is a range query. This involves locating at least one delimiter and no
* colon tokens. * colon tokens.
* *
* @param context -
*
* @returns {boolean} Is potential range query. * @returns {boolean} Is potential range query.
*/ */
function lookaheadIsRange(context) { function lookahead_is_range() {
let type; let type;
let offset = 0; let offset = 0;
let count = 0; let count = 0;
let delimFound = false; let delim_found = false;
let noColon = true; let no_colon = true;
// A range query has maximum 5 tokens when formatted as 'mf-range' / // A range query has maximum 5 tokens when formatted as 'mf-range' /
// '<mf-value> <mf-lt> <mf-name> <mf-lt> <mf-value>'. So only look ahead maximum of 6 non-whitespace tokens. // '<mf-value> <mf-lt> <mf-name> <mf-lt> <mf-value>'. So only look ahead maximum of 6 non-whitespace tokens.
do { do {
type = context.lookupNonWSType(offset++); type = this.lookupNonWSType(offset++);
if (type !== WhiteSpace) { if (type !== WhiteSpace) {
count++; count++;
} }
if (type === Delim) { if (type === Delim) {
delimFound = true; delim_found = true;
} }
if (type === Colon) { if (type === Colon) {
noColon = false; no_colon = false;
} }
if (type === LeftCurlyBracket || type === RightParenthesis) { if (type === LeftCurlyBracket || type === RightParenthesis) {
break; break;
} }
} while (type !== EOF && count <= 6); } while (type !== EOF && count <= 6);
return delimFound && noColon; return delim_found && no_colon;
} }
export function parse() { export function parse() {
const start = this.tokenStart; const start = this.tokenStart;
const children = this.createList(); const children = this.createList();
let child = null; let child = null;
let name = null; let name = null;
// Parse potential container name. // Parse potential container name.
if (this.tokenType === Ident) { if (this.tokenType === Ident) {
const containerName = this.substring(this.tokenStart, this.tokenEnd); const container_name = this.substring(this.tokenStart, this.tokenEnd);
// Container name doesn't match a query keyword, so assign it as container name. // Container name doesn't match a query keyword, so assign it as container name.
if (!CONTAINER_QUERY_KEYWORDS.has(containerName.toLowerCase())) { if (!CONTAINER_QUERY_KEYWORDS.has(container_name.toLowerCase())) {
name = containerName; name = container_name;
this.eatIdent(containerName); this.eatIdent(container_name);
} }
} }
this.skipSC(); this.skipSC();
scan: scan:
while (!this.eof) { while (!this.eof) {
switch (this.tokenType) { switch (this.tokenType) {
case Comment: case Comment:
case WhiteSpace: case WhiteSpace:
this.next(); this.next();
continue; continue;
case Ident: case Ident:
child = this.Identifier(); child = this.Identifier();
break; break;
case Function: case Function:
child = this.ContainerFeatureStyle(); child = this.ContainerFeatureStyle();
break; break;
case LeftParenthesis: case LeftParenthesis:
// Lookahead to determine if range feature. // Lookahead to determine if range feature.
child = lookaheadIsRange(this) ? this.ContainerFeatureRange() : this.ContainerFeature(); child = lookahead_is_range.call(this) ? this.ContainerFeatureRange() : this.ContainerFeature();
break; break;
default: default:
break scan; break scan;
} }
children.push(child); children.push(child);
} }
if (child === null) { if (child === null) {
this.error('Identifier or parenthesis is expected'); this.error('Identifier or parenthesis is expected');
} }
return { return {
type: 'ContainerQuery', type: 'ContainerQuery',
loc: this.getLocation(start, this.tokenStart - 1), loc: this.getLocation(start, this.tokenStart - 1),
name, name,
children children
}; };
} }
export function generate(node) { export function generate(node) {
if (typeof node.name === 'string') { if (typeof node.name === 'string') {
this.token(Ident, node.name); this.token(Ident, node.name);
} }
this.children(node); this.children(node);
} }

Loading…
Cancel
Save