mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-24 17:10:29 +00:00
Add the querySelectorSafe() function and replace querySelector() usage (#7380)
* Initial commit * Correct the over-estimation of my abilities * Add fallback and move code to dom.js * Use new function for tm-focus-selector * Replace other uses of querySelector* * Undo rash replacements of querySelector()
This commit is contained in:
parent
6820d45bf0
commit
e92e125697
@ -41,12 +41,8 @@ exports.startup = function() {
|
||||
$tw.rootWidget.addEventListener("tm-focus-selector",function(event) {
|
||||
var selector = event.param || "",
|
||||
element,
|
||||
doc = event.event && event.event.target ? event.event.target.ownerDocument : document;
|
||||
try {
|
||||
element = doc.querySelector(selector);
|
||||
} catch(e) {
|
||||
console.log("Error in selector: ",selector)
|
||||
}
|
||||
baseElement = event.event && event.event.target ? event.event.target.ownerDocument : document;
|
||||
element = $tw.utils.querySelectorSafe(selector,baseElement);
|
||||
if(element && element.focus) {
|
||||
element.focus(event.paramObject);
|
||||
}
|
||||
|
@ -129,7 +129,7 @@ function findTitleDomNode(widget,targetClass) {
|
||||
targetClass = targetClass || "tc-title";
|
||||
var domNode = widget.findFirstDomNode();
|
||||
if(domNode && domNode.querySelector) {
|
||||
return domNode.querySelector("." + targetClass);
|
||||
return $tw.utils.querySelectorSafe("." + targetClass,domNode);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -365,5 +365,25 @@ exports.collectDOMVariables = function(selectedNode,domNode,event) {
|
||||
return variables;
|
||||
};
|
||||
|
||||
/*
|
||||
Make sure the CSS selector is not invalid
|
||||
*/
|
||||
exports.querySelectorSafe = function(selector,baseElement) {
|
||||
baseElement = baseElement || document;
|
||||
try {
|
||||
return baseElement.querySelector(selector);
|
||||
} catch(e) {
|
||||
console.log("Invalid selector: ",selector);
|
||||
}
|
||||
};
|
||||
|
||||
exports.querySelectorAllSafe = function(selector,baseElement) {
|
||||
baseElement = baseElement || document;
|
||||
try {
|
||||
return baseElement.querySelectorAll(selector);
|
||||
} catch(e) {
|
||||
console.log("Invalid selector: ",selector);
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
||||
|
@ -127,8 +127,8 @@ PageScroller.prototype.scrollIntoView = function(element,callback,options) {
|
||||
};
|
||||
|
||||
PageScroller.prototype.scrollSelectorIntoView = function(baseElement,selector,callback,options) {
|
||||
baseElement = baseElement || document.body;
|
||||
var element = baseElement.querySelector(selector);
|
||||
baseElement = baseElement || document;
|
||||
var element = $tw.utils.querySelectorSafe(selector,baseElement);
|
||||
if(element) {
|
||||
this.scrollIntoView(element,callback,options);
|
||||
}
|
||||
|
@ -119,8 +119,8 @@ ScrollableWidget.prototype.scrollIntoView = function(element,callback,options) {
|
||||
};
|
||||
|
||||
ScrollableWidget.prototype.scrollSelectorIntoView = function(baseElement,selector,callback,options) {
|
||||
baseElement = baseElement || document.body;
|
||||
var element = baseElement.querySelector(selector);
|
||||
baseElement = baseElement || document;
|
||||
var element = $tw.utils.querySelectorSafe(selector,baseElement);
|
||||
if(element) {
|
||||
this.scrollIntoView(element,callback,options);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user