Update web-platform-tests to revision 58eb04cecbbec2e18531ab440225e38944a9c444

This commit is contained in:
Josh Matthews 2017-04-17 12:06:02 +10:00 committed by Anthony Ramine
parent 25e8bf69e6
commit 665817d2a6
35333 changed files with 1818077 additions and 16036 deletions

View file

@ -1,45 +1,84 @@
<!DOCTYPE html>
<meta charset=utf-8>
<meta name="timeout" content="long">
<title>text field selection: select()</title>
<link rel="author" title="Denis Ah-Kang" href="mailto:denis@w3.org">
<link rel="author" title="Domenic Denicola" href="mailto:d@domenic.me">
<link rel=help href="https://html.spec.whatwg.org/multipage/#textFieldSelection">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<textarea>foobar</textarea>
<input type="text" value="foobar">
<input type="search" value="foobar">
<input type="tel" value="1234">
<input type="url" value="https://example.com/">
<input type="password" value="hunter2">
<script>
var input_types = ["text", "search", "tel", "url", "password"],
t1 = async_test("select() on textarea queues select event"),
q1 = false;
"use strict";
input_types.forEach(function(type) {
var input = document.createElement("input"),
t = async_test("select() on input type " + type + " queues select event"),
q = false;
t.step(function() {
input.type = type;
input.value = "foobar";
document.body.appendChild(input);
input.onselect = t.step_func_done(function(e) {
assert_true(q, "event should be queued");
assert_true(e.isTrusted, "event is trusted");
assert_true(e.bubbles, "event bubbles");
assert_false(e.cancelable, "event is not cancelable");
const els = [document.querySelector("textarea"), ...document.querySelectorAll("input")];
const actions = [
{
label: "select()",
action: el => el.select()
},
{
label: "selectionStart",
action: el => el.selectionStart = 1
},
{
label: "selectionEnd",
action: el => el.selectionEnd = el.value.length - 1
},
{
label: "selectionDirection",
action: el => el.selectionDirection = "backward"
},
{
label: "setSelectionRange()",
action: el => el.setSelectionRange(1, el.value.length - 1) // changes direction implicitly to none/forward
},
{
label: "setRangeText()",
action: el => el.setRangeText("newmiddle")
}
];
for (const el of els) {
const elLabel = el.localName === "textarea" ? "textarea" : "input type " + el.type;
for (const action of actions) {
// promise_test instead of async_test is important because these need to happen in sequence (to test that events
// fire if and only if the selection changes).
promise_test(t => {
const watcher = new EventWatcher(t, el, "select");
const promise = watcher.wait_for("select").then(e => {
assert_true(e.isTrusted, "isTrusted must be true");
assert_true(e.bubbles, "bubbles must be true");
assert_false(e.cancelable, "cancelable must be false");
});
input.select();
q=true;
});
});
document.querySelector("textarea").onselect = t1.step_func_done(function(e) {
assert_true(q1, "event should be queued");
assert_true(e.isTrusted, "event is trusted");
assert_true(e.bubbles, "event bubbles");
assert_false(e.cancelable, "event is not cancelable");
});
action.action(el);
t1.step(function() {
document.querySelector("textarea").select();
q1=true;
});
return promise;
}, `${elLabel}: ${action.label}`);
promise_test(t => {
el.onselect = t.unreached_func("the select event must not fire the second time");
action.action(el);
return new Promise(resolve => {
t.step_timeout(() => {
el.onselect = null;
resolve();
}, 200);
});
}, `${elLabel}: ${action.label} a second time (must not fire select)`);
}
}
</script>

View file

@ -14,14 +14,17 @@
test(function(){
var el = document.createElement("input");
el.type = type;
assert_equals(el.selectionStart, null);
assert_equals(el.selectionEnd, null);
assert_equals(el.selectionDirection, null);
assert_throws("InvalidStateError", function(){
el.selectionStart;
el.selectionStart = 0;
});
assert_throws("InvalidStateError", function(){
el.selectionEnd;
el.selectionEnd = 0;
});
assert_throws("InvalidStateError", function(){
el.selectionDirection;
el.selectionDirection = 'none';
});
assert_throws("InvalidStateError", function(){
el.setRangeText("foobar");

View file

@ -0,0 +1,148 @@
<!doctype html>
<meta charset=utf-8>
<title></title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div id=log></div>
<script>
function createInputElement(value, append, suffix) {
var el = document.createElement("input");
el.type = "text";
el.value = value;
el.id = "input" + (append ? "-appended" : "-not-appended") + (suffix ? suffix : "");
if (append) {
document.body.appendChild(el);
}
return el;
};
function createTextareaElement(value, append, suffix) {
var el = document.createElement("textarea");
el.value = value;
el.id = "textarea" + (append ? "-appended" : "-not-appended") + (suffix ? suffix : "");
if (append) {
document.body.appendChild(el);
}
return el;
};
function createPrefocusedInputElement(value, append) {
var el = createInputElement(value, append, "-prefocused");
el.focus();
el.blur();
return el;
}
function createPrefocusedTextareaElement(value, append) {
var el = createTextareaElement(value, append, "-prefocused");
el.focus();
el.blur();
return el;
}
function createTestElements(value) {
return [ createInputElement(value, true),
createInputElement(value, false),
createPrefocusedInputElement(value, true),
createPrefocusedInputElement(value, false),
createTextareaElement(value, true),
createTextareaElement(value, false),
createPrefocusedTextareaElement(value, true),
createPrefocusedTextareaElement(value, false),
];
}
var testValue = "abcdefghij";
test(function() {
assert_equals(testValue.length, 10);
}, "Sanity check for testValue length; if this fails, variou absolute offsets in the test below need to be adjusted to be less than testValue.length");
test(function() {
for (let el of createTestElements(testValue)) {
assert_equals(el.selectionStart, testValue.length,
`Initial .value set on ${el.id} should set selectionStart to end of value`);
var t = async_test(`onselect should fire when selectionStart is changed on ${el.id}`);
el.onselect = t.step_func_done(function(e) {
assert_equals(e.type, "select");
el.remove();
});
el.selectionStart = 2;
}
}, "onselect should fire when selectionStart is changed");
test(function() {
for (let el of createTestElements(testValue)) {
assert_equals(el.selectionEnd, testValue.length,
`Initial .value set on ${el.id} should set selectionEnd to end of value`);
var t = async_test(`onselect should fire when selectionEnd is changed on ${el.id}`);
el.onselect = t.step_func_done(function(e) {
assert_equals(e.type, "select");
el.remove();
});
el.selectionEnd = 2;
}
}, "onselect should fire when selectionEnd is changed");
test(function() {
for (let el of createTestElements(testValue)) {
assert_equals(el.selectionStart, testValue.length,
`Initial .value set on ${el.id} should set selectionStart to end of value`);
el.selectionStart = 0;
el.selectionEnd = 5;
el.selectionStart = 8;
assert_equals(el.selectionStart, 8, `selectionStart on ${el.id}`);
assert_equals(el.selectionEnd, 8, `selectionEnd on ${el.id}`);
el.remove();
}
}, "Setting selectionStart to a value larger than selectionEnd should increase selectionEnd");
test(function() {
for (let el of createTestElements(testValue)) {
assert_equals(el.selectionStart, testValue.length,
`Initial .value set on ${el.id} should set selectionStart to end of value`);
assert_equals(el.selectionEnd, testValue.length,
`Initial .value set on ${el.id} should set selectionEnd to end of value`);
el.selectionStart = 8;
el.selectionEnd = 5;
assert_equals(el.selectionStart, 5, `selectionStart on ${el.id}`);
assert_equals(el.selectionEnd, 5, `selectionEnd on ${el.id}`);
el.remove();
}
}, "Setting selectionEnd to a value smaller than selectionStart should decrease selectionStart");
test(function() {
for (let el of createTestElements(testValue)) {
el.selectionStart = 0;
assert_equals(el.selectionStart, 0, `We just set it on ${el.id}`);
el.selectionStart = -1;
assert_equals(el.selectionStart, testValue.length,
`selectionStart setter on ${el.id} should convert -1 to 2^32-1`);
el.selectionStart = Math.pow(2, 32);
assert_equals(el.selectionStart, 0,
`selectionStart setter on ${el.id} should convert 2^32 to 0`);
el.selectionStart = Math.pow(2, 32) - 1;
assert_equals(el.selectionStart, testValue.length,
`selectionStart setter on ${el.id} should leave 2^32-1 as-is`);
el.remove();
}
}, "selectionStart edge-case values");
test(function() {
for (let el of createTestElements(testValue)) {
el.selectionEnd = 0;
assert_equals(el.selectionEnd, 0, `We just set it on ${el.id}`);
el.selectionEnd = -1;
assert_equals(el.selectionEnd, testValue.length,
`selectionEnd setter on ${el.id} should convert -1 to 2^32-1`);
el.selectionEnd = Math.pow(2, 32);
assert_equals(el.selectionEnd, 0,
`selectionEnd setter on ${el.id} should convert 2^32 to 0`);
el.selectionEnd = Math.pow(2, 32) - 1;
assert_equals(el.selectionEnd, testValue.length,
`selectionEnd setter on ${el.id} should leave 2^32-1 as-is`);
el.remove();
}
}, "selectionEnd edge-case values");
</script>

View file

@ -0,0 +1,95 @@
<!doctype html>
<meta charset=utf-8>
<title></title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div id=log></div>
<div id=target></div>
<script>
var target = document.getElementById("target");
var sometext = "something";
var shorttext = "abc";
var elemData = [
{
desc: "textarea not in body",
factory: () => document.createElement("textarea"),
},
{
desc: "input not in body",
factory: () => document.createElement("input"),
},
{
desc: "textarea in body",
factory: () => document.body.appendChild(document.createElement("textarea")),
},
{
desc: "input in body",
factory: () => document.body.appendChild(document.createElement("input")),
},
{
desc: "textarea in body with parsed default value",
factory: () => {
target.innerHTML = "<textarea>abcdefghij</textarea>"
return target.querySelector("textarea");
},
},
{
desc: "input in body with parsed default value",
factory: () => {
target.innerHTML = "<input value='abcdefghij'>"
return target.querySelector("input");
},
},
{
desc: "focused textarea",
factory: () => {
var t = document.body.appendChild(document.createElement("textarea"));
t.focus();
return t;
},
},
{
desc: "focused input",
factory: () => {
var i = document.body.appendChild(document.createElement("input"));
i.focus();
return i;
},
},
{
desc: "focused then blurred textarea",
factory: () => {
var t = document.body.appendChild(document.createElement("textarea"));
t.focus();
t.blur();
return t;
},
},
{
desc: "focused then blurred input",
factory: () => {
var i = document.body.appendChild(document.createElement("input"));
i.focus();
i.blur()
return i;
},
},
];
for (var data of elemData) {
test(function() {
var el = data.factory();
this.add_cleanup(() => el.remove());
el.defaultValue = sometext;
assert_true(sometext.length > 8,
"sometext too short, test won't work right");
el.selectionStart = 4;
el.selectionEnd = 6;
el.setRangeText("xyz");
el.defaultValue = "set range text";
assert_equals(el.value, sometext.slice(0, 4) + "xyz" + sometext.slice(6),
"Calling setRangeText should set the value dirty flag");
}, `value dirty flag behavior after setRangeText on ${data.desc}`);
}
</script>

View file

@ -14,18 +14,24 @@
var dirs = ['forward', 'backward', 'none'];
var sampleText = "0123456789";
var createInputElement = function(value) {
var createInputElement = function(value, append = true) {
var el = document.createElement("input");
el.type = "text";
el.value = value;
body.appendChild(el);
el.id = "input" + (append ? "-appended" : "-not-appended");
if (append) {
body.appendChild(el);
}
return el;
};
var createTextareaElement = function(value) {
var createTextareaElement = function(value, append = true) {
var el = document.createElement("textarea");
el.value = value;
body.appendChild(el);
el.id = "textarea" + (append ? "-appended" : "-not-appended");
if (append) {
body.appendChild(el);
}
return el;
};
@ -82,49 +88,74 @@
}, "test if non-ascii selection text is correct for textarea");
test(function() {
var el = createInputElement(sampleText);
// If there is no selection, then it must return the offset(in logical order)
// to the character that immediately follows the text entry cursor.
assert_equals(el.selectionStart, el.value.length, "SelectionStart offset without selection");
el.select();
assert_equals(el.selectionStart, 0, "SelectionStart offset");
el.parentNode.removeChild(el);
}, "test SelectionStart offset for input");
for (var append of [true, false]) {
test(function() {
var el = createInputElement(sampleText, append);
// If there is no selection, then it must return the offset(in logical order)
// to the character that immediately follows the text entry cursor.
assert_equals(el.selectionStart, el.value.length,
"SelectionStart offset without selection in " + el.id);
if (!el.parentNode) {
return;
}
el.select();
assert_equals(el.selectionStart, 0, "SelectionStart offset");
el.parentNode.removeChild(el);
}, "test SelectionStart offset for input that is " +
(append ? "appended" : " not appended"));
}
for (var append of [true, false]) {
test(function() {
var el = createTextareaElement(sampleText, append);
// If there is no selection, then it must return the offset(in logical order)
// to the character that immediately follows the text entry cursor.
assert_equals(el.selectionStart, el.value.length,
"SelectionStart offset without selection in " + el.id);
if (!el.parentNode) {
return;
}
el.select();
assert_equals(el.selectionStart, 0, "SelectionStart offset");
el.parentNode.removeChild(el);
}, "test SelectionStart offset for textarea that is " +
(append ? "appended" : " not appended"));
}
for (var append of [true, false]) {
test(function() {
var el = createInputElement(sampleText, append);
// If there is no selection, then it must return the offset(in logical order)
// to the character that immediately follows the text entry cursor.
assert_equals(el.selectionEnd, el.value.length,
"SelectionEnd offset without selection in " + el.id);
if (!el.parentNode) {
return;
}
el.select();
assert_equals(el.selectionEnd, el.value.length, "SelectionEnd offset");
el.parentNode.removeChild(el);
}, "test SelectionEnd offset for input that is " +
(append ? "appended" : " not appended"));
}
test(function() {
var el = createTextareaElement(sampleText);
// If there is no selection, then it must return the offset(in logical order)
// to the character that immediately follows the text entry cursor.
assert_equals(el.selectionStart, el.value.length, "SelectionStart offset without selection");
el.select();
assert_equals(el.selectionStart, 0, "SelectionStart offset");
el.parentNode.removeChild(el);
}, "test SelectionStart offset for textarea");
test(function() {
var el = createInputElement(sampleText);
// If there is no selection, then it must return the offset(in logical order)
// to the character that immediately follows the text entry cursor.
assert_equals(el.selectionEnd, el.value.length, "SelectionEnd offset without selection");
el.select();
assert_equals(el.selectionEnd, el.value.length, "SelectionEnd offset");
el.parentNode.removeChild(el);
}, "test SelectionEnd offset for input");
test(function() {
var el = createTextareaElement(sampleText);
// If there is no selection, then it must return the offset(in logical order)
// to the character that immediately follows the text entry cursor.
assert_equals(el.selectionEnd, el.value.length, "SelectionEnd offset without selection");
el.select();
assert_equals(el.selectionEnd, el.value.length, "SelectionEnd offset");
el.parentNode.removeChild(el);
}, "test SelectionEnd offset for textarea");
for (var append of [true, false]) {
test(function() {
var el = createTextareaElement(sampleText, append);
// If there is no selection, then it must return the offset(in logical order)
// to the character that immediately follows the text entry cursor.
assert_equals(el.selectionEnd, el.value.length,
"SelectionEnd offset without selection in " + el.id);
if (!el.parentNode) {
return;
}
el.select();
assert_equals(el.selectionEnd, el.value.length, "SelectionEnd offset");
el.parentNode.removeChild(el);
}, "test SelectionEnd offset for textarea that is " +
(append ? "appended" : " not appended"));
}
test(function() {
var el = createInputElement(sampleText);

View file

@ -106,15 +106,24 @@
}, element.id + " setRangeText without argument throws a type error");
async_test(function() {
var q = false;
element.onselect = this.step_func_done(function(e) {
assert_true(q, "event should be queued");
assert_true(e.isTrusted, "event is trusted");
assert_true(e.bubbles, "event bubbles");
assert_false(e.cancelable, "event is not cancelable");
});
element.setRangeText("foobar2", 0, 6);
q = true;
// At this point there are already "select" events queued up on
// "element". Give them time to fire; otherwise we can get spurious
// passes.
//
// This is unfortunately racy in that we might _still_ get spurious
// passes. I'm not sure how best to handle that.
setTimeout(this.step_func(function() {
var q = false;
element.onselect = this.step_func_done(function(e) {
assert_true(q, "event should be queued");
assert_true(e.isTrusted, "event is trusted");
assert_true(e.bubbles, "event bubbles");
assert_false(e.cancelable, "event is not cancelable");
});
element.setRangeText("foobar2", 0, 6);
q = true;
}), 10);
}, element.id + " setRangeText fires a select event");
})
</script>

View file

@ -138,6 +138,22 @@ test(function() {
assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
},'input setSelectionRange(undefined,1)');
test(function() {
input.setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1);
assert_equals(input.selectionStart, input.value.length,
"element.selectionStart should be value.length");
assert_equals(input.selectionEnd, input.value.length,
"element.selectionEnd should be value.length");
}, 'input setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1)');
test(function() {
input.setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1);
assert_equals(input.selectionStart, input.value.length,
"element.selectionStart should be value.length");
assert_equals(input.selectionEnd, input.value.length,
"element.selectionEnd should be value.length");
}, 'input setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1)');
},"test of input.setSelectionRange");
async_test(function() {
@ -257,18 +273,21 @@ test(function() {
assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
},'textarea setSelectionRange(undefined,1)');
},"test of textarea.setSelectionRange");
async_test(function() {
var q = false;
var textarea = document.getElementById("b");
textarea.addEventListener("select", this.step_func_done(function(e) {
assert_true(q, "event should be queued");
assert_true(e.isTrusted, "event is trusted");
assert_true(e.bubbles, "event bubbles");
assert_false(e.cancelable, "event is not cancelable");
}));
textarea.setSelectionRange(0, 1);
q = true;
}, "textarea setSelectionRange fires a select event");
test(function() {
textarea.setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1);
assert_equals(textarea.selectionStart, textarea.value.length,
"element.selectionStart should be value.length");
assert_equals(textarea.selectionEnd, textarea.value.length,
"element.selectionEnd should be value.length");
}, 'textarea setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1)');
test(function() {
textarea.setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1);
assert_equals(textarea.selectionStart, textarea.value.length,
"element.selectionStart should be value.length");
assert_equals(textarea.selectionEnd, textarea.value.length,
"element.selectionEnd should be value.length");
}, 'textarea setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1)');
},"test of textarea.setSelectionRange");
</script>

View file

@ -96,4 +96,22 @@ test(function () {
}, "Setting element by index should correctly append and replace elements");
test(function () {
var selection = document.createElementNS("http://www.w3.org/1999/xhtml", "foo:select");
selection.length = 5;
assert_equals(selection.length, 5,
"Number of nodes in collection should have changed");
for (var i = 0; i < 5; ++i) {
var child = selection.children[i];
assert_equals(child.localName, "option",
"new child should be an option");
assert_equals(child.namespaceURI, "http://www.w3.org/1999/xhtml",
"new child should be an HTML element");
assert_equals(child.prefix, null,
"new child should not copy select's prefix");
}
}, "Changing the length adds new nodes; The new nodes will not copy select's prefix");
</script>