Reduce use of eval

This commit is contained in:
YUKI Hiroshi 2016-01-21 19:59:34 +09:00
parent fdc472441b
commit 508fa70336

View File

@ -1,11 +1,19 @@
Components.utils.import('resource://gre/modules/XPCOMUtils.jsm'); Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
XPCOMUtils.defineLazyModuleGetter(this, XPCOMUtils.defineLazyModuleGetter(this,
'TreeStyleTabUtils', 'resource://treestyletab-modules/utils.js'); 'TreeStyleTabUtils', 'resource://treestyletab-modules/utils.js');
XPCOMUtils.defineLazyModuleGetter(this,
'TreeStyleTabBookmarksService', 'resource://treestyletab-modules/bookmark.js');
(function() { (function() {
let { ReferenceCounter } = Components.utils.import('resource://treestyletab-modules/ReferenceCounter.js', {}); let { ReferenceCounter } = Components.utils.import('resource://treestyletab-modules/ReferenceCounter.js', {});
let { inherit } = Components.utils.import('resource://treestyletab-modules/lib/inherit.jsm', {}); let { inherit } = Components.utils.import('resource://treestyletab-modules/lib/inherit.jsm', {});
var TreeStyleTabBookmarksServiceEditable = inherit(TreeStyleTabBookmarksService, {
function mydump(aString) {
if (TreeStyleTabUtils.isDebugging('bookmark'))
dump(aString);
}
var TreeStyleTabBookmarksServiceEditable = inherit(TreeStyleTabBookmarksUIService, {
instantApply : false, instantApply : false,
canceled : false, canceled : false,
@ -44,7 +52,8 @@ var TreeStyleTabBookmarksServiceEditable = inherit(TreeStyleTabBookmarksService,
init : function TSTBMEditable_init() init : function TSTBMEditable_init()
{ {
if (this.isCreatingMultipleBookmarksInFolder) return; if (this.isCreatingMultipleBookmarksInFolder)
return;
// main browser window // main browser window
if ('StarUI' in window) { if ('StarUI' in window) {
@ -56,7 +65,7 @@ var TreeStyleTabBookmarksServiceEditable = inherit(TreeStyleTabBookmarksService,
StarUI.__treestyletab__quitEditMode = StarUI.__treestyletab__quitEditMode || StarUI.quitEditMode; StarUI.__treestyletab__quitEditMode = StarUI.__treestyletab__quitEditMode || StarUI.quitEditMode;
StarUI.quitEditMode = function(...args) { StarUI.quitEditMode = function(...args) {
TreeStyleTabBookmarksServiceEditable.saveParentFor(this._itemId); TreeStyleTabBookmarksServiceEditable.saveParentFor(gEditItemOverlay.itemId);
return this.__treestyletab__quitEditMode.apply(this, args); return this.__treestyletab__quitEditMode.apply(this, args);
}; };
@ -69,12 +78,31 @@ var TreeStyleTabBookmarksServiceEditable = inherit(TreeStyleTabBookmarksService,
// Bookmarks Property dialog // Bookmarks Property dialog
if ('BookmarkPropertiesPanel' in window) { if ('BookmarkPropertiesPanel' in window) {
TreeStyleTabUtils.doPatching(BookmarkPropertiesPanel._endBatch, 'BookmarkPropertiesPanel._endBatch', function(aName, aSource) { BookmarkPropertiesPanel.__treestyletab__onDialogAccept = BookmarkPropertiesPanel.onDialogAccept;
return eval(aName+' = '+aSource.replace( BookmarkPropertiesPanel.onDialogAccept = function(...aArgs) {
/(PlacesUtils\.transactionManager\.endBatch\([^)]*\);)/, this.__treestyletab__itemId = gEditItemOverlay.itemId;
'$1 TreeStyleTabBookmarksServiceEditable.saveParentFor(this._itemId, true);' return this.__treestyletab__onDialogAccept.apply(this, aArgs);
)); };
}, 'TreeStyleTab'); BookmarkPropertiesPanel.__treestyletab__endBatch = BookmarkPropertiesPanel._endBatch;
BookmarkPropertiesPanel._endBatch = function(...aArgs) {
var id = this.__treestyletab__itemId || gEditItemOverlay.itemId;
mydump('BookmarkPropertiesPanel._endBatch for '+id+'\n');
var batching = this._batching;
var result = this.__treestyletab__endBatch.apply(this, aArgs);
if (id >= 0 && !batching && !this._batching) {
this._batching = true;
try {
TreeStyleTabBookmarksServiceEditable.saveParentFor(id, true);
}
catch(e) {
mydump(e+'\n');
}
finally {
this._batching = false;
}
}
return result;
};
} }
// Places Organizer (Library) // Places Organizer (Library)
@ -119,42 +147,24 @@ var TreeStyleTabBookmarksServiceEditable = inherit(TreeStyleTabBookmarksService,
document.getElementById('treestyletab-parent-label').setAttribute('value', TreeStyleTabUtils.treeBundle.getString('bookmarkProperty.parent.label')); document.getElementById('treestyletab-parent-label').setAttribute('value', TreeStyleTabUtils.treeBundle.getString('bookmarkProperty.parent.label'));
this.blankItem.setAttribute('label', TreeStyleTabUtils.treeBundle.getString('bookmarkProperty.parent.blank.label')); this.blankItem.setAttribute('label', TreeStyleTabUtils.treeBundle.getString('bookmarkProperty.parent.blank.label'));
if (Services.vc.compare(Services.appinfo.platformVersion, '40') >= 0) { gEditItemOverlay.__treestyletab__initPanel = gEditItemOverlay.initPanel;
// for Firefox 40 and later, after Bug 951651 gEditItemOverlay.initPanel = function(...aArgs) {
TreeStyleTabUtils.doPatching(gEditItemOverlay.initPanel, 'gEditItemOverlay.initPanel', function(aName, aSource) { var result = this.__treestyletab__initPanel.apply(this, aArgs);
return eval(aName+' = '+aSource.replace( TreeStyleTabBookmarksServiceEditable.parentRow.collapsed = (
/(\}\)?)$/, this._element('keywordRow').collapsed &&
' TreeStyleTabBookmarksServiceEditable.parentRow.collapsed = this._element("keywordRow").collapsed && this._element("folderRow").collapsed;\n' + this._element('folderRow').collapsed
' if (!TreeStyleTabBookmarksServiceEditable.parentRow.collapsed)\n' + );
' TreeStyleTabBookmarksServiceEditable.initParentMenuList();\n' + if (!TreeStyleTabBookmarksServiceEditable.parentRow.collapsed)
'$1' TreeStyleTabBookmarksServiceEditable.initParentMenuList();
)); return result;
}, 'TreeStyleTab'); };
}
else {
// for Firefox 39 and olders
TreeStyleTabUtils.doPatching(gEditItemOverlay.initPanel, 'gEditItemOverlay.initPanel', function(aName, aSource) {
return eval(aName+' = '+aSource.replace(
'if (this._itemType == Ci.nsINavBookmarksService.TYPE_BOOKMARK) {',
'$& TreeStyleTabBookmarksServiceEditable.initParentMenuList();'
));
}, 'TreeStyleTab');
TreeStyleTabUtils.doPatching(gEditItemOverlay._showHideRows, 'gEditItemOverlay._showHideRows', function(aName, aSource) { gEditItemOverlay.__treestyletab__onItemMoved = gEditItemOverlay.onItemMoved;
return eval(aName+' = '+aSource.replace( gEditItemOverlay.onItemMoved = function(...aArgs) {
/(\}\)?)$/, if (aNewParent == this._getFolderIdFromMenuList())
' TreeStyleTabBookmarksServiceEditable.parentRow.collapsed = this._element("keywordRow").collapsed && this._element("folderRow").collapsed;\n' + TreeStyleTabBookmarksServiceEditable.initParentMenuList();
'$1' return this.__treestyletab__onItemMoved.apply(this, aArgs);
)); };
}, 'TreeStyleTab');
}
TreeStyleTabUtils.doPatching(gEditItemOverlay.onItemMoved, 'gEditItemOverlay.onItemMoved', function(aName, aSource) {
return eval(aName+' = '+aSource.replace(
'{',
'$& if (aNewParent == this._getFolderIdFromMenuList()) TreeStyleTabBookmarksServiceEditable.initParentMenuList();'
));
}, 'TreeStyleTab');
this.editUIInitialized = true; this.editUIInitialized = true;
}, },
@ -255,7 +265,7 @@ var TreeStyleTabBookmarksServiceEditable = inherit(TreeStyleTabBookmarksService,
}, },
_createSiblingsFragmentInternal : function TSTBMEditable_createSiblingsFragmentInternal(aCurrentItem, aItems, aCallback) _createSiblingsFragmentInternal : function TSTBMEditable_createSiblingsFragmentInternal(aCurrentItem, aItems, aCallback)
{ {
var treeStructure = this.getTreeStructureFromItems(aItems); var treeStructure = TreeStyleTabBookmarksService.getTreeStructureFromItems(aItems);
var currentIndex = aItems.indexOf(aCurrentItem); var currentIndex = aItems.indexOf(aCurrentItem);
var selected = treeStructure[currentIndex]; var selected = treeStructure[currentIndex];
@ -327,21 +337,19 @@ var TreeStyleTabBookmarksServiceEditable = inherit(TreeStyleTabBookmarksService,
}, },
_getSiblingItemsIterator : function TSTBMEditable_getSiblingItemsIterator(aId) _getSiblingItemsIterator : function TSTBMEditable_getSiblingItemsIterator(aId)
{ {
var folderId = -1;
try { try {
var folderId = PlacesUtils.bookmarks.getFolderIdForItem(aId); folderId = PlacesUtils.bookmarks.getFolderIdForItem(aId);
return this._getItemsInFolderIterator(folderId);
} }
catch(e) { catch(e) {
dump('TSTBMEditable_getSiblingItemsIterator('+aId+') failed.\n');
dump(e+'\n');
dump(new Error().stack+'\n');
} }
return this._getItemsInFolderIterator(folderId);
}, },
saveParentFor : function TSTBMEditable_saveParentFor(aId, aJustNow) saveParentFor : function TSTBMEditable_saveParentFor(aId, aJustNow)
{ {
var newParentId = parseInt(this.menulist.value || -1); var newParentId = parseInt(this.menulist.value || -1);
if (this.canceled || newParentId == this.getParentItem(aId)) if (this.canceled || newParentId == TreeStyleTabBookmarksService.getParentItem(aId))
return; return;
var itemsIterator = this._getSiblingItemsIterator(aId); var itemsIterator = this._getSiblingItemsIterator(aId);
@ -359,7 +367,7 @@ var TreeStyleTabBookmarksServiceEditable = inherit(TreeStyleTabBookmarksService,
}, },
_saveParentForInternal : function TSTBMEditable_saveParentForInternal(aId, aNewParentId, aItems) _saveParentForInternal : function TSTBMEditable_saveParentForInternal(aId, aNewParentId, aItems)
{ {
var treeStructure = this.getTreeStructureFromItems(aItems); var treeStructure = TreeStyleTabBookmarksService.getTreeStructureFromItems(aItems);
var parentIndex = aItems.indexOf(aNewParentId); var parentIndex = aItems.indexOf(aNewParentId);
var newIndex; var newIndex;