From 7a3fb2273ed032ae6a7a516ef9592d9d2b20577e Mon Sep 17 00:00:00 2001 From: gorhill Date: Mon, 6 Jul 2015 18:11:11 -0400 Subject: [PATCH] dom inspector quirks: keep identical filters in sync, do not create duplicate filters --- src/js/logger-ui-inspector.js | 76 ++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/src/js/logger-ui-inspector.js b/src/js/logger-ui-inspector.js index f70357168..282aec161 100644 --- a/src/js/logger-ui-inspector.js +++ b/src/js/logger-ui-inspector.js @@ -50,6 +50,8 @@ var fingerprint = null; var inspector = uDom.nodeFromId('domInspector'); var domTree = uDom.nodeFromId('domTree'); var tabSelector = uDom.nodeFromId('pageSelector'); +var uidGenerator = 1; +var filterToIdMap = new Map(); /******************************************************************************/ @@ -71,13 +73,21 @@ var nodeFromDomEntry = function(entry) { node.setAttribute('data-cnt', value); li.appendChild(node); // cosmetic filter - if ( entry.filter !== undefined ) { - node = document.createElement('code'); - node.classList.add('filter'); - node.textContent = entry.filter; - li.appendChild(node); - li.classList.add('isCosmeticHide'); + if ( entry.filter === undefined ) { + return li; } + node = document.createElement('code'); + node.classList.add('filter'); + value = filterToIdMap.get(entry.filter); + if ( value === undefined ) { + value = uidGenerator.toString(); + filterToIdMap.set(entry.filter, value); + uidGenerator += 1; + } + node.setAttribute('data-filter-id', value); + node.textContent = entry.filter; + li.appendChild(node); + li.classList.add('isCosmeticHide'); return li; }; @@ -106,6 +116,8 @@ var renderDOMFull = function(response) { var ul = domTreeParent.removeChild(domTree); logger.removeAllChildren(domTree); + filterToIdMap.clear(); + var lvl = 0; var entries = response.layout; var n = entries.length; @@ -115,7 +127,6 @@ var renderDOMFull = function(response) { if ( entry.lvl === lvl ) { li = nodeFromDomEntry(entry); appendListItem(ul, li); - //expandIfBlockElement(li); continue; } if ( entry.lvl > lvl ) { @@ -124,7 +135,6 @@ var renderDOMFull = function(response) { li.classList.add('branch'); li = nodeFromDomEntry(entry); appendListItem(ul, li); - //expandIfBlockElement(li); lvl = entry.lvl; continue; } @@ -357,9 +367,15 @@ var startDialog = (function() { for ( i = 0; i < entries.length; i++ ) { taValue.push(inspectedHostname + '##' + entries[i]); } + var ids = new Set(), id; var nodes = domTree.querySelectorAll('code.filter.off'); for ( i = 0; i < nodes.length; i++ ) { node = nodes[i]; + id = node.getAttribute('data-filter-id'); + if ( ids.has(id) ) { + continue; + } + ids.add(id); unhideSelectors.push(node.textContent); taValue.push(inspectedHostname + '#@#' + node.textContent); } @@ -453,25 +469,47 @@ var onClick = function(ev) { return; } - // Toggle selector - if ( target.localName === 'code' ) { - var original = target.classList.contains('filter') === false; + // Not a node or filter + if ( target.localName !== 'code' ) { + return; + } + + // Toggle cosmetic filter + if ( target.classList.contains('filter') ) { messager.sendTo( { what: 'toggleNodes', - original: original, - target: original !== target.classList.toggle('off'), - selector: selectorFromNode(target, original ? 1 : 2), - nid: original ? nidFromNode(target) : '' + original: false, + target: target.classList.toggle('off'), + selector: selectorFromNode(target, 2), + nid: '' }, inspectedTabId, 'dom-inspector.js' ); - var cantCreate = domTree.querySelector('.off') === null; - inspector.querySelector('.permatoolbar .revert').classList.toggle('disabled', cantCreate); - inspector.querySelector('.permatoolbar .commit').classList.toggle('disabled', cantCreate); - return; + uDom('[data-filter-id="' + target.getAttribute('data-filter-id') + '"]', inspector).toggleClass( + 'off', + target.classList.contains('off') + ); } + // Toggle node + else { + messager.sendTo( + { + what: 'toggleNodes', + original: true, + target: target.classList.toggle('off') === false, + selector: selectorFromNode(target, 1), + nid: nidFromNode(target) + }, + inspectedTabId, + 'dom-inspector.js' + ); + } + + var cantCreate = domTree.querySelector('.off') === null; + inspector.querySelector('.permatoolbar .revert').classList.toggle('disabled', cantCreate); + inspector.querySelector('.permatoolbar .commit').classList.toggle('disabled', cantCreate); }; /******************************************************************************/