reader: Fix return value of xmlTextReaderReadString again

Make sure to return NULL for node types except elements or text to match
the old behavior.

Note that CDATA sections are still treated like text nodes and will have
their content returned.

Fixes #838.
This commit is contained in:
Nick Wellnhofer 2025-01-15 12:52:53 +01:00
parent 86401cc3d2
commit be579a266e
2 changed files with 75 additions and 6 deletions

View File

@ -467,6 +467,76 @@ testReaderContent(void) {
return err;
}
static int
testReaderNode(xmlTextReader *reader) {
xmlChar *string;
int type;
int err = 0;
type = xmlTextReaderNodeType(reader);
string = xmlTextReaderReadString(reader);
if (type == XML_READER_TYPE_ELEMENT) {
xmlNodePtr node = xmlTextReaderCurrentNode(reader);
if ((node->children == NULL) != (string == NULL))
err = 1;
} else if (type == XML_READER_TYPE_TEXT ||
type == XML_READER_TYPE_CDATA ||
type == XML_READER_TYPE_WHITESPACE ||
type == XML_READER_TYPE_SIGNIFICANT_WHITESPACE) {
if (string == NULL)
err = 1;
} else {
if (string != NULL)
err = 1;
}
if (err)
fprintf(stderr, "xmlTextReaderReadString failed for %d\n", type);
xmlFree(string);
return err;
}
static int
testReader(void) {
xmlTextReader *reader;
const xmlChar *xml = BAD_CAST
"<d>\n"
" x<e a='v'>y</e><f>z</f>\n"
" <![CDATA[cdata]]>\n"
" <!-- comment -->\n"
" <?pi content?>\n"
" <empty/>\n"
"</d>";
int err = 0;
reader = xmlReaderForDoc(xml, NULL, NULL, 0);
while (xmlTextReaderRead(reader) > 0) {
if (testReaderNode(reader) > 0) {
err = 1;
break;
}
if (xmlTextReaderMoveToFirstAttribute(reader) > 0) {
do {
if (testReaderNode(reader) > 0) {
err = 1;
break;
}
} while (xmlTextReaderMoveToNextAttribute(reader) > 0);
xmlTextReaderMoveToElement(reader);
}
}
xmlFreeTextReader(reader);
return err;
}
#ifdef LIBXML_XINCLUDE_ENABLED
typedef struct {
char *message;
@ -834,6 +904,7 @@ main(void) {
#ifdef LIBXML_READER_ENABLED
err |= testReaderEncoding();
err |= testReaderContent();
err |= testReader();
#ifdef LIBXML_XINCLUDE_ENABLED
err |= testReaderXIncludeError();
#endif

View File

@ -1742,8 +1742,9 @@ xmlTextReaderReadOuterXml(xmlTextReaderPtr reader)
*
* Reads the contents of an element or a text node as a string.
*
* Returns a string containing the contents of the Element or Text node,
* or NULL if the reader is positioned on any other type of node.
* Returns a string containing the contents of the non-empty Element or
* Text node (including CDATA sections), or NULL if the reader
* is positioned on any other type of node.
* The string must be deallocated by the caller.
*/
xmlChar *
@ -1766,11 +1767,8 @@ xmlTextReaderReadString(xmlTextReaderPtr reader)
(node->children == NULL))
return(NULL);
break;
case XML_ATTRIBUTE_NODE:
/* TODO */
break;
default:
break;
return(NULL);
}
buf = xmlBufCreate(50);