-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from dvoytenko/helpers2
DOM and other helper additions needed for Lightbox
- Loading branch information
Showing
6 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* Copyright 2015 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
/** | ||
* Removes all child nodes of the specified element. | ||
* @param {!Element} parent | ||
*/ | ||
export function removeChildren(parent) { | ||
while (parent.firstChild) { | ||
parent.removeChild(parent.firstChild); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Copies all children nodes of element "from" to element "to". Child nodes | ||
* are deeply cloned. Notice, that this method should be used with care and | ||
* preferrably on smaller subtrees. | ||
* @param {!Element} from | ||
* @param {!Element} to | ||
*/ | ||
export function copyChildren(from, to) { | ||
let frag = to.ownerDocument.createDocumentFragment(); | ||
for (let n = from.firstChild; n; n = n.nextSibling) { | ||
frag.appendChild(n.cloneNode(true)); | ||
} | ||
to.appendChild(frag); | ||
} | ||
|
||
|
||
/** | ||
* Finds the closest element that satisfies the callback from this element | ||
* up the DOM subtree. | ||
* @param {!Element} element | ||
* @param {function(!Element):boolean} callback | ||
* @return {?Element} | ||
*/ | ||
export function closest(element, callback) { | ||
for (let el = element; el; el = el.parentElement) { | ||
if (callback(el)) { | ||
return el; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
|
||
/** | ||
* Finds the closest element with the specified name from this element | ||
* up the DOM subtree. | ||
* @param {!Element} element | ||
* @param {string} tagName | ||
* @return {?Element} | ||
*/ | ||
export function closestByTag(element, tagName) { | ||
tagName = tagName.toUpperCase(); | ||
return closest(element, (el) => { | ||
return el.tagName == tagName; | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* Finds the first descendant element with the specified name. | ||
* @param {!Element} element | ||
* @param {string} tagName | ||
* @return {?Element} | ||
*/ | ||
export function elementByTag(element, tagName) { | ||
let elements = element.getElementsByTagName(tagName); | ||
return elements.length > 0 ? elements[0] : null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* Copyright 2015 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as dom from '../../src/dom'; | ||
|
||
|
||
describe('DOM', () => { | ||
|
||
it('should remove all children', () => { | ||
let element = document.createElement('div'); | ||
element.appendChild(document.createElement('div')); | ||
element.appendChild(document.createTextNode('ABC')); | ||
expect(element.children.length).to.equal(1); | ||
expect(element.firstChild).to.not.equal(null); | ||
expect(element.textContent).to.equal('ABC'); | ||
|
||
dom.removeChildren(element); | ||
expect(element.children.length).to.equal(0); | ||
expect(element.firstChild).to.equal(null); | ||
expect(element.textContent).to.equal(''); | ||
}); | ||
|
||
it('should copy all children', () => { | ||
let element = document.createElement('div'); | ||
element.appendChild(document.createElement('div')); | ||
element.appendChild(document.createTextNode('ABC')); | ||
|
||
let other = document.createElement('div'); | ||
dom.copyChildren(element, other); | ||
|
||
expect(element.children.length).to.equal(1); | ||
expect(element.firstChild).to.not.equal(null); | ||
expect(element.textContent).to.equal('ABC'); | ||
|
||
expect(other.children.length).to.equal(1); | ||
expect(other.firstChild).to.not.equal(null); | ||
expect(other.firstChild.tagName).to.equal('DIV'); | ||
expect(other.textContent).to.equal('ABC'); | ||
}); | ||
|
||
it('closest should find itself', () => { | ||
let element = document.createElement('div'); | ||
|
||
let child = document.createElement('div'); | ||
element.appendChild(child); | ||
|
||
expect(dom.closest(child, () => true)).to.equal(child); | ||
expect(dom.closestByTag(child, 'div')).to.equal(child); | ||
expect(dom.closestByTag(child, 'DIV')).to.equal(child); | ||
}); | ||
|
||
it('closest should find first match', () => { | ||
let parent = document.createElement('parent'); | ||
|
||
let element = document.createElement('element'); | ||
parent.appendChild(element); | ||
|
||
let child = document.createElement('child'); | ||
element.appendChild(child); | ||
|
||
expect(dom.closest(child, (e) => e.tagName == 'CHILD')).to.equal(child); | ||
expect(dom.closestByTag(child, 'child')).to.equal(child); | ||
|
||
expect(dom.closest(child, (e) => e.tagName == 'ELEMENT')).to.equal(element); | ||
expect(dom.closestByTag(child, 'element')).to.equal(element); | ||
|
||
expect(dom.closest(child, (e) => e.tagName == 'PARENT')).to.equal(parent); | ||
expect(dom.closestByTag(child, 'parent')).to.equal(parent); | ||
}); | ||
|
||
it('closest should find first match', () => { | ||
let parent = document.createElement('parent'); | ||
|
||
let element1 = document.createElement('element'); | ||
parent.appendChild(element1); | ||
|
||
let element2 = document.createElement('element'); | ||
parent.appendChild(element2); | ||
|
||
expect(dom.elementByTag(parent, 'element')).to.equal(element1); | ||
expect(dom.elementByTag(parent, 'ELEMENT')).to.equal(element1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters