forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Implements variable substitution macro for html element opacity. (a…
…mpproject#16479) * Create variable substitution macro that returns the opacity value of an element. * fix typo * Add Unit Tests * Update unit tests * rename variable to prevent linter errors * move opacity to a new file * removed opacity code from src * remove dead code * remove dead code * Added license to opacity * Added license to opacity test * Moved public function to top of file * Change getOpacity to getMinOpacity * Change getRootOpacity to getRootMinOpacity * Sort imports alphabetically * change parents to ancestors in comment
- Loading branch information
1 parent
9ffa7a1
commit 3455ad2
Showing
4 changed files
with
224 additions
and
0 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,112 @@ | ||
/** | ||
* Copyright 2018 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 {computedStyle} from '../../../src/style'; | ||
|
||
|
||
/** | ||
* Returns the min opacity found amongst the element and its ancestors | ||
* @param {!Element|null} el | ||
* @return {number} minimum opacity value | ||
*/ | ||
export function getMinOpacity(el) { | ||
const parentNodeTree = getElementNodeTree(el.parentElement); | ||
parentNodeTree.push(el); | ||
let minOpacityFound = 1; | ||
let opacity; | ||
|
||
for (let i = 0; i < parentNodeTree.length; i++) { | ||
const node = parentNodeTree[i]; | ||
opacity = getElementOpacity(node); | ||
|
||
if (opacity < minOpacityFound) { minOpacityFound = opacity; } | ||
|
||
if (minOpacityFound === 0) { return minOpacityFound; } | ||
|
||
} | ||
|
||
return minOpacityFound; | ||
|
||
} | ||
|
||
/** | ||
* Returns the Opacity value of the element. | ||
* @param {!Element} el | ||
* @return {number} | ||
*/ | ||
function getElementOpacity(el) { | ||
const win = window; | ||
const fullyVisibleValue = 1; | ||
const fullyHiddenValue = 0; | ||
|
||
if (!el) { return fullyVisibleValue; } | ||
const {visibility, opacity} = computedStyle(win, el); | ||
|
||
if (visibility === 'hidden') { | ||
return fullyHiddenValue; | ||
|
||
} | ||
const opacityValue = (opacity === '') | ||
? fullyVisibleValue | ||
: parseFloat(opacity); | ||
|
||
if (isNaN(opacityValue)) { return fullyVisibleValue; } | ||
|
||
return opacityValue; | ||
|
||
} | ||
|
||
/** | ||
* Returns the node tree of the current element starting from | ||
* the document root | ||
* @param {!Element|null} el | ||
* @return {Array} node list of the element's node tree | ||
*/ | ||
function getElementNodeTree(el) { | ||
const nodeList = []; | ||
if (!el) { return nodeList; } | ||
|
||
const CAP = 50; | ||
const DOCUMENT_NODE_TYPE = 9; | ||
const ELEMENT_WITH_PARENT_TYPE = 1; | ||
let parent; | ||
let element = el; | ||
nodeList.push(element); | ||
|
||
for (let i = 0; i < CAP; i++) { | ||
|
||
parent = element.parentNode || element.parentElement; | ||
|
||
if (parent && parent.nodeType == ELEMENT_WITH_PARENT_TYPE) { | ||
element = parent; | ||
nodeList.push(element); | ||
|
||
} else if (parent && parent.nodeType == DOCUMENT_NODE_TYPE) { | ||
parent = element.ownerDocument.defaultView.frameElement; | ||
|
||
if (parent && parent.nodeType == ELEMENT_WITH_PARENT_TYPE) { | ||
element = parent; | ||
nodeList.push(element); | ||
|
||
} else { break; } | ||
|
||
} else { break; } | ||
|
||
} | ||
|
||
return nodeList; | ||
|
||
} |
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,81 @@ | ||
/** | ||
* Copyright 2018 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 {getMinOpacity} from '../opacity'; | ||
|
||
describes.realWin('getMinOpacity', {amp: true}, env => { | ||
let win; | ||
let ampElement; | ||
let doc; | ||
let parent; | ||
let style; | ||
|
||
beforeEach(() => { | ||
win = env.win; | ||
doc = win.document; | ||
style = doc.createElement('style'); | ||
style.setAttribute('amp-custom',''); | ||
style.innerHTML = ` | ||
#img { | ||
opacity: 0.5; | ||
} | ||
#parent { | ||
height: 100px; | ||
width: 100px; | ||
}`; | ||
doc.head.appendChild(style); | ||
|
||
ampElement = doc.createElement('amp-img'); | ||
ampElement.id = 'img'; | ||
ampElement.setAttribute('width', '100'); | ||
ampElement.setAttribute('height', '100'); | ||
|
||
parent = doc.createElement('div'); | ||
parent.id = 'parent'; | ||
|
||
parent.appendChild(ampElement); | ||
doc.body.appendChild(parent); | ||
|
||
expect(getMinOpacity(ampElement)).to.equal(0.5); | ||
}); | ||
|
||
it('amp element opacity value change', () => { | ||
ampElement.style.opacity = 1; | ||
expect(getMinOpacity(ampElement)).to.equal(1); | ||
|
||
ampElement.style.opacity = 0.5; | ||
expect(getMinOpacity(ampElement)).to.equal(0.5); | ||
|
||
ampElement.style.opacity = 0; | ||
expect(getMinOpacity(ampElement)).to.equal(0); | ||
|
||
ampElement.style.opacity = 1; | ||
ampElement.style.visibility = 'hidden'; | ||
expect(getMinOpacity(ampElement)).to.equal(0); | ||
}); | ||
|
||
it('amp element\'s parent opacity value lower than amp element', () => { | ||
parent.style.opacity = 0; | ||
expect(getMinOpacity(ampElement)).to.equal(0); | ||
|
||
parent.style.opacity = 1; | ||
// since ampElement is 0.5 | ||
expect(getMinOpacity(ampElement)).to.equal(0.5); | ||
|
||
parent.style.visibility = 'hidden'; | ||
expect(getMinOpacity(ampElement)).to.equal(0); | ||
}); | ||
}); |
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