Skip to content

Commit

Permalink
Merge pull request #814 from yabwe/is-descendant-fixes
Browse files Browse the repository at this point in the history
Fix some breaks in isDescendant that were introduced by #792
  • Loading branch information
j0k3r committed Sep 16, 2015
2 parents 60cd879 + 9e6ab5a commit b72369c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
37 changes: 37 additions & 0 deletions spec/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,41 @@ describe('MediumEditor.util', function () {
expect(document.body.contains(el)).toBe(true, 'The editor element has been removed from the page');
});
});

describe('isDescendant', function () {
it('should return true for an element which is a descendant of another', function () {
var parent = this.createElement('div'),
child = parent.appendChild(document.createTextNode('text'));
expect(MediumEditor.util.isDescendant(parent, child)).toBe(true);
});

it('should return false for an element which is not a descendant of another', function () {
var parent = this.createElement('div'),
child = document.createTextNode('text');
expect(MediumEditor.util.isDescendant(parent, child)).toBe(false);
});

it('should return false when checking the same element', function () {
var parent = this.createElement('div');
expect(MediumEditor.util.isDescendant(parent, parent)).toBe(false);
});

it('should return true when checking the same element but using the equality param', function () {
var parent = this.createElement('div');
expect(MediumEditor.util.isDescendant(parent, parent, true)).toBe(true);
});

it('should return false when the elements are null', function () {
var parent = this.createElement('div');
expect(MediumEditor.util.isDescendant(parent, null)).toBe(false);
expect(MediumEditor.util.isDescendant(null, parent)).toBe(false);
expect(MediumEditor.util.isDescendant(null, null)).toBe(false);
});

it('should return false when the parent element is a text node', function () {
var parent = document.createTextNode('text'),
child = this.createElement('div');
expect(MediumEditor.util.isDescendant(parent, child)).toBe(false);
});
});
});
8 changes: 6 additions & 2 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,12 @@
if (!parent || !child) {
return false;
}
if (checkEquality && parent === child) {
return true;
if (parent === child) {
return !!checkEquality;
}
// If parent is not an element, it can't have any descendants
if (parent.nodeType !== 1) {
return false;
}
if (nodeContainsWorksWithTextNodes || child.nodeType !== 3) {
return parent.contains(child);
Expand Down

0 comments on commit b72369c

Please sign in to comment.