Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support default values for v-bind in CSS #235

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/html/util/unicode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const LATIN_SMALL_Z = 0x7A
export const LEFT_CURLY_BRACKET = 0x7B // {
export const RIGHT_CURLY_BRACKET = 0x7D // }
export const NULL_REPLACEMENT = 0xFFFD
export const COMMA = 0x2C

/**
* Check whether the code point is a whitespace.
Expand Down
43 changes: 42 additions & 1 deletion src/style/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function parseStyle(
quote,
openingParenOffset,
comments,
defaultValue,
} of iterateVBind(code, cssOptions)) {
insertComments(
document,
Expand Down Expand Up @@ -201,6 +202,32 @@ function parseStyle(
),
)
}
if (defaultValue.length) {
afterTokens.unshift(
createSimpleToken(
"Punctuator",
locationCalculator.getOffsetWithGap(
defaultValue[0].range[0],
),
locationCalculator.getOffsetWithGap(
defaultValue[0].range[1],
),
",",
locationCalculator,
),
createSimpleToken(
"HTMLRawText",
locationCalculator.getOffsetWithGap(
defaultValue[1].range[0],
),
locationCalculator.getOffsetWithGap(
defaultValue[1].range[1],
),
defaultValue[1].value,
locationCalculator,
),
)
}
const beforeLast = beforeTokens[beforeTokens.length - 1]
replaceAndSplitTokens(
document,
Expand Down Expand Up @@ -287,6 +314,7 @@ type VBindLocations = {
quote: '"' | "'" | null
openingParenOffset: number
comments: CSSCommentToken[]
defaultValue: CSSToken[]
}

/**
Expand Down Expand Up @@ -317,6 +345,7 @@ function* iterateVBind(
quote: arg.quote,
openingParenOffset: openingParen.openingParen.range[0],
comments: [...openingParen.comments, ...arg.comments],
defaultValue: arg.defaultValue,
}
}
}
Expand Down Expand Up @@ -350,6 +379,7 @@ function parseVBindArg(tokenizer: CSSTokenScanner): {
quote: '"' | "'" | null
closingParen: CSSPunctuatorToken
comments: CSSCommentToken[]
defaultValue: CSSToken[]
} | null {
const tokensBuffer: CSSToken[] = []
const comments: CSSCommentToken[] = []
Expand All @@ -370,14 +400,25 @@ function parseVBindArg(tokenizer: CSSTokenScanner): {
quote: quotedToken.quote,
closingParen: token,
comments,
defaultValue: [],
}
}
const startToken = tokensBuffer[0] || token
const commaToken = tokens.find(
(tk) =>
tk.type === CSSTokenType.Punctuator && tk.value === ",",
)
return {
exprRange: [startToken.range[0], token.range[0]],
exprRange: [
startToken.range[0],
(commaToken || token).range[0],
],
quote: null,
closingParen: token,
comments: [],
defaultValue: commaToken
? tokens.slice(tokens.indexOf(commaToken))
: [],
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/style/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SEMICOLON,
LEFT_SQUARE_BRACKET,
RIGHT_SQUARE_BRACKET,
COMMA,
} from "../html/util/unicode"

export const enum CSSTokenType {
Expand Down Expand Up @@ -315,6 +316,7 @@ export class CSSTokenizer {

function isPunctuator(cp: number): boolean {
return (
cp === COMMA ||
cp === COLON ||
cp === SEMICOLON ||
// Brackets
Expand Down
Loading