Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

fix child method: off-by-one error: child(0) == child(1) #149

Merged
merged 2 commits into from

4 participants

@triccardi-systran
var doc = libxml.parseXmlString('<?xml version="1.0"?>' +
                                '<root><child1>text node 1</child1><child2>text node 2</child2></root>');

console.log("doc.root().length: " + doc.root().childNodes().length);

var child1 = doc.root().child(0);
var child2 = doc.root().child(1);
var child3 = doc.root().child(2);

console.log("child1 text: " + child1.text());
console.log("child2 text: " + child2.text());
console.log("child3 text: " + child3.text());

This code should crash on the last line: child3 is null.
However we get this output:

doc.root().length: 2
child1 text: text node 1
child2 text: text node 1
child3 text: text node 2
@travisbot

This pull request fails (merged 1fa4f5d into f4707b5).

@travisbot

This pull request passes (merged d2f398f into f4707b5).

@polotek
Owner

This looks good, but the auto-merge isn't working and I'm not setup to do it on this laptop. Can someone land this change? If not I'll do it later this evening.

@defunctzombie defunctzombie merged commit 76696fa into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 18, 2012
  1. @triccardi-systran
Commits on Jul 19, 2012
  1. @triccardi-systran
This page is out of date. Refresh to see the latest.
Showing with 13 additions and 13 deletions.
  1. +1 −1  src/xml_element.cc
  2. +2 −2 test/document.js
  3. +10 −10 test/traversal.js
View
2  src/xml_element.cc
@@ -340,7 +340,7 @@ XmlElement::get_child(int32_t idx) {
v8::HandleScope scope;
xmlNode* child = xml_obj->children;
- int32_t i = 1;
+ int32_t i = 0;
while (child && i < idx) {
child = child->next;
++i;
View
4 test/document.js
@@ -44,8 +44,8 @@ module.exports.new_root = function(assert) {
module.exports.one_child = function(assert) {
var doc = libxml.Document();
var root = doc.node('root').node('child-one').parent().node('child-two');
- assert.equal('child-one', doc.child(1).name());
- assert.equal('child-two', doc.child(2).name());
+ assert.equal('child-one', doc.child(0).name());
+ assert.equal('child-two', doc.child(1).name());
assert.done();
};
View
20 test/traversal.js
@@ -11,9 +11,9 @@ module.exports.built = function(assert) {
assert.equal(doc, doc.root().parent());
assert.equal(child, gchild.parent());
- assert.equal(gchild, doc.child(1).child(0));
+ assert.equal(gchild, doc.child(0).child(0));
- assert.equal(sibling, doc.child(2));
+ assert.equal(sibling, doc.child(1));
assert.done();
};
@@ -27,7 +27,7 @@ module.exports.children = function(assert) {
assert.equal(children.length, doc.childNodes().length);
for (var i = 0; i < children.length; ++i) {
- assert.equal(children[i], doc.child(i+1));
+ assert.equal(children[i], doc.child(i));
}
assert.done();
};
@@ -51,18 +51,18 @@ module.exports.parsed = function(assert) {
'<?xml version="1.0"?>' +
'<root><child><grandchild /></child><sibling/></root>');
assert.equal(doc, doc.child(0).doc());
- assert.equal(doc, doc.child(2).doc());
- assert.equal(doc, doc.child(0).child(1).doc());
+ assert.equal(doc, doc.child(1).doc());
+ assert.equal(doc, doc.child(0).child(0).doc());
assert.equal(doc, doc.root().parent());
// down and back up
assert.equal('child', doc.child(0).child(0).parent().name());
// propertly access inner nodes
- assert.equal('grandchild', doc.child(1).child(0).name());
+ assert.equal('grandchild', doc.child(0).child(0).name());
// sibling nodes
- assert.equal('sibling', doc.child(2).name());
+ assert.equal('sibling', doc.child(1).name());
assert.done();
};
@@ -75,11 +75,11 @@ module.exports.parsed_children = function(assert) {
// childNodes
assert.equal(3, doc.childNodes().length);
for (var i = 0; i < children.length; ++i) {
- assert.equal(children[i], doc.child(i+1).name());
+ assert.equal(children[i], doc.child(i).name());
}
// check prev/next sibling
- var child = doc.child(2);
+ var child = doc.child(1);
assert.equal('child', child.name());
assert.equal(children[0], child.prevSibling().name());
assert.equal(children[2], child.nextSibling().name());
@@ -87,7 +87,7 @@ module.exports.parsed_children = function(assert) {
assert.equal(null, child.nextSibling().nextSibling());
// prev/next Element
- var child = doc.child(2);
+ var child = doc.child(1);
assert.equal('child', child.name());
assert.equal(children[0], child.prevElement().name());
assert.equal(children[2], child.nextElement().name());
Something went wrong with that request. Please try again.