ssword or username field in a document"
      );
    }

    let form =
      aField.form ||
      lazy.FormLikeFactory.closestFormIgnoringShadowRoots(aField);
    if (form) {
      return this.createFromForm(form);
    } else if (aField.hasAttribute("form")) {
      lazy.log.debug(
        "createFromField: field has form attribute but no form: ",
        aField.getAttribute("form")
      );
    }

    let formLike = lazy.FormLikeFactory.createFromField(aField);
    formLike.action = lazy.Logic.getLoginOrigin(aField.ownerDocument.baseURI);
    lazy.log.debug(
      "Created non-form LoginForm for rootElement:",
      aField.ownerDocument.documentElement
    );

    this._addLoginFormToRootElementsSet(formLike);
    return formLike;
  },

  getRootElementsWeakSetForDocument(aDocument) {
    let rootElementsSet = this._loginFormRootElementsByDocument.get(aDocument);
    if (!rootElementsSet) {
      rootElementsSet = new WeakSet();
      this._loginFormRootElementsByDocument.set(aDocument, rootElementsSet);
    }
    return rootElementsSet;
  },

  getForRootElement(aRootElement) {
    return this._loginFormsByRootElement.get(aRootElement);
  },

  setForRootElement(aRootElement, aLoginForm) {
    return this._loginFormsByRootElement.set(aRootElement, aLoginForm);
  },

  _addLoginFormToRootElementsSet(formLike) {
    let rootElementsSet = this.getRootElementsWeakSetForDocument(
      formLike.ownerDocument
    );
    rootElementsSet.add(formLike.rootElement);
    lazy.log.debug(
      "adding",
      formLike.rootElement,
      "to root elements for",
      formLike.ownerDocument
    );

    this._loginFormsByRootElement.set(formLike.rootElement, formLike);
  },
};
PK