mirror of
https://github.com/vuejs/vue.git
synced 2024-11-21 20:28:54 +00:00
refactor: add types for html parser options
This commit is contained in:
parent
ba08e96877
commit
846e3165d8
@ -14,7 +14,7 @@ export interface SFCCustomBlock {
|
||||
content: string
|
||||
attrs: { [key: string]: string | true }
|
||||
start: number
|
||||
end?: number
|
||||
end: number
|
||||
map?: RawSourceMap
|
||||
}
|
||||
|
||||
@ -115,6 +115,7 @@ 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
|
||||
@ -122,7 +123,15 @@ export function parseComponent(
|
||||
}
|
||||
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
|
||||
|
@ -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 =
|
||||
@ -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
|
||||
@ -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,
|
||||
@ -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] || ''
|
||||
|
@ -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
|
||||
)
|
||||
}
|
||||
})
|
||||
@ -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) {
|
||||
|
@ -20,6 +20,7 @@ export type CompilerOptions = {
|
||||
shouldDecodeNewlines?: boolean
|
||||
shouldDecodeNewlinesForHref?: boolean
|
||||
outputSourceRange?: boolean
|
||||
shouldKeepComment?: boolean
|
||||
|
||||
// runtime user-configurable
|
||||
delimiters?: [string, string] // template delimiters
|
||||
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user