DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5f2e62175414)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
<!DOCTYPE html>
<html>
<head>
<title>Shadow DOM: slotchange event</title>
<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
<meta name="author" title="Hayato Ito" href="mailto:hayato@google.com">
<link rel="help" href="https://dom.spec.whatwg.org/#signaling-slot-change">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<div id="log"></div>
<script>
function treeName(mode, connectedToDocument)
{
    return (mode == 'open' ? 'an ' : 'a ') + mode + ' shadow root '
        + (connectedToDocument ? '' : ' not') + ' in a document';
}

function testAppendingSpanToShadowRootWithDefaultSlot(mode, connectedToDocument)
{
    var test = async_test('slotchange event must fire on a default slot element inside '
        + treeName(mode, connectedToDocument));

    var host;
    var slot;
    var eventCount = 0;

    test.step(function () {
        host = document.createElement('div');
        if (connectedToDocument)
            document.body.appendChild(host);

        var shadowRoot = host.attachShadow({'mode': mode});
        slot = document.createElement('slot');

        slot.addEventListener('slotchange', function (event) {
            if (event.isFakeEvent)
                return;

            test.step(function () {
                assert_equals(event.type, 'slotchange', 'slotchange event\'s type must be "slotchange"');
                assert_equals(event.target, slot, 'slotchange event\'s target must be the slot element');
                assert_equals(event.relatedTarget, undefined, 'slotchange must not set relatedTarget');
            });
            eventCount++;
        });

        shadowRoot.appendChild(slot);

        host.appendChild(document.createElement('span'));
        host.appendChild(document.createElement('b'));

        assert_equals(eventCount, 0, 'slotchange event must not be fired synchronously');
    });

    setTimeout(function () {
        test.step(function () {
            assert_equals(eventCount, 1, 'slotchange must be fired exactly once after the assigned nodes changed');

            host.appendChild(document.createElement('i'));
        });

        setTimeout(function () {
            test.step(function () {
                assert_equals(eventCount, 2, 'slotchange must be fired exactly once after the assigned nodes changed');

                host.appendChild(document.createTextNode('hello'));

                var fakeEvent = new Event('slotchange');
                fakeEvent.isFakeEvent = true;
                slot.dispatchEvent(fakeEvent);
            });

            setTimeout(function () {
                test.step(function () {
                    assert_equals(eventCount, 3, 'slotchange must be fired exactly once after the assigned nodes changed'
                        + ' event if there was a synthetic slotchange event fired');
                });
                test.done();
            }, 1);
        }, 1);
    }, 1);
}

testAppendingSpanToShadowRootWithDefaultSlot('open', true);
testAppendingSpanToShadowRootWithDefaultSlot('closed', true);
testAppendingSpanToShadowRootWithDefaultSlot('open', false);
testAppendingSpanToShadowRootWithDefaultSlot('closed', false);

function testAppendingSpanToShadowRootWithNamedSlot(mode, connectedToDocument)
{
    var test = async_test('slotchange event must fire on a named slot element inside'
        + treeName(mode, connectedToDocument));

    var host;
    var slot;
    var eventCount = 0;

    test.step(function () {
        host = document.createElement('div');
        if (connectedToDocument)
            document.body.appendChild(host);

        var shadowRoot = host.attachShadow({'mode': mode});
        slot = document.createElement('slot');
        slot.name = 'someSlot';

        slot.addEventListener('slotchange', function (event) {
            if (event.isFakeEvent)
                return;

            test.step(function () {
                assert_equals(event.type, 'slotchange', 'slotchange event\'s type must be "slotchange"');
                assert_equals(event.target, slot, 'slotchange event\'s target must be the slot element');
                assert_equals(event.relatedTarget, undefined, 'slotchange must not set relatedTarget');
            });
            eventCount++;
        });

        shadowRoot.appendChild(slot);

        var span = document.createElement('span');
        span.slot = 'someSlot';
        host.appendChild(span);

        var b = document.createElement('b');
        b.slot = 'someSlot';
        host.appendChild(b);

        assert_equals(eventCount, 0, 'slotchange event must not be fired synchronously');
    });

    setTimeout(function () {
        test.step(function () {
            assert_equals(eventCount, 1, 'slotchange must be fired exactly once after the assigned nodes changed');

            var i = document.createElement('i');
            i.slot = 'someSlot';
            host.appendChild(i);
        });

        setTimeout(function () {
            test.step(function () {
                assert_equals(eventCount, 2, 'slotchange must be fired exactly once after the assigned nodes changed');

                var em = document.createElement('em');
                em.slot = 'someSlot';
                host.appendChild(em);

                var fakeEvent = new Event('slotchange');
                fakeEvent.isFakeEvent = true;
                slot.dispatchEvent(fakeEvent);
            });

            setTimeout(function () {
                test.step(function () {
                    assert_equals(eventCount, 3, 'slotchange must be fired exactly once after the assigned nodes changed'
                        + ' event if there was a synthetic slotchange event fired');
                });
                test.done();
            }, 1);

        }, 1);
    }, 1);
}

