blob: fbe594ba7c0ac824f1ab46ff43f0e476c70a747c [file] [log] [blame]
tkent@chromium.org02e12212010-04-20 07:03:06 +00001<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2<html>
3<head>
mark.lam@apple.com93720da2013-09-07 23:31:07 +00004<script src="../../resources/js-test-pre.js"></script>
tkent@chromium.org02e12212010-04-20 07:03:06 +00005</head>
6<body>
7<p id="description"></p>
8<div id="console"></div>
tkent@chromium.orga0139532012-02-15 06:56:00 +00009<script>
10description('HTMLFormElement::checkValidity() with cases that event handlers called by checkValidity() updates DOM structure.')
11
12var parent = document.createElement('div');
13document.body.appendChild(parent);
14
15// ----------------------------------------------------------------
16debug('The target form is removed.');
17parent.innerHTML = '<form id=f1><input name=i id=i required></form>';
18var handler = function(event) {
19 parent.innerHTML = '';
20};
21document.getElementById('i').addEventListener('invalid', handler, false);
22// The specificiation doesn't define the behavior in this case.
23// It's ok if WebKit doesn't crash.
24shouldBeFalse('document.getElementById("f1").checkValidity()');
25
26// ----------------------------------------------------------------
27debug('');
28debug('A control to be checked is removed.');
29parent.innerHTML = '<form id=f1><input name=i1 id=i1 required><input name=i2 id=i2 required></form>';
30var handler1 = function(event) {
31 document.getElementById('f1').removeChild(document.getElementById('i2'));
32};
33document.getElementById('i1').addEventListener('invalid', handler1, false);
34var handler2Called = false;
35var handler2 = function(event) {
36 handler2Called = true;
37};
38document.getElementById('i2').addEventListener('invalid', handler2, false);
39shouldBeFalse('document.getElementById("f1").checkValidity()');
40// If the node was removed from the form, i2.checkValidity() is called, but an
41// invalid event is not fired because it is not in any documents.
42shouldBeFalse('handler2Called');
43
44// ----------------------------------------------------------------
45debug('');
46debug('A new control is added.');
47parent.innerHTML = '<form id=f1><input name=i1 id=i1 required></form>';
48handler2Called = false;
49handler2 = function(event) {
50 handler2Called = true;
51};
52handler1 = function(event) {
53 var input = document.createElement('input');
54 input.name = 'i2';
55 input.required = true;
56 input.addEventListener('invalid', handler2, false);
57 document.getElementById('f1').appendChild(input);
58};
59document.getElementById('i1').addEventListener('invalid', handler1, false);
60shouldBeFalse('document.getElementById("f1").checkValidity()');
61// If a new node is added to the form, checkValidity() doesn't handle it.
62shouldBeFalse('handler2Called');
63
64// ----------------------------------------------------------------
65debug('');
66debug('A control is moved to another form.');
67parent.innerHTML = '<form id=f1><input name=i1 id=i1 required><input name=i2 id=i2 required></form>'
68 + '<form id=f2></form>';
69handler1 = function(event) {
70 document.getElementById('f2').appendChild(document.getElementById('i2'));
71};
72document.getElementById('i1').addEventListener('invalid', handler1, false);
73handler2Called = false;
74handler2 = function(event) {
75 handler2Called = true;
76};
77document.getElementById('i2').addEventListener('invalid', handler2, false);
78shouldBeFalse('document.getElementById("f1").checkValidity()');
79// The moved control is not checked.
80shouldBeFalse('handler2Called');
81
82// ----------------------------------------------------------------
83debug('');
84debug('A control is moved to another document.');
85parent.innerHTML = '<form id=f1><input name=i1 id=i1 required></form>';
86var doc2 = document.implementation.createHTMLDocument();
87handler1 = function(event) {
88 doc2.body.appendChild(doc2.adoptNode(document.getElementById('i1')));
89};
90document.getElementById('i1').addEventListener('invalid', handler1, false);
91// i1 is not listed in 'unhandled invalid controls' because it was moved to
92// another document.
93shouldBeTrue('document.getElementById("f1").checkValidity()');
94
95parent.innerHTML = '';
96</script>
mark.lam@apple.com93720da2013-09-07 23:31:07 +000097<script src="../../resources/js-test-post.js"></script>
tkent@chromium.org02e12212010-04-20 07:03:06 +000098</body>
99</html>