<commit_before>moduleAid.VERSION = '1.1.9';

this.SHORT_DELAY = 25;
this.LONG_DELAY = 1500;

this.__defineGetter__('documentHighlighted', function() {
	return (contentDocument && contentDocument.documentElement && contentDocument.documentElement.getAttribute('highlighted') == 'true');
});
this.__defineSetter__('documentHighlighted', function(v) {
	if(contentDocument) { toggleAttribute(contentDocument.documentElement, 'highlighted', v); }
});
this.__defineGetter__('documentReHighlight', function() {
	return (contentDocument && contentDocument.documentElement && contentDocument.documentElement.getAttribute('reHighlight') == 'true');
});
this.__defineSetter__('documentReHighlight', function(v) {
	if(contentDocument) { toggleAttribute(contentDocument.documentElement, 'reHighlight', v); }
});

this.emptyNoFindUpdating = function(e) {
	if(e.detail.res == gFindBar.nsITypeAheadFind.FIND_NOTFOUND && !gFindBar._findField.value) {
		e.preventDefault();
		e.stopPropagation();
		gFindBar._updateStatusUI(gFindBar.nsITypeAheadFind.FIND_FOUND);
	}
};

this.keepStatusUI = function(e) {
	linkedPanel._statusUI = e.detail.res;
};

this.escHighlights = function(e) {
	if(e.keyCode == e.DOM_VK_ESCAPE) {
		gFindBar.toggleHighlight(false);
	}
};

this.highlightOnClose = function() {
	// Cancel a delayed highlight when closing the find bar
	timerAid.cancel('delayHighlight');
	
	// To remove the grid and the esc key listener if there are no highlights
	if(documentHighlighted && (!gFindBar._findField.value || linkedPanel._notFoundHighlights)) {
		gFindBar.toggleHighlight(false);
	}
};

this.highlightsOnToggling = function(e) {
	var aHighlight = e.detail;
	// Remove highlights when hitting Esc
	if(aHighlight) { // this is aHighlight
		listenerAid.add(contentDocument, 'keyup', escHighlights);
	} else {
		listenerAid.remove(contentDocument, 'keyup', escHighlights);
	}
	
	documentHighlighted = aHighlight;
	documentReHighlight = false;
	
	// This is only used by gFindBar.close(), to remove the grid and the esc event if they're not needed
	linkedPanel._notFoundHighlights = false;
	
	// Make sure we cancel any highlight timer that might be running
	timerAid.cancel('delayHighlight');
};

// Handler for when switching tabs
this.highlightsTabSelected = function() {
	if(linkedPanel._findWord && (documentHighlighted || documentReHighlight)) {
		gFindBar._findField.value = linkedPanel._findWord;
		gFindBar._enableFindButtons(gFindBar._findField.value);
		if(linkedPanel._statusUI != undefined) {
			gFindBar._updateStatusUI(linkedPanel._statusUI);
		}
		if(findBarHidden) {
			documentReHighlight = false;
		}
	}
	
	if(documentReHighlight) {
		gFindBar.getElement("highlight").checked = documentHighlighted;
		reHighlight(documentHighlighted);
	} else if(linkedPanel._statusUI != undefined && (!linkedPanel._findWord || (!documentHighlighted && !documentReHighlight)) ) {
		gFindBar._updateStatusUI(linkedPanel._statusUI);
	}
};

// Commands a reHighlight if needed on any tab, triggered from frames as well
// Mainly for back/forward actions
this.highlightsContentLoaded = function(e) {
	// this is the content document of the loaded page.
	var doc = e.originalTarget;
	if(doc instanceof window.HTMLDocument) {
		// is this an inner frame?
		// Find the root document:
		while(doc.defaultView.frameElement) {
			doc = doc.defaultView.frameElement.ownerDocument;
		}
		
		if(doc == contentDocument) {
			reHighlight(documentHighlighted);
		} else {
			setAttribute(doc.documentElement, 'reHighlight', 'true');
		}
	}
};

// Tab progress listeners, handles opening and closing of pages and location changes
this.highlightsProgressListener = {
	// Commands a reHighlight if needed, triggered from history navigation as well
	onLocationChange: function(browser, webProgress, request, location) {
		// Frames don't need to trigger this
		if(webProgress.DOMWindow == browser.contentWindow) {
			// No need to call if there is nothing to find
			if(browser == gBrowser.mCurrentBrowser) {
				if(request && !request.isPending()) {
					reHighlight(documentHighlighted);
				}
			}
			else if(browser.contentDocument) {
				setAttribute(browser.contentDocument.documentElement, 'reHighlight', 'true');
			}
		}
	}
};

