-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
block.js
116 lines (94 loc) · 2.77 KB
/
block.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
// Source copied and then modified from
// https://github.com/remarkjs/remark/blob/master/packages/remark-parse/lib/tokenize/html-block.js
//
// MIT License https://github.com/remarkjs/remark/blob/master/license
const {openCloseTag} = require('./tag')
module.exports = blockHtml
const tab = '\t'
const space = ' '
const lineFeed = '\n'
const lessThan = '<'
const rawOpenExpression = /^<(script|pre|style)(?=(\s|>|$))/i
const rawCloseExpression = /<\/(script|pre|style)>/i
const commentOpenExpression = /^<!--/
const commentCloseExpression = /-->/
const instructionOpenExpression = /^<\?/
const instructionCloseExpression = /\?>/
const directiveOpenExpression = /^<![A-Za-z]/
const directiveCloseExpression = />/
const cdataOpenExpression = /^<!\[CDATA\[/
const cdataCloseExpression = /\]\]>/
const elementCloseExpression = /^$/
const otherElementOpenExpression = new RegExp(openCloseTag.source + '\\s*$')
const fragmentOpenExpression = /^<>/
function blockHtml(eat, value, silent) {
const blocks = '[a-z\\.]*(\\.){0,1}[a-z][a-z0-9\\.]*'
const elementOpenExpression = new RegExp(
'^</?(' + blocks + ')(?=(\\s|/?>|$))',
'i'
)
const length = value.length
let index = 0
let next
let line
let offset
let character
let count
let sequence
let subvalue
const sequences = [
[rawOpenExpression, rawCloseExpression, true],
[commentOpenExpression, commentCloseExpression, true],
[instructionOpenExpression, instructionCloseExpression, true],
[directiveOpenExpression, directiveCloseExpression, true],
[cdataOpenExpression, cdataCloseExpression, true],
[elementOpenExpression, elementCloseExpression, true],
[fragmentOpenExpression, elementCloseExpression, true],
[otherElementOpenExpression, elementCloseExpression, false]
]
// Eat initial spacing.
while (index < length) {
character = value.charAt(index)
if (character !== tab && character !== space) {
break
}
index++
}
if (value.charAt(index) !== lessThan) {
return
}
next = value.indexOf(lineFeed, index + 1)
next = next === -1 ? length : next
line = value.slice(index, next)
offset = -1
count = sequences.length
while (++offset < count) {
if (sequences[offset][0].test(line)) {
sequence = sequences[offset]
break
}
}
if (!sequence) {
return
}
if (silent) {
return sequence[2]
}
index = next
if (!sequence[1].test(line)) {
while (index < length) {
next = value.indexOf(lineFeed, index + 1)
next = next === -1 ? length : next
line = value.slice(index + 1, next)
if (sequence[1].test(line)) {
if (line) {
index = next
}
break
}
index = next
}
}
subvalue = value.slice(0, index)
return eat(subvalue)({type: 'html', value: subvalue})
}