Skip to content

Commit 846e316

Browse files
committed
refactor: add types for html parser options
1 parent ba08e96 commit 846e316

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

packages/compiler-sfc/src/parseComponent.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface SFCCustomBlock {
1414
content: string
1515
attrs: { [key: string]: string | true }
1616
start: number
17-
end?: number
17+
end: number
1818
map?: RawSourceMap
1919
}
2020

@@ -115,14 +115,23 @@ export function parseComponent(
115115
type: tag,
116116
content: '',
117117
start: end,
118+
end: 0, // will be set on tag close
118119
attrs: attrs.reduce((cumulated, { name, value }) => {
119120
cumulated[name] = value || true
120121
return cumulated
121122
}, {})
122123
}
123124
if (isSpecialTag(tag)) {
124125
checkAttrs(currentBlock, attrs)
125-
if (tag === 'style') {
126+
if (tag === 'script') {
127+
const block = currentBlock as SFCScriptBlock
128+
if (block.attrs.setup) {
129+
block.setup = currentBlock.attrs.setup
130+
sfc.scriptSetup = block
131+
} else {
132+
sfc.script = block
133+
}
134+
} else if (tag === 'style') {
126135
sfc.styles.push(currentBlock)
127136
} else {
128137
sfc[tag] = currentBlock

src/compiler/parser/html-parser.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import { makeMap, no } from 'shared/util'
1313
import { isNonPhrasingTag } from 'web/compiler/util'
1414
import { unicodeRegExp } from 'core/util/lang'
15+
import { ASTAttr, CompilerOptions } from 'types/compiler'
1516

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

57-
export function parseHTML(html, options) {
58+
export interface HTMLParserOptions extends CompilerOptions {
59+
start?: (
60+
tag: string,
61+
attrs: ASTAttr[],
62+
unary: boolean,
63+
start: number,
64+
end: number
65+
) => void
66+
end?: (tag: string, start: number, end: number) => void
67+
chars?: (text: string, start?: number, end?: number) => void
68+
comment?: (content: string, start: number, end: number) => void
69+
}
70+
71+
export function parseHTML(html, options: HTMLParserOptions) {
5872
const stack: any[] = []
5973
const expectHTML = options.expectHTML
6074
const isUnaryTag = options.isUnaryTag || no
@@ -72,7 +86,7 @@ export function parseHTML(html, options) {
7286
const commentEnd = html.indexOf('-->')
7387

7488
if (commentEnd >= 0) {
75-
if (options.shouldKeepComment) {
89+
if (options.shouldKeepComment && options.comment) {
7690
options.comment(
7791
html.substring(4, commentEnd),
7892
index,
@@ -242,7 +256,7 @@ export function parseHTML(html, options) {
242256
const unary = isUnaryTag(tagName) || !!unarySlash
243257

244258
const l = match.attrs.length
245-
const attrs = new Array(l)
259+
const attrs: ASTAttr[] = new Array(l)
246260
for (let i = 0; i < l; i++) {
247261
const args = match.attrs[i]
248262
const value = args[3] || args[4] || args[5] || ''

src/compiler/parser/index.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,12 @@ export function parse(template: string, options: CompilerOptions): ASTElement {
252252
warn(
253253
`Invalid dynamic argument expression: attribute names cannot contain ` +
254254
`spaces, quotes, <, >, / or =.`,
255-
{
256-
start: attr.start + attr.name.indexOf(`[`),
257-
end: attr.start + attr.name.length
258-
}
255+
options.outputSourceRange
256+
? {
257+
start: attr.start! + attr.name.indexOf(`[`),
258+
end: attr.start! + attr.name.length
259+
}
260+
: undefined
259261
)
260262
}
261263
})
@@ -322,7 +324,7 @@ export function parse(template: string, options: CompilerOptions): ASTElement {
322324
closeElement(element)
323325
},
324326

325-
chars(text: string, start: number, end: number) {
327+
chars(text: string, start?: number, end?: number) {
326328
if (!currentParent) {
327329
if (__DEV__) {
328330
if (text === template) {

src/types/compiler.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type CompilerOptions = {
2020
shouldDecodeNewlines?: boolean
2121
shouldDecodeNewlinesForHref?: boolean
2222
outputSourceRange?: boolean
23+
shouldKeepComment?: boolean
2324

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

0 commit comments

Comments
 (0)