-
Notifications
You must be signed in to change notification settings - Fork 3
/
parseHTML3.js
89 lines (68 loc) · 1.38 KB
/
parseHTML3.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
// 解析标签
const EOF = Symbol("EOF")
function data(char) {
if (char == "<") {
return tagOpen
} else if (char == EOF) {
return
} else {
return data
}
}
// 1. 开始标签
// 2. 结束标签
// 3. 自封闭标签
function tagOpen(char) {
if (char == "/") { // 结束标签
return endTagOpen
} else if (char.match(/^[a-zA-Z]$/)) { // 开始标签
return tagName(char)
} else {
return
}
}
function endTagOpen(char) {
if (char.match(/^[a-zA-Z]$/)) {
return tagName(char)
} else if (char == ">") {
} else if(char == EOF) {
}
}
function tagName(char) {
if (char.match(/^[\t\n\f ]$/)) {
return beforeAttributeName
} else if (char == "/") {
return selfClosingStartTag
} else if (char.match(/^[a-zA-Z]$/)) {
return tagName
} else if (char == ">") {
return data
} else {
return tagName
}
}
function beforeAttributeName(char) {
if (char.match(/^[\t\n\f ]$/)) {
return beforeAttributeName
} else if (char == ">") {
return data
} else if (char == "=") {
return beforeAttributeName
} else {
return beforeAttributeName
}
}
function selfClosingStartTag(char) {
if (char == ">") {
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)
}