diff --git a/core/inject.ts b/core/inject.ts index e6d97b118..1fd3d58d6 100644 --- a/core/inject.ts +++ b/core/inject.ts @@ -47,8 +47,8 @@ export function inject( containerElement = container; } // Verify that the container is in document. - if (!containerElement || !dom.containsNode(document, containerElement)) { - throw Error('Error: container is not in current document.'); + if (!document.contains(containerElement)) { + throw Error('Error: container is not in current document'); } const options = new Options(opt_options || {} as BlocklyOptions); const subContainer = (document.createElement('div')); @@ -56,7 +56,7 @@ export function inject( subContainer.tabIndex = 0; aria.setState(subContainer, aria.State.LABEL, Msg['WORKSPACE_ARIA_LABEL']); - containerElement.appendChild(subContainer); + containerElement!.appendChild(subContainer); const svg = createDom(subContainer, options); // Create surfaces for dragging things. These are optimizations diff --git a/core/utils/dom.ts b/core/utils/dom.ts index 363000480..6fe450351 100644 --- a/core/utils/dom.ts +++ b/core/utils/dom.ts @@ -7,6 +7,7 @@ import * as goog from '../../closure/goog/goog.js'; goog.declareModuleId('Blockly.utils.dom'); +import * as deprecation from './deprecation.js'; import type {Svg} from './svg.js'; @@ -33,7 +34,6 @@ export enum NodeType { ELEMENT_NODE = 1, TEXT_NODE = 3, COMMENT_NODE = 8, - DOCUMENT_POSITION_CONTAINED_BY = 16 } /** Temporary cache of text widths. */ @@ -160,11 +160,13 @@ export function insertAfter(newNode: Element, refNode: Element) { * @param parent The node that should contain the other node. * @param descendant The node to test presence of. * @returns Whether the parent node contains the descendant node. + * @deprecated Use native 'contains' DOM method. */ export function containsNode(parent: Node, descendant: Node): boolean { - return !!( - parent.compareDocumentPosition(descendant) & - NodeType.DOCUMENT_POSITION_CONTAINED_BY); + deprecation.warn( + 'Blockly.utils.dom.containsNode', 'version 10', 'version 11', + 'Use native "contains" DOM method'); + return parent.contains(descendant); } /** diff --git a/core/workspace_svg.ts b/core/workspace_svg.ts index c5a07ab04..735573e31 100644 --- a/core/workspace_svg.ts +++ b/core/workspace_svg.ts @@ -641,8 +641,8 @@ export class WorkspaceSvg extends Workspace implements IASTNodeLocationSvg { let x = 0; let y = 0; let scale = 1; - if (dom.containsNode(this.getCanvas(), element) || - dom.containsNode(this.getBubbleCanvas(), element)) { + if (this.getCanvas().contains(element) || + this.getBubbleCanvas().contains(element)) { // Before the SVG canvas, scale the coordinates. scale = this.scale; }