Fix async lifecycle ordering, add _spa_init boot phase, update to jqhtml _load_only/_load_render_only flags

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2026-03-06 22:33:38 +00:00
parent 11c95a2886
commit d1ac456279
2718 changed files with 70593 additions and 6320 deletions

View File

@@ -160,6 +160,16 @@ function maybeMultiplied(scanner, node) {
return maybeMultiplied(scanner, multiplier);
}
// https://www.w3.org/TR/css-values-4/#component-multipliers
// > the # and ? multipliers, {A} and ? multipliers, and {A,B} and ? multipliers
// > may be stacked as #?, {A}?, and {A,B}?, respectively
// Represent "{}?" as nested multipliers as well as "+#".
// The "#?" case is already handled above, in maybeMultiplied()
if (scanner.charCode() === QUESTIONMARK &&
scanner.charCodeAt(scanner.pos - 1) === RIGHTCURLYBRACKET) {
return maybeMultiplied(scanner, multiplier);
}
return multiplier;
}
@@ -376,35 +386,65 @@ function regroupTerms(terms, combinators) {
return combinator;
}
function readImplicitGroup(scanner, stopCharCode) {
function readImplicitGroup(scanner, stopCharCode = -1) {
const combinators = Object.create(null);
const terms = [];
let token;
let prevToken = null;
let prevTokenPos = scanner.pos;
let prevTokenIsFunction = false;
while (scanner.charCode() !== stopCharCode && (token = peek(scanner, stopCharCode))) {
if (token.type !== 'Spaces') {
if (token.type === 'Combinator') {
// check for combinator in group beginning and double combinator sequence
if (prevToken === null || prevToken.type === 'Combinator') {
scanner.pos = prevTokenPos;
scanner.error('Unexpected combinator');
}
while (scanner.charCode() !== stopCharCode) {
let token = prevTokenIsFunction
? readImplicitGroup(scanner, RIGHTPARENTHESIS)
: peek(scanner);
combinators[token.value] = true;
} else if (prevToken !== null && prevToken.type !== 'Combinator') {
combinators[' '] = true; // a b
terms.push({
type: 'Combinator',
value: ' '
});
if (!token) {
break;
}
if (token.type === 'Spaces') {
continue;
}
if (prevTokenIsFunction) {
if (token.terms.length === 0) {
prevTokenIsFunction = false;
continue;
}
terms.push(token);
prevToken = token;
prevTokenPos = scanner.pos;
if (token.combinator === ' ') {
while (token.terms.length > 1) {
combinators[' '] = true; // a b
terms.push({
type: 'Combinator',
value: ' '
}, token.terms.shift());
}
token = token.terms[0];
}
}
if (token.type === 'Combinator') {
// check for combinator in group beginning and double combinator sequence
if (prevToken === null || prevToken.type === 'Combinator') {
scanner.pos = prevTokenPos;
scanner.error('Unexpected combinator');
}
combinators[token.value] = true;
} else if (prevToken !== null && prevToken.type !== 'Combinator') {
combinators[' '] = true; // a b
terms.push({
type: 'Combinator',
value: ' '
});
}
terms.push(token);
prevToken = token;
prevTokenPos = scanner.pos;
prevTokenIsFunction = token.type === 'Function';
}
// check for combinator in group ending
@@ -422,11 +462,11 @@ function readImplicitGroup(scanner, stopCharCode) {
};
}
function readGroup(scanner, stopCharCode) {
function readGroup(scanner) {
let result;
scanner.eat(LEFTSQUAREBRACKET);
result = readImplicitGroup(scanner, stopCharCode);
result = readImplicitGroup(scanner, RIGHTSQUAREBRACKET);
scanner.eat(RIGHTSQUAREBRACKET);
result.explicit = true;
@@ -439,7 +479,7 @@ function readGroup(scanner, stopCharCode) {
return result;
}
function peek(scanner, stopCharCode) {
function peek(scanner) {
let code = scanner.charCode();
switch (code) {
@@ -448,7 +488,7 @@ function peek(scanner, stopCharCode) {
break;
case LEFTSQUAREBRACKET:
return maybeMultiplied(scanner, readGroup(scanner, stopCharCode));
return maybeMultiplied(scanner, readGroup(scanner));
case LESSTHANSIGN:
return scanner.nextCharCode() === APOSTROPHE

View File

@@ -23,12 +23,6 @@ function processChildren(node, delimeter) {
node.children.forEach(this.node, this);
}
function processChunk(chunk) {
tokenize(chunk, (type, start, end) => {
this.token(type, chunk.slice(start, end));
});
}
export function createGenerator(config) {
const types = new Map();
@@ -52,9 +46,13 @@ export function createGenerator(config) {
}
},
tokenBefore: tokenBefore.safe,
token(type, value) {
token(type, value, suppressAutoWhiteSpace) {
prevCode = this.tokenBefore(prevCode, type, value);
if (!suppressAutoWhiteSpace && prevCode & 1) {
this.emit(' ', WhiteSpace, true);
}
this.emit(value, type, false);
if (type === Delim && value.charCodeAt(0) === REVERSESOLIDUS) {
@@ -87,7 +85,14 @@ export function createGenerator(config) {
node: (node) => handlers.node(node),
children: processChildren,
token: (type, value) => handlers.token(type, value),
tokenize: processChunk
tokenize: (raw) =>
tokenize(raw, (type, start, end) => {
handlers.token(
type,
raw.slice(start, end),
start !== 0 // suppress auto whitespace for internal value tokens
);
})
};
handlers.node(node);

View File

@@ -1,5 +1,4 @@
import {
WhiteSpace,
Delim,
Ident,
Function as FunctionToken,
@@ -20,17 +19,20 @@ import {
const PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
const HYPHENMINUS = 0x002D; // U+002D HYPHEN-MINUS (-)
// code:
// 0xxxxxxx x0000000 - char code (0x80 for non-ASCII) or delim value
// 00000000 0xxxxxx0 - token type (0 for delim, 1 for token)
// 00000000 0000000x - reserved for carriage emit flag (0 for no space, 1 for space)
const code = (type, value) => {
if (type === Delim) {
type = value;
}
if (typeof type === 'string') {
const charCode = type.charCodeAt(0);
return charCode > 0x7F ? 0x8000 : charCode << 8;
type = Math.min(type.charCodeAt(0), 0x80) << 6; // replace non-ASCII with 0x80
}
return type;
return type << 1;
};
// https://www.w3.org/TR/css-syntax-3/#serialization
@@ -167,14 +169,10 @@ function createMap(pairs) {
type !== FunctionToken &&
type !== CDC) ||
(nextCharCode === PLUSSIGN)
? isWhiteSpaceRequired.has(prevCode << 16 | nextCharCode << 8)
: isWhiteSpaceRequired.has(prevCode << 16 | nextCode);
? isWhiteSpaceRequired.has((prevCode & 0xFFFE) << 16 | nextCharCode << 7)
: isWhiteSpaceRequired.has((prevCode & 0xFFFE) << 16 | nextCode);
if (emitWs) {
this.emit(' ', WhiteSpace, true);
}
return nextCode;
return nextCode | emitWs;
};
}

View File

@@ -34,7 +34,91 @@ import {
RightCurlyBracket
} from '../tokenizer/index.js';
const calcFunctionNames = ['calc(', '-moz-calc(', '-webkit-calc('];
// CSS mathematical functions categorized by return type behavior
// See: https://www.w3.org/TR/css-values-4/#math
// Calculation functions that return different types depending on input
const calcFunctionNames = [
'calc(',
'-moz-calc(',
'-webkit-calc('
];
// Comparison functions that return different types depending on input
const comparisonFunctionNames = [
'min(',
'max(',
'clamp('
];
// Functions that return a stepped value, i.e. a value that is rounded to the nearest step
const steppedValueFunctionNames = [
'round(',
'mod(',
'rem('
];
// Trigonometrical functions that return a <number>
const trigNumberFunctionNames = [
'sin(',
'cos(',
'tan('
];
// Trigonometrical functions that return a <angle>
const trigAngleFunctionNames = [
'asin(',
'acos(',
'atan(',
'atan2('
];
// Other functions that return a <number>
const otherNumberFunctionNames = [
'pow(',
'sqrt(',
'log(',
'exp(',
'sign('
];
// Exponential functions that return a <number> or <dimension> or <percentage>
const expNumberDimensionPercentageFunctionNames = [
'hypot('
];
// Return the same type as the input
const signFunctionNames = [
'abs('
];
const numberFunctionNames = [
...calcFunctionNames,
...comparisonFunctionNames,
...steppedValueFunctionNames,
...trigNumberFunctionNames,
...otherNumberFunctionNames,
...expNumberDimensionPercentageFunctionNames,
...signFunctionNames
];
const percentageFunctionNames = [
...calcFunctionNames,
...comparisonFunctionNames,
...steppedValueFunctionNames,
...expNumberDimensionPercentageFunctionNames,
...signFunctionNames
];
const dimensionFunctionNames = [
...calcFunctionNames,
...comparisonFunctionNames,
...steppedValueFunctionNames,
...trigAngleFunctionNames,
...expNumberDimensionPercentageFunctionNames,
...signFunctionNames
];
const balancePair = new Map([
[FunctionToken, RightParenthesis],
[LeftParenthesis, RightParenthesis],
@@ -141,16 +225,17 @@ function consumeFunction(token, getNextToken) {
return length;
}
// TODO: implement
// can be used wherever <length>, <frequency>, <angle>, <time>, <percentage>, <number>, or <integer> values are allowed
// https://drafts.csswg.org/css-values/#calc-notation
function calc(next) {
function math(next, functionNames) {
return function(token, getNextToken, opts) {
if (token === null) {
return 0;
}
if (token.type === FunctionToken && eqStrAny(token.value, calcFunctionNames)) {
if (token.type === FunctionToken && eqStrAny(token.value, functionNames)) {
return consumeFunction(token, getNextToken);
}
@@ -557,12 +642,12 @@ export const productionTypes = {
'ident': tokenType(Ident),
// percentage
'percentage': calc(percentage),
'percentage': math(percentage, percentageFunctionNames),
// numeric
'zero': zero(),
'number': calc(number),
'integer': calc(integer),
'number': math(number, numberFunctionNames),
'integer': math(integer, numberFunctionNames),
// complex types
'custom-ident': customIdent,
@@ -601,15 +686,45 @@ export function createDemensionTypes(units) {
} = units || {};
return {
'dimension': calc(dimension(null)),
'angle': calc(dimension(angle)),
'decibel': calc(dimension(decibel)),
'frequency': calc(dimension(frequency)),
'flex': calc(dimension(flex)),
'length': calc(zero(dimension(length))),
'resolution': calc(dimension(resolution)),
'semitones': calc(dimension(semitones)),
'time': calc(dimension(time))
'dimension': math(dimension(null), dimensionFunctionNames),
'angle': math(dimension(angle), dimensionFunctionNames),
'decibel': math(dimension(decibel), dimensionFunctionNames),
'frequency': math(dimension(frequency), dimensionFunctionNames),
'flex': math(dimension(flex), dimensionFunctionNames),
'length': math(zero(dimension(length)), dimensionFunctionNames),
'resolution': math(dimension(resolution), dimensionFunctionNames),
'semitones': math(dimension(semitones), dimensionFunctionNames),
'time': math(dimension(time), dimensionFunctionNames)
};
}
// The <attr-unit> production matches any identifier that is an ASCII case-insensitive
// match for the name of a CSS dimension unit, such as px, or the <delim-token> %.
function createAttrUnit(units) {
const unitSet = new Set();
for (const group of unitGroups) {
if (Array.isArray(units[group])) {
for (const unit of units[group]) {
unitSet.add(unit.toLowerCase());
}
}
}
return function attrUnit(token) {
if (token === null) {
return 0;
}
if (token.type === Delim && token.value === '%') {
return 1;
}
if (token.type === Ident && unitSet.has(token.value.toLowerCase())) {
return 1;
}
return 0;
};
}
@@ -617,6 +732,7 @@ export function createGenericTypes(units) {
return {
...tokenTypes,
...productionTypes,
...createDemensionTypes(units)
...createDemensionTypes(units),
'attr-unit': createAttrUnit(units)
};
};

View File

@@ -29,6 +29,36 @@ const SEMICOLON = 0x003B; // U+003B SEMICOLON (;)
const LEFTCURLYBRACKET = 0x007B; // U+007B LEFT CURLY BRACKET ({)
const NULL = 0;
const arrayMethods = {
createList() {
return [];
},
createSingleNodeList(node) {
return [node];
},
getFirstListNode(list) {
return list && list[0] || null;
},
getLastListNode(list) {
return list && list.length > 0 ? list[list.length - 1] : null;
}
};
const listMethods = {
createList() {
return new List();
},
createSingleNodeList(node) {
return new List().appendData(node);
},
getFirstListNode(list) {
return list && list.first;
},
getLastListNode(list) {
return list && list.last;
}
};
function createParseContext(name) {
return function() {
return this[name]();
@@ -109,18 +139,10 @@ export function createParser(config) {
return code === SEMICOLON ? 2 : 0;
},
createList() {
return new List();
},
createSingleNodeList(node) {
return new List().appendData(node);
},
getFirstListNode(list) {
return list && list.first;
},
getLastListNode(list) {
return list && list.last;
},
createList: NOOP,
createSingleNodeList: NOOP,
getFirstListNode: NOOP,
getLastListNode: NOOP,
parseWithFallback(consumer, fallback) {
const startIndex = this.tokenIndex;
@@ -292,6 +314,36 @@ export function createParser(config) {
);
}
});
const createTokenIterateAPI = () => ({
filename,
source,
tokenCount: parser.tokenCount,
getTokenType: (index) =>
parser.getTokenType(index),
getTokenTypeName: (index) =>
tokenNames[parser.getTokenType(index)],
getTokenStart: (index) =>
parser.getTokenStart(index),
getTokenEnd: (index) =>
parser.getTokenEnd(index),
getTokenValue: (index) =>
parser.source.substring(parser.getTokenStart(index), parser.getTokenEnd(index)),
substring: (start, end) =>
parser.source.substring(start, end),
balance: parser.balance.subarray(0, parser.tokenCount + 1),
isBlockOpenerTokenType: parser.isBlockOpenerTokenType,
isBlockCloserTokenType: parser.isBlockCloserTokenType,
getBlockTokenPairIndex: (index) =>
parser.getBlockTokenPairIndex(index),
getLocation: (offset) =>
locationMap.getLocation(offset, filename),
getRangeLocation: (start, end) =>
locationMap.getLocationRange(start, end, filename)
});
const parse = function(source_, options) {
source = source_;
@@ -315,12 +367,22 @@ export function createParser(config) {
parser.parseValue = 'parseValue' in options ? Boolean(options.parseValue) : true;
parser.parseCustomProperty = 'parseCustomProperty' in options ? Boolean(options.parseCustomProperty) : false;
const { context = 'default', onComment } = options;
const { context = 'default', list = true, onComment, onToken } = options;
if (context in parser.context === false) {
throw new Error('Unknown context `' + context + '`');
}
Object.assign(parser, list ? listMethods : arrayMethods);
if (Array.isArray(onToken)) {
parser.forEachToken((type, start, end) => {
onToken.push({ type, start, end });
});
} else if (typeof onToken === 'function') {
parser.forEachToken(onToken.bind(createTokenIterateAPI()));
}
if (typeof onComment === 'function') {
parser.forEachToken((type, start, end) => {
if (type === Comment) {

View File

@@ -8,23 +8,31 @@ function appendOrSet(a, b) {
return b || null;
}
function sliceProps(obj, props) {
function extractProps(obj, props) {
const result = Object.create(null);
for (const [key, value] of Object.entries(obj)) {
if (value) {
result[key] = {};
for (const prop of Object.keys(value)) {
if (props.includes(prop)) {
result[key][prop] = value[prop];
}
}
for (const prop of Object.keys(obj)) {
if (props.includes(prop)) {
result[prop] = obj[prop];
}
}
return result;
}
function mergeDicts(base, ext, fields) {
const result = { ...base };
for (const [key, props] of Object.entries(ext)) {
result[key] = {
...result[key],
...fields ? extractProps(props, fields) : props
};
}
return result;
}
export default function mix(dest, src) {
const result = { ...dest };
@@ -87,14 +95,6 @@ export default function mix(dest, src) {
}
break;
case 'scope':
case 'features':
result[prop] = { ...dest[prop] };
for (const [name, props] of Object.entries(value)) {
result[prop][name] = { ...result[prop][name], ...props };
}
break;
case 'parseContext':
result[prop] = {
...dest[prop],
@@ -102,19 +102,18 @@ export default function mix(dest, src) {
};
break;
case 'scope':
case 'features':
result[prop] = mergeDicts(dest[prop], value);
break;
case 'atrule':
case 'pseudo':
result[prop] = {
...dest[prop],
...sliceProps(value, ['parse'])
};
result[prop] = mergeDicts(dest[prop], value, ['parse']);
break;
case 'node':
result[prop] = {
...dest[prop],
...sliceProps(value, ['name', 'structure', 'parse', 'generate', 'walkContext'])
};
result[prop] = mergeDicts(dest[prop], value, ['name', 'structure', 'parse', 'generate', 'walkContext']);
break;
}
}

View File

@@ -17,14 +17,25 @@ import {
const OFFSET_MASK = 0x00FFFFFF;
const TYPE_SHIFT = 24;
const BLOCK_OPEN_TOKEN = 1;
const BLOCK_CLOSE_TOKEN = 2;
const balancePair = new Uint8Array(32); // 32b of memory ought to be enough for anyone (any number of tokens)
balancePair[FunctionToken] = RightParenthesis;
balancePair[LeftParenthesis] = RightParenthesis;
balancePair[LeftSquareBracket] = RightSquareBracket;
balancePair[LeftCurlyBracket] = RightCurlyBracket;
function isBlockOpenerToken(tokenType) {
return balancePair[tokenType] !== 0;
const blockTokens = new Uint8Array(32);
blockTokens[FunctionToken] = BLOCK_OPEN_TOKEN;
blockTokens[LeftParenthesis] = BLOCK_OPEN_TOKEN;
blockTokens[LeftSquareBracket] = BLOCK_OPEN_TOKEN;
blockTokens[LeftCurlyBracket] = BLOCK_OPEN_TOKEN;
blockTokens[RightParenthesis] = BLOCK_CLOSE_TOKEN;
blockTokens[RightSquareBracket] = BLOCK_CLOSE_TOKEN;
blockTokens[RightCurlyBracket] = BLOCK_CLOSE_TOKEN;
function boundIndex(index, min, max) {
return index < min ? min : index > max ? max : index;
}
export class TokenStream {
@@ -76,7 +87,7 @@ export class TokenStream {
// pop state
balanceStart = prevBalanceStart;
balanceCloseType = balancePair[offsetAndType[prevBalanceStart] >> TYPE_SHIFT];
} else if (isBlockOpenerToken(type)) { // check for FunctionToken, <(-token>, <[-token> and <{-token>
} else if (this.isBlockOpenerTokenType(type)) { // check for FunctionToken, <(-token>, <[-token> and <{-token>
// push state
balanceStart = index;
balanceCloseType = balancePair[type];
@@ -194,14 +205,53 @@ export class TokenStream {
return this.firstCharOffset;
}
getTokenEnd(tokenIndex) {
if (tokenIndex === this.tokenIndex) {
return this.tokenEnd;
}
return this.offsetAndType[boundIndex(tokenIndex, 0, this.tokenCount)] & OFFSET_MASK;
}
getTokenType(tokenIndex) {
if (tokenIndex === this.tokenIndex) {
return this.tokenType;
}
return this.offsetAndType[boundIndex(tokenIndex, 0, this.tokenCount)] >> TYPE_SHIFT;
}
substrToCursor(start) {
return this.source.substring(start, this.tokenStart);
}
isBalanceEdge(pos) {
return this.balance[this.tokenIndex] < pos;
// return this.balance[this.balance[pos]] !== this.tokenIndex;
isBlockOpenerTokenType(tokenType) {
return blockTokens[tokenType] === BLOCK_OPEN_TOKEN;
}
isBlockCloserTokenType(tokenType) {
return blockTokens[tokenType] === BLOCK_CLOSE_TOKEN;
}
getBlockTokenPairIndex(tokenIndex) {
const type = this.getTokenType(tokenIndex);
if (blockTokens[type] === 1) {
// block open token
const pairIndex = this.balance[tokenIndex];
const closeType = this.getTokenType(pairIndex);
return balancePair[type] === closeType ? pairIndex : -1;
} else if (blockTokens[type] === 2) {
// block close token
const pairIndex = this.balance[tokenIndex];
const openType = this.getTokenType(pairIndex);
return balancePair[openType] === type ? pairIndex : -1;
}
return -1;
}
isBalanceEdge(tokenIndex) {
return this.balance[this.tokenIndex] < tokenIndex;
}
isDelim(code, offset) {
if (offset) {
return (
@@ -278,7 +328,7 @@ export class TokenStream {
default:
// fast forward to the end of balanced block for an open block tokens
if (isBlockOpenerToken(this.offsetAndType[cursor] >> TYPE_SHIFT)) {
if (this.isBlockOpenerTokenType(this.offsetAndType[cursor] >> TYPE_SHIFT)) {
cursor = balanceEnd;
}
}