// ReDo highlights when hitting FindAgain if necessary (should rarely be triggered actually)
this.highlightsFindAgain = function() {
	if(documentReHighlight) {
		reHighlight(documentHighlighted);
	}
};

// This always calls toggleHighlight() at least once with a false argument, then with a true argument if reDo is true.
// This way we ensure the old highlights are removed before adding new ones.
this.reHighlight = function(reDo) {
	gFindBar.toggleHighlight(false);
	if(reDo && dispatch(gFindBar, { type: 'WillReHighlight' })) {
		gFindBar.toggleHighlight(true);
	}
};

// Add the reHighlight attribute to all tabs
this.reHighlightAll = function() {
	// Timer prevents unnecessary multiple rehighlights
	timerAid.init('reHighlightAll', function() {
		// This happens sometimes when opening new windows, I can't find out how this is getting called before viewSource is defined but it makes no functional difference
		if(typeof(viewSource) == 'undefined') { return; }
		
		if(!viewSource) {
			for(var i=0; i<gBrowser.tabContainer.childNodes.length; i++) {
				setAttribute(gBrowser.tabContainer.childNodes[i], 'reHighlight', 'true');
			}
		}
		
		reHighlight(documentHighlighted);
	}, 0);
};

this.toggleHighlightByDefault = function() {
	moduleAid.loadIf('highlightByDefault', prefAid.highlightByDefault);
};

this.toggleHideOnClose = function() {
	moduleAid.loadIf('hideOnClose', prefAid.hideWhenFinderHidden);
};

