Skip to content

Commit

Permalink
Create stub for form.requestSubmit
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyGu committed Sep 27, 2019
1 parent 6b89146 commit bcb520b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
27 changes: 20 additions & 7 deletions lib/jsdom/living/helpers/form-controls.js
Expand Up @@ -24,8 +24,6 @@ const { closest, firstChildWithLocalName } = require("./traversal");
const NODE_TYPE = require("../node-type");
const { HTML_NS } = require("./namespaces");

const submittableLocalNames = new Set(["button", "input", "keygen", "object", "select", "textarea"]);

exports.isDisabled = formControl => {
if (formControl.localName === "button" || formControl.localName === "input" || formControl.localName === "select" ||
formControl.localName === "textarea") {
Expand All @@ -48,15 +46,30 @@ exports.isDisabled = formControl => {
return false;
};

// https://html.spec.whatwg.org/multipage/forms.html#category-listed
const listedElements = new Set(["button", "fieldset", "input", "object", "output", "select", "textarea"]);
exports.isListed = formControl => listedElements.has(formControl._localName) && formControl.namespaceURI === HTML_NS;

// https://html.spec.whatwg.org/multipage/forms.html#category-submit
const submittableElements = new Set(["button", "input", "object", "select", "textarea"]);
exports.isSubmittable = formControl => {
// https://html.spec.whatwg.org/multipage/forms.html#category-submit
return submittableLocalNames.has(formControl.localName);
return submittableElements.has(formControl._localName) && formControl.namespaceURI === HTML_NS;
};

// https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
const submitButtonInputTypes = new Set(["submit", "image"]);
exports.isSubmitButton = formControl => {
return ((formControl._localName === "input" && submitButtonInputTypes.has(formControl.type)) ||
(formControl._localName === "button" && formControl.type === "submit")) &&
formControl.namespaceURI === HTML_NS;
};

// https://html.spec.whatwg.org/multipage/forms.html#concept-button
const buttonInputTypes = new Set([...submitButtonInputTypes, "reset", "button"]);
exports.isButton = formControl => {
// https://html.spec.whatwg.org/multipage/forms.html#concept-button
return formControl.type === "button" || formControl.type === "submit" || formControl.type === "reset" ||
formControl.type === "image" || formControl.localName === "button";
return ((formControl._localName === "input" && buttonInputTypes.has(formControl.type)) ||
formControl._localName === "button") &&
formControl.namespaceURI === HTML_NS;
};

exports.normalizeToCRLF = string => {
Expand Down
32 changes: 21 additions & 11 deletions lib/jsdom/living/nodes/HTMLFormElement-impl.js
@@ -1,19 +1,14 @@
"use strict";

const DOMException = require("domexception");
const HTMLElementImpl = require("./HTMLElement-impl").implementation;
const { domSymbolTree } = require("../helpers/internal-constants");
const { fireAnEvent } = require("../helpers/events");
const { HTML_NS } = require("../helpers/namespaces");
const { isListed, isSubmittable, isSubmitButton } = require("../helpers/form-controls");
const HTMLCollection = require("../generated/HTMLCollection");
const notImplemented = require("../../browser/not-implemented");
const { reflectURLAttribute } = require("../../utils");

// http://www.whatwg.org/specs/web-apps/current-work/#category-listed
const listedElements = new Set(["button", "fieldset", "input", "keygen", "object", "output", "select", "textarea"]);

// https://html.spec.whatwg.org/multipage/forms.html#category-submit
const submittableElements = new Set(["button", "input", "object", "select", "textarea"]);

const encTypes = new Set([
"application/x-www-form-urlencoded",
"multipart/form-data",
Expand Down Expand Up @@ -57,9 +52,7 @@ class HTMLFormElementImpl extends HTMLElementImpl {
return HTMLCollection.createImpl([], {
element: this,
query: () => domSymbolTree.treeToArray(this, {
filter: node => listedElements.has(node._localName) &&
node._namespaceURI === HTML_NS &&
node.type !== "image"
filter: node => isListed(node) && (node._localName !== "input" || node.type !== "image")
})
});
}
Expand All @@ -84,6 +77,23 @@ class HTMLFormElementImpl extends HTMLElementImpl {
notImplemented("HTMLFormElement.prototype.submit", this._ownerDocument._defaultView);
}

requestSubmit(submitter = undefined) {
if (submitter !== undefined) {
if (!isSubmitButton(submitter)) {
throw new TypeError("The specified element is not a submit button");
}
if (submitter.form !== this) {
throw new DOMException("The specified element is not owned by this form element", "NotFoundError");
}
}

if (!fireAnEvent("submit", this, undefined, { bubbles: true, cancelable: true })) {
return;
}

notImplemented("HTMLFormElement.prototype.requestSubmit", this._ownerDocument._defaultView);
}

_doReset() {
if (!this.isConnected) {
return;
Expand Down Expand Up @@ -165,7 +175,7 @@ class HTMLFormElementImpl extends HTMLElementImpl {
_staticallyValidateConstraints() {
const controls = [];
for (const el of domSymbolTree.treeIterator(this)) {
if (el.form === this && submittableElements.has(el.nodeName.toLowerCase())) {
if (el.form === this && isSubmittable(el)) {
controls.push(el);
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/jsdom/living/nodes/HTMLFormElement.webidl
Expand Up @@ -20,6 +20,7 @@ interface HTMLFormElement : HTMLElement {
// getter (RadioNodeList or Element) (DOMString name);

void submit();
void requestSubmit(optional HTMLElement submitter);
[CEReactions] void reset();
boolean checkValidity();
boolean reportValidity();
Expand Down

0 comments on commit bcb520b

Please sign in to comment.