-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: (multi-domain) add support for screenshot blackout (#20150)
- Loading branch information
Showing
11 changed files
with
229 additions
and
192 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
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,21 @@ | ||
import $ from 'jquery' | ||
|
||
function addCssAnimationDisabler ($body) { | ||
$(` | ||
<style id="__cypress-animation-disabler"> | ||
*, *:before, *:after { | ||
transition-property: none !important; | ||
animation: none !important; | ||
} | ||
</style> | ||
`).appendTo($body) | ||
} | ||
|
||
function removeCssAnimationDisabler ($body) { | ||
$body.find('#__cypress-animation-disabler').remove() | ||
} | ||
|
||
export default { | ||
addCssAnimationDisabler, | ||
removeCssAnimationDisabler, | ||
} |
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,58 @@ | ||
import $ from 'jquery' | ||
import $dimensions from '@packages/runner-shared/src/dimensions' | ||
|
||
const resetStyles = ` | ||
border: none !important; | ||
margin: 0 !important; | ||
padding: 0 !important; | ||
` | ||
|
||
const styles = (styleString) => { | ||
return styleString.replace(/\s*\n\s*/g, '') | ||
} | ||
|
||
function addBlackoutForElement ($body, $el) { | ||
const dimensions = $dimensions.getElementDimensions($el) | ||
const width = dimensions.widthWithBorder | ||
const height = dimensions.heightWithBorder | ||
const top = dimensions.offset.top | ||
const left = dimensions.offset.left | ||
|
||
const style = styles(` | ||
${resetStyles} | ||
position: absolute; | ||
top: ${top}px; | ||
left: ${left}px; | ||
width: ${width}px; | ||
height: ${height}px; | ||
background-color: black; | ||
z-index: 2147483647; | ||
`) | ||
|
||
$(`<div class="__cypress-blackout" style="${style}">`).appendTo($body) | ||
} | ||
|
||
function addBlackouts ($body, selector) { | ||
let $el | ||
|
||
try { | ||
$el = $body.find(selector) | ||
if (!$el.length) return | ||
} catch (err) { | ||
// if it's an invalid selector, just ignore it | ||
return | ||
} | ||
|
||
$el.each(function (this: HTMLElement) { | ||
addBlackoutForElement($body, $(this)) | ||
}) | ||
} | ||
|
||
function removeBlackouts ($body) { | ||
$body.find('.__cypress-blackout').remove() | ||
} | ||
|
||
export default { | ||
addBlackouts, | ||
removeBlackouts, | ||
} |
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,89 @@ | ||
import _ from 'lodash' | ||
|
||
const getElementDimensions = ($el) => { | ||
const el = $el.get(0) | ||
|
||
const { offsetHeight, offsetWidth } = el | ||
|
||
const box = { | ||
// offset disregards margin but takes into account border + padding | ||
offset: $el.offset(), | ||
// dont use jquery here for width/height because it uses getBoundingClientRect() which returns scaled values. | ||
// TODO: switch back to using jquery when upgrading to jquery 3.4+ | ||
paddingTop: getPadding($el, 'top'), | ||
paddingRight: getPadding($el, 'right'), | ||
paddingBottom: getPadding($el, 'bottom'), | ||
paddingLeft: getPadding($el, 'left'), | ||
borderTop: getBorder($el, 'top'), | ||
borderRight: getBorder($el, 'right'), | ||
borderBottom: getBorder($el, 'bottom'), | ||
borderLeft: getBorder($el, 'left'), | ||
marginTop: getMargin($el, 'top'), | ||
marginRight: getMargin($el, 'right'), | ||
marginBottom: getMargin($el, 'bottom'), | ||
marginLeft: getMargin($el, 'left'), | ||
} | ||
|
||
// NOTE: offsetWidth/height always give us content + padding + border, so subtract them | ||
// to get the true "clientHeight" and "clientWidth". | ||
// we CANNOT just use "clientHeight" and "clientWidth" because those always return 0 | ||
// for inline elements >_< | ||
box.width = offsetWidth - (box.paddingLeft + box.paddingRight + box.borderLeft + box.borderRight) | ||
box.height = offsetHeight - (box.paddingTop + box.paddingBottom + box.borderTop + box.borderBottom) | ||
|
||
// innerHeight: Get the current computed height for the first | ||
// element in the set of matched elements, including padding but not border. | ||
|
||
// outerHeight: Get the current computed height for the first | ||
// element in the set of matched elements, including padding, border, | ||
// and optionally margin. Returns a number (without 'px') representation | ||
// of the value or null if called on an empty set of elements. | ||
box.heightWithPadding = box.height + box.paddingTop + box.paddingBottom | ||
|
||
box.heightWithBorder = box.heightWithPadding + box.borderTop + box.borderBottom | ||
|
||
box.heightWithMargin = box.heightWithBorder + box.marginTop + box.marginBottom | ||
|
||
box.widthWithPadding = box.width + box.paddingLeft + box.paddingRight | ||
|
||
box.widthWithBorder = box.widthWithPadding + box.borderLeft + box.borderRight | ||
|
||
box.widthWithMargin = box.widthWithBorder + box.marginLeft + box.marginRight | ||
|
||
return box | ||
} | ||
|
||
const getNumAttrValue = ($el, attr) => { | ||
// nuke anything thats not a number or a negative symbol | ||
const num = _.toNumber($el.css(attr).replace(/[^0-9\.-]+/, '')) | ||
|
||
if (!_.isFinite(num)) { | ||
throw new Error('Element attr did not return a valid number') | ||
} | ||
|
||
return num | ||
} | ||
|
||
const getPadding = ($el, dir) => { | ||
return getNumAttrValue($el, `padding-${dir}`) | ||
} | ||
|
||
const getBorder = ($el, dir) => { | ||
return getNumAttrValue($el, `border-${dir}-width`) | ||
} | ||
|
||
const getMargin = ($el, dir) => { | ||
return getNumAttrValue($el, `margin-${dir}`) | ||
} | ||
|
||
const getOuterSize = ($el) => { | ||
return { | ||
width: $el.outerWidth(true), | ||
height: $el.outerHeight(true), | ||
} | ||
} | ||
|
||
export default { | ||
getOuterSize, | ||
getElementDimensions, | ||
} |
Oops, something went wrong.