-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
524 lines (493 loc) · 15.3 KB
/
tree.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package csshtml
import (
"bufio"
"fmt"
"io"
"regexp"
"sort"
"strings"
"golang.org/x/net/html"
"github.com/PuerkitoBio/goquery"
"github.com/andybalholm/cascadia"
"github.com/speedata/css/scanner"
)
var (
level int
out io.Writer
dimen = regexp.MustCompile(`^^[+\-]?(?:(?:0+|[1-9]\d*)(?:\.\d*)?|\.\d+)(px|mm|cm|in|pt|pc|ch|em|ex|lh|rem|0)$`)
zeroDimen = regexp.MustCompile(`^0+(px|mm|cm|in|pt|pc|ch|em|ex|lh|rem)?`)
style = regexp.MustCompile(`^none|hidden|dotted|dashed|solid|double|groove|ridge|inset|outset$`)
colorMatcher = regexp.MustCompile(`^rgba?\s*\(`)
toprightbottomleft = [...]string{"top", "right", "bottom", "left"}
)
func normalizespace(input string) string {
return strings.Join(strings.Fields(input), " ")
}
func stringValue(toks tokenstream) string {
ret := []string{}
negative := false
prevNegative := false
for _, tok := range toks {
prevNegative = negative
negative = false
switch tok.Type {
case scanner.Ident:
ret = append(ret, tok.Value)
case scanner.String:
ret = append(ret, fmt.Sprintf("%q", tok.Value))
case scanner.Number, scanner.Dimension:
if prevNegative {
ret = append(ret, "-"+tok.Value)
} else {
ret = append(ret, tok.Value)
}
case scanner.Percentage:
ret = append(ret, tok.Value+"%")
case scanner.Hash:
ret = append(ret, "#"+tok.Value)
case scanner.Function:
ret = append(ret, tok.Value+"(")
case scanner.S:
// ret = append(ret, " ")
case scanner.Delim:
switch tok.Value {
case ";":
// ignore
case ",", ")":
ret = append(ret, tok.Value)
case "-":
negative = true
default:
fmt.Println("unhandled delimiter", tok)
}
case scanner.URI:
ret = append(ret, "url("+tok.Value+")")
case scanner.Local:
ret = append(ret, "local("+tok.Value+")")
case scanner.Format:
ret = append(ret, "format("+tok.Value+")")
case scanner.Tech:
ret = append(ret, "tech("+tok.Value+")")
default:
fmt.Println("tree: unhandled token", tok)
}
}
return strings.Join(ret, " ")
}
// Recurse through the HTML tree and resolve the style attribute
func resolveStyle(i int, sel *goquery.Selection) {
a, b := sel.Attr("style")
if b {
var tokens tokenstream
s := scanner.New(a)
for {
token := s.Next()
if token.Type == scanner.EOF || token.Type == scanner.Error {
break
}
switch token.Type {
case scanner.Comment:
// ignore
default:
tokens = append(tokens, token)
}
}
block := consumeBlock(tokens, true)
for _, rule := range block.rules {
sel.SetAttr("!"+stringValue(rule.key), stringValue(rule.value))
}
}
sel.Children().Each(resolveStyle)
}
func isDimension(str string) (bool, string) {
switch str {
case "thick":
return true, "2pt"
case "medium":
return true, "1pt"
case "thin":
return true, "0.5pt"
}
return dimen.MatchString(str), str
}
func isBorderStyle(str string) (bool, string) {
return style.MatchString(str), str
}
// getFourValues fills all four values for top, bottom, left and right from one
// to four values in margin/padding etc.
func getFourValues(str string) map[string]string {
fields := strings.Fields(str)
fourValues := make(map[string]string)
switch len(fields) {
case 1:
fourValues["top"] = fields[0]
fourValues["right"] = fields[0]
fourValues["bottom"] = fields[0]
fourValues["left"] = fields[0]
case 2:
fourValues["top"] = fields[0]
fourValues["right"] = fields[1]
fourValues["bottom"] = fields[0]
fourValues["left"] = fields[1]
case 3:
fourValues["top"] = fields[0]
fourValues["right"] = fields[1]
fourValues["bottom"] = fields[2]
fourValues["left"] = fields[1]
case 4:
fourValues["top"] = fields[0]
fourValues["right"] = fields[1]
fourValues["bottom"] = fields[2]
fourValues["left"] = fields[3]
}
return fourValues
}
// parseBorderAttribute splits "1pt solid black" into three parts.
func parseBorderAttribute(input string) (width string, style string, color string) {
s := bufio.NewScanner(strings.NewReader(input))
s.Split(bufio.ScanWords)
width = "1pt"
style = "none"
color = "currentcolor"
// 0 = width, 1 = style, 2 = color
for s.Scan() {
t := s.Text()
// looking for width
ok, wd := isDimension(t)
if ok {
width = wd
continue
}
// looking for style
if t := t; t == "none" || t == "hidden" || t == "dotted" || t == "dashed" || t == "solid" || t == "double" || t == "groove" || t == "ridge" || t == "inset" || t == "outset" {
style = t
continue
}
if strings.HasPrefix(t, "#") {
color = t
return
}
if colorMatcher.MatchString(t) {
color = t
for s.Scan() {
color += " " + s.Text()
}
return
}
color = t
}
return
}
// GetAttributes returns a map from all HTML attributes.
func GetAttributes(attrs []html.Attribute) map[string]string {
resolved := make(map[string]string, len(attrs))
for _, attr := range attrs {
resolved[strings.TrimPrefix(attr.Key, "*")] = attr.Val
}
return resolved
}
// ResolveAttributes returns the resolved styles and the attributes of the node.
// The argument attrs is unchanged. This function transforms rules such as
// "margin: 1cm;" into "margin-left: 1cm; margin-right: 1cm; ...". Attributes
// without a ! prefix are not resolved, just copied to the newAttributes return
// value. All other attributes are copied to the newAttributes prefixed with a *
// and all the resolved attributes prefixed with an exclamation mark. The
// resolved map contains only the resolved attributes and values without a prefix.
func ResolveAttributes(attrs []html.Attribute) (resolved map[string]string, newAttributes []html.Attribute) {
resolved = make(map[string]string)
newAttributes = make([]html.Attribute, 0, len(attrs))
// attribute resolving must be in order of appearance.
// For example the following border-left-style has no effect:
// border-left-style: dotted;
// border-left: thick green;
// because the second line overrides the first line (style defaults to "none")
for _, attr := range attrs {
key := attr.Key
if !strings.HasPrefix(key, "!") {
newAttributes = append(newAttributes, attr)
continue
}
key = strings.TrimPrefix(key, "!")
newAttributes = append(newAttributes, html.Attribute{Key: "*" + key, Val: attr.Val})
switch key {
case "margin":
values := getFourValues(attr.Val)
for _, margin := range toprightbottomleft {
resolved["margin-"+margin] = values[margin]
newAttributes = append(newAttributes, html.Attribute{Key: "*margin-" + margin, Val: values[margin]})
}
case "list-style":
for _, part := range strings.Split(attr.Val, " ") {
switch part {
case "inside", "outside":
resolved["list-style-position"] = part
newAttributes = append(newAttributes, html.Attribute{Key: "*list-style-position", Val: part})
default:
if strings.HasPrefix(part, "url") {
resolved["list-style-image"] = part
newAttributes = append(newAttributes, html.Attribute{Key: "*list-style-image", Val: part})
} else {
resolved["list-style-type"] = part
newAttributes = append(newAttributes, html.Attribute{Key: "*list-style-type", Val: part})
}
}
}
case "padding":
values := getFourValues(attr.Val)
for _, padding := range toprightbottomleft {
resolved["padding-"+padding] = values[padding]
newAttributes = append(newAttributes, html.Attribute{Key: "*padding-" + padding, Val: values[padding]})
}
case "border":
wd, style, color := parseBorderAttribute(attr.Val)
for _, loc := range toprightbottomleft {
resolved["border-"+loc+"-style"] = style
resolved["border-"+loc+"-width"] = wd
resolved["border-"+loc+"-color"] = color
newAttributes = append(newAttributes,
html.Attribute{Key: "*border-" + loc + "-style", Val: style},
html.Attribute{Key: "*border-" + loc + "-width", Val: wd},
html.Attribute{Key: "*border-" + loc + "-color", Val: color},
)
}
case "border-radius":
for _, lr := range []string{"left", "right"} {
for _, tb := range []string{"top", "bottom"} {
resolved["border-"+tb+"-"+lr+"-radius"] = attr.Val
newAttributes = append(newAttributes,
html.Attribute{Key: "*border-" + tb + "-" + lr + "-radius", Val: attr.Val},
)
}
}
case "border-top", "border-right", "border-bottom", "border-left":
wd, sty, col := parseBorderAttribute(attr.Val)
resolved[key+"-width"], resolved[key+"-style"], resolved[key+"-color"] = wd, sty, col
newAttributes = append(newAttributes,
html.Attribute{Key: "*" + key + "-width", Val: wd},
html.Attribute{Key: "*" + key + "-style", Val: sty},
html.Attribute{Key: "*" + key + "-color", Val: col},
)
case "border-color":
values := getFourValues(attr.Val)
for _, loc := range toprightbottomleft {
resolved["border-"+loc+"-color"] = values[loc]
newAttributes = append(newAttributes,
html.Attribute{Key: "*border-" + loc + "-color", Val: values[loc]},
)
}
case "border-style":
values := getFourValues(attr.Val)
for _, loc := range toprightbottomleft {
resolved["border-"+loc+"-style"] = values[loc]
newAttributes = append(newAttributes,
html.Attribute{Key: "*border-" + loc + "-style", Val: values[loc]},
)
}
case "border-width":
values := getFourValues(attr.Val)
for _, loc := range toprightbottomleft {
resolved["border-"+loc+"-width"] = values[loc]
newAttributes = append(newAttributes,
html.Attribute{Key: "*border-" + loc + "-width", Val: values[loc]},
)
}
resolved[key] = attr.Val
case "font":
fontstyle := "normal"
fontweight := "normal"
/*
it must include values for:
<font-size>
<font-family>
it may optionally include values for:
<font-style>
<font-variant>
<font-weight>
<font-stretch>
<line-height>
* font-style, font-variant and font-weight must precede font-size
* font-variant may only specify the values defined in CSS 2.1, that is normal and small-caps
* font-stretch may only be a single keyword value.
* line-height must immediately follow font-size, preceded by "/", like this: "16px/3"
* font-family must be the last value specified.
*/
val := attr.Val
fields := strings.Fields(val)
l := len(fields)
for idx, field := range fields {
if idx > l-3 {
if dimen.MatchString(field) || strings.Contains(field, "%") {
resolved["font-size"] = field
newAttributes = append(newAttributes,
html.Attribute{Key: "*font-size", Val: field},
)
} else {
resolved["font-name"] = field
newAttributes = append(newAttributes,
html.Attribute{Key: "*font-name", Val: field},
)
}
}
}
resolved["font-style"] = fontstyle
resolved["font-weight"] = fontweight
newAttributes = append(newAttributes,
html.Attribute{Key: "*font-style", Val: fontstyle},
html.Attribute{Key: "*font-weight", Val: fontweight},
)
// font-stretch: ultra-condensed; extra-condensed; condensed;
// semi-condensed; normal; semi-expanded; expanded; extra-expanded;
// ultra-expanded;
case "text-decoration":
for _, part := range strings.Split(attr.Val, " ") {
if part == "none" || part == "underline" || part == "overline" || part == "line-through" {
resolved["text-decoration-line"] = part
newAttributes = append(newAttributes,
html.Attribute{Key: "*text-decoration-line", Val: part},
)
} else if part == "solid" || part == "double" || part == "dotted" || part == "dashed" || part == "wavy" {
resolved["text-decoration-style"] = part
newAttributes = append(newAttributes,
html.Attribute{Key: "*text-decoration-style", Val: part},
)
}
}
case "background":
// background-clip, background-color, background-image,
// background-origin, background-position, background-repeat,
// background-size, and background-attachment
for _, part := range strings.Split(attr.Val, " ") {
resolved["background-color"] = part
newAttributes = append(newAttributes,
html.Attribute{Key: "*background-color", Val: part},
)
}
default:
resolved[key] = attr.Val
newAttributes = append(newAttributes, attr)
}
}
if str, ok := resolved["text-decoration-line"]; ok && str != "none" {
resolved["text-decoration-style"] = "solid"
newAttributes = append(newAttributes,
html.Attribute{Key: "*text-decoration-style", Val: "solid"},
)
}
return
}
// ApplyCSS resolves CSS rules in the DOM. Each CSS rule is added to the
// selection as an attribute (prefixed with a !). Pseudo elements are prefixed
// with ::.
func (c *CSS) ApplyCSS(doc *goquery.Document) (*goquery.Document, error) {
type selRule struct {
selector cascadia.Sel
rule []qrule
}
rules := map[int][]selRule{}
for _, stylesheet := range c.stylesheet {
for _, block := range stylesheet.blocks {
selector := block.componentValues.String()
selectors, err := cascadia.ParseGroupWithPseudoElements(selector)
if err != nil {
return nil, err
}
for _, sel := range selectors {
selSpecificity := sel.Specificity()
s := selSpecificity[0]*100 + selSpecificity[1]*10 + selSpecificity[2]
rules[s] = append(rules[s], selRule{selector: sel, rule: block.rules})
}
}
}
// sort map keys
keys := make([]int, 0, len(rules))
for k := range rules {
keys = append(keys, k)
}
// now sorted by specificity
sort.Ints(keys)
root := doc.Get(0)
for _, k := range keys {
for _, r := range rules[k] {
for _, singlerule := range r.rule {
for _, node := range cascadia.QueryAll(root, r.selector) {
var prefix string
if pe := r.selector.PseudoElement(); pe != "" {
prefix = pe + "::"
}
// remove attributes with the same name, since the new ones
// must override the old ones.
key := "!" + prefix + stringValue(singlerule.key)
newAttributes := make([]html.Attribute, 0, len(node.Attr))
for _, attr := range node.Attr {
if attr.Key != key {
newAttributes = append(newAttributes, attr)
}
}
newAttributes = append(newAttributes, html.Attribute{Key: key, Val: stringValue(singlerule.value)})
node.Attr = newAttributes
}
}
}
}
doc.Each(resolveStyle)
return doc, nil
}
// PapersizeWidthHeight converts the spec to the width and height. The parameter
// can be a known paper size (such as A4 or letter) or a one or two parameter
// string such as 20cm 20cm. The return values are in one of the units 'mm' or
// 'in'. 'mm' if the parameter spec is one of a5, a4, a3, b5, b4, jis-b5 or
// jis-b4 and 'in' if the parameter spec is one of letter, legal or ledger.
func PapersizeWidthHeight(spec string) (string, string) {
spec = strings.ToLower(spec)
var width, height string
portrait := true
for i, e := range strings.Fields(spec) {
switch e {
case "portrait":
// good, nothing to do
case "landscape":
portrait = false
case "a5":
width = "148mm"
height = "210mm"
case "a4":
width = "210mm"
height = "297mm"
case "a3":
width = "297mm"
height = "420mm"
case "b5":
width = "176mm"
height = "250mm"
case "b4":
width = "250mm"
height = "353mm"
case "jis-b5":
width = "182mm"
height = "257mm"
case "jis-b4":
width = "257mm"
height = "364mm"
case "letter":
width = "8.5in"
height = "11in"
case "legal":
width = "8.5in"
height = "14in"
case "ledger":
width = "11in"
height = "17in"
default:
if i == 0 {
width = e
height = e
} else {
height = e
}
}
}
if portrait {
return width, height
}
return height, width
}