feat(ipub,ast): Name() method to determina XML element/tag name

This commit is contained in:
Guz
2025-05-20 10:05:06 -03:00
parent b1f6bde29f
commit f7704b4f18
3 changed files with 25 additions and 3 deletions

View File

@@ -2,12 +2,14 @@ package ast
import (
"encoding/xml"
"errors"
"fmt"
"io"
"slices"
)
type Element interface {
Name() ElementName
Kind() ElementKind
NextSibling() Element
@@ -181,6 +183,18 @@ func (e *BaseElement) InsertBefore(self, v1, insertee Element) {
}
func (e *BaseElement) UnmarshalChildren(self Element, d *xml.Decoder, start xml.StartElement) error {
elErr := fmt.Errorf("unable to unmarshal element kind %q", self.Kind())
if n := self.Name(); n != (xml.Name{}) {
if n != start.Name {
return errors.Join(
elErr,
fmt.Errorf("element has different name (%q) than expected (%q)",
fmtXMLName(start.Name), fmtXMLName(n)),
)
}
}
for {
token, err := d.Token()
if err != nil {
@@ -196,7 +210,7 @@ func (e *BaseElement) UnmarshalChildren(self Element, d *xml.Decoder, start xml.
return a.Name.Local == "data-ipub-element"
})
if i == -1 {
return fmt.Errorf("element kind not specified")
return errors.Join(elErr, fmt.Errorf("element kind not specified"))
}
kind := tt.Attr[i].Value

View File

@@ -11,7 +11,11 @@ type Content struct {
var KindContent = NewElementKind("content", &Content{})
func (n *Content) Kind() ElementKind {
func (e Content) Name() ElementName {
return ElementName{Local: "section"}
}
func (e Content) Kind() ElementKind {
return KindContent
}

View File

@@ -14,7 +14,11 @@ type Body struct {
var KindBody = NewElementKind("body", &Body{})
func (n *Body) Kind() ElementKind {
func (e Body) Name() ElementName {
return ElementName{Local: "body"}
}
func (e Body) Kind() ElementKind {
return KindBody
}