From a6792a2b4baa1b710aae0958e5b2ea8ad3bc8b19 Mon Sep 17 00:00:00 2001 From: Piro / YUKI Hiroshi Date: Sun, 23 Sep 2012 15:43:49 +0900 Subject: [PATCH] Reduce usecases of evaluateXPath() --- content/treestyletab/windowHelperHacks.js | 2 +- modules/autoHide.js | 2 +- modules/browser.js | 46 +++++------ modules/tabbarDNDObserver.js | 8 +- modules/utils.js | 95 +++++++---------------- modules/window.js | 5 +- tests/unit/treestyletab_tabUtils.test.js | 19 ++--- 7 files changed, 67 insertions(+), 110 deletions(-) diff --git a/content/treestyletab/windowHelperHacks.js b/content/treestyletab/windowHelperHacks.js index f1cdf9e8..1d5762a1 100644 --- a/content/treestyletab/windowHelperHacks.js +++ b/content/treestyletab/windowHelperHacks.js @@ -205,7 +205,7 @@ TreeStyleTabWindowHelper.overrideExtensionsPreInit = function TSTWH_overrideExte 'var tabcount = ', 1 && + sv.getTabs(b).length > 1 && !aEvent.altKey && w.TreeStyleTabService.accelKeyPressed ) { diff --git a/modules/browser.js b/modules/browser.js index b856b896..95a2aa55 100644 --- a/modules/browser.js +++ b/modules/browser.js @@ -374,7 +374,7 @@ TreeStyleTabBrowser.prototype = { getTabFromCoordinate : function TSTBrowser_getTabFromCoordinate(aCoordinate) { var tab = null; - this.getTabsArray(this.mTabBrowser).some(function(aTab) { + this.getTabs(this.mTabBrowser).some(function(aTab) { var box = aTab.boxObject; if (box[this.screenPositionProp] > aCoordinate || box[this.screenPositionProp] + box[this.sizeProp] < aCoordinate) { @@ -558,7 +558,7 @@ TreeStyleTabBrowser.prototype = { updateTabsZIndex : function TSTBrowser_updateTabsZIndex(aStacked) { - var tabs = this.getTabsArray(this.mTabBrowser); + var tabs = this.getTabs(this.mTabBrowser); var count = tabs.length; for (let i = 0; i < count; i++) { @@ -678,7 +678,7 @@ TreeStyleTabBrowser.prototype = { this._initTabbrowserContextMenu(); w.TreeStyleTabWindowHelper.updateTabDNDObserver(b); - this.getAllTabsArray(b).forEach(this.initTab, this); + this.getAllTabs(b).forEach(this.initTab, this); this.onPrefChange('extensions.treestyletab.maxTreeLevel'); this.onPrefChange('extensions.treestyletab.tabbar.style'); @@ -1159,7 +1159,7 @@ TreeStyleTabBrowser.prototype = { [aTarget] : (typeof aTarget == 'object' && 'length' in aTarget) ? Array.slice(aTarget) : - self.getAllTabsArray(b); + self.getAllTabs(b); for (let i = 0, maxi = tabs.length; i < maxi; i++) { self.initTabContentsOrder(tabs[i]); @@ -1401,7 +1401,7 @@ TreeStyleTabBrowser.prototype = { } } - var tabs = this.getAllTabsArray(b); + var tabs = this.getAllTabs(b); for (let i = 0, maxi = tabs.length; i < maxi; i++) { let tab = tabs[i]; @@ -1745,7 +1745,7 @@ TreeStyleTabBrowser.prototype = { if (splitter.collapsed || splitter.getAttribute('state') != 'collapsed') { this._tabStripPlaceHolder.collapsed = splitter.collapsed = - (this.getPref('browser.tabs.autoHide') && this.getTabsArray(this.mTabBrowser).length == 1); + (this.getPref('browser.tabs.autoHide') && this.getTabs(this.mTabBrowser).length == 1); } var strip = this.tabStrip; @@ -1943,7 +1943,7 @@ TreeStyleTabBrowser.prototype = { reinitAllTabs : function TSTBrowser_reinitAllTabs(aSouldUpdateCount) { - var tabs = this.getAllTabsArray(this.mTabBrowser); + var tabs = this.getAllTabs(this.mTabBrowser); for (let i = 0, maxi = tabs.length; i < maxi; i++) { let tab = tabs[i]; @@ -1984,7 +1984,7 @@ TreeStyleTabBrowser.prototype = { var b = this.mTabBrowser; delete b.tabContainer.treeStyleTab; - var tabs = this.getAllTabsArray(b); + var tabs = this.getAllTabs(b); for (let i = 0, maxi = tabs.length; i < maxi; i++) { let tab = tabs[i]; @@ -2294,7 +2294,7 @@ TreeStyleTabBrowser.prototype = { var b = this.mTabBrowser; var value = this.getPref(aPrefName); var tabContainer = b.mTabContainer; - var tabs = this.getAllTabsArray(b); + var tabs = this.getAllTabs(b); switch (aPrefName) { case 'extensions.treestyletab.tabbar.position': @@ -2413,7 +2413,7 @@ TreeStyleTabBrowser.prototype = { return this.updateInvertedTabContentsOrder(true); case 'browser.tabs.autoHide': - if (this.getTabsArray(this.mTabBrowser).length == 1) + if (this.getTabs(this.mTabBrowser).length == 1) this.updateFloatingTabbar(this.kTABBAR_UPDATE_BY_SHOWHIDE_TABBAR); return; @@ -2824,7 +2824,7 @@ TreeStyleTabBrowser.prototype = { // if there is only one tab and new another tab is opened, // closebox appearance is possibly changed. - var tabs = this.getTabsArray(b); + var tabs = this.getTabs(b); if (tabs.length == 2) this.updateInvertedTabContentsOrder(tabs); @@ -3150,7 +3150,7 @@ TreeStyleTabBrowser.prototype = { var old = aEvent.detail; if (old > tab._tPos) old--; - var tabs = this.getAllTabsArray(b); + var tabs = this.getAllTabs(b); old = tabs[old]; prev = this.getPreviousSiblingTab(old); @@ -3195,7 +3195,7 @@ TreeStyleTabBrowser.prototype = { if (aOldPosition === void(0)) aOldPosition = aTab._tPos; var pos = this.getChildIndex(aTab, parent); - var oldPos = this.getChildIndex(this.getAllTabsArray(this.mTabBrowser)[aOldPosition], parent); + var oldPos = this.getChildIndex(this.getAllTabs(this.mTabBrowser)[aOldPosition], parent); var delta; if (pos == oldPos) { // no move? return; @@ -3374,7 +3374,7 @@ TreeStyleTabBrowser.prototype = { { this.internallyTabMovingCount++; - var allTabs = this.getAllTabsArray(this.mTabBrowser); + var allTabs = this.getAllTabs(this.mTabBrowser); var normalTabs = allTabs.filter(function(aTab) { return !aTab.hasAttribute('pinned'); }); @@ -3453,7 +3453,7 @@ TreeStyleTabBrowser.prototype = { this.internallyTabMovingCount++; var w = this.window; var b = this.mTabBrowser; - var lastCount = this.getAllTabs(b).snapshotLength - 1; + var lastCount = this.getAllTabs(b).length - 1; this.detachTab(aParent); b.moveTabTo(aParent, lastCount); @@ -4693,7 +4693,7 @@ TreeStyleTabBrowser.prototype = { resetAllTabs : function TSTBrowser_resetAllTabs(aDetachAllChildren) { - var tabs = this.getAllTabsArray(this.mTabBrowser); + var tabs = this.getAllTabs(this.mTabBrowser); for (let i = 0, maxi = tabs.length; i < maxi; i++) { this.resetTab(tabs[i], aDetachAllChildren); @@ -4731,7 +4731,7 @@ TreeStyleTabBrowser.prototype = { this.allowSubtreeCollapseExpand = true; delete this._lastAllowSubtreeCollapseExpand; - let tabs = this.getAllTabsArray(this.browser); + let tabs = this.getAllTabs(this.browser); for (let i = 0, maxi = tabs.length; i < maxi; i++) { let tab = tabs[i]; @@ -4746,7 +4746,7 @@ TreeStyleTabBrowser.prototype = { this.updateTabsIndent(this.rootTabs, undefined, true); } else { - let tabs = this.getAllTabsArray(this.browser); + let tabs = this.getAllTabs(this.browser); for (let i = 0, maxi = tabs.length; i < maxi; i++) { let tab = tabs[i]; @@ -5002,7 +5002,7 @@ TreeStyleTabBrowser.prototype = { if ( this.isGroupTab(aTab) && - this.getTabsArray(b).filter(function(aTab) { + this.getTabs(b).filter(function(aTab) { return !b._removingTabs || b._removingTabs.indexOf(aTab) < 0; }).length == children.length ) { @@ -5390,7 +5390,7 @@ TreeStyleTabBrowser.prototype = { if (this.maxTreeLevel < 0 || !this.maxTreeLevelPhisical) return; - var tabs = aParent ? this.getDescendantTabs(aParent) : this.getAllTabsArray(this.mTabBrowser) ; + var tabs = aParent ? this.getDescendantTabs(aParent) : this.getAllTabs(this.mTabBrowser) ; for (let i = 0, maxi = tabs.length; i < maxi; i++) { let level = parseInt(tab.getAttribute(this.kNEST) || 0); @@ -5513,7 +5513,7 @@ TreeStyleTabBrowser.prototype = { var shouldClose = ( !aOptions.duplicate && - sourceService.getAllTabsArray(sourceBrowser).length == aTabs.length + sourceService.getAllTabs(sourceBrowser).length == aTabs.length ); var newTabs = []; var treeStructure = sourceService.getTreeStructureFromTabs(aTabs); @@ -5529,7 +5529,7 @@ TreeStyleTabBrowser.prototype = { (sourceService != this || aOptions.duplicate) ); - var tabs = this.getTabsArray(targetBrowser); + var tabs = this.getTabs(targetBrowser); var lastTabIndex = tabs[tabs.length -1]._tPos; for (let i in aTabs) { @@ -6239,7 +6239,7 @@ TreeStyleTabBrowser.prototype = { return; var onlyVisible = level <= this.kRESTORE_TREE_ONLY_VISIBLE; - var tabs = this.getAllTabsArray(this.mTabBrowser); + var tabs = this.getAllTabs(this.mTabBrowser); tabs = tabs.filter(function(aTab) { return ( aTab.linkedBrowser.__SS_restoreState && diff --git a/modules/tabbarDNDObserver.js b/modules/tabbarDNDObserver.js index 128f07b0..7c7a4c6a 100644 --- a/modules/tabbarDNDObserver.js +++ b/modules/tabbarDNDObserver.js @@ -250,7 +250,7 @@ catch(e) { var d = this.document; var tab = sv.getTabFromEvent(aEvent) || sv.getTabFromTabbarEvent(aEvent) || aEvent.target; - var tabs = sv.getTabsArray(b); + var tabs = sv.getTabs(b); var firstTab = sv.getFirstNormalTab(b) || tabs[0]; var lastTabIndex = tabs.length - 1; var isInverted = sv.isVertical ? false : b.ownerDocument.defaultView.getComputedStyle(b.parentNode, null).direction == 'rtl'; @@ -433,7 +433,7 @@ catch(e) { var targetBrowser = b; - var tabs = sv.getTabsArray(targetBrowser); + var tabs = sv.getTabs(targetBrowser); var draggedWholeTree = [].concat(draggedRoots); for (let i = 0, maxi = draggedRoots.length; i < maxi; i++) @@ -597,12 +597,12 @@ catch(e) { action : sv.kACTIONS_FOR_DESTINATION | sv.kACTION_IMPORT }; var tabsInfo = this.getDraggedTabsInfoFromOneTab(actionInfo, aTab); - return tabsInfo.draggedTabs.length == (aTabs || sv.getAllTabsArray(b)).length; + return tabsInfo.draggedTabs.length == (aTabs || sv.getAllTabs(b)).length; }, isDraggingAllCurrentTabs : function TabbarDND_isDraggingAllCurrentTabs(aTab) { - return this.isDraggingAllTabs(aTab, this.treeStyleTab.getTabsArray(this.treeStyleTab.browser)); + return this.isDraggingAllTabs(aTab, this.treeStyleTab.getTabs(this.treeStyleTab.browser)); }, handleEvent : function TabbarDND_handleEvent(aEvent) diff --git a/modules/utils.js b/modules/utils.js index 36883ec9..fc2f8399 100644 --- a/modules/utils.js +++ b/modules/utils.js @@ -1428,7 +1428,7 @@ var TreeStyleTabUtils = { { var b = aTabBrowser || this.browser; var top = aFrame.top; - var tabs = this.getAllTabsArray(b); + var tabs = this.getAllTabs(b); for (let i = 0, maxi = tabs.length; i < maxi; i++) { let tab = tabs[i]; @@ -1606,11 +1606,7 @@ var TreeStyleTabUtils = { if (this.tabsHash) // XPath-less implementation return this.tabsHash[aId] || null; - return this.evaluateXPath( - 'descendant::xul:tab[@'+this.kID+' = "'+aId+'"]', - b.mTabContainer, - Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE - ).singleNodeValue; + return b.mTabContainer.querySelector('tab['+this.kID+'="'+aId+'"]'); }, isTabDuplicated : function TSTUtils_isTabDuplicated(aTab) @@ -1625,54 +1621,36 @@ var TreeStyleTabUtils = { ).booleanValue; }, - /** - * Returns all tabs in the current group as a XPathResult. - * It includes tabs hidden by Tab Panorama. - */ - getAllTabs : function TSTUtils_getTabs(aTabBrowserChild) /* OBSOLETE */ - { - var b = this.getTabBrowserFromChild(aTabBrowserChild || this.browser); - this.assertBeforeDestruction(b && b.mTabContainer); - return this.evaluateXPath( - 'descendant::xul:tab', - b.mTabContainer - ); - }, - - /** - * Returns all tabs in the current group as a XPathResult. - * It excludes tabs hidden by Tab Panorama. - */ - getTabs : function TSTUtils_getTabs(aTabBrowserChild) /* OBSOLETE */ - { - var b = this.getTabBrowserFromChild(aTabBrowserChild || this.browser); - this.assertBeforeDestruction(b && b.mTabContainer); - return this.evaluateXPath( - 'descendant::xul:tab[not(@hidden="true")]', - b.mTabContainer - ); - }, - /** * Returns all tabs in the current group as an array. * It includes tabs hidden by Tab Panorama. */ - getAllTabsArray : function TSTUtils_getAllTabsArray(aTabBrowserChild) + getAllTabs : function TSTUtils_getTabs(aTabBrowserChild) { var b = this.getTabBrowserFromChild(aTabBrowserChild || this.browser); this.assertBeforeDestruction(b && b.mTabContainer); - return Array.slice(b.mTabContainer.childNodes) ; + return Array.slice(b.mTabContainer.querySelectorAll('tab')); }, /** * Returns all tabs in the current group as an array. * It excludes tabs hidden by Tab Panorama. */ - getTabsArray : function TSTUtils_getTabsArray(aTabBrowserChild) + getTabs : function TSTUtils_getTabs(aTabBrowserChild) { var b = this.getTabBrowserFromChild(aTabBrowserChild || this.browser); this.assertBeforeDestruction(b && b.mTabContainer); - return b.visibleTabs || Array.slice(b.mTabContainer.childNodes) ; + return Array.slice(b.mTabContainer.querySelectorAll('tab:not([hidden="true"])')); + }, + + getAllTabsArray : function TSTUtils_getAllTabsArray(aTabBrowserChild) /* for backward compatibility */ + { + return this.getAllTabs(aTabBrowserChild); + }, + + getTabsArray : function TSTUtils_getTabsArray(aTabBrowserChild) /* for backward compatibility */ + { + return this.getTabs(aTabBrowserChild); }, /** @@ -1696,7 +1674,6 @@ var TreeStyleTabUtils = { return this.evaluateXPath( 'descendant::xul:tab[not(@pinned="true") and not(@hidden="true")]', b.mTabContainer, - Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE ).singleNodeValue; }, @@ -1755,7 +1732,7 @@ var TreeStyleTabUtils = { { if (!aTab) return -1; var b = this.getTabBrowserFromChild(aTab); - return this.getTabsArray(b).indexOf(aTab); + return this.getTabs(b).indexOf(aTab); }, /** @@ -1769,7 +1746,7 @@ var TreeStyleTabUtils = { if (!this.canCollapseSubtree(b)) return this.getNextTab(aTab); - var tabs = this.getVisibleTabsArray(b); + var tabs = this.getVisibleTabs(b); if (tabs.indexOf(aTab) < 0) tabs.push(aTab); tabs.sort(this.sortTabsByOrder); @@ -1788,7 +1765,7 @@ var TreeStyleTabUtils = { if (!this.canCollapseSubtree(b)) return this.getPreviousTab(aTab); - var tabs = this.getVisibleTabsArray(b); + var tabs = this.getVisibleTabs(b); if (tabs.indexOf(aTab) < 0) tabs.push(aTab); tabs.sort(this.sortTabsByOrder); @@ -1803,7 +1780,7 @@ var TreeStyleTabUtils = { { var b = this.getTabBrowserFromChild(aTabBrowserChild || this.browser); if (!b) return null; - var tabs = this.getVisibleTabsArray(b); + var tabs = this.getVisibleTabs(b); return tabs.length ? tabs[tabs.length-1] : null ; }, @@ -1815,26 +1792,12 @@ var TreeStyleTabUtils = { var b = this.getTabBrowserFromChild(aTabBrowserChild || this.browser); if (!this.canCollapseSubtree(b)) return this.getTabs(b); - - var XPathResult = this.evaluateXPath( - 'child::xul:tab[not(@'+this.kCOLLAPSED+'="true") and not(@hidden="true")]', - b.mTabContainer - ); - return XPathResult; + return Array.slice(b.mTabContainer.querySelectorAll('tab:not(['+this.kCOLLAPSED+'="true"]):not([hidden="true"])')); }, - /** - * Returns an array of not collapsed tabs in the current group. - */ - getVisibleTabsArray : function TSTUtils_getVisibleTabsArray(aTabBrowserChild) + getVisibleTabsArray : function TSTUtils_getVisibleTabsArray(aTabBrowserChild) /* for backward compatibility */ { - var b = this.getTabBrowserFromChild(aTabBrowserChild || this.browser); - var tabs = this.getTabsArray(b); - return this.canCollapseSubtree(b) ? - tabs.filter(function(aTab) { - return aTab.getAttribute(this.kCOLLAPSED) != 'true'; - }, this) : - tabs ; + return this.getVisibleTabs(aTabBrowserChild); }, /** @@ -1845,7 +1808,7 @@ var TreeStyleTabUtils = { { if (!aTab) return -1; var b = this.getTabBrowserFromChild(aTab); - return this.getVisibleTabsArray(b).indexOf(aTab); + return this.getVisibleTabs(b).indexOf(aTab); }, /** @@ -1863,7 +1826,7 @@ var TreeStyleTabUtils = { */ getNewTabsFromPreviousTabsInfo : function TSTUtils_getNewTabsFromPreviousTabsInfo(aTabBrowser, aTabsInfo) { - var tabs = this.getTabsArray(aTabBrowser); + var tabs = this.getTabs(aTabBrowser); var currentTabsInfo = this.getTabsInfo(aTabBrowser); return tabs.filter(function(aTab, aIndex) { return aTabsInfo.indexOf(currentTabsInfo[aIndex]) < 0; @@ -1871,7 +1834,7 @@ var TreeStyleTabUtils = { }, getTabsInfo : function TSTUtils_getTabsInfo(aTabBrowser) { - var tabs = this.getTabsArray(aTabBrowser); + var tabs = this.getTabs(aTabBrowser); return tabs.map(function(aTab) { return aTab.getAttribute(this.kID)+'\n'+ aTab.getAttribute('busy')+'\n'+ @@ -2129,7 +2092,7 @@ var TreeStyleTabUtils = { return ( b && this.shouldCloseTabSubtreeOf(aTab) && - this.getDescendantTabs(aTab).length + 1 == this.getAllTabsArray(b).length + this.getDescendantTabs(aTab).length + 1 == this.getAllTabs(b).length ); }, shouldCloseLastTabSubTreeOf : function() { return this.shouldCloseLastTabSubtreeOf.apply(this, arguments); }, // obsolete, for backward compatibility @@ -2622,12 +2585,12 @@ var TreeStyleTabUtils = { getTreeStructureFromTabBrowser : function TSTUtils_getTreeStructureFromTabBrowser(aTabBrowser) { - return this.getTreeStructureFromTabs(this.getAllTabsArray(aTabBrowser)); + return this.getTreeStructureFromTabs(this.getAllTabs(aTabBrowser)); }, applyTreeStructureToTabBrowser : function TSTUtils_applyTreeStructureToTabBrowser(aTabBrowser, aTreeStructure, aExpandAllTree) { - var tabs = this.getAllTabsArray(aTabBrowser); + var tabs = this.getAllTabs(aTabBrowser); return this.applyTreeStructureToTabs(tabs, aTreeStructure, aExpandAllTree); }, diff --git a/modules/window.js b/modules/window.js index e3cf1951..f75b3933 100644 --- a/modules/window.js +++ b/modules/window.js @@ -483,7 +483,7 @@ TreeStyleTabWindow.prototype = { items = items.slice(firstItemIndex); var b = this.getTabBrowserFromChild(aEvent.originalTarget) || this.browser; - var tabs = this.getTabsArray(b); + var tabs = this.getTabs(b); for (let i = 0, maxi = tabs.length; i < maxi; i++) { items[i].style.marginLeft = tabs[i].getAttribute(this.kNEST)+'em'; @@ -1548,8 +1548,7 @@ TreeStyleTabWindow.prototype = { { var keepTabs = [aTab].concat(this.getDescendantTabs(aTab)); var b = this.getTabBrowserFromChild(aTab); - var closeTabs = this.getTabsArray(b) - .filter(function(aTab) { + var closeTabs = this.getTabs(b).filter(function(aTab) { return keepTabs.indexOf(aTab) < 0 && !aTab.hasAttribute('pinned'); }); diff --git a/tests/unit/treestyletab_tabUtils.test.js b/tests/unit/treestyletab_tabUtils.test.js index 6045ce4d..382b3f61 100644 --- a/tests/unit/treestyletab_tabUtils.test.js +++ b/tests/unit/treestyletab_tabUtils.test.js @@ -120,19 +120,16 @@ function test_tabID() function test_getTabs() { var result = sv.getTabs(gBrowser); - assert.isTrue(result instanceof XPathResult); - assert.equals(4, result.snapshotLength); + assert.equals(4, result.length); var gotTabs = []; - for (var i = 0, maxi = result.snapshotLength; i < maxi; i++) + for (var i = 0, maxi = result.length; i < maxi; i++) { - gotTabs.push(result.snapshotItem(i)); + gotTabs.push(result[i]); } assert.equals(4, gotTabs.length); assert.equals(tabs, gotTabs); - assert.equals(gotTabs, sv.getTabsArray(gBrowser)); - assert.equals(tabs[0], sv.getFirstTab(gBrowser)); assert.equals(tabs[3], sv.getLastTab(gBrowser)); assert.equals(tabs[1], sv.getNextTab(tabs[0])); @@ -235,13 +232,12 @@ function test_getVisibleTabs() gBrowser.setAttribute(sv.kALLOW_COLLAPSE, true); var visibleResult = sv.getVisibleTabs(tabs[0]); - assert.implementsInterface(Ci.nsIDOMXPathResult, visibleResult); - assert.equals(2, visibleResult.snapshotLength); + assert.equals(2, visibleResult.length); var visibleTabs = []; - for (let i = 0, maxi = visibleResult.snapshotLength; i < maxi; i++) + for (let i = 0, maxi = visibleResult.length; i < maxi; i++) { - visibleTabs.push(visibleResult.snapshotItem(i)); + visibleTabs.push(visibleResult[i]); } assert.equals(2, visibleTabs.length); assert.equals([tabs[0], tabs[2]], visibleTabs); @@ -249,8 +245,7 @@ function test_getVisibleTabs() gBrowser.removeAttribute(sv.kALLOW_COLLAPSE); visibleResult = sv.getVisibleTabs(tabs[0]); - assert.implementsInterface(Ci.nsIDOMXPathResult, visibleResult); - assert.equals(4, visibleResult.snapshotLength); + assert.equals(4, visibleResult.length); } function test_getVisibleTabsArray()