1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @constructor
* @extends {WebInspector.View}
* @param {!Document} parsedXML
*/
WebInspector.XMLView = function(parsedXML)
{
WebInspector.View.call(this, true);
this.registerRequiredCSS("ui/xmlView.css");
this.contentElement.classList.add("shadow-xml-view", "source-code");
WebInspector.XMLView.Node.populate(new TreeOutline(this.contentElement.createChild("ol")), parsedXML);
}
/**
* @param {string} text
* @param {string} mimeType
* @return {?Document}
*/
WebInspector.XMLView.parseXML = function(text, mimeType)
{
var parsedXML;
try {
parsedXML = (new DOMParser()).parseFromString(text, mimeType);
} catch (e) {
return null;
}
if (parsedXML.body)
return null;
return parsedXML;
}
WebInspector.XMLView.prototype = {
__proto__: WebInspector.View.prototype
}
/**
* @constructor
* @extends {TreeElement}
* @param {!Node} node
* @param {boolean} closeTag
*/
WebInspector.XMLView.Node = function(node, closeTag)
{
TreeElement.call(this, "", null, true);
this._node = node;
this._closeTag = closeTag;
this._populated = false;
this.selectable = false;
this.hasChildren = !closeTag && !!node.childElementCount;
this._updateTitle();
}
/**
* @param {!TreeOutline|!TreeElement} root
* @param {!Node} xmlNode
*/
WebInspector.XMLView.Node.populate = function(root, xmlNode)
{
var node = xmlNode.firstChild;
while (node) {
var currentNode = node;
node = node.nextSibling;
var nodeType = currentNode.nodeType;
// ignore empty TEXT
if (nodeType === 3 && currentNode.nodeValue.match(/\s+/))
continue;
// ignore ATTRIBUTE, ENTITY_REFERENCE, ENTITY, DOCUMENT, DOCUMENT_TYPE, DOCUMENT_FRAGMENT, NOTATION
if ((nodeType !== 1) && (nodeType !== 3) && (nodeType !== 4) && (nodeType !== 7) && (nodeType !== 8))
continue;
root.appendChild(new WebInspector.XMLView.Node(currentNode, false));
}
}
WebInspector.XMLView.Node.prototype = {
_updateTitle: function()
{
var node = this._node;
switch (node.nodeType) {
case 1: // ELEMENT
var tag = node.tagName;
if (this._closeTag) {
this._setTitle(["</" + tag + ">", "shadow-xml-view-tag"]);
return;
}
var titleItems = ["<" + tag, "shadow-xml-view-tag"];
var attributes = node.attributes;
for (var i = 0; i < attributes.length; ++i) {
var attributeNode = attributes.item(i);
titleItems.push(
"\u00a0", "shadow-xml-view-tag",
attributeNode.name, "shadow-xml-view-attribute-name",
"=\"", "shadow-xml-view-tag",
attributeNode.value, "shadow-xml-view-attribute-value",
"\"", "shadow-xml-view-tag")
}
if (!this.expanded) {
if (node.childElementCount) {
titleItems.push(
">", "shadow-xml-view-tag",
"\u2026", "shadow-xml-view-comment",
"</" + tag, "shadow-xml-view-tag");
} else if (this._node.textContent) {
titleItems.push(
">", "shadow-xml-view-tag",
node.textContent, "shadow-xml-view-text",
"</" + tag, "shadow-xml-view-tag");
} else {
titleItems.push(" /", "shadow-xml-view-tag");
}
}
titleItems.push(">", "shadow-xml-view-tag");
this._setTitle(titleItems);
return;
case 3: // TEXT
this._setTitle([node.nodeValue, "shadow-xml-view-text"]);
return;
case 4: // CDATA
this._setTitle([
"<![CDATA[", "shadow-xml-view-cdata",
node.nodeValue, "shadow-xml-view-text",
"]]>", "shadow-xml-view-cdata"]);
return;
case 7: // PROCESSING_INSTRUCTION
this._setTitle(["<?" + node.nodeName + " " + node.nodeValue + "?>", "shadow-xml-view-processing-instruction"]);
return;
case 8: // COMMENT
this._setTitle(["<!--" + node.nodeValue + "-->", "shadow-xml-view-comment"]);
return;
}
},
/**
* @param {!Array.<string>} items
*/
_setTitle: function(items)
{
var titleFragment = createDocumentFragment();
for (var i = 0; i < items.length; i += 2)
titleFragment.createChild("span", items[i + 1]).textContent = items[i];
this.title = titleFragment;
},
onattach: function()
{
this.listItemElement.classList.toggle("shadow-xml-view-close-tag", this._closeTag);
},
onexpand: function()
{
this._updateTitle();
},
oncollapse: function()
{
this._updateTitle();
},
onpopulate: function()
{
if (this._populated || this._closeTag)
return;
WebInspector.XMLView.Node.populate(this, this._node);
this.appendChild(new WebInspector.XMLView.Node(this._node, true));
this._populated = true;
},
__proto__: TreeElement.prototype
}
|