Skip to content

Commit

Permalink
Add content method to node
Browse files Browse the repository at this point in the history
Return the whole content of a node, not just the text
  • Loading branch information
cptaffe committed Aug 3, 2023
1 parent 5ff7cdf commit 5686b41
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions clib/clib.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ package clib
#include <libxml/c14n.h>
#include <libxml/xmlschemas.h>
#include <libxml/schemasInternals.h>
#include <libxml/tree.h>
static inline void MY_nilErrorHandler(void *ctx, const char *msg, ...) {}
Expand Down Expand Up @@ -897,6 +898,18 @@ func XMLNodeValue(n PtrSource) (string, error) {
return s, nil
}

func XMLNodeGetContent(n PtrSource) (string, error) {
nptr, err := validNodePtr(n)
if err != nil {
return "", err
}

xc := C.xmlNodeGetContent(nptr)
s := xmlCharToString(xc)
C.MY_xmlFree(unsafe.Pointer(xc))
return s, nil
}

func XMLAddChild(n PtrSource, child PtrSource) error {
nptr, err := validNodePtr(n)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions dom/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func (n *XMLNode) String() string {
return n.ToString(0, false)
}

func (n *XMLNode) Content() string {
s, err := clib.XMLNodeGetContent(n)
if err != nil {
return ""
}
return s
}

// OwnerDocument returns the Document that this node belongs to
func (n *XMLNode) OwnerDocument() (types.Document, error) {
ptr, err := clib.XMLOwnerDocument(n)
Expand Down
5 changes: 5 additions & 0 deletions dom/node_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ func (d *Document) TextContent() string {
return clib.XMLTextContent(d)
}

// Content always returns an empty string for Document
func (d *Document) Content() string {
return ""
}

// ToString is currently just an alias to Dump(false)
func (d *Document) ToString(x int, b bool) string {
return d.Dump(b)
Expand Down
1 change: 1 addition & 0 deletions types/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type Node interface {
String() string
TextContent() string
ToString(int, bool) string
Content() string
Walk(func(Node) error) error

MakeMortal()
Expand Down

0 comments on commit 5686b41

Please sign in to comment.