HaxeFoundation / HaxeFoundation/haxe

[std] Xml treats empty elements and self-closing elements differently

Open
#6,574 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

Haxe's `Xml.hx` treats `` and `` differently - the former has one (empty) child, while the latter has no children. I'm not sure what the XML spec say about this, but JavaScript's native XML parser treats both the same (neither have any children):

```haxe
import js.html.DOMParser;
using Lambda;

class Test {
static function main() {
trace(Xml.parse("
").firstChild().count()); // 0
trace(Xml.parse("
").firstChild().count()); // 1


trace(new DOMParser().parseFromString("", TEXT_XML).childNodes[0].childNodes.length); // 0
trace(new DOMParser().parseFromString("
", TEXT_XML).childNodes[0].childNodes.length); // 0
}
}
```

On the other hand, [`TestXML`'s `testWhitespaces()` seems to insist on this behavior](https://github.com/HaxeFoundation/haxe/blob/development/tests/unit/src/unit/TestXML.hx#L85). Looks like those tests were added by @ncannasse.

This came up in the Haxe Gitter, because this can make for some confusing behavior (an empty child won't show up in a `trace` of course):

```haxe
class Test {
static function main() {
var xml = Xml.parse("");
trace(xml); //

var a = xml.firstChild();

a.addChild(Xml.createElement("b"));
trace(xml); //

trace(a.removeChild(a.firstChild())); // true
trace(xml); //
// remove() successfully removed the first child(), but it's still there?
// nope, it just removed the empty first child that doesn't show up in traces..
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.