-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ Implements variable substitution macro for html element opacity. #16479
Merged
Merged
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f074a91
Create variable substitution macro that returns the opacity value of …
joel-regus f03b509
fix typo
joel-regus a0b0b84
Add Unit Tests
joel-regus f96dc48
Update unit tests
joel-regus 1ed982a
rename variable to prevent linter errors
joel-regus 0d08d84
Merge branch 'master' of github.com:ampproject/amphtml into moat_opac…
joel-regus b2139a7
Merge branch 'master' of github.com:ampproject/amphtml into moat_opac…
joel-regus e5ccc52
move opacity to a new file
joel-regus b8fba70
removed opacity code from src
joel-regus 2066b65
remove dead code
joel-regus 8f4656e
remove dead code
joel-regus 4ab2a53
Added license to opacity
joel-regus 3eccb56
Added license to opacity test
joel-regus bc7a0e8
Merge branch 'master' of github.com:ampproject/amphtml into moat_opac…
joel-regus 13afe15
Moved public function to top of file
joel-regus 64995e2
Change getOpacity to getMinOpacity
joel-regus e27b52b
Change getRootOpacity to getRootMinOpacity
joel-regus c4ea3e0
Merge branch 'master' of github.com:ampproject/amphtml into moat_opac…
joel-regus 1546ab4
Sort imports alphabetically
joel-regus 67b2fdc
change parents to ancestors in comment
joel-regus 0e6da80
Merge branch 'master' of github.com:ampproject/amphtml into moat_opac…
joel-regus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,113 @@ | ||
/** | ||
* 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 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; | ||
|
||
} | ||
|
||
/** | ||
* Returns the min opacity found amongst the element and its parents | ||
* @param {!Element|null} el | ||
* @return {number} minimum opacity value | ||
*/ | ||
export function getOpacity(el) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we multiply all the opacity values together? |
||
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; | ||
|
||
} | ||
|
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 {getOpacity} from '../opacity'; | ||
|
||
describes.realWin('getOpacity', {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(getOpacity(ampElement)).to.equal(0.5); | ||
}); | ||
|
||
it('amp element opacity value change', () => { | ||
ampElement.style.opacity = 1; | ||
expect(getOpacity(ampElement)).to.equal(1); | ||
|
||
ampElement.style.opacity = 0.5; | ||
expect(getOpacity(ampElement)).to.equal(0.5); | ||
|
||
ampElement.style.opacity = 0; | ||
expect(getOpacity(ampElement)).to.equal(0); | ||
|
||
ampElement.style.opacity = 1; | ||
ampElement.style.visibility = 'hidden'; | ||
expect(getOpacity(ampElement)).to.equal(0); | ||
}); | ||
|
||
it('amp element\'s parent opacity value lower than amp element', () => { | ||
parent.style.opacity = 0; | ||
expect(getOpacity(ampElement)).to.equal(0); | ||
|
||
parent.style.opacity = 1; | ||
// since ampElement is 0.5 | ||
expect(getOpacity(ampElement)).to.equal(0.5); | ||
|
||
parent.style.visibility = 'hidden'; | ||
expect(getOpacity(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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: pls put public functions at the top