testAppendingSpanToShadowRootWithNamedSlot('open', true);
testAppendingSpanToShadowRootWithNamedSlot('closed', true);
testAppendingSpanToShadowRootWithNamedSlot('open', false);
testAppendingSpanToShadowRootWithNamedSlot('closed', false);

function testSlotchangeDoesNotFireWhenOtherSlotsChange(mode, connectedToDocument)
{
    var test = async_test('slotchange event must not fire on a slot element inside '
        + treeName(mode, connectedToDocument)
        + ' when another slot\'s assigned nodes change');

    var host;
    var defaultSlotEventCount = 0;
    var namedSlotEventCount = 0;

    test.step(function () {
        host = document.createElement('div');
        if (connectedToDocument)
            document.body.appendChild(host);

        var shadowRoot = host.attachShadow({'mode': mode});
        var defaultSlot = document.createElement('slot');
        defaultSlot.addEventListener('slotchange', function (event) {
            test.step(function () {
                assert_equals(event.target, defaultSlot, 'slotchange event\'s target must be the slot element');
            });
            defaultSlotEventCount++;
        });

        var namedSlot = document.createElement('slot');
        namedSlot.name = 'slotName';
        namedSlot.addEventListener('slotchange', function (event) {
            test.step(function () {
                assert_equals(event.target, namedSlot, 'slotchange event\'s target must be the slot element');
            });
            namedSlotEventCount++;
        });

        shadowRoot.appendChild(defaultSlot);
        shadowRoot.appendChild(namedSlot);

        host.appendChild(document.createElement('span'));

        assert_equals(defaultSlotEventCount, 0, 'slotchange event must not be fired synchronously');
        assert_equals(namedSlotEventCount, 0, 'slotchange event must not be fired synchronously');
    });

    setTimeout(function () {
        test.step(function () {
            assert_equals(defaultSlotEventCount, 1,
                'slotchange must be fired exactly once after the assigned nodes change on a default slot');
            assert_equals(namedSlotEventCount, 0,
                'slotchange must not be fired on a named slot after the assigned nodes change on a default slot');

            var span = document.createElement('span');
            span.slot = 'slotName';
            host.appendChild(span);
        });

        setTimeout(function () {
            test.step(function () {
                assert_equals(defaultSlotEventCount, 1,
                    'slotchange must not be fired on a default slot after the assigned nodes change on a named slot');
                assert_equals(namedSlotEventCount, 1,
                    'slotchange must be fired exactly once after the assigned nodes change on a default slot');
            });
            test.done();
        }, 1);
    }, 1);
}

testSlotchangeDoesNotFireWhenOtherSlotsChange('open', true);
testSlotchangeDoesNotFireWhenOtherSlotsChange('closed', true);
testSlotchangeDoesNotFireWhenOtherSlotsChange('open', false);
testSlotchangeDoesNotFireWhenOtherSlotsChange('closed', false);

