Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit 293dbc1

Browse files
authored
Merge pull request #1754 from atlassian/html-dumping
Handling early <body> changes
2 parents 95e465e + 85a51c4 commit 293dbc1

6 files changed

Lines changed: 55 additions & 59 deletions

File tree

.size-snapshot.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"dist/react-beautiful-dnd.js": {
3-
"bundled": 364318,
4-
"minified": 133393,
5-
"gzipped": 39449
3+
"bundled": 364497,
4+
"minified": 133470,
5+
"gzipped": 39473
66
},
77
"dist/react-beautiful-dnd.min.js": {
8-
"bundled": 307797,
9-
"minified": 109292,
10-
"gzipped": 31782
8+
"bundled": 307976,
9+
"minified": 109369,
10+
"gzipped": 31806
1111
},
1212
"dist/react-beautiful-dnd.esm.js": {
13-
"bundled": 240708,
14-
"minified": 125264,
15-
"gzipped": 32618,
13+
"bundled": 240875,
14+
"minified": 125341,
15+
"gzipped": 32643,
1616
"treeshaked": {
1717
"rollup": {
1818
"code": 21121,

src/view/use-announcer/use-announcer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ export default function useAnnouncer(contextId: ContextId): Announce {
4141
// unmounting after a timeout to let any announcements
4242
// during a mount be published
4343
setTimeout(function remove() {
44-
// checking if element exists before remove to prevent errors
45-
if (getBodyElement().contains(el)) {
46-
// not clearing the ref as it might have been set by a new effect
47-
getBodyElement().removeChild(el);
44+
// checking if element exists as the body might have been changed by things like 'turbolinks'
45+
const body: HTMLBodyElement = getBodyElement();
46+
if (body.contains(el)) {
47+
body.removeChild(el);
4848
}
4949
// if el was the current ref - clear it so that
5050
// we can get a warning if announce is called

src/view/use-hidden-text-element/use-hidden-text-element.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ export default function useHiddenTextElement({
4646
getBodyElement().appendChild(el);
4747

4848
return function unmount() {
49-
// Remove from body
50-
getBodyElement().removeChild(el);
49+
// checking if element exists as the body might have been changed by things like 'turbolinks'
50+
const body: HTMLBodyElement = getBodyElement();
51+
if (body.contains(el)) {
52+
body.removeChild(el);
53+
}
5154
};
5255
},
5356
[id, text],
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// @flow
2+
import React from 'react';
3+
import { render } from '@testing-library/react';
4+
import { isDragging } from './util/helpers';
5+
import App from './util/app';
6+
import { forEachSensor, simpleLift, type Control } from './util/controls';
7+
import getBodyElement from '../../../src/view/get-body-element';
8+
9+
it('should have any errors when body is changed just before unmount', () => {
10+
jest.useFakeTimers();
11+
const { unmount } = render(<App />);
12+
13+
expect(() => {
14+
getBodyElement().innerHTML = '';
15+
unmount();
16+
jest.runOnlyPendingTimers();
17+
}).not.toThrow();
18+
19+
jest.useRealTimers();
20+
});
21+
22+
forEachSensor((control: Control) => {
23+
it('should have any errors when body is changed just before unmount: mid drag', () => {
24+
const { unmount, getByText } = render(<App />);
25+
const handle: HTMLElement = getByText('item: 0');
26+
27+
// mid drag
28+
simpleLift(control, handle);
29+
expect(isDragging(handle)).toEqual(true);
30+
31+
expect(() => {
32+
getBodyElement().innerHTML = '';
33+
unmount();
34+
jest.runOnlyPendingTimers();
35+
}).not.toThrow();
36+
});
37+
});

test/unit/integration/no-logging-when-unmounting-mid-drag.spec.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

test/unit/view/announcer.spec.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,6 @@ it('should remove the element when unmounting after a timeout', () => {
5959
expect(getElement('5')).not.toBeTruthy();
6060
});
6161

62-
it('should not remove the element when unmounting after a timeout if the element does not exist', () => {
63-
const { unmount } = render(
64-
<WithAnnouncer contextId="5">{getMock()}</WithAnnouncer>,
65-
);
66-
67-
const doc = new DOMParser().parseFromString(
68-
'<!doctype html><title>wat</title>',
69-
'text/html',
70-
);
71-
// $FlowFixMe
72-
document.write(doc);
73-
unmount();
74-
expect(() => jest.runOnlyPendingTimers()).not.toThrow(
75-
// $FlowFixMe
76-
new DOMException(
77-
"Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.",
78-
),
79-
);
80-
});
81-
8262
it('should set the text content of the announcement element', () => {
8363
// arrange
8464
const mock = getMock();

0 commit comments

Comments
 (0)