mirror of
https://github.com/jlengrand/webcomponentsjs.git
synced 2026-03-10 08:51:22 +00:00
styling issues and support the filter as a function. IE requires it.
This commit is contained in:
@@ -121,11 +121,21 @@
|
||||
|
||||
var originalCreateTreeWalker = document.createTreeWalker;
|
||||
var TreeWalkerWrapper = scope.wrappers.TreeWalker;
|
||||
Document.prototype.createTreeWalker = function(root, whatToShow, filter, expandEntityReferences ) {
|
||||
Document.prototype.createTreeWalker = function(root,whatToShow,
|
||||
filter,expandEntityReferences ) {
|
||||
|
||||
var newFilter;
|
||||
if (filter && filter.acceptNode && typeof filter.acceptNode === 'function'){
|
||||
newFilter = { acceptNode:function(node) { return filter.acceptNode(wrap(node)); } }
|
||||
var newFilter = null; // IE does not like undefined.
|
||||
|
||||
// Support filter as a function or object with function defined as acceptNode.
|
||||
// IE supports filter as a function only. Chrome and FF support both formats.
|
||||
if (filter){
|
||||
if (filter.acceptNode && typeof filter.acceptNode === 'function'){
|
||||
newFilter = {
|
||||
acceptNode:function(node) { return filter.acceptNode(wrap(node)); }
|
||||
};
|
||||
}else if (typeof filter === 'function'){
|
||||
newFilter = function(node) { return filter(wrap(node)); }
|
||||
}
|
||||
}
|
||||
|
||||
return new TreeWalkerWrapper(originalCreateTreeWalker.call(unwrap(this), unwrap(root),
|
||||
|
||||
@@ -53,10 +53,12 @@
|
||||
}
|
||||
};
|
||||
|
||||
// Not all browsers support Selection.extend. Some versions of IE do not support extend, code that checks
|
||||
// if extend exists in the Selection would fail the test if we define extend on the wrapper and it does not exist in
|
||||
// Not all browsers support Selection.extend. IE does not support extend.
|
||||
// https://msdn.microsoft.com/en-us/library/ie/ms535869%28v=vs.85%29.aspx
|
||||
// Code that checks if extend exists in the Selection would
|
||||
// fail the test if we define extend on the wrapper and it does not exist in
|
||||
// the browser Selection object.
|
||||
if (window.getSelection().extend){
|
||||
if (OriginalSelection.prototype.extend) {
|
||||
Selection.prototype.extend = function(node, offset) {
|
||||
unsafeUnwrap(this).extend(unwrapIfNeeded(node), offset);
|
||||
};
|
||||
|
||||
@@ -73,13 +73,20 @@ suite('TreeWalker', function() {
|
||||
assert(isWrapper(treeWalker.nextNode()));
|
||||
assert(isWrapper(treeWalker.parentNode()));
|
||||
assert(isWrapper(treeWalker.firstChild()));
|
||||
assert(isWrapper(treeWalker.lastChild()) || isWrapper(treeWalker.lastChild())===null);
|
||||
assert(isWrapper(treeWalker.previousSibling()) || isWrapper(treeWalker.previousSibling())===null);
|
||||
assert(isWrapper(treeWalker.lastChild()) ||
|
||||
isWrapper(treeWalker.lastChild())===null);
|
||||
assert(isWrapper(treeWalker.previousSibling()) ||
|
||||
isWrapper(treeWalker.previousSibling())===null);
|
||||
assert(isWrapper(treeWalker.previousNode()));
|
||||
|
||||
});
|
||||
|
||||
test('createTreeWalker with filter', function() {
|
||||
test('createTreeWalker with filter as object with acceptNode function', function() {
|
||||
|
||||
// NodeFilter.acceptNode as filter does not work in IE.
|
||||
// https://dom.spec.whatwg.org/#nodefilter
|
||||
if (/Trident/.test(navigator.userAgent))
|
||||
return;
|
||||
|
||||
var treeWalker = document.createTreeWalker(childDiv, NodeFilter.SHOW_ELEMENT, {
|
||||
acceptNode:function(node){
|
||||
@@ -92,7 +99,26 @@ suite('TreeWalker', function() {
|
||||
});
|
||||
|
||||
assert.isNotNull(treeWalker.filter);
|
||||
assert.instanceOf(treeWalker.filter,NodeFilter);
|
||||
// in FF and IE treeWalker.filter is just a js object
|
||||
//assert.instanceOf(treeWalker.filter, NodeFilter);
|
||||
|
||||
// we should have one node only.
|
||||
assert.isNotNull(treeWalker.nextNode());
|
||||
assert.isNull(treeWalker.nextNode());
|
||||
|
||||
});
|
||||
|
||||
test('createTreeWalker with filter as a function (works under IE and others)', function() {
|
||||
|
||||
var treeWalker = document.createTreeWalker(childDiv, NodeFilter.SHOW_ELEMENT, function(node){
|
||||
assert(isWrapper(node));
|
||||
if (node.hasAttribute("title") && node.getAttribute("title")==='a'){
|
||||
return NodeFilter.FILTER_ACCEPT;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
assert.isNotNull(treeWalker.filter);
|
||||
|
||||
// we should have one node only.
|
||||
assert.isNotNull(treeWalker.nextNode());
|
||||
|
||||
Reference in New Issue
Block a user