// Copyright (c) 2014 Rafael Caricio. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


var InlineAnnotations = function(template) {
  this.mapping = {
    'ml-bind': {
      method: 'bindToContent',
      config: function(element, attrValue) {
        return {elements: [element], scopeKey: attrValue};
      }
    },
    'ml-model': {
      method: 'bindToValue',
      config: function(element, attrValue) {
        return {elements: [element], scopeKey: attrValue};
      }
    },
    'ml-style': {
      method: 'bindToStyle',
      config: function(element, attrValue) {
        return {elements: [element], expression: attrValue};
      }
    },
    'ml-show': {
      method: 'bindToVisible',
      config: function(element, attrValue) {
        return {elements: [element], expression: attrValue};
      }
    }
  };
  this.template = template;
};

InlineAnnotations.prototype.parse = function(scope) {
  this.parseNodes(scope, this.template.getContent().childNodes[0]);
  return this.template.getContent();
};

InlineAnnotations.prototype.parseNodes = function(scope, rootNode) {
  if (!rootNode) {
    return;
  }

  if (rootNode.getAttribute) {
    for (var magicalAttribute in this.mapping) {
      var attrValue = rootNode.getAttribute(magicalAttribute);
      if (attrValue) {
        var mapSettings = this.mapping[magicalAttribute];
        this.template[mapSettings.method](scope, mapSettings.config(rootNode, attrValue));
      }
    }
  }

  this.parseNodes(scope, rootNode.nextElementSibling);
  for (var i = 0; i < rootNode.childNodes.length; i++) {
    this.parseNodes(scope, rootNode.childNodes[i]);
  }
};

module.exports = InlineAnnotations;

@@ -1037,22 +1037,8 @@
 nt()
-.childNodes%5B0%5D
 );%0A 
@@ -1152,69 +1152,189 @@
 %7B%0A  
-if (!rootNode) %7B%0A    return;%0A  %7D%0A%0A  if (rootNode.getAttribute
+var walker = document.createTreeWalker(%0A    rootNode,%0A    NodeFilter.SHOW_ELEMENT,%0A    function(node) %7B return NodeFilter.FILTER_ACCEPT; %7D,%0A    true%0A  );%0A%0A  while (walker.nextNode()
 ) %7B%0A
@@ -1392,16 +1392,53 @@
     var 
+node = walker.currentNode,%0A          
 attrValu
@@ -1441,21 +1441,17 @@
 Value = 
-rootN
+n
 ode.getA
@@ -1478,16 +1478,17 @@
 ibute);%0A
+%0A
       if
@@ -1489,16 +1489,18 @@
     if (
+!!
 attrValu
@@ -1630,21 +1630,17 @@
 .config(
-rootN
+n
 ode, att
@@ -1670,177 +1670,8 @@
 %0A  %7D
-%0A%0A  this.parseNodes(scope, rootNode.nextElementSibling);%0A  for (var i = 0; i %3C rootNode.childNodes.length; i++) %7B%0A    this.parseNodes(scope, rootNode.childNodes%5Bi%5D);%0A  %7D
 %0A%7D;%0A

Use brower API to walk through DOM nodes