Skip to content

Commit

Permalink
Fix the type property on buttons
Browse files Browse the repository at this point in the history
It now restricts to the valid values, and correctly defaults to "submit". We also add tests for the previous commit, about clicking a button triggering a submit, which only pass after this fix to the type property.
  • Loading branch information
domenic committed Jun 30, 2015
1 parent e5c7b24 commit 0548f6c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/jsdom/level2/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,14 +1119,39 @@ define('HTMLButtonElement', {
}
}
}
},
get type() {
const typeAttr = (this.getAttribute('type') || '').toLowerCase();
switch (typeAttr) {
case 'submit':
case 'reset':
case 'button':
case 'menu':
return typeAttr;
default:
return 'submit';
}
},
set type(v) {
v = String(v).toLowerCase();
switch (v) {
case 'submit':
case 'reset':
case 'button':
case 'menu':
this.setAttribute('type', v);
break;
default:
this.setAttribute('type', 'submit');
break;
}
}
},
attributes: [
'accessKey',
{prop: 'disabled', type: 'boolean'},
'name',
{prop: 'tabIndex', type: 'long'},
'type',
'value'
]
});
Expand Down
62 changes: 62 additions & 0 deletions test/living-html/htmlbuttonelement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";
const jsdom = require("../..");

exports["a button's type should be submit by default"] = function (t) {
const doc = jsdom.jsdom();
const button = doc.createElement("button");

t.equal(button.type, "submit");
t.done();
};

exports["a button's type should stay within the range of valid values"] = function (t) {
const doc = jsdom.jsdom();
const button = doc.createElement("button");

for (const type of ["reset", "button", "menu", "submit"]) {
button.type = type;
t.equal(button.type, type);

button.type = type.toUpperCase();
t.equal(button.type, type);
}

button.type = "reset";
button.type = "asdfgdsafd";
t.equal(button.type, "submit");

button.type = "reset";
button.type = "";
t.equal(button.type, "submit");

t.done();
};

exports["clicking a button with .click() should trigger a submit"] = function (t) {
const doc = jsdom.jsdom();
const form = doc.createElement("form");
const button = doc.createElement("button");

form.appendChild(button);
form.addEventListener("submit", function (ev) {
t.equal(ev.target, form);
t.done();
});

button.click();
};

exports["clicking a button by dispatching an event should trigger a submit"] = function (t) {
const doc = jsdom.jsdom();
const form = doc.createElement("form");
const button = doc.createElement("button");

form.appendChild(button);
form.addEventListener("submit", function (ev) {
t.equal(ev.target, form);
t.done();
});

const e = new doc.defaultView.MouseEvent("click");
button.dispatchEvent(e);
};
1 change: 1 addition & 0 deletions test/runner
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var files = [
"living-html/cookie.js",
"living-html/current-script.js",
"living-html/focus.js",
"living-html/htmlbuttonelement.js",
"living-html/htmlelement.js",
"living-html/htmlinputelement.js",
"living-html/htmloptionelement.js",
Expand Down
2 changes: 2 additions & 0 deletions test/worker-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ self.onmessage = function (e) {
"living-dom/node-parent-element.js": require("../test/living-dom/node-parent-element.js"), // 0/11
"living-dom/non-document-type-child-node.js": require("../test/living-dom/non-document-type-child-node.js"),

"living-html/htmlbuttonelement.js": require("../test/living-html/htmlbuttonelement.js"), // ok
"living-html/htmlelement.js": require("../test/living-html/htmlelement.js"), // ok
"living-html/location.js": require("../test/living-html/location.js"), // ok
"living-html/message-event.js": require("../test/living-html/message-event.js"), // ok
Expand Down Expand Up @@ -81,6 +82,7 @@ self.onmessage = function (e) {
"living-dom/event-target.js",
"living-dom/node-clone-node.js",
"living-dom/non-document-type-child-node.js",
"living-html/htmlbuttonelement.js",
"living-html/location.js",
"living-html/message-event.js",
"living-html/navigator.js",
Expand Down

0 comments on commit 0548f6c

Please sign in to comment.