https://github.com/angular/angular
Raw File
Tip revision: 440b51b7afec991c7d6b8d4cc2c301bbbb5fb2b5 authored by Pawel Kozlowski on 15 February 2023, 17:41:25 UTC
release: cut the v15.2.0-rc.0 release
Tip revision: 440b51b
jsonp_mock.ts
/**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */

export class MockScriptElement {
  constructor(public ownerDocument: MockDocument) {}

  listeners: {
    load?: (event: Event) => void,
    error?: (err: Error) => void,
  } = {};

  addEventListener(event: 'load'|'error', handler: Function): void {
    this.listeners[event] = handler as any;
  }

  removeEventListener(event: 'load'|'error'): void {
    delete this.listeners[event];
  }
}

export class MockDocument {
  // TODO(issue/24571): remove '!'.
  mock!: MockScriptElement|null;
  readonly body: any = this;

  implementation = {
    createHTMLDocument: () => new MockDocument(),
  };

  createElement(tag: 'script'): HTMLScriptElement {
    return new MockScriptElement(this) as any as HTMLScriptElement;
  }

  appendChild(node: any): void {
    this.mock = node;
  }

  removeNode(node: any): void {
    if (this.mock === node) {
      this.mock = null;
    }
  }

  adoptNode(node: any) {
    node.ownerDocument = this;
  }

  mockLoad(): void {
    // Mimic behavior described by
    // https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-block
    if (this.mock!.ownerDocument === this) {
      this.mock!.listeners.load!(null as any);
    }
  }

  mockError(err: Error) {
    // Mimic behavior described by
    // https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-block
    if (this.mock!.ownerDocument === this) {
      this.mock!.listeners.error!(err);
    }
  }
}
back to top