Skip to content

Instantly share code, notes, and snippets.

@QuarkGluonPlasma
Created April 1, 2021 04:11
Show Gist options
  • Save QuarkGluonPlasma/28aae9441304c8c5dc705397799fa159 to your computer and use it in GitHub Desktop.
Save QuarkGluonPlasma/28aae9441304c8c5dc705397799fa159 to your computer and use it in GitHub Desktop.
抽象了下vue template compiler的大概结构
function parse(template, options) {
// 处理options
var stack = [];
var currentParent;
var root;
function closeElement(element) {
currentParent.children.push(element);
element.parent = currentParent;
}
parseHTML(template, {
//其他处理后的options
start (tag, attrs) {
var element = createASTElement(tag, attrs, currentParent);
processFor(element);
processIf(element);
processOnce(element);
currentParent = element;
stack.push(element);
if(!root) {
root = element;
}
},
end (tag) {
currentParent = stack[stack.length - 1];
closeElement(element);
},
chars (text) {
if (有表达式) {
currentParent.children.push({
type: 2,
expression: res.expression,
tokens: res.tokens,
text: text
})
}
currentParent.children.push({
type: 3,
text: text
});
},
comment () {
currentParent.children.push({
type: 3,
text: text,
isComment: true
});
}
});
return root;
}
function parseHTML (html, options) {
while (html) {
if(如果以<开头) {
if(如果是'<!--'开头) {
const commentEnd = 找到-->的位置
options.comment(这个comment);
}
if (如果是'<['开头){
const conditionalEnd = 找到']>'的位置
不处理
continue;
}
if (如果是<!doctype xxx>) {
不处理
continue;
}
if (如果是结束标签) {
options.end(结束标签);
}
options.end(开始标签);
} else {
处理文本
options.chars(文本)
}
}
处理结束标签
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment