-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for form-associated custom elements
This is for WICG/webcomponents#187. Specification PR: whatwg/html#4383.
- Loading branch information
1 parent
1de2d96
commit 3ccd79e
Showing
9 changed files
with
927 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
custom-elements/form-associated/ElementInternals-NotSupportedError.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!DOCTYPE html> | ||
<body> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script> | ||
test(() => { | ||
class NotFormAssociatedElement extends HTMLElement {} | ||
customElements.define('my-element1', NotFormAssociatedElement); | ||
const element = new NotFormAssociatedElement(); | ||
const i = element.attachInternals(); | ||
|
||
assert_throws('NotSupportedError', () => i.setFormValue('')); | ||
assert_throws('NotSupportedError', () => i.form); | ||
assert_throws('NotSupportedError', () => i.setValidity({})); | ||
assert_throws('NotSupportedError', () => i.willValidate); | ||
assert_throws('NotSupportedError', () => i.validity); | ||
assert_throws('NotSupportedError', () => i.validationMessage); | ||
assert_throws('NotSupportedError', () => i.checkValidity()); | ||
assert_throws('NotSupportedError', () => i.reportValidity()); | ||
assert_throws('NotSupportedError', () => i.labels); | ||
}, 'Form-related operations and attributes should throw NotSupportedErrors' + | ||
' for non-form-associated custom elements.'); | ||
</script> | ||
</body> |
50 changes: 50 additions & 0 deletions
50
custom-elements/form-associated/ElementInternals-labels.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<!DOCTYPE html> | ||
<title>labels attribute of ElementInternals, and label association</title> | ||
<body> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<div id="container"></div> | ||
<script> | ||
class MyControl extends HTMLElement { | ||
static get formAssociated() { return true; } | ||
|
||
constructor() { | ||
super(); | ||
this.internals_ = this.attachInternals(); | ||
} | ||
get i() { return this.internals_; } | ||
} | ||
customElements.define('my-control', MyControl); | ||
const container = document.querySelector('#container'); | ||
|
||
test(() => { | ||
container.innerHTML = '<label><span><my-control></my-control></span></label>'; | ||
let label = container.querySelector('label'); | ||
let control = container.querySelector('my-control'); | ||
assert_equals(label.control, control); | ||
assert_true(control.i.labels instanceof NodeList); | ||
assert_array_equals(control.i.labels, [label]); | ||
|
||
container.innerHTML = '<label for="mc"></label><form><my-control id="mc"></my-control></form>'; | ||
label = container.querySelector('label'); | ||
control = container.querySelector('my-control'); | ||
assert_equals(label.control, control); | ||
assert_equals(label.form, control.i.form); | ||
assert_array_equals(control.i.labels, [label]); | ||
|
||
container.innerHTML = '<label for="mc"></label><label for="mc"><my-control id="mc">'; | ||
const labels = container.querySelectorAll('label'); | ||
control = container.querySelector('my-control'); | ||
assert_array_equals(control.i.labels, labels); | ||
}, 'LABEL association'); | ||
|
||
test(() => { | ||
container.innerHTML = '<label for="mc"></label><form><my-control id="mc"></my-control></form>'; | ||
const control = container.querySelector('my-control'); | ||
let clickCount = 0; | ||
control.addEventListener('click', e => { ++clickCount; }); | ||
container.querySelector('label').click(); | ||
assert_equals(clickCount, 1); | ||
}, 'LABEL click'); | ||
</script> | ||
</body> |
125 changes: 125 additions & 0 deletions
125
custom-elements/form-associated/ElementInternals-setFormValue.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<!DOCTYPE html> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<div id="container"></div> | ||
<script> | ||
class MyControl extends HTMLElement { | ||
static get formAssociated() { return true; } | ||
|
||
constructor() { | ||
super(); | ||
this.internals_ = this.attachInternals(); | ||
this.value_ = ''; | ||
} | ||
|
||
get value() { | ||
return this.value_; | ||
} | ||
set value(v) { | ||
this.internals_.setFormValue(v); | ||
this.value_ = v; | ||
} | ||
setValues(nameValues) { | ||
const formData = new FormData(); | ||
for (let p of nameValues) { | ||
formData.append(p[0], p[1]); | ||
} | ||
this.internals_.setFormValue(formData); | ||
} | ||
} | ||
customElements.define('my-control', MyControl); | ||
const $ = document.querySelector.bind(document); | ||
|
||
function submitPromise(t) { | ||
return new Promise((resolve, reject) => { | ||
const iframe = $('iframe'); | ||
iframe.onload = () => resolve(iframe.contentWindow.location.search); | ||
iframe.onerror = () => reject(new Error('iframe onerror fired')); | ||
$('form').submit(); | ||
}); | ||
} | ||
|
||
promise_test(t => { | ||
$('#container').innerHTML = '<form action="/common/blank.html" target="if1">' + | ||
'<input name=name-pd1 value="value-pd1">' + | ||
'<my-control></my-control>' + | ||
'</form>' + | ||
'<iframe name="if1"></iframe>'; | ||
return submitPromise(t).then(query => { | ||
assert_equals(query, '?name-pd1=value-pd1'); | ||
}); | ||
}, 'Single value - name is missing'); | ||
|
||
promise_test(t => { | ||
$('#container').innerHTML = '<form action="/common/blank.html" target="if1">' + | ||
'<input name=name-pd1 value="value-pd1">' + | ||
'<my-control name=""></my-control>' + | ||
'<input name=name-pd2 value="value-pd2">' + | ||
'</form>' + | ||
'<iframe name="if1"></iframe>'; | ||
$('my-control').value = 'value-ce1'; | ||
return submitPromise(t).then(query => { | ||
assert_equals(query, '?name-pd1=value-pd1&name-pd2=value-pd2'); | ||
}); | ||
}, 'Single value - empty name exists'); | ||
|
||
promise_test(t => { | ||
$('#container').innerHTML = '<form action="/common/blank.html" target="if1" accept-charset=utf-8>' + | ||
'<input name=name-pd1 value="value-pd1">' + | ||
'<my-control name="name-ce1"></my-control>' + | ||
'<my-control name="name-usv"></my-control>' + | ||
'<my-control name="name-file"></my-control>' + | ||
'</form>' + | ||
'<iframe name="if1"></iframe>'; | ||
const USV_INPUT = 'abc\uDC00\uD800def'; | ||
const USV_OUTPUT = 'abc\uFFFD\uFFFDdef'; | ||
const FILE_NAME = 'test_file.txt'; | ||
$('[name=name-usv]').value = USV_INPUT; | ||
$('[name=name-file]').value = new File(['file content'], FILE_NAME); | ||
return submitPromise(t).then(query => { | ||
assert_equals(query, `?name-pd1=value-pd1&name-ce1=&name-usv=${encodeURIComponent(USV_OUTPUT)}&name-file=${FILE_NAME}`); | ||
}); | ||
}, 'Single value - Non-empty name exists'); | ||
|
||
promise_test(t => { | ||
$('#container').innerHTML = '<form action="/common/blank.html" target="if1">' + | ||
'<input name=name-pd1 value="value-pd1">' + | ||
'<my-control name="name-ce1"></my-control>' + | ||
'<my-control name="name-ce2"></my-control>' + | ||
'</form>' + | ||
'<iframe name="if1"></iframe>'; | ||
$('my-control').value = null; | ||
return submitPromise(t).then(query => { | ||
assert_equals(query, '?name-pd1=value-pd1&name-ce2='); | ||
}); | ||
}, 'Null value should submit nothing'); | ||
|
||
promise_test(t => { | ||
$('#container').innerHTML = '<form action="/common/blank.html" target="if1">' + | ||
'<input name=name-pd1 value="value-pd1">' + | ||
'<my-control name=name-ce1></my-control>' + | ||
'</form>' + | ||
'<iframe name="if1"></iframe>'; | ||
$('my-control').value = 'value-ce1'; | ||
$('my-control').setValues([]); | ||
$('my-control').setValues([['sub1', 'subvalue1'], | ||
['sub2', 'subvalue2'], | ||
['sub2', 'subvalue3']]); | ||
return submitPromise(t).then(query => { | ||
assert_equals(query, '?name-pd1=value-pd1&sub1=subvalue1&sub2=subvalue2&sub2=subvalue3'); | ||
}); | ||
}, 'Multiple values - name content attribute is ignored'); | ||
|
||
promise_test(t => { | ||
$('#container').innerHTML = '<form action="/common/blank.html" target="if1">' + | ||
'<input name=name-pd1 value="value-pd1">' + | ||
'<my-control name=name-ce1></my-control>' + | ||
'</form>' + | ||
'<iframe name="if1"></iframe>'; | ||
$('my-control').value = 'value-ce1'; | ||
$('my-control').setValues([]); | ||
return submitPromise(t).then(query => { | ||
assert_equals(query, '?name-pd1=value-pd1'); | ||
}); | ||
}, 'setFormValue with an empty FormData should submit nothing'); | ||
</script> |
Oops, something went wrong.