feat(ipub,ast): xml.Unmarshaller implementation for Elements
This commit is contained in:
@@ -34,6 +34,8 @@ type Element interface {
|
||||
InsertBefore(self, v1, insertee Element)
|
||||
InsertAfter(self, v1, insertee Element)
|
||||
|
||||
xml.Unmarshaler
|
||||
}
|
||||
|
||||
type BaseElement struct {
|
||||
next Element
|
||||
@@ -178,6 +180,41 @@ func (e *BaseElement) InsertBefore(self, v1, insertee Element) {
|
||||
}
|
||||
}
|
||||
|
||||
func (e *BaseElement) UnmarshalChildren(self Element, d *xml.Decoder, start xml.StartElement) error {
|
||||
for {
|
||||
token, err := d.Token()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch tt := token.(type) {
|
||||
case xml.StartElement:
|
||||
i := slices.IndexFunc(tt.Attr, func(a xml.Attr) bool {
|
||||
return a.Name.Local == "data-ipub-element"
|
||||
})
|
||||
if i == -1 {
|
||||
return fmt.Errorf("element kind not specified")
|
||||
}
|
||||
|
||||
kind := tt.Attr[i].Value
|
||||
kt, ok := elementKindList[ElementKind(kind)]
|
||||
if !ok {
|
||||
return fmt.Errorf("element kind not found")
|
||||
}
|
||||
|
||||
err := d.DecodeElement(kt, &tt)
|
||||
if err != nil && err != io.EOF {
|
||||
return err
|
||||
}
|
||||
|
||||
e.AppendChild(self, kt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ensureIsolated(e Element) {
|
||||
if p := e.Parent(); p != nil {
|
||||
p.RemoveChild(p, e)
|
||||
|
||||
@@ -15,3 +15,7 @@ func (n *Content) Kind() ElementKind {
|
||||
return KindContent
|
||||
}
|
||||
|
||||
func (n *Content) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
return n.UnmarshalChildren(n, d, start)
|
||||
}
|
||||
|
||||
|
||||
@@ -18,3 +18,6 @@ func (n *Body) Kind() ElementKind {
|
||||
return KindBody
|
||||
}
|
||||
|
||||
func (n *Body) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
return n.UnmarshalChildren(n, d, start)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user