Skip to content

Commit

Permalink
fix: fixing issue with blurring shadow dom elements (#29125)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschile committed Mar 20, 2024
1 parent 7735405 commit 75eba1e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

_Released 3/26/2024 (PENDING)_

**Bugfixes:**

- Fixed an issue blurring shadow dom elements. Fixed in [#29125](https://github.com/cypress-io/cypress/pull/29125).

**Dependency Updates:**

- Updated jose from `4.11.2` to `4.15.5`. Addressed in [#29086](https://github.com/cypress-io/cypress/pull/29086).
Expand Down
28 changes: 27 additions & 1 deletion packages/driver/cypress/e2e/commands/actions/focus.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ describe('src/cy/commands/actions/focus', () => {
})
})

it('can focus element in nested shadow dom', () => {
const onFocus = cy.stub()

cy.visit('/fixtures/shadow-dom.html')
cy.get('.shadow-5 + input', { includeShadowDom: true }).as('shadow-input').then(($el) => {
$el.on('focus', onFocus)
})

cy.get('@shadow-input').focus().then(() => {
expect(onFocus).to.be.calledOnce
})
})

describe('assertion verification', () => {
beforeEach(function () {
cy.on('log:added', (attrs, log) => {
Expand Down Expand Up @@ -673,7 +686,7 @@ describe('src/cy/commands/actions/focus', () => {
})
})

it('can focus svg elements', () => {
it('can blur svg elements', () => {
const onBlur = cy.stub()

cy.$$('[data-cy=rect]').blur(onBlur)
Expand All @@ -683,6 +696,19 @@ describe('src/cy/commands/actions/focus', () => {
})
})

it('can blur element in nested shadow dom', () => {
const onBlur = cy.stub()

cy.visit('/fixtures/shadow-dom.html')
cy.get('.shadow-5 + input', { includeShadowDom: true }).as('shadow-input').then(($el) => {
$el.on('blur', onBlur).get(0).focus()
})

cy.get('@shadow-input').blur().then(() => {
expect(onBlur).to.be.calledOnce
})
})

describe('assertion verification', () => {
beforeEach(function () {
cy.on('log:added', (attrs, log) => {
Expand Down
13 changes: 9 additions & 4 deletions packages/driver/src/cy/focused.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,19 @@ export const create = (state: StateFunc) => ({
needsFocus ($elToFocus, $previouslyFocusedEl) {
const $focused = this.getFocused()

// if we dont have a focused element
// if we don't have a focused element
// we know we want to fire a focus event
if (!$focused) {
return true
}

// if we didnt have a previously focused el
// if we didn't have a previously focused el
// then always return true
if (!$previouslyFocusedEl) {
return true
}

// if we are attemping to focus a differnet element
// if we are attempting to focus a different element
// than the one we currently have, we know we want
// to fire a focus event
if ($focused.get(0) !== $elToFocus.get(0)) {
Expand All @@ -273,11 +273,16 @@ export const create = (state: StateFunc) => ({
return false
},

getFocused (document = state('document')) {
getFocused (document: Document | ShadowRoot | undefined = state('document')) {
if (document) {
const { activeElement } = document

if ($dom.isFocused(activeElement)) {
// if the active element is a shadow root, we need to recursively get the active element of the shadow root
if (activeElement?.shadowRoot && activeElement.shadowRoot.activeElement) {
return this.getFocused(activeElement.shadowRoot)
}

return $dom.wrap(activeElement)
}
}
Expand Down

5 comments on commit 75eba1e

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 75eba1e Mar 20, 2024

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/13.7.1/linux-arm64/develop-75eba1eb6a478aa9576a75d807d6e69923aeb6d1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 75eba1e Mar 20, 2024

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/13.7.1/linux-x64/develop-75eba1eb6a478aa9576a75d807d6e69923aeb6d1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 75eba1e Mar 20, 2024

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/13.7.1/darwin-x64/develop-75eba1eb6a478aa9576a75d807d6e69923aeb6d1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 75eba1e Mar 20, 2024

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/13.7.1/darwin-arm64/develop-75eba1eb6a478aa9576a75d807d6e69923aeb6d1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 75eba1e Mar 20, 2024

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/13.7.1/win32-x64/develop-75eba1eb6a478aa9576a75d807d6e69923aeb6d1/cypress.tgz

Please sign in to comment.