Skip to content

Commit

Permalink
实现解析 text
Browse files Browse the repository at this point in the history
  • Loading branch information
jindy committed Jun 19, 2022
1 parent ae3c3bc commit 3d3f7e4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/compiler-core/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export const enum NodeTypes {
// simple_expression
SIMPLE_EXPRESSION,
ELEMENT,
TEXT,
}
32 changes: 29 additions & 3 deletions src/compiler-core/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,36 @@ function parseChildren(context) {
node = parseElement(context)
}
}

if (!node) {
node = parseText(context)
}

nodes.push(node)
return nodes
}

function parseText(context) {
//1.获取内容 content
const content = parseTextData(context, context.source.length)
// 2. 推进 -》 删除处理完成的代码???
// advanceBy(context, content.length)

return {
type: NodeTypes.TEXT,
content,
}
}

function parseTextData(context, length) {
const content = context.source.slice(0, length)

// 2. 推进 -》 删除处理完成的代码???
advanceBy(context, length)

return content
}

const enum TagType {
START,
END,
Expand All @@ -30,7 +56,7 @@ function parseElement(context) {

parseTag(context, TagType.END)

console.log(context.source, "1111")
// console.log(context.source, "1111")

return element
}
Expand Down Expand Up @@ -63,13 +89,13 @@ function parseInterpolation(context) {
advanceBy(context, openDelimiter.length)

const rawContentLength = closeIndex - openDelimiter.length
const rawContent = context.source.slice(0, rawContentLength)
const rawContent = parseTextData(context, rawContentLength) //context.source.slice(0, rawContentLength)
const content = rawContent.trim()

// context.source = context.source.slice(
// rawContentLength + closeDelimiter.length
// )
advanceBy(context, rawContentLength + closeDelimiter.length)
advanceBy(context, closeDelimiter.length)
return {
type: NodeTypes.INTERPOLATION,
content: {
Expand Down
12 changes: 11 additions & 1 deletion src/compiler-core/tests/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("Parse", () => {
})
})
describe("element", () => {
test("simple element div", () => {
it("simple element div", () => {
const ast: any = baseParse("<div></div>")

expect(ast.children[0]).toStrictEqual({
Expand All @@ -26,4 +26,14 @@ describe("Parse", () => {
})
})
})
describe("text", () => {
it("simple text", () => {
const ast: any = baseParse("some text")

expect(ast.children[0]).toStrictEqual({
type: NodeTypes.TEXT,
content: "some text",
})
})
})
})

0 comments on commit 3d3f7e4

Please sign in to comment.