Co-authored-by: Emily Rohrbough <emilyrohrbough@users.noreply.github.com>
Co-authored-by: Matt Henkes <mjhenkes@gmail.com>
This commit is contained in:
Kukhyeon Heo
2021-12-11 02:17:03 +09:00
committed by GitHub
parent dd0fc9b9f7
commit 09bcc5b0ff
3 changed files with 74 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<script>
function defaultBehavior(e) {
document.getElementById('result').innerHTML = JSON.stringify(e.target.getAttribute('target'))
}
function setAttributeBehavior(e) {
e.target.setAttribute('target', '_top')
document.getElementById('result2').innerHTML = JSON.stringify(e.target.getAttribute('target'))
}
function removeAttributeBehavior(e) {
e.target.setAttribute('target', '_top')
e.target.removeAttribute('target')
document.getElementById('result3').innerHTML = JSON.stringify(e.target.getAttribute('target'))
}
</script>
<div>
<a href="#" id="link" onclick="defaultBehavior(event)">link 1</a>
<div id="result"></div>
</div>
<div>
<a href="#" id="link2" onclick="setAttributeBehavior(event)">link 2</a>
<div id="result2"></div>
</div>
<div>
<a href="#" id="link3" onclick="removeAttributeBehavior(event)">link 3</a>
<div id="result3"></div>
</div>
</body>
</html>

View File

@@ -0,0 +1,16 @@
describe('issue 17512', () => {
beforeEach(() => {
cy.visit('fixtures/issue-17512.html')
})
it('returns null when target is not defined', () => {
cy.get('#link').click()
cy.get('#result').should('have.text', 'null')
cy.get('#link2').click()
cy.get('#result2').should('have.text', '"_top"')
cy.get('#link3').click()
cy.get('#result3').should('have.text', 'null')
})
})

View File

@@ -8,16 +8,24 @@ const invalidTargets = new Set(['_parent', '_top'])
*/
export function handleInvalidEventTarget (e: Event & {target: HTMLFormElement | HTMLAnchorElement}) {
let targetValue = e.target.target
let targetSet = e.target.hasAttribute('target')
if (invalidTargets.has(e.target.target)) {
e.target.target = ''
}
const { getAttribute, setAttribute } = e.target
const { getAttribute, setAttribute, removeAttribute } = e.target
const targetDescriptor = Object.getOwnPropertyDescriptor(e.target, 'target')
e.target.getAttribute = function (k) {
if (k === 'target') {
// https://github.com/cypress-io/cypress/issues/17512
// When the target attribute doesn't exist, it should return null.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute#non-existing_attributes
if (!targetSet) {
return null
}
return targetValue
}
@@ -26,6 +34,7 @@ export function handleInvalidEventTarget (e: Event & {target: HTMLFormElement |
e.target.setAttribute = function (k, v) {
if (k === 'target') {
targetSet = true
targetValue = v
return $elements.callNativeMethod(this, 'setAttribute', 'cyTarget', v)
@@ -34,6 +43,16 @@ export function handleInvalidEventTarget (e: Event & {target: HTMLFormElement |
return setAttribute.call(this, k, v)
}
e.target.removeAttribute = function (k) {
if (k === 'target') {
targetSet = false
targetValue = ''
// We're not using `$elements.callNativeMethod` here because it disallows `removeAttribute`.
return removeAttribute.call(this, k)
}
}
if (!targetDescriptor) {
Object.defineProperty(e.target, 'target', {
configurable: false,