Skip to content

Commit 08bc759

Browse files
committed
refactor: simplify html parser regex
1 parent 31d3ec6 commit 08bc759

File tree

1 file changed

+4
-19
lines changed

1 file changed

+4
-19
lines changed

Diff for: src/compiler/parser/html-parser.js

+4-19
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,14 @@ import { makeMap, no } from 'shared/util'
1313
import { isNonPhrasingTag } from 'web/compiler/util'
1414

1515
// Regular Expressions for parsing tags and attributes
16-
const singleAttrIdentifier = /([^\s"'<>/=]+)/
17-
const singleAttrAssign = /(=)/
18-
const singleAttrValues = [
19-
// attr value double quotes
20-
/"([^"]*)"+/.source,
21-
// attr value, single quotes
22-
/'([^']*)'+/.source,
23-
// attr value, no quotes
24-
/([^\s"'=<>`]+)/.source
25-
]
26-
const attribute = new RegExp(
27-
'^\\s*' + singleAttrIdentifier.source +
28-
'(?:\\s*' + singleAttrAssign.source +
29-
'\\s*(?:' + singleAttrValues.join('|') + '))?'
30-
)
31-
16+
const attribute = /^\s*([^\s"'<>\/=]+)(?:\s*(=)\s*(?:"([^"]*)"+|'([^']*)'+|([^\s"'=<>`]+)))?/
3217
// could use https://www.w3.org/TR/1999/REC-xml-names-19990114/#NT-QName
3318
// but for Vue templates we can enforce a simple charset
3419
const ncname = '[a-zA-Z_][\\w\\-\\.]*'
35-
const qnameCapture = '((?:' + ncname + '\\:)?' + ncname + ')'
36-
const startTagOpen = new RegExp('^<' + qnameCapture)
20+
const qnameCapture = `((?:${ncname}\\:)?${ncname})`
21+
const startTagOpen = new RegExp(`^<${qnameCapture}`)
3722
const startTagClose = /^\s*(\/?)>/
38-
const endTag = new RegExp('^<\\/' + qnameCapture + '[^>]*>')
23+
const endTag = new RegExp(`^<\\/${qnameCapture}[^>]*>`)
3924
const doctype = /^<!DOCTYPE [^>]+>/i
4025
const comment = /^<!--/
4126
const conditionalComment = /^<!\[/

0 commit comments

Comments
 (0)