This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
/
fragment-opcode-compiler.js
79 lines (61 loc) · 2.12 KB
/
fragment-opcode-compiler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import TemplateVisitor from "./template-visitor";
import { processOpcodes } from "./utils";
import { getAttrNamespace } from "../htmlbars-util";
import { forEach } from "../htmlbars-util/array-utils";
function FragmentOpcodeCompiler() {
this.opcodes = [];
}
export default FragmentOpcodeCompiler;
FragmentOpcodeCompiler.prototype.compile = function(ast) {
var templateVisitor = new TemplateVisitor();
templateVisitor.visit(ast);
processOpcodes(this, templateVisitor.actions);
return this.opcodes;
};
FragmentOpcodeCompiler.prototype.opcode = function(type, params) {
this.opcodes.push([type, params]);
};
FragmentOpcodeCompiler.prototype.text = function(text) {
this.opcode('createText', [text.chars]);
this.opcode('appendChild');
};
FragmentOpcodeCompiler.prototype.comment = function(comment) {
this.opcode('createComment', [comment.value]);
this.opcode('appendChild');
};
FragmentOpcodeCompiler.prototype.openElement = function(element) {
this.opcode('createElement', [element.tag]);
forEach(element.attributes, this.attribute, this);
};
FragmentOpcodeCompiler.prototype.closeElement = function() {
this.opcode('appendChild');
};
FragmentOpcodeCompiler.prototype.startProgram = function() {
this.opcodes.length = 0;
this.opcode('createFragment');
};
FragmentOpcodeCompiler.prototype.endProgram = function() {
this.opcode('returnNode');
};
FragmentOpcodeCompiler.prototype.mustache = function() {
this.pushMorphPlaceholderNode();
};
FragmentOpcodeCompiler.prototype.component = function() {
this.pushMorphPlaceholderNode();
};
FragmentOpcodeCompiler.prototype.block = function() {
this.pushMorphPlaceholderNode();
};
FragmentOpcodeCompiler.prototype.pushMorphPlaceholderNode = function() {
this.opcode('createComment', [""]);
this.opcode('appendChild');
};
FragmentOpcodeCompiler.prototype.attribute = function(attr) {
if (attr.value.type === 'TextNode') {
var namespace = getAttrNamespace(attr.name);
this.opcode('setAttribute', [attr.name, attr.value.chars, namespace]);
}
};
FragmentOpcodeCompiler.prototype.setNamespace = function(namespace) {
this.opcode('setNamespace', [namespace]);
};