Repository: puppeteer/puppeteer
File: packages/puppeteer-core/src/cdp/Accessibility.ts (TypeScript)

Code before:

    return this.isLeafNode() && !!this.#name;
  }

  public serialize(): SerializedAXNode {
    const properties = new Map<string, number | string | boolean>();
    for (const property of this.payload.properties || []) {
      properties.set(property.name.toLowerCase(), property.value.value);
    }
    if (this.payload.name) {
      properties.set('name', this.payload.name.value);
    }
    if (this.payload.value) {
      properties.set('value', this.payload.value.value);
    }
    if (this.payload.description) {
      properties.set('description', this.payload.description.value);
    }

    const node: SerializedAXNode = {
      role: this.#role,
      elementHandle: async (): Promise<ElementHandle | null> => {
        if (!this.payload.backendDOMNodeId) {
          return null;
        }
        const handle = await this.#realm.adoptBackendNode(
          this.payload.backendDOMNodeId,
        );

        if (!handle || !(handle instanceof ElementHandle)) {
          return null;
        }

        const isTextNode = await handle.evaluate(node => {
          return node.nodeType === Node.TEXT_NODE;
        });
        // Text nodes are not elements, so we can't return a handle to them.
        // Instead, we find the parent element and return a handle to the parent.
        if (isTextNode) {
          using parent = await handle.evaluateHandle(node => {
            return node.parentElement;
          });

          void handle.dispose().catch(debugError);

          return parent.move() as ElementHandle<Element>;
        }

        return handle;
      },
    };

Review comment (suggestion):
```suggestion
        using handle = await this.#realm.adoptBackendNode(
```

Code after:
    return this.isLeafNode() && !!this.#name;
  }

  public serialize(): SerializedAXNode {
    const properties = new Map<string, number | string | boolean>();
    for (const property of this.payload.properties || []) {
      properties.set(property.name.toLowerCase(), property.value.value);
    }
    if (this.payload.name) {
      properties.set('name', this.payload.name.value);
    }
    if (this.payload.value) {
      properties.set('value', this.payload.value.value);
    }
    if (this.payload.description) {
      properties.set('description', this.payload.description.value);
    }

    const node: SerializedAXNode = {
      role: this.#role,
      elementHandle: async (): Promise<ElementHandle | null> => {
        if (!this.payload.backendDOMNodeId) {
          return null;
        }
        using handle = await this.#realm.adoptBackendNode(
          this.payload.backendDOMNodeId,
        );

        // Since Text nodes are not elements, we want to
        // return a handle to the parent element for them.
        return (await handle.evaluateHandle(node => {
          return node.nodeType === Node.TEXT_NODE ? node.parentElement : node;
        })) as ElementHandle<Element>;
      },
    };

    type UserStringProperty =
      | 'name'
      | 'value'
      | 'description'
      | 'keyshortcuts'
      | 'roledescription'
      | 'valuetext';

    const userStringProperties: UserStringProperty[] = [
      'name',
      'value',
      'description',
      'keyshortcuts',
      'roledescription',
      'valuetext',