Debian Codegraph @

transform/urlreplacers/absurlreplacer.go

raw
1 // Copyright 2018 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package urlreplacers
15
16 import (
17 "bytes"
18 "io"
19 "net/url"
20 "unicode"
21 "unicode/utf8"
22
23 "github.com/gohugoio/hugo/common/paths"
24 "github.com/gohugoio/hugo/transform"
25 )
26
27 type absurllexer struct {
28 // the source to absurlify
29 content []byte
30 // the target for the new absurlified content
31 w io.Writer
32
33 // path may be set to a "." relative path
34 path []byte
35
36 // The root path, without leading slash.
37 root []byte
38
39 pos int // input position
40 start int // item start position
41
42 quotes [][]byte
43 }
44
45 type prefix struct {
46 disabled bool
47 b []byte
48 f func(l *absurllexer)
49
50 nextPos int
51 }
52
53 func (p *prefix) find(bs []byte, start int) bool {
54 if p.disabled {
55 return false
56 }
57
58 if p.nextPos == -1 {
59 idx := bytes.Index(bs[start:], p.b)
60
61 if idx == -1 {
62 p.disabled = true
63 // Find the closest match
64 return false
65 }
66
67 p.nextPos = start + idx + len(p.b)
68 }
69
70 return true
71 }
72
73 func newPrefixState() []*prefix {
74 return []*prefix{
75 {b: []byte("src="), f: checkCandidateBase},
76 {b: []byte("href="), f: checkCandidateBase},
77 {b: []byte("url="), f: checkCandidateBase},
78 {b: []byte("action="), f: checkCandidateBase},
79 {b: []byte("srcset="), f: checkCandidateSrcset},
80 }
81 }
82
83 func (l *absurllexer) emit() {
84 l.w.Write(l.content[l.start:l.pos])
85 l.start = l.pos
86 }
87
88 var (
89 relURLPrefix = []byte("/")
90 relURLPrefixLen = len(relURLPrefix)
91 )
92
93 func (l *absurllexer) consumeQuote() []byte {
94 for _, q := range l.quotes {
95 if bytes.HasPrefix(l.content[l.pos:], q) {
96 l.pos += len(q)
97 l.emit()
98 return q
99 }
100 }
101 return nil
102 }
103
104 // handle URLs in src and href.
105 func checkCandidateBase(l *absurllexer) {
106 l.consumeQuote()
107
108 if !bytes.HasPrefix(l.content[l.pos:], relURLPrefix) {
109 return
110 }
111
112 // check for schemaless URLs
113 posAfter := l.pos + relURLPrefixLen
114 if posAfter >= len(l.content) {
115 return
116 }
117 r, _ := utf8.DecodeRune(l.content[posAfter:])
118 if r == '/' {
119 // schemaless: skip
120 return
121 }
122 if l.pos > l.start {
123 l.emit()
124 }
125 l.pos += relURLPrefixLen
126 l.w.Write(l.path)
127 if len(l.root) > 0 && bytes.HasPrefix(l.content[l.pos:], l.root) {
128 l.pos += len(l.root)
129 }
130 l.start = l.pos
131 }
132
133 func (l *absurllexer) posAfterURL(q []byte) int {
134 if len(q) > 0 {
135 // look for end quote
136 return bytes.Index(l.content[l.pos:], q)
137 }
138
139 return bytes.IndexFunc(l.content[l.pos:], func(r rune) bool {
140 return r == '>' || unicode.IsSpace(r)
141 })
142 }
143
144 // handle URLs in srcset.
145 func checkCandidateSrcset(l *absurllexer) {
146 q := l.consumeQuote()
147 if q == nil {
148 // srcset needs to be quoted.
149 return
150 }
151
152 // special case, not frequent (me think)
153 if !bytes.HasPrefix(l.content[l.pos:], relURLPrefix) {
154 return
155 }
156
157 // check for schemaless URLs
158 posAfter := l.pos + relURLPrefixLen
159 if posAfter >= len(l.content) {
160 return
161 }
162 r, _ := utf8.DecodeRune(l.content[posAfter:])
163 if r == '/' {
164 // schemaless: skip
165 return
166 }
167
168 posEnd := l.posAfterURL(q)
169
170 // safe guard
171 if posEnd < 0 || posEnd > 2000 {
172 return
173 }
174
175 if l.pos > l.start {
176 l.emit()
177 }
178
179 section := l.content[l.pos : l.pos+posEnd+1]
180
181 fields := bytes.Fields(section)
182 for i, f := range fields {
183 if f[0] == '/' {
184 l.w.Write(l.path)
185 n := 1
186 if len(l.root) > 0 && bytes.HasPrefix(f[n:], l.root) {
187 n += len(l.root)
188 }
189 l.w.Write(f[n:])
190
191 } else {
192 l.w.Write(f)
193 }
194
195 if i < len(fields)-1 {
196 l.w.Write([]byte(" "))
197 }
198 }
199
200 l.pos += len(section)
201 l.start = l.pos
202 }
203
204 // main loop
205 func (l *absurllexer) replace() {
206 contentLength := len(l.content)
207
208 prefixes := newPrefixState()
209
210 for {
211 if l.pos >= contentLength {
212 break
213 }
214
215 var match *prefix
216
217 for _, p := range prefixes {
218 if !p.find(l.content, l.pos) {
219 continue
220 }
221
222 if match == nil || p.nextPos < match.nextPos {
223 match = p
224 }
225 }
226
227 if match == nil {
228 // Done!
229 l.pos = contentLength
230 break
231 } else {
232 l.pos = match.nextPos
233 match.nextPos = -1
234 match.f(l)
235 }
236 }
237 // Done!
238 if l.pos > l.start {
239 l.emit()
240 }
241 }
242
243 func doReplace(path string, ct transform.FromTo, quotes [][]byte) {
244 var root string
245 if u, err := url.Parse(path); err == nil {
246 root = paths.TrimLeading(u.Path)
247 }
248 lexer := &absurllexer{
249 content: ct.From().Bytes(),
250 w: ct.To(),
251 path: []byte(path),
252 root: []byte(root),
253 quotes: quotes,
254 }
255
256 lexer.replace()
257 }
258
259 type absURLReplacer struct {
260 htmlQuotes [][]byte
261 xmlQuotes [][]byte
262 }
263
264 func newAbsURLReplacer() *absURLReplacer {
265 return &absURLReplacer{
266 htmlQuotes: [][]byte{[]byte("\""), []byte("'")},
267 xmlQuotes: [][]byte{[]byte("&#34;"), []byte("&#39;")},
268 }
269 }
270
271 func (au *absURLReplacer) replaceInHTML(path string, ct transform.FromTo) {
272 doReplace(path, ct, au.htmlQuotes)
273 }
274
275 func (au *absURLReplacer) replaceInXML(path string, ct transform.FromTo) {
276 doReplace(path, ct, au.xmlQuotes)
277 }