forked from b3log/lute
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathescape_encode.go
More file actions
121 lines (115 loc) · 3.15 KB
/
escape_encode.go
File metadata and controls
121 lines (115 loc) · 3.15 KB
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
// Lute - A structured markdown engine.
// Copyright (c) 2019-present, b3log.org
//
// Lute is licensed under the Mulan PSL v1.
// You can use this software according to the terms and conditions of the Mulan PSL v1.
// You may obtain a copy of Mulan PSL v1 at:
// http://license.coscl.org.cn/MulanPSL
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v1 for more details.
package lute
import (
"strings"
"unicode/utf8"
)
var (
amp = strToBytes("&")
lt = strToBytes("<")
gt = strToBytes(">")
quot = strToBytes(""")
)
func escapeHTML(html []byte) (ret []byte) {
length := len(html)
var start, i int
inited := false
ret = html
for ; i < length; i++ {
switch html[i] {
case itemAmpersand:
if !inited { // 通过延迟初始化减少内存分配,下同
ret = make([]byte, 0, length+128)
inited = true
}
ret = append(ret, html[start:i]...)
ret = append(ret, amp...)
start = i + 1
case itemLess:
if !inited {
ret = make([]byte, 0, length+128)
inited = true
}
ret = append(ret, html[start:i]...)
ret = append(ret, lt...)
start = i + 1
case itemGreater:
if !inited {
ret = make([]byte, 0, length+128)
inited = true
}
ret = append(ret, html[start:i]...)
ret = append(ret, gt...)
start = i + 1
case itemDoublequote:
if !inited {
ret = make([]byte, 0, length+128)
inited = true
}
ret = append(ret, html[start:i]...)
ret = append(ret, quot...)
start = i + 1
}
}
if inited {
ret = append(ret, html[start:]...)
}
return
}
// encodeDestination percent-encodes rawurl, avoiding double encoding.
// It doesn't touch:
// - alphanumeric characters ([0-9a-zA-Z]);
// - percent-encoded characters (%[0-9a-fA-F]{2});
// - excluded characters ([;/?:@&=+$,-_.!~*'()#]).
// Invalid UTF-8 sequences are replaced with U+FFFD.
func encodeDestination(rawurl []byte) (ret []byte) {
// 鸣谢 https://gitlab.com/golang-commonmark/mdurl
const hexdigit = "0123456789ABCDEF"
ret = make([]byte, 0, 256)
i := 0
var token byte
for i < len(rawurl) {
r, rlen := utf8.DecodeRune(rawurl[i:])
if utf8.RuneSelf <= r {
for j, n := i, i+rlen; j < n; j++ {
b := rawurl[j]
token = rawurl[j]
ret = append(ret, '%')
ret = append(ret, hexdigit[(b>>4)&0xf])
ret = append(ret, hexdigit[b&0xf])
}
} else if r == '%' {
token = rawurl[i]
if i+2 < len(rawurl) && isHexDigit(rawurl[i+1]) && isHexDigit(rawurl[i+2]) {
ret = append(ret, '%')
ret = append(ret, tokenToUpper(rawurl[i+1]))
ret = append(ret, tokenToUpper(rawurl[i+2]))
i += 2
} else {
ret = append(ret, '%')
ret = append(ret, '2')
ret = append(ret, '5')
}
} else if strings.IndexByte("!#$&'()*+,-./0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~", byte(r)) == -1 {
token = rawurl[i]
ret = append(ret, '%')
ret = append(ret, hexdigit[(r>>4)&0xf])
ret = append(ret, hexdigit[r&0xf])
} else {
token = rawurl[i]
ret = append(ret, token)
}
i += rlen
}
return
}