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

fix(runtime): 修复 html 解析器分析属性时对空格的错误处理,fix #6911 #7247

Merged
merged 2 commits into from
Aug 4, 2020
Merged
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
4 changes: 3 additions & 1 deletion packages/taro-runtime/src/dom/html/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ function splitEqual (str: string) {
const sep = '='
const idx = str.indexOf(sep)
if (idx === -1) return [str]
return [str.slice(0, idx), str.slice(idx + sep.length)]
const key = str.slice(0, idx).trim()
const value = str.slice(idx + sep.length).trim()
return [key, value]
}

function format (children: ChildNode[]) {
Expand Down
32 changes: 30 additions & 2 deletions packages/taro-runtime/src/dom/html/scaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ function isWhitespaceChar (char: string) {
return whitespace.test(char)
}

const equalSign = /=/
function isEqualSignChar (char: string) {
return equalSign.test(char)
}

function shouldBeIgnore (tagName: string) {
const name = tagName.toLowerCase()
if (options.html.skipElements.has(name)) {
Expand All @@ -86,6 +91,30 @@ function findTextEnd (str: string, index: number) {
}
}

function isWordEnd (cursor: number, wordBegin: number, html: string) {
if (!isWhitespaceChar(html.charAt(cursor))) return false

const len = html.length

// backwrad
for (let i = cursor - 1; i > wordBegin; i--) {
const char = html.charAt(i)
if (!isWhitespaceChar(char)) {
if (isEqualSignChar(char)) return false
break
}
}

// forward
for (let i = cursor + 1; i < len; i++) {
const char = html.charAt(i)
if (!isWhitespaceChar(char)) {
if (isEqualSignChar(char)) return false
return true
}
}
}

export class Scaner {
private tokens: Token[] = []

Expand Down Expand Up @@ -248,8 +277,7 @@ export class Scaner {
break
}

const isWordEnd = isWhitespaceChar(char)
if (isWordEnd) {
if (isWordEnd(cursor, wordBegin, html)) {
if (cursor !== wordBegin) {
words.push(html.slice(wordBegin, cursor))
}
Expand Down