moduleAid.LOADMODULE = function() {
	this.backups = {
		_setHighlightTimeout: gFindBar._setHighlightTimeout
	};
	
	gFindBar._setHighlightTimeout = function() {
		// We want this to be updated regardless of what happens<commit_msg>bugfix sometimes not updating on changed settings<commit_after>moduleAid.VERSION = '1.1.10';

this.SHORT_DELAY = 25;
this.LONG_DELAY = 1500;

this.__defineGetter__('documentHighlighted', function() {
	return (contentDocument && contentDocument.documentElement && contentDocument.documentElement.getAttribute('highlighted') == 'true');
});
this.__defineSetter__('documentHighlighted', function(v) {
	if(contentDocument) { toggleAttribute(contentDocument.documentElement, 'highlighted', v); }
});
this.__defineGetter__('documentReHighlight', function() {
	return (contentDocument && contentDocument.documentElement && contentDocument.documentElement.getAttribute('reHighlight') == 'true');
});
this.__defineSetter__('documentReHighlight', function(v) {
	if(contentDocument) { toggleAttribute(contentDocument.documentElement, 'reHighlight', v); }
});

this.emptyNoFindUpdating = function(e) {
	if(e.detail.res == gFindBar.nsITypeAheadFind.FIND_NOTFOUND && !gFindBar._findField.value) {
		e.preventDefault();
		e.stopPropagation();
		gFindBar._updateStatusUI(gFindBar.nsITypeAheadFind.FIND_FOUND);
	}
};

this.keepStatusUI = function(e) {
	linkedPanel._statusUI = e.detail.res;
};

this.escHighlights = function(e) {
	if(e.keyCode == e.DOM_VK_ESCAPE) {
		gFindBar.toggleHighlight(false);
	}
};

this.highlightOnClose = function() {
	// Cancel a delayed highlight when closing the find bar
	timerAid.cancel('delayHighlight');
	
	// To remove the grid and the esc key listener if there are no highlights
	if(documentHighlighted && (!gFindBar._findField.value || linkedPanel._notFoundHighlights)) {
		gFindBar.toggleHighlight(false);
	}
};

this.highlightsOnToggling = function(e) {
	var aHighlight = e.detail;
	// Remove highlights when hitting Esc
	if(aHighlight) { // this is aHighlight
		listenerAid.add(contentDocument, 'keyup', escHighlights);
	} else {
		listenerAid.remove(contentDocument, 'keyup', escHighlights);
	}
	
	documentHighlighted = aHighlight;
	documentReHighlight = false;
	
	// This is only used by gFindBar.close(), to remove the grid and the esc event if they're not needed
	linkedPanel._notFoundHighlights = false;
	
	// Make sure we cancel any highlight timer that might be running
	timerAid.cancel('delayHighlight');
};

// Handler for when switching tabs
this.highlightsTabSelected = function() {
	if(linkedPanel._findWord && (documentHighlighted || documentReHighlight)) {
		gFindBar._findField.value = linkedPanel._findWord;
		gFindBar._enableFindButtons(gFindBar._findField.value);
		if(linkedPanel._statusUI != undefined) {
			gFindBar._updateStatusUI(linkedPanel._statusUI);
		}
		if(findBarHidden) {
			documentReHighlight = false;
		}
	}
	
	if(documentReHighlight) {
		gFindBar.getElement("highlight").checked = documentHighlighted;
		reHighlight(documentHighlighted);
	} else if(linkedPanel._statusUI != undefined && (!linkedPanel._findWord || (!documentHighlighted && !documentReHighlight)) ) {
		gFindBar._updateStatusUI(linkedPanel._statusUI);
	}
};

// Commands a reHighlight if needed on any tab, triggered from frames as well
// Mainly for back/forward actions
this.highlightsContentLoaded = function(e) {
	// this is the content document of the loaded page.
	var doc = e.originalTarget;
	if(doc instanceof window.HTMLDocument) {
		// is this an inner frame?
		// Find the root document:
		while(doc.defaultView.frameElement) {
			doc = doc.defaultView.frameElement.ownerDocument;
		}
		
		if(doc == contentDocument) {
			reHighlight(documentHighlighted);
		} else {
			setAttribute(doc.documentElement, 'reHighlight', 'true');
		}
	}
};

// Tab progress listeners, handles opening and closing of pages and location changes
this.highlightsProgressListener = {
	// Commands a reHighlight if needed, triggered from history navigation as well
	onLocationChange: function(browser, webProgress, request, location) {
		// Frames don't need to trigger this
		if(webProgress.DOMWindow == browser.contentWindow) {
			// No need to call if there is nothing to find
			if(browser == gBrowser.mCurrentBrowser) {
				if(request && !request.isPending()) {
					reHighlight(documentHighlighted);
				}
			}
			else if(browser.contentDocument) {
				setAttribute(browser.contentDocument.documentElement, 'reHighlight', 'true');
			}
		}
	}
};

// ReDo highlights when hitting FindAgain if necessary (should rarely be triggered actually)
this.highlightsFindAgain = function() {
	if(documentReHighlight) {
		reHighlight(documentHighlighted);
	}
};

// This always calls toggleHighlight() at least once with a false argument, then with a true argument if reDo is true.
// This way we ensure the old highlights are removed before adding new ones.
this.reHighlight = function(reDo) {
	gFindBar.toggleHighlight(false);
	if(reDo && dispatch(gFindBar, { type: 'WillReHighlight' })) {
		gFindBar.toggleHighlight(true);
	}
};

// Add the reHighlight attribute to all tabs
this.reHighlightAll = function() {
	// Timer prevents unnecessary multiple rehighlights
	timerAid.init('reHighlightAll', function() {
		// This happens sometimes when opening new windows, I can't find out how this is getting called before viewSource is defined but it makes no functional difference
		if(typeof(viewSource) == 'undefined') { return; }
		
		if(!viewSource) {
			for(var i=0; i<gBrowser.tabContainer.childNodes.length; i++) {
				setAttribute(gBrowser.tabContainer.childNodes[i].linkedBrowser.contentDocument.documentElement, 'reHighlight', 'true');
			}
		}
		
		reHighlight(documentHighlighted);
	}, 0);
};

this.toggleHighlightByDefault = function() {
	moduleAid.loadIf('highlightByDefault', prefAid.highlightByDefault);
};

this.toggleHideOnClose = function() {
	moduleAid.loadIf('hideOnClose', prefAid.hideWhenFinderHidden);
};

moduleAid.LOADMODULE = function() {
	this.backups = {
		_setHighlightTimeout: gFindBar._setHighlightTimeout
	};
	
	gFindBar._setHighlightTimeout = function() {
		// We want this to be updated regardless of what happens
{'fraction_non_alphanumeric': 0.11491235498348717, 'fraction_numerical': 0.0019476670336184266, 'mean_word_length': 5.170323928944619, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1532879', 'n_tokens_mistral': 3772, 'n_tokens_neox': 3452, 'n_words': 831}