Skip to content

Commit

Permalink
refactor: add types for html parser options
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 14, 2022
1 parent ba08e96 commit 846e316
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
13 changes: 11 additions & 2 deletions packages/compiler-sfc/src/parseComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface SFCCustomBlock {
content: string
attrs: { [key: string]: string | true }
start: number
end?: number
end: number
map?: RawSourceMap
}

Expand Down Expand Up @@ -115,14 +115,23 @@ export function parseComponent(
type: tag,
content: '',
start: end,
end: 0, // will be set on tag close
attrs: attrs.reduce((cumulated, { name, value }) => {
cumulated[name] = value || true
return cumulated
}, {})
}
if (isSpecialTag(tag)) {
checkAttrs(currentBlock, attrs)
if (tag === 'style') {
if (tag === 'script') {
const block = currentBlock as SFCScriptBlock
if (block.attrs.setup) {
block.setup = currentBlock.attrs.setup
sfc.scriptSetup = block
} else {
sfc.script = block
}
} else if (tag === 'style') {
sfc.styles.push(currentBlock)
} else {
sfc[tag] = currentBlock
Expand Down
20 changes: 17 additions & 3 deletions src/compiler/parser/html-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { makeMap, no } from 'shared/util'
import { isNonPhrasingTag } from 'web/compiler/util'
import { unicodeRegExp } from 'core/util/lang'
import { ASTAttr, CompilerOptions } from 'types/compiler'

// Regular Expressions for parsing tags and attributes
const attribute =
Expand Down Expand Up @@ -54,7 +55,20 @@ function decodeAttr(value, shouldDecodeNewlines) {
return value.replace(re, match => decodingMap[match])
}

export function parseHTML(html, options) {
export interface HTMLParserOptions extends CompilerOptions {
start?: (
tag: string,
attrs: ASTAttr[],
unary: boolean,
start: number,
end: number
) => void
end?: (tag: string, start: number, end: number) => void
chars?: (text: string, start?: number, end?: number) => void
comment?: (content: string, start: number, end: number) => void
}

export function parseHTML(html, options: HTMLParserOptions) {
const stack: any[] = []
const expectHTML = options.expectHTML
const isUnaryTag = options.isUnaryTag || no
Expand All @@ -72,7 +86,7 @@ export function parseHTML(html, options) {
const commentEnd = html.indexOf('-->')

if (commentEnd >= 0) {
if (options.shouldKeepComment) {
if (options.shouldKeepComment && options.comment) {
options.comment(
html.substring(4, commentEnd),
index,
Expand Down Expand Up @@ -242,7 +256,7 @@ export function parseHTML(html, options) {
const unary = isUnaryTag(tagName) || !!unarySlash

const l = match.attrs.length
const attrs = new Array(l)
const attrs: ASTAttr[] = new Array(l)
for (let i = 0; i < l; i++) {
const args = match.attrs[i]
const value = args[3] || args[4] || args[5] || ''
Expand Down
12 changes: 7 additions & 5 deletions src/compiler/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,12 @@ export function parse(template: string, options: CompilerOptions): ASTElement {
warn(
`Invalid dynamic argument expression: attribute names cannot contain ` +
`spaces, quotes, <, >, / or =.`,
{
start: attr.start + attr.name.indexOf(`[`),
end: attr.start + attr.name.length
}
options.outputSourceRange
? {
start: attr.start! + attr.name.indexOf(`[`),
end: attr.start! + attr.name.length
}
: undefined
)
}
})
Expand Down Expand Up @@ -322,7 +324,7 @@ export function parse(template: string, options: CompilerOptions): ASTElement {
closeElement(element)
},

chars(text: string, start: number, end: number) {
chars(text: string, start?: number, end?: number) {
if (!currentParent) {
if (__DEV__) {
if (text === template) {
Expand Down
9 changes: 5 additions & 4 deletions src/types/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type CompilerOptions = {
shouldDecodeNewlines?: boolean
shouldDecodeNewlinesForHref?: boolean
outputSourceRange?: boolean
shouldKeepComment?: boolean

// runtime user-configurable
delimiters?: [string, string] // template delimiters
Expand Down Expand Up @@ -47,13 +48,13 @@ export type CompiledResult = {
export type ModuleOptions = {
// transform an AST node before any attributes are processed
// returning an ASTElement from pre/transforms replaces the element
preTransformNode: (el: ASTElement) => ASTElement | null
preTransformNode?: (el: ASTElement) => ASTElement | null | void
// transform an AST node after built-ins like v-if, v-for are processed
transformNode: (el: ASTElement) => ASTElement | null
transformNode?: (el: ASTElement) => ASTElement | null | void
// transform an AST node after its children have been processed
// cannot return replacement in postTransform because tree is already finalized
postTransformNode: (el: ASTElement) => void
genData: (el: ASTElement) => string // generate extra data string for an element
postTransformNode?: (el: ASTElement) => void
genData?: (el: ASTElement) => string // generate extra data string for an element
transformCode?: (el: ASTElement, code: string) => string // further transform generated code for an element
staticKeys?: Array<string> // AST properties to be considered static
}
Expand Down

0 comments on commit 846e316

Please sign in to comment.