Skip to content

Commit

Permalink
schema: preserve extra content on CT_R/CT_SdtPr
Browse files Browse the repository at this point in the history
The extra content is non-standard, but Libre seems to support
it well so we just round-trip it for now.
  • Loading branch information
tbaliance committed Sep 27, 2017
1 parent 427f7b1 commit ad46a6a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
14 changes: 11 additions & 3 deletions schema/soo/wml/CT_R.go
Expand Up @@ -12,7 +12,8 @@ package wml
import (
"encoding/xml"
"fmt"
"log"

"baliance.com/gooxml"
)

type CT_R struct {
Expand All @@ -25,6 +26,7 @@ type CT_R struct {
// Run Properties
RPr *CT_RPr
EG_RunInnerContent []*EG_RunInnerContent
Extra []gooxml.Any
}

func NewCT_R() *CT_R {
Expand Down Expand Up @@ -55,6 +57,11 @@ func (m *CT_R) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
c.MarshalXML(e, xml.StartElement{})
}
}
for _, any := range m.Extra {
if err := any.MarshalXML(e, xml.StartElement{}); err != nil {
return err
}
}
e.EncodeToken(xml.EndElement{Name: start.Name})
return nil
}
Expand Down Expand Up @@ -330,10 +337,11 @@ lCT_R:
}
m.EG_RunInnerContent = append(m.EG_RunInnerContent, tmpruninnercontent)
default:
log.Printf("skipping unsupported element on CT_R %v", el.Name)
if err := d.Skip(); err != nil {
any := &gooxml.XSDAny{}
if err := d.DecodeElement(any, &el); err != nil {
return err
}
m.Extra = append(m.Extra, any)
}
case xml.EndElement:
break lCT_R
Expand Down
14 changes: 11 additions & 3 deletions schema/soo/wml/CT_SdtPr.go
Expand Up @@ -11,7 +11,8 @@ package wml

import (
"encoding/xml"
"log"

"baliance.com/gooxml"
)

type CT_SdtPr struct {
Expand All @@ -38,6 +39,7 @@ type CT_SdtPr struct {
// Structured Document Tag Navigation Order Index
TabIndex *CT_UnsignedDecimalNumber
Choice *CT_SdtPrChoice
Extra []gooxml.Any
}

func NewCT_SdtPr() *CT_SdtPr {
Expand Down Expand Up @@ -94,6 +96,11 @@ func (m *CT_SdtPr) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if m.Choice != nil {
m.Choice.MarshalXML(e, xml.StartElement{})
}
for _, any := range m.Extra {
if err := any.MarshalXML(e, xml.StartElement{}); err != nil {
return err
}
}
e.EncodeToken(xml.EndElement{Name: start.Name})
return nil
}
Expand Down Expand Up @@ -225,10 +232,11 @@ lCT_SdtPr:
return err
}
default:
log.Printf("skipping unsupported element on CT_SdtPr %v", el.Name)
if err := d.Skip(); err != nil {
any := &gooxml.XSDAny{}
if err := d.DecodeElement(any, &el); err != nil {
return err
}
m.Extra = append(m.Extra, any)
}
case xml.EndElement:
break lCT_SdtPr
Expand Down
27 changes: 24 additions & 3 deletions xsdany.go
Expand Up @@ -22,11 +22,23 @@ type XSDAny struct {
}

var wellKnownSchemas = map[string]string{
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
"dcterms": "http://purl.org/dc/terms/",
"a": "http://schemas.openxmlformats.org/drawingml/2006/main",
"dc": "http://purl.org/dc/elements/1.1/",
"dcterms": "http://purl.org/dc/terms/",
"mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
"mo": "http://schemas.microsoft.com/office/mac/office/2008/main",
"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
"w10": "urn:schemas-microsoft-com:office:word",
"w14": "http://schemas.microsoft.com/office/word/2010/wordml",
"w15": "http://schemas.microsoft.com/office/word/2012/wordml",
"wne": "http://schemas.microsoft.com/office/word/2006/wordml",
"wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
"wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
"wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
"wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
"wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
"wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
}

var wellKnownSchemasInv = func() map[string]string {
Expand All @@ -40,8 +52,14 @@ var wellKnownSchemasInv = func() map[string]string {
type any struct {
XMLName xml.Name
Attrs []xml.Attr `xml:",any,attr"`
Data []byte `xml:",chardata"`
Nodes []*any `xml:",any"`
Data []byte `xml:",chardata"`
}

func dd(a *any) {
for _, n := range a.Nodes {
dd(n)
}
}

// UnmarshalXML implements the xml.Unmarshaler interface.
Expand All @@ -50,6 +68,7 @@ func (x *XSDAny) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
if err := d.DecodeElement(&a, &start); err != nil {
return err
}
dd(&a)
x.XMLName = a.XMLName
x.Attrs = a.Attrs
x.Data = a.Data
Expand Down Expand Up @@ -159,6 +178,7 @@ func convertToXNodes(an []*any) []*XSDAny {
x := &XSDAny{}
x.XMLName = a.XMLName
x.Attrs = a.Attrs
x.Data = a.Data
x.Nodes = convertToXNodes(a.Nodes)
ret = append(ret, x)
}
Expand All @@ -170,6 +190,7 @@ func convertToNodes(xn []*XSDAny) []*any {
a := &any{}
a.XMLName = x.XMLName
a.Attrs = x.Attrs
a.Data = x.Data
a.Nodes = convertToNodes(x.Nodes)
ret = append(ret, a)
}
Expand Down
6 changes: 2 additions & 4 deletions xsdany_test.go
Expand Up @@ -18,10 +18,8 @@ import (

func TestXSDAny(t *testing.T) {
any := gooxml.XSDAny{}
anyXml := `<w:settings xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" mc:Ignorable="w14 w15 w16se">
<w:hdrShapeDefaults><o:shapedefaults v:ext="edit" spidmax="2049"><o:idmap v:ext="edit" data="1"/></o:shapedefaults></w:hdrShapeDefaults></w:settings>`

exp := `<m:settings ma:Ignorable="w14 w15 w16se" xmlns:m="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:ma="http://schemas.openxmlformats.org/markup-compatibility" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml">&#xA;&#x9;<m:hdrShapeDefaults><o:shapedefaults v:ext="edit" spidmax="2049"><o:idmap v:ext="edit" data="1"></o:idmap></o:shapedefaults></m:hdrShapeDefaults></m:settings>`
anyXml := `<w:settings xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" mc:Ignorable="w14 w15 w16se"><w:hdrShapeDefaults><o:shapedefaults v:ext="edit" spidmax="2049"><o:idmap v:ext="edit" data="1"/>foobar</o:shapedefaults></w:hdrShapeDefaults></w:settings>`
exp := `<w:settings mc:Ignorable="w14 w15 w16se" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml"><w:hdrShapeDefaults><o:shapedefaults v:ext="edit" spidmax="2049"><o:idmap v:ext="edit" data="1"></o:idmap>foobar</o:shapedefaults></w:hdrShapeDefaults></w:settings>`
dec := xml.NewDecoder(strings.NewReader(anyXml))
if err := dec.Decode(&any); err != nil {
t.Errorf("error decoding XSDAny: %s", err)
Expand Down

0 comments on commit ad46a6a

Please sign in to comment.