Commit ce9ff678 authored by Dong Hwi Lee's avatar Dong Hwi Lee Committed by Commit Bot

Reduce unnecessary generation of HTML elements in DocumentXMLTreeViewer.js.

The current structure looks (in a very abstract form for readability,
e.g. <div class="collapsible"...> -> <collapsible>):
<collapsible>             (stay)
  <expanded>              (remove)
    <line>                (stay)
	  <collapse-button>   (-> collapsible-button)
	  <html-tag>          (stay)
	<collapsible-content> (-> expanded-content)
	  <collapsible>       (stay)
	<line>                (stay)
	  <html-tag>          (stay)
  <collapsed>             (-> collapsed-content,
                              become <text>...</text>,
                              move under <collapsible>)
    <line>                (remove)
	  <expand-button>     (merged to collapsible-button)
	  <html-tag>          (remove)
	  <text>...           (remove)
	  <html-tag>          (remove)

The changed structure looks:
<collapsible>
 <line>
   <collapsible-button>
   <html-tag>
 <expanded-content>
   <collapsible>
 <collapsed-content>...
 <line>
   <html-tag>

The saving in terms of number of elements ranges 20%-30% depending on the size of the text in an XML element. For example, a longer text in XML will create more HTML elements, which means the saving ratio is higher in that case.

Tested XML files (from random searches):
  https://www.sitemaps.org/sitemap.xml
  https://ririkaburbe.tk/sitemap_0.xml
  http://big5.sputniknews.cn/export/rss2/archive/index.xml

