Skip to content

Commit

Permalink
chore: convert dimensions.js to ts (#31003)
Browse files Browse the repository at this point in the history
* chore: convert dimensions js to ts

* Update all $el params to accept any

* try jQuery<HTMLElement> for $el type

* update dimensions.offset call to handle possibly being undefined.
  • Loading branch information
jennifer-shehane authored Feb 6, 2025
1 parent e947378 commit 99d182f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/driver/src/dom/blackout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ function addBlackoutForElement ($body: JQuery<HTMLBodyElement>, $el: JQuery<HTML
const dimensions = $dimensions.getElementDimensions($el)
const width = dimensions.widthWithBorder
const height = dimensions.heightWithBorder
const top = dimensions.offset.top
const left = dimensions.offset.left
const top = dimensions.offset?.top
const left = dimensions.offset?.left

const style = styles(`
${resetStyles}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,39 @@

import _ from 'lodash'

const getElementDimensions = ($el) => {
const el = $el.get(0)
interface Box {
offset: { top: number, left: number } | undefined
paddingTop: number
paddingRight: number
paddingBottom: number
paddingLeft: number
borderTop: number
borderRight: number
borderBottom: number
borderLeft: number
marginTop: number
marginRight: number
marginBottom: number
marginLeft: number
width?: number
height?: number
heightWithPadding?: number
heightWithBorder?: number
heightWithMargin?: number
widthWithPadding?: number
widthWithBorder?: number
widthWithMargin?: number
}

const getElementDimensions = ($el: JQuery<HTMLElement>) => {
const el: HTMLElement = $el.get(0)

const { offsetHeight, offsetWidth } = el

const box = {
const box: 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'),
Expand Down Expand Up @@ -57,7 +80,10 @@ const getElementDimensions = ($el) => {
return box
}

const getNumAttrValue = ($el, attr) => {
type dir = 'top' | 'right' | 'bottom' | 'left'
type attr = `padding-${dir}` | `border-${dir}-width` | `margin-${dir}`

const getNumAttrValue = ($el: JQuery<HTMLElement>, attr: attr) => {
// nuke anything thats not a number or a negative symbol
const num = _.toNumber($el.css(attr).replace(/[^0-9\.-]+/, ''))

Expand All @@ -68,15 +94,15 @@ const getNumAttrValue = ($el, attr) => {
return num
}

const getPadding = ($el, dir) => {
const getPadding = ($el: JQuery<HTMLElement>, dir: dir) => {
return getNumAttrValue($el, `padding-${dir}`)
}

const getBorder = ($el, dir) => {
const getBorder = ($el: JQuery<HTMLElement>, dir: dir) => {
return getNumAttrValue($el, `border-${dir}-width`)
}

const getMargin = ($el, dir) => {
const getMargin = ($el: JQuery<HTMLElement>, dir: dir) => {
return getNumAttrValue($el, `margin-${dir}`)
}

Expand Down

5 comments on commit 99d182f

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 99d182f Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/14.0.3/linux-x64/develop-99d182f539714cb13189589b3aa7a2912d87f431/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 99d182f Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/14.0.3/linux-arm64/develop-99d182f539714cb13189589b3aa7a2912d87f431/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 99d182f Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/14.0.3/win32-x64/develop-99d182f539714cb13189589b3aa7a2912d87f431/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 99d182f Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/14.0.3/darwin-arm64/develop-99d182f539714cb13189589b3aa7a2912d87f431/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 99d182f Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/14.0.3/darwin-x64/develop-99d182f539714cb13189589b3aa7a2912d87f431/cypress.tgz

Please sign in to comment.