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 (fb59957df144)

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
<!DOCTYPE html>
<html>
<head>
    <title>Shadow DOM: Firing an event with relatedTarget inside a shadow tree</title>
    <meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
    <meta name="assert" content="The retargeting algorithm is used to determine relative targets">
    <link rel="help" href="https://w3c.github.io/webcomponents/spec/shadow/#retargeting-relatedtarget">
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="resources/event-path-test-helpers.js"></script>
</head>
<body>
    <div id="log"></div>
    <script>

        /*
        -SR: ShadowRoot  -S: Slot  target: (~)  relatedTarget: [~]  *: indicates start  digit: event path order
        A ----------------------------------------------- A-SR
        + B ----------- B-SR (4)                          + A1 --- A1-SR
          + C           + B1 (3) [*; 0-4] --- B1-SR (2)   + A2-S   + A1a
          + D --- D-SR    + B1a (*; 0)        + B1b [1,2] --- B1b-SR
                  + D1                        + B1c-S (1)     + B1b1
                                                              + B1b2
        */
        function testEventAtB1aWithB1a(mode) {
            test(function () {
                var nodes = createFixedTestTree(mode);

                log = dispatchEventWithEventLog(nodes, nodes.B1a, new MouseEvent('foo', {bubbles: true, composed: true, relatedTarget: nodes.B1}));

                assert_array_equals(log.eventPath,
                    ['B1a', 'B1c-S', 'B1-SR', 'B1', 'B-SR'], 'The event path must be correct.');
                assert_array_equals(log.relatedTargets,
                    ['B1',  'B1',    'B1',    'B1', 'B1'], 'The related targets must be correct.');

            }, 'Firing an event at B1a with relatedNode at B1 with ' + mode + ' mode shadow trees');
        }

        testEventAtB1aWithB1a('open');
        testEventAtB1aWithB1a('closed');

        /*
        -SR: ShadowRoot  -S: Slot  target: (~)  relatedTarget: [~]  *: indicates start  digit: event path order
        A ------------------------------------------------- A-SR
        + B ----------- B-SR (4)                            + A1 --- A1-SR
          + C           + B1 (3) [0,3-4] --- B1-SR (2)      + A2-S   + A1a
          + D --- D-SR    + B1a (*; 0)       + B1b [1,2] --- B1b-SR
                  + D1                       + B1c-S (1)     + B1b1 [*]
                                                             + B1b2
        */
        function testEventAtB1aWithB1b1(mode) {
            test(function () {
                var nodes = createFixedTestTree(mode);

                log = dispatchEventWithEventLog(nodes, nodes.B1a, new MouseEvent('foo', {bubbles: true, composed: true, relatedTarget: nodes.B1b1}));

                assert_array_equals(log.eventPath,
                    ['B1a', 'B1c-S', 'B1-SR', 'B1', 'B-SR'], 'The event path must be correct.');
                assert_array_equals(log.relatedTargets,
                    ['B1',  'B1b',   'B1b',   'B1', 'B1'], 'The related targets must be correct.');

            }, 'Firing an event at B1a with relatedNode at B1b1 with ' + mode + ' mode shadow trees');
        }

        testEventAtB1aWithB1b1('open');
        testEventAtB1aWithB1b1('closed');

        /*
        -SR: ShadowRoot  -S: Slot  target: (~)  relatedTarget: [~]  *: indicates start  digit: event path order
        A -------------------------------------------------- A-SR
        + B ------------- B-SR (5)                           + A1 --- A1-SR
          + C             + B1 (4) ------- B1-SR (3)         + A2-S   + A1a
          + D --- D-SR    + B1a [*; 0-5]   + B1b (2) --- B1b-SR (1)
                  + D1                     + B1c-S       + B1b1 (*; 0)
                                                         + B1b2
        */
        function testEventAtB1b1WithB1a(mode) {
            test(function () {
                var nodes = createFixedTestTree(mode);

                log = dispatchEventWithEventLog(nodes, nodes.B1b1, new MouseEvent('foo', {bubbles: true, composed: true, relatedTarget: nodes.B1a}));

                assert_array_equals(log.eventPath,
                    ['B1b1', 'B1b-SR', 'B1b', 'B1-SR', 'B1', 'B-SR'], 'The event path must be correct.');
                assert_array_equals(log.relatedTargets,
                    ['B1a',  'B1a',    'B1a', 'B1a',   'B1a', 'B1a'], 'The related targets must be correct.');

            }, 'Firing an event at B1b1 with relatedNode at B1a with ' + mode + ' mode shadow trees');
        }

        testEventAtB1b1WithB1a('open');
        testEventAtB1b1WithB1a('closed');

        /*
        -SR: ShadowRoot  -S: Slot  target: (~)  relatedTarget: [~]  *: indicates start  digit: event path order
        A (8) -------------------------------------------------- A-SR (7)
        + B (5) -------------- B-SR (4)                          + A1 -------- A1-SR
          + C                  + B1 (3) [*; 0-4] --- B1-SR (2)   + A2-S (6)    + A1a
          + D [0-8] --- D-SR     + B1a (*; 0)        + B1b ------ B1b-SR
                        + D1 [*]                     + B1c-S (1)  + B1b1
                                                                  + B1b2
        */
        function testEventAtB1aWithD1(mode) {
            test(function () {
                var nodes = createFixedTestTree(mode);

                log = dispatchEventWithEventLog(nodes, nodes.B1a, new MouseEvent('foo', {bubbles: true, composed: true, relatedTarget: nodes.D1}));

                assert_array_equals(log.eventPath,
                    ['B1a', 'B1c-S', 'B1-SR', 'B1', 'B-SR', 'B', 'A2-S', 'A-SR', 'A'], 'The event path must be correct.');
                assert_array_equals(log.relatedTargets,
                    ['D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D'], 'The related targets must be correct.');

            }, 'Firing an event at B1a with relatedNode at D1 with ' + mode + ' mode shadow trees');
        }

        testEventAtB1aWithD1('open');
        testEventAtB1aWithD1('closed');

        /*
        -SR: ShadowRoot  -S: Slot  target: (~)  relatedTarget: [~]  *: indicates start  digit: event path order
        A (6) ----------------------------------- A-SR (5)
        + B (3) [0] ----------- B-SR              + A1 ------ A1-SR
          + C                   + B1 ----- B1-SR  + A2-S (4)  + A1a
          + D (2) --- D-SR (1)  + B1a [*]  + B1b --- B1b-SR
                        + D1 (*; 0)         + B1c-S   + B1b1
                                                      + B1b2
        */
        function testEventAtD1WithB1a(mode) {
            test(function () {
                var nodes = createFixedTestTree(mode);

                log = dispatchEventWithEventLog(nodes, nodes.D1, new MouseEvent('foo', {bubbles: true, composed: true, relatedTarget: nodes.B1a}));

                assert_array_equals(log.eventPath,
                    ['D1', 'D-SR', 'D', 'B', 'A2-S', 'A-SR', 'A'], 'The event path must be correct.');
                assert_array_equals(log.relatedTargets,
                    ['B', 'B', 'B', 'B', 'B', 'B', 'B'], 'The related targets must be correct.');

            }, 'Firing an event at D1 with relatedNode at B1a with ' + mode + ' mode shadow trees');
        }

        testEventAtD1WithB1a('open');
        testEventAtD1WithB1a('closed');

        /*
        -SR: ShadowRoot  -S: Slot  target: (~)  relatedTarget: [~]  *: indicates start  digit: event path order
        A (8) [0-5,8] ---------------------------------------- A-SR (7)
        + B (5)  ------- B-SR (4)                              + A1 [6,7] --- A1-SR
          + C            + B1 (3) ----- B1-SR (2)              + A2-S (6)     + A1a [*]
          + D --- D-SR   + B1a (*; 0)   + B1b ------- B1b-SR
                  + D1                  + B1c-S (1)   + B1b1
                                                      + B1b2
        */
        function testEventAtB1aWithA1a(mode) {
            test(function () {
                var nodes = createFixedTestTree(mode);

                log = dispatchEventWithEventLog(nodes, nodes.B1a, new MouseEvent('foo', {bubbles: true, composed: true, relatedTarget: nodes.A1a}));

                assert_array_equals(log.eventPath,
                    ['B1a', 'B1c-S', 'B1-SR', 'B1', 'B-SR', 'B', 'A2-S', 'A-SR', 'A'], 'The event path must be correct.');
                assert_array_equals(log.relatedTargets,
                    ['A',   'A',     'A',     'A',   'A',   'A', 'A1',   'A1',   'A'], 'The related targets must be correct.');

            }, 'Firing an event at B1a with relatedNode at A1a with ' + mode + ' mode shadow trees');
        }

        testEventAtB1aWithA1a('open');
        testEventAtB1aWithA1a('closed');

        /*
        -SR: ShadowRoot  -S: Slot  target: (~)  relatedTarget: [~]  *: indicates start  digit: event path order
        A (4) ----------------------------------------- A-SR (3)
        + B [0-4]  ----- B-SR                           + A1 (2) --- A1-SR (1)
          + C            + B1 ------- B1-SR             + A2-S       + A1a (*; 0)
          + D --- D-SR     + B1a [*]  + B1b --- B1b-SR
                  + D1                + B1c-S   + B1b1
                                                + B1b2
        */
        function testEventAtA1aWithB1a(mode) {
            test(function () {
                var nodes = createFixedTestTree(mode);

                log = dispatchEventWithEventLog(nodes, nodes.A1a, new MouseEvent('foo', {bubbles: true, composed: true, relatedTarget: nodes.B1a}));

                assert_array_equals(log.eventPath,
                    ['A1a', 'A1-SR', 'A1', 'A-SR', 'A'], 'The event path must be correct.');
                assert_array_equals(log.relatedTargets,
                    ['B', 'B', 'B', 'B', 'B'], 'The related targets must be correct.');

            }, 'Firing an event at A1a with relatedNode at B1a with ' + mode + ' mode shadow trees');
        }

        testEventAtA1aWithB1a('open');
        testEventAtA1aWithB1a('closed');

        /*
        -SR: ShadowRoot  -S: Slot  target: (~)  relatedTarget: [~]  *: indicates start  digit: event path order
        A (8) ----------------------------------- A-SR (7)
        + B (5)  ----- B-SR (4)                   + A2-S (6)
          + C          + B1 (3) ----- B1-SR (2)
          + D --- D-SR   + B1a (*; 0) + B1b ------- B1b-SR
                  + D1                + B1c-S (1)   + B1b1
                                                    + B1b2
        A1 [0-6] --- A1-SR
                   + A1a [*]
        */
        function testEventAtB1aWithDetachedA1a(mode) {
            test(function () {
                var nodes = createFixedTestTree(mode);

                nodes['A-SR'].removeChild(nodes.A1);
                log = dispatchEventWithEventLog(nodes, nodes.B1a, new MouseEvent('foo', {bubbles: true, composed: true, relatedTarget: nodes.A1a}));

                assert_array_equals(log.eventPath,
                    ['B1a', 'B1c-S', 'B1-SR', 'B1', 'B-SR', 'B', 'A2-S', 'A-SR', 'A'], 'The event path must be correct.');
                assert_array_equals(log.relatedTargets,
                    ['A1', 'A1', 'A1', 'A1', 'A1', 'A1', 'A1', 'A1', 'A1'], 'The related targets must be correct.');

            }, 'Firing an event at B1a with relatedNode at A1a (detached) with ' + mode + ' mode shadow trees');
        }

        testEventAtB1aWithDetachedA1a('open');
        testEventAtB1aWithDetachedA1a('closed');

        /*
        -SR: ShadowRoot  -S: Slot  target: (~)  relatedTarget: [~]  *: indicates start  digit: event path order
        A ----------------------------------- A-SR
        + B [0-3]  ----- B-SR                 + A2-S
          + C            + B1 -------- B1-SR
          + D --- D-SR     + B1a [*]   + B1b --- B1b-SR
                  + D1                 + B1c-S   + B1b1
                                                 + B1b2
        A1 (2) --- A1-SR (1)
                   + A1a (*; 0)
        */
        function testEventAtA1aWithDetachedB1a(mode) {
            test(function () {
                var nodes = createFixedTestTree(mode);

                nodes['A-SR'].removeChild(nodes.A1);
                log = dispatchEventWithEventLog(nodes, nodes.A1a, new MouseEvent('foo', {bubbles: true, composed: true, relatedTarget: nodes.B1a}));

                assert_array_equals(log.eventPath,      ['A1a', 'A1-SR', 'A1'], 'The event path must be correct.');
                assert_array_equals(log.relatedTargets, ['B',   'B',     'B' ], 'The related targets must be correct.');

            }, 'Firing an event at A1a with relatedNode at B1a (detached) with ' + mode + ' mode shadow trees');
        }

        testEventAtA1aWithDetachedB1a('open');
        testEventAtA1aWithDetachedB1a('closed');

    </script>
    </body>
</html>