mirror of
https://github.com/unidoc/unioffice.git
synced 2025-04-27 13:48:54 +08:00
document: fix space preserve on run text content
This commit is contained in:
parent
10ca06e4c6
commit
09041bcba5
@ -56,6 +56,10 @@ func (r Run) AddText(s string) {
|
|||||||
ic := wml.NewEG_RunInnerContent()
|
ic := wml.NewEG_RunInnerContent()
|
||||||
r.x.EG_RunInnerContent = append(r.x.EG_RunInnerContent, ic)
|
r.x.EG_RunInnerContent = append(r.x.EG_RunInnerContent, ic)
|
||||||
ic.T = wml.NewCT_Text()
|
ic.T = wml.NewCT_Text()
|
||||||
|
if gooxml.NeedsSpacePreserve(s) {
|
||||||
|
p := "preserve"
|
||||||
|
ic.T.SpaceAttr = &p
|
||||||
|
}
|
||||||
ic.T.Content = s
|
ic.T.Content = s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
32
xml.go
32
xml.go
@ -9,24 +9,26 @@ package gooxml
|
|||||||
|
|
||||||
import "encoding/xml"
|
import "encoding/xml"
|
||||||
|
|
||||||
|
// NeedsSpacePreserve returns true if the string has leading or trailing space.
|
||||||
|
func NeedsSpacePreserve(s string) bool {
|
||||||
|
if len(s) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
switch s[0] {
|
||||||
|
case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
switch s[len(s)-1] {
|
||||||
|
case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// AddPreserveSpaceAttr adds an xml:space="preserve" attribute to a start
|
// AddPreserveSpaceAttr adds an xml:space="preserve" attribute to a start
|
||||||
// element if it is required for the string s.
|
// element if it is required for the string s.
|
||||||
func AddPreserveSpaceAttr(se *xml.StartElement, s string) {
|
func AddPreserveSpaceAttr(se *xml.StartElement, s string) {
|
||||||
if len(s) == 0 {
|
if NeedsSpacePreserve(s) {
|
||||||
return
|
|
||||||
}
|
|
||||||
preserveSpace := false
|
|
||||||
switch s[0] {
|
|
||||||
case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0:
|
|
||||||
preserveSpace = true
|
|
||||||
}
|
|
||||||
if !preserveSpace {
|
|
||||||
switch s[len(s)-1] {
|
|
||||||
case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0:
|
|
||||||
preserveSpace = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if preserveSpace {
|
|
||||||
se.Attr = append(se.Attr, xml.Attr{Name: xml.Name{Local: "xml:space"}, Value: "preserve"})
|
se.Attr = append(se.Attr, xml.Attr{Name: xml.Name{Local: "xml:space"}, Value: "preserve"})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user