ref: 76e69c809a0edf60a350d61f2de3092ce000d8e3
parent: 9ea2a17822bdb50432247821c07d55e596a5fc70
author: Philip Silva <[email protected]>
date: Fri Mar 19 16:57:57 EDT 2021
Provide text wrapping for non-inline tags
--- a/browser/browser.go
+++ b/browser/browser.go
@@ -85,6 +85,32 @@
}
}
+func NewText(content []string, n *nodes.Node) (el []*Element) {
+ tt := strings.Join(content, " ")
+
+ // '\n' is nowhere visible
+ tt = strings.Replace(tt, "\n", " ", -1)
+
+ ts := strings.Split(tt, " ")
+ ls := make([]*Element, 0, len(ts))
+
+ for _, t := range ts {
+ t = strings.TrimSpace(t)
+
+ if t == "" {
+ continue
+ }
+
+ l := &Element{
+ UI: NewLabel(t, n),
+ n: n,
+ }
+ ls = append(ls, l)
+ }
+
+ return ls
+}
+
func (ui *Label) Draw(dui *duit.DUI, self *duit.Kid, img *draw.Image, orig image.Point, m draw.Mouse, force bool) {
c := ui.n.Map.Color()
i, ok := colorCache[c]
@@ -1064,18 +1090,18 @@
fallthrough
default:
// Internal node object
- var innerContent duit.UI
- if nodes.IsPureTextContent(*n) {
+ //var innerContent duit.UI
+ /*if nodes.IsPureTextContent(*n) {
t := n.ContentString()
innerContent = NewLabel(t, n)
- } else {
+ } else {*/
return InnerNodesToBox(r+1, b, n)
- }
+ //}
- return NewElement(
+ /*return NewElement(
innerContent,
n,
- )
+ )*/
}
} else if n.Type() == html.TextNode {
// Leaf text object
@@ -1114,25 +1140,19 @@
continue
}
if isWrapped(c) {
- tt := strings.Join(c.Content(), " ")
-
- // '\n' is nowhere visible
- tt = strings.Replace(tt, "\n", " ", -1)
-
- ts := strings.Split(tt, " ")
- for _, t := range ts {
- t = strings.TrimSpace(t)
-
- if t == "" {
- continue
- }
-
- el := &Element{
- UI: NewLabel(t, c),
- n: c,
- }
- els = append(els, el)
+ ls := NewText(c.Content(), c)
+ els = append(els, ls...)
+ } else if nodes.IsPureTextContent(*n) {
+ // Handle text wrapped in unwrappable tags like p, div, ...
+ ls := NewText(c.Content(), c.Children[0])
+ if len(ls) == 0 {
+ continue
}
+ el := NewElement(horizontalSeq(true, ls), c)
+ if el == nil {
+ continue
+ }
+ els = append(els, el)
} else if el := NodeToBox(r+1, b, c); el != nil {
els = append(els, el)
}
--- a/browser/browser_test.go
+++ b/browser/browser_test.go
@@ -343,8 +343,8 @@
<html>
<body>
<div class="wrapper">
- <main></main>
- <footer></footer>
+ <main>main content</main>
+ <footer>footer</footer>
</div>
</body>
</html>