| var cleanRegex = /^([^\/]+?\/)?[^\:]+?\:/; |
| // SnippetTree constructor |
| function SnippetTree (obj) { |
| this.cleanTitle = function (title) { |
| return title.replace(cleanRegex, ""); |
| this.addChild = function (childData) { |
| var c = new SnippetTree (childData); |
| // Recursively parse children |
| this.parseChildren = function (children) { |
| for (var i in children) { |
| if (c.getAttribute("title")) { |
| var title = this.cleanTitle(c.getAttribute("title")); |
| var childTree = this.addChild({ type : title }); |
| childTree.parseChildren(c.childNodes); |
| else if (c.hasChildNodes()) |
| this.parseChildren(c.childNodes); |
| else if (c.nodeType === 3) |
| if (c.nodeValue.match(/[-a-z0-9]/i)) { |
| // Make tree from snippet |
| function translateTree (snippet) { |
| var html = document.createElement("tree"); |
| html.innerHTML = snippet; |
| return new SnippetTree({ type : "ROOT" }).parseChildren(html.childNodes); |