Skip to content

Changed the parsing of custom block to parse like Vue3 parser. #90

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

Merged
merged 4 commits into from
Dec 7, 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
45 changes: 45 additions & 0 deletions scripts/update-fixtures-document-fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,53 @@ for (const name of TARGETS) {
const actual = result.services.getDocumentFragment()

const resultPath = path.join(ROOT, `${name}/document-fragment.json`)
const tokenRangesPath = path.join(ROOT, `${name}/token-ranges.json`)
const treePath = path.join(ROOT, `${name}/tree.json`)

console.log("Update:", name)

const tokenRanges = getAllTokens(actual).map(t =>
source.slice(t.range[0], t.range[1])
)
const tree = getTree(source, actual)

fs.writeFileSync(resultPath, JSON.stringify(actual, replacer, 4))
fs.writeFileSync(tokenRangesPath, JSON.stringify(tokenRanges, replacer, 4))
fs.writeFileSync(treePath, JSON.stringify(tree, replacer, 4))
}

function getAllTokens(fgAst) {
const tokenArrays = [fgAst.tokens, fgAst.comments]

return Array.prototype.concat.apply([], tokenArrays)
}

/**
* Create simple tree.
* @param {string} source The source code.
* @param {VDocumentFragment} fgAst The root node.
* @returns {object} Simple tree.
*/
function getTree(source, fgAst) {
const stack = []
const root = { children: [] }
let current = root

parser.AST.traverseNodes(fgAst, {
enterNode(node) {
stack.push(current)
current.children.push(
(current = {
type: node.type,
text: source.slice(node.range[0], node.range[1]),
children: [],
})
)
},
leaveNode() {
current = stack.pop()
},
})

return root.children
}
49 changes: 33 additions & 16 deletions src/html/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
import * as path from "path"
import assert from "assert"
import last from "lodash/last"
import findLastIndex from "lodash/findLastIndex"
Expand Down Expand Up @@ -150,6 +151,7 @@ export class Parser {
private tokenizer: IntermediateTokenizer
private locationCalculator: LocationCalculator
private parserOptions: ParserOptions
private isSFC: boolean
private document: VDocumentFragment
private elementStack: VElement[]
private vPreElement: VElement | null
Expand Down Expand Up @@ -230,6 +232,8 @@ export class Parser {
tokenizer.lineTerminators,
)
this.parserOptions = parserOptions
this.isSFC =
path.extname(parserOptions.filePath || "unknown.vue") === ".vue"
this.document = {
type: "VDocumentFragment",
range: [0, 0],
Expand Down Expand Up @@ -516,27 +520,40 @@ export class Parser {

// Update the content type of this element.
if (namespace === NS.HTML) {
if (
element.name === "template" &&
element.parent.type === "VDocumentFragment"
) {
if (element.parent.type === "VDocumentFragment") {
const langAttr = element.startTag.attributes.find(
a => !a.directive && a.key.name === "lang",
) as VAttribute | undefined
const lang =
(langAttr && langAttr.value && langAttr.value.value) ||
"html"

if (lang !== "html") {
const lang = langAttr?.value?.value

if (element.name === "template") {
if (lang && lang !== "html") {
// It is not an HTML template.
this.tokenizer.state = "RAWTEXT"
}
this.expressionEnabled = true
} else if (this.isSFC) {
// Element is Custom Block. e.g. <i18n>
// Referred to the Vue parser. See https://github.com/vuejs/vue-next/blob/cbaa3805064cb581fc2007cf63774c91d39844fe/packages/compiler-sfc/src/parse.ts#L127
if (!lang || lang !== "html") {
// Custom Block is not HTML.
this.tokenizer.state = "RAWTEXT"
}
} else {
if (HTML_RCDATA_TAGS.has(element.name)) {
this.tokenizer.state = "RCDATA"
}
if (HTML_RAWTEXT_TAGS.has(element.name)) {
this.tokenizer.state = "RAWTEXT"
}
}
} else {
if (HTML_RCDATA_TAGS.has(element.name)) {
this.tokenizer.state = "RCDATA"
}
if (HTML_RAWTEXT_TAGS.has(element.name)) {
this.tokenizer.state = "RAWTEXT"
}
this.expressionEnabled = true
}
if (HTML_RCDATA_TAGS.has(element.name)) {
this.tokenizer.state = "RCDATA"
}
if (HTML_RAWTEXT_TAGS.has(element.name)) {
this.tokenizer.state = "RAWTEXT"
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/html/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ export class Tokenizer {
this.endToken()
return "BEFORE_ATTRIBUTE_NAME"
}
if (!isLetter(cp)) {
if (!isLetter(cp) && !maybeValidCustomBlock.call(this, cp)) {
this.rollbackProvisionalToken()
this.appendTokenValue(LESS_THAN_SIGN, "HTMLRawText")
this.appendTokenValue(SOLIDUS, "HTMLRawText")
Expand All @@ -973,6 +973,16 @@ export class Tokenizer {

cp = this.consumeNextCodePoint()
}

function maybeValidCustomBlock(this: Tokenizer, nextCp: number) {
return (
this.currentToken &&
this.lastTagOpenToken &&
this.lastTagOpenToken.value.startsWith(
this.currentToken.value + String.fromCodePoint(nextCp),
)
)
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions test/document-fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ describe("services.getDocumentFragment", () => {
expected
)
})

it("should have correct range.", () => {
const resultPath = path.join(ROOT, `${name}/token-ranges.json`)
const expectedText = fs.readFileSync(resultPath, "utf8")
const tokens = getAllTokens(actual).map(t =>
source.slice(t.range[0], t.range[1])
)
const actualText = JSON.stringify(tokens, null, 4)

assert.strictEqual(actualText, expectedText)
})
})
}
})

function getAllTokens(fgAst) {
const tokenArrays = [fgAst.tokens, fgAst.comments]

return Array.prototype.concat.apply([], tokenArrays)
}
Loading