forked from Ele-Peng/toy-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseHTML4.js
121 lines (98 loc) · 2.07 KB
/
parseHTML4.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// emitToken 创建元素
let currentToken = null
function emit(token) {
if (token.type != "text")
console.log(token)
}
const EOF = Symbol("EOF")
function data(char) {
if (char == "<") {
return tagOpen
} else if (char == "/") {
return selfClosingStartTag
} else if (char == ">") {
emit(currentToken)
return data
} else if (char == EOF) {
emit({
type: "EOF"
})
return
} else {
return data
}
}
// 1. 开始标签
// 2. 结束标签
// 3. 自封闭标签
function tagOpen(char) {
if (char == "/") { // 结束标签
return endTagOpen
} else if (char.match(/^[a-zA-Z]$/)) { // 开始标签
currentToken = {
type: "startTag",
tagName: ""
}
return tagName(char)
} else {
return
}
}
function endTagOpen(char) {
if (char.match(/^[a-zA-Z]$/)) {
currentToken = {
type: "endTag",
tagName: ""
}
return tagName(char)
} else if (char == ">") {
} else if(char == EOF) {
}
}
function tagName(char) {
if (char.match(/^[\t\n\f ]$/)) {
return beforeAttributeName(char)
} else if (char == "/") {
return selfClosingStartTag
} else if (char.match(/^[a-zA-Z]$/)) {
currentToken.tagName += char.toLowerCase()
return tagName
} else if (char == ">") {
emit(currentToken)
return data
} else {
return tagName
}
}
function beforeAttributeName(char) {
if (char.match(/^[\t\n\f ]$/)) {
return beforeAttributeName
} else if (char == ">") {
return beforeAttributeName
} else if (char == "/") {
return selfClosingStartTag
} else if (char == EOF) {
return
} else if (char == "=") {
return data
} else {
return beforeAttributeName
}
}
function selfClosingStartTag(char) {
if (char == ">" || char == "/") {
currentToken.isSelfClosing = true
currentToken.type = "selfClosingTag"
emit(currentToken)
return data
} else if (char == "EOF") {
} else {
}
}
module.exports.parseHTML = function parseHTML(html){
let state = data
for (let char of html) {
state = state(char)
}
state = state(EOF)
}