Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import valueParser from 'postcss-value-parser';
import { CSSStyleValue } from './CSSStyleValue';
import { CSSVariableReferenceValue } from './CSSVariableReferenceValue';
import { errorMsg, warnMsg } from '../../../shared/logUtils';

Expand Down Expand Up @@ -39,16 +38,16 @@ function splitComponentValueListByComma(
const memoizedValues = new Map<string, CSSUnparsedValue>();

// https://drafts.css-houdini.org/css-typed-om-1/#cssunparsedvalue
export class CSSUnparsedValue extends CSSStyleValue {
static #resolveVariableName(input: PostCSSValueASTNode[]): string | null {
export class CSSUnparsedValue /*extends CSSStyleValue*/ {
static _resolveVariableName(input: PostCSSValueASTNode[]): string | null {
const cleanedInput = input.filter((i) => i.type === 'word');
if (cleanedInput.length !== 1) {
return null;
}
return valueParser.stringify(cleanedInput[0]);
}

static #resolveUnparsedValue(
static _resolveUnparsedValue(
input: PostCSSValueASTNode[],
depth: number = 0
): CSSUnparsedValue {
Expand Down Expand Up @@ -78,7 +77,7 @@ export class CSSUnparsedValue extends CSSStyleValue {
if (currentValue.type === 'function') {
if (currentValue.value === 'var') {
const args = splitComponentValueListByComma(currentValue.nodes);
const variableName = CSSUnparsedValue.#resolveVariableName(args[0]);
const variableName = CSSUnparsedValue._resolveVariableName(args[0]);
if (variableName == null) {
if (__DEV__) {
warnMsg(
Expand All @@ -92,7 +91,7 @@ export class CSSUnparsedValue extends CSSStyleValue {

const fallbackValue =
args[1] != null
? CSSUnparsedValue.#resolveUnparsedValue(args[1], depth + 1)
? CSSUnparsedValue._resolveUnparsedValue(args[1], depth + 1)
: undefined;

try {
Expand All @@ -110,7 +109,7 @@ export class CSSUnparsedValue extends CSSStyleValue {
} else {
// stringify the function manually but still attempt to resolve the args
appendString(`${currentValue.value}(`);
const functionArgs = CSSUnparsedValue.#resolveUnparsedValue(
const functionArgs = CSSUnparsedValue._resolveUnparsedValue(
currentValue.nodes,
depth + 1
);
Expand Down Expand Up @@ -140,32 +139,32 @@ export class CSSUnparsedValue extends CSSStyleValue {
}
const componentValueList = valueParser(input).nodes;
const parsedValue =
CSSUnparsedValue.#resolveUnparsedValue(componentValueList);
CSSUnparsedValue._resolveUnparsedValue(componentValueList);
memoizedValues.set(input, parsedValue);
return parsedValue;
}

#tokens: CSSUnparsedSegment[];
_tokens: CSSUnparsedSegment[];

constructor(members: CSSUnparsedSegment[]) {
super();
this.#tokens = members;
// No super() call because it's slow in Hermes
this._tokens = members;
}

get(index: number): CSSUnparsedSegment {
return this.#tokens[index];
return this._tokens[index];
}

get size(): number {
return this.#tokens.length;
return this._tokens.length;
}

values(): Iterator<CSSUnparsedSegment> {
return this.#tokens.values();
return this._tokens.values();
}

toString(): string {
return this.#tokens
return this._tokens
.map((t) => t.toString())
.join('')
.trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,42 @@

import type { CSSUnparsedValue } from './CSSUnparsedValue';

import { CSSStyleValue } from './CSSStyleValue';

// https://drafts.css-houdini.org/css-typed-om-1/#cssvariablereferencevalue
export class CSSVariableReferenceValue extends CSSStyleValue {
export class CSSVariableReferenceValue /*extends CSSStyleValue*/ {
// https://drafts.css-houdini.org/css-typed-om-1/#custom-property-name-string
static #validateVariableName(variable: string): void {
static _validateVariableName(variable: string): void {
if (!variable.startsWith('--')) {
throw new TypeError(`Invalid custom property name: ${variable}`);
}
}

#variable: string;
#fallback: CSSUnparsedValue | null;
_variable: string;
_fallback: CSSUnparsedValue | null;

constructor(variable: string, fallback?: CSSUnparsedValue) {
if (__DEV__) {
CSSVariableReferenceValue.#validateVariableName(variable);
CSSVariableReferenceValue._validateVariableName(variable);
}
super();
this.#variable = variable;
this.#fallback = fallback ?? null;
// No super() call because it's slow in Hermes
this._variable = variable;
this._fallback = fallback ?? null;
}

get variable(): string {
return this.#variable;
return this._variable;
}

set variable(variable: string): void {
this.#variable = variable;
this._variable = variable;
}

get fallback(): CSSUnparsedValue | null {
return this.#fallback;
return this._fallback;
}

toString(): string {
return `var(${this.#variable}${
this.#fallback ? `, ${this.#fallback.toString()}` : ''
return `var(${this._variable}${
this._fallback ? `, ${this._fallback.toString()}` : ''
})`;
}
}