From 5686b41fd83da62fba3d112abbfb1a725f7946fa Mon Sep 17 00:00:00 2001 From: Connor Taffe Date: Thu, 3 Aug 2023 01:12:41 -0500 Subject: [PATCH] Add content method to node Return the whole content of a node, not just the text --- clib/clib.go | 13 +++++++++++++ dom/node.go | 8 ++++++++ dom/node_document.go | 5 +++++ types/interface.go | 1 + 4 files changed, 27 insertions(+) diff --git a/clib/clib.go b/clib/clib.go index 2e43832..b7a6cae 100644 --- a/clib/clib.go +++ b/clib/clib.go @@ -35,6 +35,7 @@ package clib #include #include #include +#include static inline void MY_nilErrorHandler(void *ctx, const char *msg, ...) {} @@ -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 { diff --git a/dom/node.go b/dom/node.go index 3903308..75ee64a 100644 --- a/dom/node.go +++ b/dom/node.go @@ -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) diff --git a/dom/node_document.go b/dom/node_document.go index 8e72b11..06a52ca 100644 --- a/dom/node_document.go +++ b/dom/node_document.go @@ -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) diff --git a/types/interface.go b/types/interface.go index cd17660..0a2adc9 100644 --- a/types/interface.go +++ b/types/interface.go @@ -103,6 +103,7 @@ type Node interface { String() string TextContent() string ToString(int, bool) string + Content() string Walk(func(Node) error) error MakeMortal()