function testSlotchangeDoesFireAtInsertedAndDoesNotFireForMutationAfterRemoved(mode, connectedToDocument)
{
    var test = async_test('slotchange event must fire on a slot element when a shadow host has a slotable and the slot was inserted'
                        + ' and must not fire when the shadow host was mutated after the slot was removed inside '
                        + treeName(mode, connectedToDocument));

    var host;
    var slot;
    var eventCount = 0;

    test.step(function () {
        host = document.createElement('div');
        if (connectedToDocument)
            document.body.appendChild(host);

        var shadowRoot = host.attachShadow({'mode': mode});
        slot = document.createElement('slot');
        slot.addEventListener('slotchange', function (event) {
            test.step(function () {
                assert_equals(event.target, slot, 'slotchange event\'s target must be the slot element');
            });
            eventCount++;
        });

        host.appendChild(document.createElement('span'));
        shadowRoot.appendChild(slot);

        assert_equals(eventCount, 0, 'slotchange event must not be fired synchronously');
    });

    setTimeout(function () {
        test.step(function () {
            assert_equals(eventCount, 1,
                          'slotchange must be fired on a slot element if there is assigned nodes when the slot was inserted');
            host.removeChild(host.firstChild);
        });

        setTimeout(function () {
            test.step(function () {
                assert_equals(eventCount, 2,
                              'slotchange must be fired after the assigned nodes change on a slot while the slot element was in the tree');
                slot.parentNode.removeChild(slot);
                host.appendChild(document.createElement('span'));
            });

            setTimeout(function () {
                assert_equals(eventCount, 2,
                              'slotchange must not be fired on a slot element if the assigned nodes changed after the slot was removed');
                test.done();
            }, 1);
        }, 1);
    }, 1);
}

testSlotchangeDoesFireAtInsertedAndDoesNotFireForMutationAfterRemoved('open', true);
testSlotchangeDoesFireAtInsertedAndDoesNotFireForMutationAfterRemoved('closed', true);
testSlotchangeDoesFireAtInsertedAndDoesNotFireForMutationAfterRemoved('open', false);
testSlotchangeDoesFireAtInsertedAndDoesNotFireForMutationAfterRemoved('closed', false);

function testSlotchangeFiresOnTransientlyPresentSlot(mode, connectedToDocument)
{
    var test = async_test('slotchange event must fire on a slot element inside '
        + treeName(mode, connectedToDocument)
        + ' even if the slot was removed immediately after the assigned nodes were mutated');

    var host;
    var slot;
    var anotherSlot;
    var slotEventCount = 0;
    var anotherSlotEventCount = 0;

    test.step(function () {
        host = document.createElement('div');
        if (connectedToDocument)
            document.body.appendChild(host);

        var shadowRoot = host.attachShadow({'mode': mode});
        slot = document.createElement('slot');
        slot.name = 'someSlot';
        slot.addEventListener('slotchange', function (event) {
            test.step(function () {
                assert_equals(event.target, slot, 'slotchange event\'s target must be the slot element');
            });
            slotEventCount++;
        });

        anotherSlot = document.createElement('slot');
        anotherSlot.name = 'someSlot';
        anotherSlot.addEventListener('slotchange', function (event) {
            test.step(function () {
                assert_equals(event.target, anotherSlot, 'slotchange event\'s target must be the slot element');
            });
            anotherSlotEventCount++;
        });

        shadowRoot.appendChild(slot);

        var span = document.createElement('span');
        span.slot = 'someSlot';
        host.appendChild(span);

        shadowRoot.removeChild(slot);
        shadowRoot.appendChild(anotherSlot);

        var span = document.createElement('span');
        span.slot = 'someSlot';
        host.appendChild(span);

        shadowRoot.removeChild(anotherSlot);

        assert_equals(slotEventCount, 0, 'slotchange event must not be fired synchronously');
        assert_equals(anotherSlotEventCount, 0, 'slotchange event must not be fired synchronously');
    });

    setTimeout(function () {
        test.step(function () {
            assert_equals(slotEventCount, 1,
                'slotchange must be fired on a slot element if the assigned nodes changed while the slot was present');
            assert_equals(anotherSlotEventCount, 1,
                'slotchange must be fired on a slot element if the assigned nodes changed while the slot was present');
        });
        test.done();
    }, 1);
}

testSlotchangeFiresOnTransientlyPresentSlot('open', true);
testSlotchangeFiresOnTransientlyPresentSlot('closed', true);
testSlotchangeFiresOnTransientlyPresentSlot('open', false);
testSlotchangeFiresOnTransientlyPresentSlot('closed', false);

