Skip to content

Commit

Permalink
chore: Repalce containsOrEqual with utility from the toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Aug 28, 2023
1 parent b28e7f5 commit 05448f4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 44 deletions.
29 changes: 1 addition & 28 deletions src/internal/utils/__tests__/dom.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { findUpUntil, parseCssVariable, containsOrEqual } from '../../../../lib/components/internal/utils/dom';
import { findUpUntil, parseCssVariable } from '../../../../lib/components/internal/utils/dom';

describe('findUpUntil', () => {
test('returns null if there is no match', () => {
Expand Down Expand Up @@ -86,30 +86,3 @@ describe('parseCssVariable', () => {
});
});
});

describe('containsOrEqual', () => {
test('returns "true", when the node and the container are the same element', () => {
const div = document.createElement('div');
div.innerHTML = `
<div id="container1"></div>
`;
expect(containsOrEqual(div.querySelector('#container1'), div.querySelector('#container1') as Node)).toBe(true);
});
test('returns "true", when the node is descendant from the container', () => {
const div = document.createElement('div');
div.innerHTML = `
<div id="container1">
<div id="node"></div>
</div>
`;
expect(containsOrEqual(div.querySelector('#container1'), div.querySelector('#node') as Node)).toBe(true);
});
test('returns "false", when the node is not a child of the container', () => {
const div = document.createElement('div');
div.innerHTML = `
<div id="container1"></div>
<div id="node"></div>
`;
expect(containsOrEqual(div.querySelector('#container1'), div.querySelector('#node') as Node)).toBe(false);
});
});
13 changes: 0 additions & 13 deletions src/internal/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,3 @@ export function parseCssVariable(value: string) {
const match = expr.body.match(cssVariableExpression);
return match ? match[1] : value;
}

/**
* Checks whether the given node is a descendant of a container.
* @deprecated use nodeContains from component-toolkit
* @param container Container node
* @param node Node that is checked to be a descendant of the container
*/
export function containsOrEqual(container: Node | null, node: Node): boolean {
if (container === null) {
return false;
}
return container === node || container.contains(node);
}
7 changes: 4 additions & 3 deletions src/internal/utils/node-belongs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { containsOrEqual, findUpUntil } from './dom';
import { findUpUntil } from './dom';
import { nodeContains } from '@cloudscape-design/component-toolkit/dom';

/**
* Checks whether the given node (target) belongs to the container.
* The function is similar to containsOrEqual but also accounts for dropdowns with expandToViewport=true.
* The function is similar to nodeContains but also accounts for dropdowns with expandToViewport=true.
*
* @param container Container node
* @param target Node that is checked to be a descendant of the container
Expand All @@ -16,5 +17,5 @@ export function nodeBelongs(container: Node | null, target: Node | EventTarget |
}
const portal = findUpUntil(target as HTMLElement, node => !!node.dataset.awsuiReferrerId);
const referrer = portal instanceof HTMLElement ? document.getElementById(portal.dataset.awsuiReferrerId ?? '') : null;
return referrer ? containsOrEqual(container, referrer) : containsOrEqual(container, target);
return referrer ? nodeContains(container, referrer) : nodeContains(container, target);
}

0 comments on commit 05448f4

Please sign in to comment.