-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstd_a.go
More file actions
150 lines (134 loc) · 3.18 KB
/
std_a.go
File metadata and controls
150 lines (134 loc) · 3.18 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
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
package ltml
import (
"fmt"
"strings"
"unicode"
)
type StdA struct {
StdSpan
uri string
target string
}
// linkedInlineText decorates normal inline text with LTML link semantics while
// leaving styling decisions to the surrounding paragraph/label/span logic.
type linkedInlineText struct {
inlineText
uri string
target string
id string
}
func (t linkedInlineText) LinkURI() string { return t.uri }
func (t linkedInlineText) LinkTarget() string { return t.target }
func (t linkedInlineText) LinkID() string { return t.id }
func (t linkedInlineText) Font() *FontStyle {
if withFont, ok := t.inlineText.(inlineTextWithFont); ok {
return withFont.Font()
}
return nil
}
func (a *StdA) AddText(text string) {
a.AddTextWithFont(text, a.explicitFont())
}
func (a *StdA) AddTextWithFont(text string, font *FontStyle) {
text = normalizeLinkedText(a.container, text)
if text == "" {
return
}
a.AddInlineWithFont(staticInlineText(text), font)
}
func (a *StdA) AddInlineWithFont(content inlineText, font *FontStyle) {
if content == nil {
return
}
a.container.(AddInlineWithFonter).AddInlineWithFont(linkedInlineText{
inlineText: content,
uri: a.uri,
target: a.target,
id: a.AccessibilityLogicalID(),
}, font)
}
func (a *StdA) LinkURI() string {
return a.uri
}
func (a *StdA) LinkTarget() string {
return a.target
}
func (a *StdA) SetAttrs(attrs map[string]string) {
a.StdSpan.SetAttrs(attrs)
a.uri = attrs["uri"]
a.target = attrs["target"]
}
func (a *StdA) SetContainer(container Container) error {
switch container.(type) {
case *StdA:
case *StdSpan:
case *StdParagraph:
case *StdLabel:
case *StdSector:
default:
return fmt.Errorf("a must be child of p, label, sector, span or another a")
}
a.container = container
return nil
}
func normalizeLinkedText(container Container, text string) string {
if container == nil {
return normalizeXMLText(text)
}
root := inlineRootContainer(container)
switch root.(type) {
case *StdLabel:
case *StdSector:
text = normalizeLabelXMLText(text)
default:
text = normalizeXMLText(text)
}
if text == "" {
return ""
}
last := lastInlineResolvedText(root)
if last == "" {
return strings.TrimLeftFunc(text, unicode.IsSpace)
}
if strings.HasSuffix(last, " ") && strings.HasPrefix(text, " ") {
return text[1:]
}
return text
}
func inlineRootContainer(container Container) Container {
for container != nil {
switch value := container.(type) {
case *StdA:
container = value.container
case *StdSpan:
container = value.container
default:
return container
}
}
return nil
}
func lastInlineResolvedText(container Container) string {
switch value := container.(type) {
case *StdParagraph:
if len(value.textPieces) == 0 {
return ""
}
return value.textPieces[len(value.textPieces)-1].ResolvedText(nil)
case *StdLabel:
if len(value.textPieces) == 0 {
return ""
}
return value.textPieces[len(value.textPieces)-1].ResolvedText(nil)
case *StdSector:
if len(value.textPieces) == 0 {
return ""
}
return value.textPieces[len(value.textPieces)-1].ResolvedText(nil)
default:
return ""
}
}
func init() {
registerTag(DefaultSpace, "a", func() any { return &StdA{} })
}