DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (47d7c18620c8)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
/**
 * Provides infrastructure for tests that would require mock document.
 */

"use strict";

var EXPORTED_SYMBOLS = ["MockDocument"];

const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

const MockDocument = {
  /**
   * Create a document for the given URL containing the given HTML with the ownerDocument of all <form>s having a mocked location.
   */
  createTestDocument(aDocumentURL, aContent = "<form>", aType = "text/html") {
    let parser = new DOMParser();
    let parsedDoc = parser.parseFromString(aContent, aType);

    // Assign ownerGlobal to documentElement as well for the form-less
    // inputs treating it as rootElement.
    this.mockOwnerGlobalProperty(parsedDoc.documentElement);

    for (let form of parsedDoc.forms) {
      this.mockOwnerDocumentProperty(form, parsedDoc, aDocumentURL);
      this.mockOwnerGlobalProperty(form);
      for (let field of form.elements) {
        this.mockOwnerGlobalProperty(field);
      }
    }
    return parsedDoc;
  },

  mockOwnerDocumentProperty(aElement, aDoc, aURL) {
    // Mock the document.location object so we can unit test without a frame. We use a proxy
    // instead of just assigning to the property since it's not configurable or writable.
    let document = new Proxy(aDoc, {
      get(target, property, receiver) {
        // document.location is normally null when a document is outside of a "browsing context".
        // See https://html.spec.whatwg.org/#the-location-interface
        if (property == "location") {
          return new URL(aURL);
        }
        return target[property];
      },
    });

    // Assign element.ownerDocument to the proxy so document.location works.
    Object.defineProperty(aElement, "ownerDocument", {
      value: document,
    });
  },

  mockOwnerGlobalProperty(aElement) {
    Object.defineProperty(aElement, "ownerGlobal", {
      value: {
        windowUtils: {
          addManuallyManagedState() {},
          removeManuallyManagedState() {},
        },
        UIEvent: Event,
        Event,
      },
      configurable: true,
    });
  },

  mockNodePrincipalProperty(aElement, aURL) {
    Object.defineProperty(aElement, "nodePrincipal", {
      value: Services.scriptSecurityManager.createContentPrincipal(
        Services.io.newURI(aURL),
        {}
      ),
    });
  },

  createTestDocumentFromFile(aDocumentURL, aFile) {
    let fileStream = Cc[
      "@mozilla.org/network/file-input-stream;1"
    ].createInstance(Ci.nsIFileInputStream);
    fileStream.init(aFile, -1, -1, 0);

    let data = NetUtil.readInputStreamToString(
      fileStream,
      fileStream.available()
    );

    return this.createTestDocument(aDocumentURL, data);
  },
};