xmlTreeViewer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. window.xmlTreeViewer = (function() {
  2. var documentNode = null;
  3. var nodeParentPairs = [];
  4. var tree;
  5. var container;
  6. function prepareXMLViewer(node) {
  7. documentNode = node;
  8. var body = createHTMLElement('div');
  9. var sourceXML = createHTMLElement('div');
  10. sourceXML.id = 'xml-viewer-source-xml';
  11. body.appendChild(sourceXML);
  12. var child;
  13. while (child = documentNode.firstChild) {
  14. documentNode.removeChild(child);
  15. if (child.nodeType != Node.DOCUMENT_TYPE_NODE)
  16. sourceXML.appendChild(child);
  17. }
  18. tree = createHTMLElement('div');
  19. body.appendChild(tree);
  20. tree.classList.add('pretty-print');
  21. //window.onload = sourceXMLLoaded;
  22. container = body;
  23. sourceXMLLoaded();
  24. return body;
  25. }
  26. function sourceXMLLoaded() {
  27. //var sourceXML = container.getElementById('webkit-xml-viewer-source-xml');
  28. var sourceXML = container.firstChild;
  29. if (!sourceXML)
  30. return; // Stop if some XML tree extension is already processing this document
  31. for (var child = sourceXML.firstChild; child; child = child.nextSibling)
  32. nodeParentPairs.push({parentElement: tree, node: child});
  33. for (var i = 0; i < nodeParentPairs.length; i++)
  34. processNode(nodeParentPairs[i].parentElement, nodeParentPairs[i].node);
  35. initButtons();
  36. return false;
  37. }
  38. // Tree processing.
  39. function processNode(parentElement, node) {
  40. var map = processNode.processorsMap;
  41. if (!map) {
  42. map = {};
  43. processNode.processorsMap = map;
  44. map[Node.PROCESSING_INSTRUCTION_NODE] = processProcessingInstruction;
  45. map[Node.ELEMENT_NODE] = processElement;
  46. map[Node.COMMENT_NODE] = processComment;
  47. map[Node.TEXT_NODE] = processText;
  48. map[Node.CDATA_SECTION_NODE] = processCDATA;
  49. }
  50. if (processNode.processorsMap[node.nodeType])
  51. processNode.processorsMap[node.nodeType].call(this, parentElement, node);
  52. }
  53. function processElement(parentElement, node) {
  54. if (!node.firstChild)
  55. processEmptyElement(parentElement, node);
  56. else {
  57. var child = node.firstChild;
  58. if (child.nodeType == Node.TEXT_NODE && isShort(child.nodeValue) && !child.nextSibling)
  59. processShortTextOnlyElement(parentElement, node);
  60. else
  61. processComplexElement(parentElement, node);
  62. }
  63. }
  64. function processEmptyElement(parentElement, node) {
  65. var line = createLine();
  66. line.appendChild(createTag(node, false, true));
  67. parentElement.appendChild(line);
  68. }
  69. function processShortTextOnlyElement(parentElement, node) {
  70. var line = createLine();
  71. line.appendChild(createTag(node, false, false));
  72. for (var child = node.firstChild; child; child = child.nextSibling)
  73. line.appendChild(createText(child.nodeValue));
  74. line.appendChild(createTag(node, true, false));
  75. parentElement.appendChild(line);
  76. }
  77. function processComplexElement(parentElement, node) {
  78. var collapsible = createCollapsible();
  79. collapsible.expanded.start.appendChild(createTag(node, false, false));
  80. for (var child = node.firstChild; child; child = child.nextSibling)
  81. nodeParentPairs.push({parentElement: collapsible.expanded.content, node: child});
  82. collapsible.expanded.end.appendChild(createTag(node, true, false));
  83. collapsible.collapsed.content.appendChild(createTag(node, false, false));
  84. collapsible.collapsed.content.appendChild(createText('...'));
  85. collapsible.collapsed.content.appendChild(createTag(node, true, false));
  86. parentElement.appendChild(collapsible);
  87. }
  88. function processComment(parentElement, node) {
  89. if (isShort(node.nodeValue)) {
  90. var line = createLine();
  91. line.appendChild(createComment('<!-- ' + node.nodeValue + ' -->'));
  92. parentElement.appendChild(line);
  93. } else {
  94. var collapsible = createCollapsible();
  95. collapsible.expanded.start.appendChild(createComment('<!--'));
  96. collapsible.expanded.content.appendChild(createComment(node.nodeValue));
  97. collapsible.expanded.end.appendChild(createComment('-->'));
  98. collapsible.collapsed.content.appendChild(createComment('<!--'));
  99. collapsible.collapsed.content.appendChild(createComment('...'));
  100. collapsible.collapsed.content.appendChild(createComment('-->'));
  101. parentElement.appendChild(collapsible);
  102. }
  103. }
  104. function processCDATA(parentElement, node) {
  105. if (isShort(node.nodeValue)) {
  106. var line = createLine();
  107. line.appendChild(createText('<![CDATA[ ' + node.nodeValue + ' ]]>'));
  108. parentElement.appendChild(line);
  109. } else {
  110. var collapsible = createCollapsible();
  111. collapsible.expanded.start.appendChild(createText('<![CDATA['));
  112. collapsible.expanded.content.appendChild(createText(node.nodeValue));
  113. collapsible.expanded.end.appendChild(createText(']]>'));
  114. collapsible.collapsed.content.appendChild(createText('<![CDATA['));
  115. collapsible.collapsed.content.appendChild(createText('...'));
  116. collapsible.collapsed.content.appendChild(createText(']]>'));
  117. parentElement.appendChild(collapsible);
  118. }
  119. }
  120. function processProcessingInstruction(parentElement, node) {
  121. if (isShort(node.nodeValue)) {
  122. var line = createLine();
  123. line.appendChild(createComment('<?' + node.nodeName + ' ' + node.nodeValue + '?>'));
  124. parentElement.appendChild(line);
  125. } else {
  126. var collapsible = createCollapsible();
  127. collapsible.expanded.start.appendChild(createComment('<?' + node.nodeName));
  128. collapsible.expanded.content.appendChild(createComment(node.nodeValue));
  129. collapsible.expanded.end.appendChild(createComment('?>'));
  130. collapsible.collapsed.content.appendChild(createComment('<?' + node.nodeName));
  131. collapsible.collapsed.content.appendChild(createComment('...'));
  132. collapsible.collapsed.content.appendChild(createComment('?>'));
  133. parentElement.appendChild(collapsible);
  134. }
  135. }
  136. function processText(parentElement, node) {
  137. parentElement.appendChild(createText(node.nodeValue));
  138. }
  139. // Processing utils.
  140. function trim(value) {
  141. return value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  142. }
  143. function isShort(value) {
  144. return trim(value).length <= 50;
  145. }
  146. // Tree rendering.
  147. function createHTMLElement(elementName) {
  148. //return document.createElementNS('http://www.w3.org/1999/xhtml', elementName);
  149. return document.createElement(elementName);
  150. }
  151. function createCollapsible() {
  152. var collapsible = createHTMLElement('div');
  153. collapsible.classList.add('collapsible');
  154. collapsible.expanded = createHTMLElement('div');
  155. collapsible.expanded.classList.add('expanded');
  156. collapsible.appendChild(collapsible.expanded);
  157. collapsible.expanded.start = createLine();
  158. collapsible.expanded.start.appendChild(createCollapseButton());
  159. collapsible.expanded.appendChild(collapsible.expanded.start);
  160. collapsible.expanded.content = createHTMLElement('div');
  161. collapsible.expanded.content.classList.add('collapsible-content');
  162. collapsible.expanded.appendChild(collapsible.expanded.content);
  163. collapsible.expanded.end = createLine();
  164. collapsible.expanded.appendChild(collapsible.expanded.end);
  165. collapsible.collapsed = createHTMLElement('div');
  166. collapsible.collapsed.classList.add('collapsed');
  167. collapsible.collapsed.classList.add('hidden');
  168. collapsible.appendChild(collapsible.collapsed);
  169. collapsible.collapsed.content = createLine();
  170. collapsible.collapsed.content.appendChild(createExpandButton());
  171. collapsible.collapsed.appendChild(collapsible.collapsed.content);
  172. return collapsible;
  173. }
  174. function createButton() {
  175. var button = createHTMLElement('span');
  176. button.classList.add('button');
  177. return button;
  178. }
  179. function createCollapseButton(str) {
  180. var button = createButton();
  181. button.classList.add('collapse-button');
  182. return button;
  183. }
  184. function createExpandButton(str) {
  185. var button = createButton();
  186. button.classList.add('expand-button');
  187. return button;
  188. }
  189. function createComment(commentString) {
  190. var comment = createHTMLElement('span');
  191. comment.classList.add('comment');
  192. comment.classList.add('html-comment');
  193. comment.textContent = commentString;
  194. return comment;
  195. }
  196. function createText(value) {
  197. var text = createHTMLElement('span');
  198. text.textContent = trim(value);
  199. text.classList.add('html-text');
  200. return text;
  201. }
  202. function createLine() {
  203. var line = createHTMLElement('div');
  204. line.classList.add('line');
  205. return line;
  206. }
  207. function createTag(node, isClosing, isEmpty) {
  208. var tag = createHTMLElement('span');
  209. tag.classList.add('html-tag');
  210. var stringBeforeAttrs = '<';
  211. if (isClosing)
  212. stringBeforeAttrs += '/';
  213. //stringBeforeAttrs += node.nodeName;
  214. var textBeforeAttrs = document.createTextNode(stringBeforeAttrs);
  215. tag.appendChild(textBeforeAttrs);
  216. var tagNode = createHTMLElement('span');
  217. tagNode.classList.add('html-tag-name');
  218. tagNode.textContent = node.nodeName;
  219. tag.appendChild(tagNode);
  220. if (!isClosing) {
  221. for (var i = 0; i < node.attributes.length; i++)
  222. tag.appendChild(createAttribute(node.attributes[i]));
  223. }
  224. var stringAfterAttrs = '';
  225. if (isEmpty)
  226. stringAfterAttrs += '/';
  227. stringAfterAttrs += '>';
  228. var textAfterAttrs = document.createTextNode(stringAfterAttrs);
  229. tag.appendChild(textAfterAttrs);
  230. return tag;
  231. }
  232. function createAttribute(attributeNode) {
  233. var attribute = createHTMLElement('span');
  234. attribute.classList.add('html-attribute');
  235. var attributeName = createHTMLElement('span');
  236. attributeName.classList.add('html-attribute-name');
  237. attributeName.textContent = attributeNode.name;
  238. var textBefore = document.createTextNode(' ');
  239. var textBetween = document.createTextNode('="');
  240. var attributeValue = createHTMLElement('span');
  241. attributeValue.classList.add('html-attribute-value');
  242. attributeValue.textContent = attributeNode.value;
  243. var textAfter = document.createTextNode('"');
  244. attribute.appendChild(textBefore);
  245. attribute.appendChild(attributeName);
  246. attribute.appendChild(textBetween);
  247. attribute.appendChild(attributeValue);
  248. attribute.appendChild(textAfter);
  249. return attribute;
  250. }
  251. function expandFunction(sectionId) {
  252. return function () {
  253. container.querySelector('#' + sectionId + ' > .expanded').className = 'expanded';
  254. container.querySelector('#' + sectionId + ' > .collapsed').className = 'collapsed hidden';
  255. };
  256. }
  257. function collapseFunction(sectionId) {
  258. return function () {
  259. container.querySelector('#' + sectionId + ' > .expanded').className = 'expanded hidden';
  260. container.querySelector('#' + sectionId + ' > .collapsed').className = 'collapsed';
  261. };
  262. }
  263. function initButtons() {
  264. var sections = container.querySelectorAll('.collapsible');
  265. for (var i = 0; i < sections.length; i++) {
  266. var sectionId = 'collapsible' + i;
  267. sections[i].id = sectionId;
  268. var expandedPart = sections[i].querySelector('#' + sectionId + ' > .expanded');
  269. var collapseButton = expandedPart.querySelector('.collapse-button');
  270. collapseButton.onclick = collapseFunction(sectionId);
  271. collapseButton.onmousedown = handleButtonMouseDown;
  272. var collapsedPart = sections[i].querySelector('#' + sectionId + ' > .collapsed');
  273. var expandButton = collapsedPart.querySelector('.expand-button');
  274. expandButton.onclick = expandFunction(sectionId);
  275. expandButton.onmousedown = handleButtonMouseDown;
  276. }
  277. }
  278. function handleButtonMouseDown(e) {
  279. // To prevent selection on double click
  280. e.preventDefault();
  281. }
  282. var parseXML = function( data ) {
  283. var xml, tmp;
  284. if ( !data || typeof data !== "string" ) {
  285. return null;
  286. }
  287. try {
  288. if ( window.DOMParser ) { // Standard
  289. tmp = new window.DOMParser();
  290. xml = tmp.parseFromString( data, "text/xml" );
  291. } else { // IE
  292. xml = new window.ActiveXObject( "Microsoft.XMLDOM" );
  293. xml.async = "false";
  294. xml.loadXML( data );
  295. }
  296. } catch ( e ) {
  297. xml = undefined;
  298. }
  299. var errMsg;
  300. if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) {
  301. errMsg = xml.getElementsByTagName( "parsererror" )[0].textContent;
  302. }
  303. return {xml: xml, errMsg: errMsg};
  304. };
  305. return {
  306. getXMLViewerNode: prepareXMLViewer,
  307. parseXML: parseXML
  308. }
  309. })();