function testSlotchangeFiresOnInnerHTML(mode, connectedToDocument)
{
    var test = async_test('slotchange event must fire on a slot element inside '
        + treeName(mode, connectedToDocument)
        + ' when innerHTML modifies the children of the shadow host');

    var host;
    var defaultSlot;
    var namedSlot;
    var defaultSlotEventCount = 0;
    var namedSlotEventCount = 0;

    test.step(function () {
        host = document.createElement('div');
        if (connectedToDocument)
            document.body.appendChild(host);

        var shadowRoot = host.attachShadow({'mode': mode});
        defaultSlot = document.createElement('slot');
        defaultSlot.addEventListener('slotchange', function (event) {
            test.step(function () {
                assert_equals(event.target, defaultSlot, 'slotchange event\'s target must be the slot element');
            });
            defaultSlotEventCount++;
        });

        namedSlot = document.createElement('slot');
        namedSlot.name = 'someSlot';
        namedSlot.addEventListener('slotchange', function (event) {
            test.step(function () {
                assert_equals(event.target, namedSlot, 'slotchange event\'s target must be the slot element');
            });
            namedSlotEventCount++;
        });
        shadowRoot.appendChild(namedSlot);
        shadowRoot.appendChild(defaultSlot);
        host.innerHTML = 'foo <b>bar</b>';

        assert_equals(defaultSlotEventCount, 0, 'slotchange event must not be fired synchronously');
        assert_equals(namedSlotEventCount, 0, 'slotchange event must not be fired synchronously');
    });

    setTimeout(function () {
        test.step(function () {
            assert_equals(defaultSlotEventCount, 1,
                'slotchange must be fired on a slot element if the assigned nodes are changed by innerHTML');
            assert_equals(namedSlotEventCount, 0,
                'slotchange must not be fired on a slot element if the assigned nodes are not changed by innerHTML');
            host.innerHTML = 'baz';
        });
        setTimeout(function () {
            test.step(function () {
                assert_equals(defaultSlotEventCount, 2,
                    'slotchange must be fired on a slot element if the assigned nodes are changed by innerHTML');
                assert_equals(namedSlotEventCount, 0,
                    'slotchange must not be fired on a slot element if the assigned nodes are not changed by innerHTML');
                host.innerHTML = '';
            });
            setTimeout(function () {
                test.step(function () {
                    assert_equals(defaultSlotEventCount, 3,
                        'slotchange must be fired on a slot element if the assigned nodes are changed by innerHTML');
                    assert_equals(namedSlotEventCount, 0,
                        'slotchange must not be fired on a slot element if the assigned nodes are not changed by innerHTML');
                    host.innerHTML = '<b slot="someSlot">content</b>';
                });
                setTimeout(function () {
                    test.step(function () {
                        assert_equals(defaultSlotEventCount, 3,
                            'slotchange must not be fired on a slot element if the assigned nodes are not changed by innerHTML');
                        assert_equals(namedSlotEventCount, 1,
                            'slotchange must not be fired on a slot element if the assigned nodes are changed by innerHTML');
                        host.innerHTML = '';
                    });
                    setTimeout(function () {
                        test.step(function () {
                            // FIXME: This test would fail in the current implementation because we can't tell
                            // whether a text node was removed in AllChildrenRemoved or not.
//                            assert_equals(defaultSlotEventCount, 3,
//                                'slotchange must not be fired on a slot element if the assigned nodes are not changed by innerHTML');
                            assert_equals(namedSlotEventCount, 2,
                                'slotchange must not be fired on a slot element if the assigned nodes are changed by innerHTML');
                        });
                        test.done();
                    }, 1);
                }, 1);
            }, 1);
        }, 1);
    }, 1);
}

testSlotchangeFiresOnInnerHTML('open', true);
testSlotchangeFiresOnInnerHTML('closed', true);
testSlotchangeFiresOnInnerHTML('open', false);
testSlotchangeFiresOnInnerHTML('closed', false);

