mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-12 12:59:57 +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) {
|
$tw.rootWidget.addEventListener("tm-focus-selector",function(event) {
|
||||||
var selector = event.param || "",
|
var selector = event.param || "",
|
||||||
element,
|
element,
|
||||||
doc = event.event && event.event.target ? event.event.target.ownerDocument : document;
|
baseElement = event.event && event.event.target ? event.event.target.ownerDocument : document;
|
||||||
try {
|
element = $tw.utils.querySelectorSafe(selector,baseElement);
|
||||||
element = doc.querySelector(selector);
|
|
||||||
} catch(e) {
|
|
||||||
console.log("Error in selector: ",selector)
|
|
||||||
}
|
|
||||||
if(element && element.focus) {
|
if(element && element.focus) {
|
||||||
element.focus(event.paramObject);
|
element.focus(event.paramObject);
|
||||||
}
|
}
|
||||||
|
@ -129,7 +129,7 @@ function findTitleDomNode(widget,targetClass) {
|
|||||||
targetClass = targetClass || "tc-title";
|
targetClass = targetClass || "tc-title";
|
||||||
var domNode = widget.findFirstDomNode();
|
var domNode = widget.findFirstDomNode();
|
||||||
if(domNode && domNode.querySelector) {
|
if(domNode && domNode.querySelector) {
|
||||||
return domNode.querySelector("." + targetClass);
|
return $tw.utils.querySelectorSafe("." + targetClass,domNode);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -365,5 +365,25 @@ exports.collectDOMVariables = function(selectedNode,domNode,event) {
|
|||||||
return variables;
|
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) {
|
PageScroller.prototype.scrollSelectorIntoView = function(baseElement,selector,callback,options) {
|
||||||
baseElement = baseElement || document.body;
|
baseElement = baseElement || document;
|
||||||
var element = baseElement.querySelector(selector);
|
var element = $tw.utils.querySelectorSafe(selector,baseElement);
|
||||||
if(element) {
|
if(element) {
|
||||||
this.scrollIntoView(element,callback,options);
|
this.scrollIntoView(element,callback,options);
|
||||||
}
|
}
|
||||||
|
@ -119,8 +119,8 @@ ScrollableWidget.prototype.scrollIntoView = function(element,callback,options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ScrollableWidget.prototype.scrollSelectorIntoView = function(baseElement,selector,callback,options) {
|
ScrollableWidget.prototype.scrollSelectorIntoView = function(baseElement,selector,callback,options) {
|
||||||
baseElement = baseElement || document.body;
|
baseElement = baseElement || document;
|
||||||
var element = baseElement.querySelector(selector);
|
var element = $tw.utils.querySelectorSafe(selector,baseElement);
|
||||||
if(element) {
|
if(element) {
|
||||||
this.scrollIntoView(element,callback,options);
|
this.scrollIntoView(element,callback,options);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user