var secret = require("./secret"),
    Attr = require("./attr"),
    Node = require("./node"),
    NodeList = require("./node-list");

function Element(_, ownerDocument, name, namespaceURI) {
  Node.call(this, _, ownerDocument, name, null, Node.ELEMENT_NODE);
  Object.defineProperties(this, {
    namespaceURI: {enumerable: true, value: namespaceURI}
  });
}

var prototype = Element.prototype = Object.create(Node.prototype, {
  tagName: {get: function() { return this.nodeName; }}
});

prototype.constructor = Element;

prototype.getAttribute = function(name) {
  var attr = this.attributes.getNamedItem(name);
  return attr && attr.nodeValue;
};

prototype.setAttribute = function(name, value) {
  var attr = new Attr(secret, this.ownerDocument, name, true, value);
  this.attributes.setNamedItem(attr);
};

prototype.removeAttribute = function(name) {
  this.attributes.removeNamedItem(name);
};

prototype.getAttributeNode = function(name) {
  throw new Error("not yet implemented");
};

prototype.setAttributeNode = function(newAttr) {
  throw new Error("not yet implemented");
};

prototype.removeAttributeNode = function(oldAttr) {
  throw new Error("not yet implemented");
};

prototype.getElementsByTagName = function(tagName) {
  var nodes = [],
      child = this.firstChild;

  while (child) {
    if (child.tagName === tagName) nodes.push(child);
    child = child.firstChild || child.nextSibling || child.parentNode && child.parentNode.nextSibling;
  }

  return new NodeList(secret, nodes);
};

prototype.normalize = function() {
  throw new Error("not yet implemented");
};

prototype.toString = function() {
  return "<" + this.nodeName + ">";
};

module.exports = Element;

@@ -1283,16 +1283,21 @@
 hild;%0A%0A 
+ out:
  while (
@@ -1305,16 +1305,16 @@
 hild) %7B%0A
-
     if (
@@ -1362,16 +1362,38 @@
 ld);%0A   
+ if (child.firstChild)
  child =
@@ -1409,20 +1409,31 @@
 rstChild
- %7C%7C 
+;%0A    else if (
 child.ne
@@ -1445,48 +1445,187 @@
 ling
- %7C%7C child.parentNode && child.parentNode
+) child = child.nextSibling;%0A    else %7B%0A      do %7B%0A        child = child.parentNode;%0A        if (child === this) break out;%0A      %7D while (!child.nextSibling);%0A      child = child
 .nex
@@ -1634,16 +1634,22 @@
 ibling;%0A
+    %7D%0A
   %7D%0A%0A  r

Fix tree traversal.