function testSlotchangeFiresWhenNestedSlotChange(mode, connectedToDocument)
{
    var test = async_test('slotchange event must fire on a slot element inside '
        + treeName(mode, connectedToDocument)
        + ' when nested slots\'s contents change');

    var outerHost;
    var innerHost;
    var outerSlot;
    var innerSlot;
    var outerSlotEventCount = 0;
    var innerSlotEventCount = 0;

    test.step(function () {
        outerHost = document.createElement('div');
        if (connectedToDocument)
            document.body.appendChild(outerHost);

        var outerShadow = outerHost.attachShadow({'mode': mode});
        outerShadow.appendChild(document.createElement('span'));
        outerSlot = document.createElement('slot');
        outerSlot.addEventListener('slotchange', function (event) {
            test.step(function () {
                assert_equals(event.target, outerSlot, 'slotchange event\'s target must be the slot element');
            });
            outerSlotEventCount++;
        });

        innerHost = document.createElement('div');
        innerHost.appendChild(outerSlot);
        outerShadow.appendChild(innerHost);

        var innerShadow = innerHost.attachShadow({'mode': mode});
        innerShadow.appendChild(document.createElement('span'));
        innerSlot = document.createElement('slot');
        innerSlot.addEventListener('slotchange', function (event) {
            event.stopPropagation();
            test.step(function () {
                if (innerSlotEventCount === 0) {
                    assert_equals(event.target, innerSlot, 'slotchange event\'s target must be the inner slot element at 1st slotchange');
                } else if (innerSlotEventCount === 1) {
                    assert_equals(event.target, outerSlot, 'slotchange event\'s target must be the outer slot element at 2nd sltochange');
                }
            });
            innerSlotEventCount++;
        });
        innerShadow.appendChild(innerSlot);

        outerHost.appendChild(document.createElement('span'));

        assert_equals(innerSlotEventCount, 0, 'slotchange event must not be fired synchronously');
        assert_equals(outerSlotEventCount, 0, 'slotchange event must not be fired synchronously');
    });

    setTimeout(function () {
        test.step(function () {
            assert_equals(outerSlotEventCount, 1,
                'slotchange must be fired on a slot element if the assigned nodes changed');
            assert_equals(innerSlotEventCount, 2,
                'slotchange must be fired on a slot element and must bubble');
        });
        test.done();
    }, 1);
}

testSlotchangeFiresWhenNestedSlotChange('open', true);
testSlotchangeFiresWhenNestedSlotChange('closed', true);
testSlotchangeFiresWhenNestedSlotChange('open', false);
testSlotchangeFiresWhenNestedSlotChange('closed', false);

function testSlotchangeFiresAtEndOfMicroTask(mode, connectedToDocument)
{
    var test = async_test('slotchange event must fire at the end of current microtask after mutation observers are invoked inside '
        + treeName(mode, connectedToDocument) + ' when slots\'s contents change');

    var host;
    var slot;
    var eventCount = 0;

    test.step(function () {
        host = document.createElement('div');
        if (connectedToDocument)
            document.body.appendChild(host);

        var shadowRoot = host.attachShadow({'mode': mode});
        slot = document.createElement('slot');

        slot.addEventListener('slotchange', function (event) {
            test.step(function () {
                assert_equals(event.type, 'slotchange', 'slotchange event\'s type must be "slotchange"');
                assert_equals(event.target, slot, 'slotchange event\'s target must be the slot element');
            });
            eventCount++;
        });

        shadowRoot.appendChild(slot);
    });

    var element = document.createElement('div');

    new MutationObserver(function () {
        test.step(function () {
            assert_equals(eventCount, 0, 'slotchange event must not be fired before mutation records are delivered');
        });
        host.appendChild(document.createElement('span'));
        element.setAttribute('title', 'bar');
    }).observe(element, {attributes: true, attributeFilter: ['id']});

    new MutationObserver(function () {
        test.step(function () {
            assert_equals(eventCount, 1, 'slotchange event must be fired during a single compound microtask');
        });
    }).observe(element, {attributes: true, attributeFilter: ['title']});

    element.setAttribute('id', 'foo');
    host.appendChild(document.createElement('div'));

    setTimeout(function () {
        test.step(function () {
            assert_equals(eventCount, 2,
                'a distinct slotchange event must be enqueued for changes made during a mutation observer delivery');
        });
        test.done();
    }, 0);
}

testSlotchangeFiresAtEndOfMicroTask('open', true);
testSlotchangeFiresAtEndOfMicroTask('closed', true);
testSlotchangeFiresAtEndOfMicroTask('open', false);
testSlotchangeFiresAtEndOfMicroTask('closed', false);
</script>
</body>
</html>