Change-Id: Ibfefb175b58bc54c3f31e65456ff832d432d5c6a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2062797
Commit-Queue: Dong-Hwi Lee <leedh@google.com>
Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/master@{#742705}
parent a945b5f6
...@@ -13,6 +13,10 @@ div.collapsible > div.hidden { ...@@ -13,6 +13,10 @@ div.collapsible > div.hidden {
display:none; display:none;
} }
div.collapsible > span.hidden {
display:none;
}
.pretty-print { .pretty-print {
margin-top: 1em; margin-top: 1em;
margin-left: 20px; margin-left: 20px;
...@@ -24,14 +28,15 @@ div.collapsible > div.hidden { ...@@ -24,14 +28,15 @@ div.collapsible > div.hidden {
display: none; display: none;
} }
.collapsible-content { .expanded-content {
margin-left: 1em; margin-left: 1em;
} }
.comment { .comment {
white-space: pre; white-space: pre;
} }
.button { .collapsible-button {
-webkit-user-select: none; -webkit-user-select: none;
cursor: pointer; cursor: pointer;
display: inline-block; display: inline-block;
......
...@@ -116,16 +116,13 @@ function processShortTextOnlyElement(parentElement, node) ...@@ -116,16 +116,13 @@ function processShortTextOnlyElement(parentElement, node)
function processComplexElement(parentElement, node) function processComplexElement(parentElement, node)
{ {
var collapsible = createCollapsible(); var collapsible = createCollapsible();
collapsible.expanded.start.appendChild(createTag(node, false, false)); collapsible.start.appendChild(createTag(node, false, false));
for (var child = node.firstChild; child; child = child.nextSibling) for (var child = node.firstChild; child; child = child.nextSibling)
processNode(collapsible.expanded.content, child); processNode(collapsible.expandedContent, child);
collapsible.expanded.end.appendChild(createTag(node, true, false)); collapsible.end.appendChild(createTag(node, true, false));
collapsible.collapsed.content.appendChild(createTag(node, false, false));
collapsible.collapsed.content.appendChild(createText('...'));
collapsible.collapsed.content.appendChild(createTag(node, true, false));
parentElement.appendChild(collapsible); parentElement.appendChild(collapsible);
} }
...@@ -138,13 +135,10 @@ function processComment(parentElement, node) ...@@ -138,13 +135,10 @@ function processComment(parentElement, node)
} else { } else {
var collapsible = createCollapsible(); var collapsible = createCollapsible();
collapsible.expanded.start.appendChild(createComment('<!--')); collapsible.start.appendChild(createComment('<!--'));
collapsible.expanded.content.appendChild(createComment(node.nodeValue)); collapsible.expandedContent.appendChild(createComment(node.nodeValue));
collapsible.expanded.end.appendChild(createComment('-->')); collapsible.end.appendChild(createComment('-->'));
collapsible.collapsed.content.appendChild(createComment('<!--'));
collapsible.collapsed.content.appendChild(createComment('...'));
collapsible.collapsed.content.appendChild(createComment('-->'));
parentElement.appendChild(collapsible); parentElement.appendChild(collapsible);
} }
} }
...@@ -158,13 +152,9 @@ function processCDATA(parentElement, node) ...@@ -158,13 +152,9 @@ function processCDATA(parentElement, node)
} else { } else {
var collapsible = createCollapsible(); var collapsible = createCollapsible();
collapsible.expanded.start.appendChild(createText('<![CDATA[')); collapsible.start.appendChild(createText('<![CDATA['));
collapsible.expanded.content.appendChild(createText(node.nodeValue)); collapsible.expandedContent.appendChild(createText(node.nodeValue));
collapsible.expanded.end.appendChild(createText(']]>')); collapsible.end.appendChild(createText(']]>'));
collapsible.collapsed.content.appendChild(createText('<![CDATA['));
collapsible.collapsed.content.appendChild(createText('...'));
collapsible.collapsed.content.appendChild(createText(']]>'));
parentElement.appendChild(collapsible); parentElement.appendChild(collapsible);
} }
} }
...@@ -178,13 +168,10 @@ function processProcessingInstruction(parentElement, node) ...@@ -178,13 +168,10 @@ function processProcessingInstruction(parentElement, node)
} else { } else {
var collapsible = createCollapsible(); var collapsible = createCollapsible();
collapsible.expanded.start.appendChild(createComment('<?' + node.nodeName)); collapsible.start.appendChild(createComment('<?' + node.nodeName));
collapsible.expanded.content.appendChild(createComment(node.nodeValue)); collapsible.expandedContent.appendChild(createComment(node.nodeValue));
collapsible.expanded.end.appendChild(createComment('?>')); collapsible.end.appendChild(createComment('?>'));
collapsible.collapsed.content.appendChild(createComment('<?' + node.nodeName));
collapsible.collapsed.content.appendChild(createComment('...'));
collapsible.collapsed.content.appendChild(createComment('?>'));
parentElement.appendChild(collapsible); parentElement.appendChild(collapsible);
} }
} }
...@@ -212,28 +199,23 @@ function createCollapsible() ...@@ -212,28 +199,23 @@ function createCollapsible()
{ {
var collapsible = createHTMLElement('div'); var collapsible = createHTMLElement('div');
collapsible.classList.add('collapsible'); collapsible.classList.add('collapsible');
collapsible.expanded = createHTMLElement('div');
collapsible.expanded.classList.add('expanded');
collapsible.appendChild(collapsible.expanded);
collapsible.expanded.start = createLine(); collapsible.start = createLine();
collapsible.expanded.start.appendChild(createCollapseButton()); collapsible.start.appendChild(createCollapsibleButton());
collapsible.expanded.appendChild(collapsible.expanded.start); collapsible.appendChild(collapsible.start);
collapsible.expanded.content = createHTMLElement('div'); collapsible.expandedContent = createHTMLElement('div');
collapsible.expanded.content.classList.add('collapsible-content'); collapsible.expandedContent.classList.add('expanded-content');
collapsible.expanded.appendChild(collapsible.expanded.content); collapsible.appendChild(collapsible.expandedContent);
collapsible.expanded.end = createLine(); // Collapsed content.
collapsible.expanded.appendChild(collapsible.expanded.end); collapsible.collapsedContent = createText('...');
collapsible.collapsedContent.classList.add('collapsed-content');
collapsible.collapsedContent.classList.add('hidden');
collapsible.appendChild(collapsible.collapsedContent);
collapsible.collapsed = createHTMLElement('div'); collapsible.end = createLine();
collapsible.collapsed.classList.add('collapsed'); collapsible.appendChild(collapsible.end);
collapsible.collapsed.classList.add('hidden');
collapsible.appendChild(collapsible.collapsed);
collapsible.collapsed.content = createLine();
collapsible.collapsed.content.appendChild(createExpandButton());
collapsible.collapsed.appendChild(collapsible.collapsed.content);
return collapsible; return collapsible;
} }
...@@ -245,20 +227,13 @@ function createButton() ...@@ -245,20 +227,13 @@ function createButton()
return button; return button;
} }
function createCollapseButton(str) function createCollapsibleButton(str) {
{
var button = createButton(); var button = createButton();
button.classList.add('collapsible-button');
button.classList.add('collapse-button'); button.classList.add('collapse-button');
return button; return button;
} }
function createExpandButton(str)
{
var button = createButton();
button.classList.add('expand-button');
return button;
}
function createComment(commentString) function createComment(commentString)
{ {
var comment = createHTMLElement('span'); var comment = createHTMLElement('span');
...@@ -336,21 +311,35 @@ function createAttribute(attributeNode) ...@@ -336,21 +311,35 @@ function createAttribute(attributeNode)
return attribute; return attribute;
} }
function expandFunction(sectionId) function toggleFunction(sectionId) {
{ return function() {
return function() var collapsedContent =
{ document.querySelector('#' + sectionId + ' > .collapsed-content');
document.querySelector('#' + sectionId + ' > .expanded').className = 'expanded'; var expandedContent =
document.querySelector('#' + sectionId + ' > .collapsed').className = 'collapsed hidden'; document.querySelector('#' + sectionId + ' > .expanded-content');
}; var collapsible_button = document.querySelector(
} '#' + sectionId + ' > .line > .collapsible-button');
function collapseFunction(sectionId) if (collapsedContent) {
{ if (collapsedContent.className.includes('hidden'))
return function() collapsedContent.className = 'collapsed-content';
{ else
document.querySelector('#' + sectionId + ' > .expanded').className = 'expanded hidden'; collapsedContent.className = 'collapsed-content hidden';
document.querySelector('#' + sectionId + ' > .collapsed').className = 'collapsed'; }
if (expandedContent) {
if (expandedContent.className.includes('hidden'))
expandedContent.className = 'expanded-content';
else
expandedContent.className = 'expanded-content hidden';
}
if (collapsible_button) {
if (collapsible_button.className.includes('expand-button'))
collapsible_button.className = 'collapsible-button collapse-button';
else
collapsible_button.className = 'collapsible-button expand-button';
}
}; };
} }
...@@ -361,15 +350,9 @@ function initButtons() ...@@ -361,15 +350,9 @@ function initButtons()
var sectionId = 'collapsible' + i; var sectionId = 'collapsible' + i;
sections[i].id = sectionId; sections[i].id = sectionId;
var expandedPart = sections[i].querySelector('#' + sectionId + ' > .expanded'); var collapseButton = sections[i].querySelector('.collapsible-button');
var collapseButton = expandedPart.querySelector('.collapse-button'); collapseButton.onclick = toggleFunction(sectionId);
collapseButton.onclick = collapseFunction(sectionId);
collapseButton.onmousedown = handleButtonMouseDown; collapseButton.onmousedown = handleButtonMouseDown;
var collapsedPart = sections[i].querySelector('#' + sectionId + ' > .collapsed');
var expandButton = collapsedPart.querySelector('.expand-button');
expandButton.onclick = expandFunction(sectionId);
expandButton.onmousedown = handleButtonMouseDown;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment