Skip to content

Commit

Permalink
实现解析 element
Browse files Browse the repository at this point in the history
  • Loading branch information
jindy committed Jun 18, 2022
1 parent 6893188 commit ae3c3bc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 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 @@ -3,4 +3,5 @@ export const enum NodeTypes {
INTERPOLATION,
// simple_expression
SIMPLE_EXPRESSION,
ELEMENT,
}
38 changes: 37 additions & 1 deletion src/compiler-core/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,49 @@ export function baseParse(content: string) {
function parseChildren(context) {
const nodes: any = []
let node
if (context.source.startsWith("{{")) {
const s = context.source
if (s.startsWith("{{")) {
node = parseInterpolation(context)
} else if (s[0] === "<") {
if (/[a-z]/i.test(s[1])) {
node = parseElement(context)
}
}
nodes.push(node)
return nodes
}

const enum TagType {
START,
END,
}

function parseElement(context) {
const element = parseTag(context, TagType.START)

parseTag(context, TagType.END)

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

return element
}

function parseTag(context: any, TagType) {
// 1.解析tag
const match: any = /^<\/?([a-z]*)/i.exec(context.source)
console.log(match)
const tag = match[1]
// 2.删除处理完成的代码
advanceBy(context, match[0].length)
advanceBy(context, 1)
// console.log(context.source)
if (TagType === TagType.END) return
return {
type: NodeTypes.ELEMENT,
tag,
}
}

function parseInterpolation(context) {
const openDelimiter = "{{"
const closeDelimiter = "}}"
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 @@ -4,9 +4,9 @@ import { baseParse } from "../src/parse"
describe("Parse", () => {
describe("interpolation", () => {
test("simple interpolation", () => {
// root
const ast: any = baseParse("{{massage}}")

// root
expect(ast.children[0]).toStrictEqual({
type: NodeTypes.INTERPOLATION,
content: {
Expand All @@ -16,4 +16,14 @@ describe("Parse", () => {
})
})
})
describe("element", () => {
test("simple element div", () => {
const ast: any = baseParse("<div></div>")

expect(ast.children[0]).toStrictEqual({
type: NodeTypes.ELEMENT,
tag: "div",
})
})
})
})

0 comments on commit ae3c3bc

Please sign in to comment.