#1587 dom add child not working in js

jessevdam Tue 26 Jul 2011

fan.dom.ElemPeer.prototype.add = function(self, child)
{
  this.elem.appendChild(child.elem);
  return this;
}

fan.dom.ElemPeer.prototype.remove = function(self, child)
{
  this.elem.removeChild(child.elem);
  return this;
}

Should this not be

fan.dom.ElemPeer.prototype.add = function(self, child)
{
  this.elem.appendChild(child.peer.elem);
  return this;
}

fan.dom.ElemPeer.prototype.remove = function(self, child)
{
  this.elem.removeChild(child.peer.elem);
  return this;
}

Because it currently trying to get the attribute elem on the fantom object itself

The javascript compiler generates the following code for the add function in fantom

fan.dom.Elem.prototype.add = function(child)
{
  return this.peer.add(this,child);
}

This test case is not included in the test script

andy Tue 26 Jul 2011

Thanks - pushed a fix

jessevdam Tue 26 Jul 2011

Thanks

It seems some other elements are not working any more. I am now trying to run the test script but some elements are failing. The first one I checked till now is the peer.name and peer.name$ are changed to peer.$name and peer.$name$

jessevdam Tue 26 Jul 2011

this

fan.dom.ElemPeer.prototype.get = function(self, name, def)
{
  if (name == "id")      return this.id(self);
  if (name == "name")    return this.name(self);
  if (name == "class")   return this.className(self);
  if (name == "style")   return this.style(self);
  if (name == "value")   return this.val(self);
  if (name == "checked") return this.checked(self);

  var val = this.elem[name];
  if (val != null) return val;
  if (def != null) return def;
  return null;
}

fan.dom.ElemPeer.prototype.set = function(self, name, val)
{
  if (name == "id")           this.id$(self, val);
  else if (name == "name")    this.name$(self, val);
  else if (name == "class")   this.className$(self, val);
  else if (name == "value")   this.val$(self, val);
  else if (name == "checked") this.checked$(self, val);
  else this.elem.setAttribute(name, val);
}

should become

fan.dom.ElemPeer.prototype.get = function(self, name, def)
{
  if (name == "id")      return self.peer.id(self);
  if (name == "name")    return self.peer.$name(self);
  if (name == "class")   return self.peer.className(self);
  if (name == "style")   return self.peer.style(self);
  if (name == "value")   return self.peer.val(self);
  if (name == "checked") return self.peer.checked(self);

  var val = self.peer.elem[name];
  if (val != null) return val;
  if (def != null) return def;
  return null;
}

fan.dom.ElemPeer.prototype.set = function(self, name, val)
{
  if (name == "id")           self.peer.id$(self, val);
  else if (name == "name")    self.peer.$name$(self, val);
  else if (name == "class")   self.peer.className$(self, val);
  else if (name == "value")   self.peer.val$(self, val);
  else if (name == "checked") self.peer.checked$(self, val);
  else self.peer.elem.setAttribute(name, val);
}

After fixing this all the test passed in the existing test script

jessevdam Tue 26 Jul 2011

Here is an extra test function for the test script

Void testAddRemove()
{
  parent := Win.cur.doc.createElem("div")
  child1 := Win.cur.doc.createElem("div")
  child2 := Win.cur.doc.createElem("div")
  child3 := Win.cur.doc.createElem("div")
  child4 := Win.cur.doc.createElem("div")

  parent.add(child1)
  parent.add(child2)
  parent.add(child3)
  parent.add(child4)

  verifyEq(parent.children[0],child1)
  verifyEq(parent.children[1],child2)
  verifyEq(parent.children[2],child3)
  verifyEq(parent.children[3],child4)

  parent.remove(child2)
  parent.remove(child3)

  verifyEq(parent.children[0],child1)
  verifyEq(parent.children[1],child4)
}

andy Tue 26 Jul 2011

Yep that slipped thru - pushed a fix - and added add/remove to the test suite. Changeset

jessevdam Tue 26 Jul 2011

Thanks

Login or Signup to reply.