test_disconnectedRoot_webcomponent.html (5790B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Test DOMLocalization.prototype.connectRoot with Web Components</title> 6 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> 8 <script type="application/javascript"> 9 "use strict"; 10 const l10nReg = new L10nRegistry(); 11 const fs = [ 12 { path: "/localization/en-US/mock.ftl", source: ` 13 key1 = Key 1 14 key2 = Key 2 15 key3 = Key 3 16 key4 = Key 4 17 ` }, 18 ]; 19 const source = L10nFileSource.createMock("test", "app", ["en-US"], "/localization/{locale}", fs); 20 l10nReg.registerSources([source]); 21 22 document.domLoc = new DOMLocalization( 23 ["/mock.ftl"], 24 false, 25 l10nReg, 26 ["en-US"], 27 ); 28 document.domLoc.connectRoot(document.documentElement); 29 </script> 30 <script type="application/javascript"> 31 // In this test we're going to use two elements - `shadowLabel` and `lightLabel`. 32 // We create a new `DOMLocalization` and connect it to the document's root first. 33 // 34 // Then, we connect and disconnect it on root and element within the shadow DOM and 35 // apply new `data-l10n-id` onto both labels. 36 // Once the `lightLabel` get a new translation, we check what happened to the `shadowLabel` 37 // to ensure that depending on the status of connection between the shadow DOM and the `DOMLocalization` 38 // the `shadowLabel` either gets translated or not. 39 40 SimpleTest.waitForExplicitFinish(); 41 42 class FluentWidget extends HTMLElement { 43 constructor() { 44 super(); 45 const shadowRoot = this.attachShadow({mode: "open"}); 46 const t = document.querySelector("#fluent-widget-template"); 47 const instance = t.content.cloneNode(true); 48 shadowRoot.appendChild(instance); 49 } 50 connectedCallback() { 51 this.runTests(); 52 } 53 runTests() { 54 // First, let's verify that the mutation will not be applied since 55 // the shadow DOM is not connected to the `DOMLocalization`. 56 let shadowLabel = this.shadowRoot.getElementById("shadowLabel"); 57 let lightLabel = document.getElementById("lightLabel"); 58 59 let verifyL10n = () => { 60 if (lightLabel.textContent == "Key 1") { 61 is(shadowLabel.textContent, "", "document.l10n content not applied to an element in the shadow DOM"); 62 window.removeEventListener("MozAfterPaint", verifyL10n); 63 this.testPart2(); 64 } 65 }; 66 window.addEventListener("MozAfterPaint", verifyL10n); 67 68 document.domLoc.setAttributes(shadowLabel, "key1"); 69 document.domLoc.setAttributes(lightLabel, "key1"); 70 } 71 testPart2() { 72 // Next, we connect the shadow root to DOMLocalization and the next attribute 73 // change should result in a translation being applied. 74 document.domLoc.connectRoot(this.shadowRoot); 75 76 let shadowLabel = this.shadowRoot.getElementById("shadowLabel"); 77 let lightLabel = document.getElementById("lightLabel"); 78 79 // Test that mutation was applied. 80 let verifyL10n = () => { 81 if (lightLabel.textContent == "Key 2") { 82 is(shadowLabel.textContent, "Key 2", "document.l10n content applied to an element in the shadow DOM"); 83 window.removeEventListener("MozAfterPaint", verifyL10n); 84 this.testPart3(); 85 } 86 }; 87 window.addEventListener("MozAfterPaint", verifyL10n); 88 89 document.domLoc.setAttributes(shadowLabel, "key2"); 90 document.domLoc.setAttributes(lightLabel, "key2"); 91 } 92 testPart3() { 93 // After we disconnect the shadow root, the mutations should 94 // not be applied onto the `shadowLabel`. 95 document.domLoc.disconnectRoot(this.shadowRoot); 96 97 let shadowLabel = this.shadowRoot.getElementById("shadowLabel"); 98 let lightLabel = document.getElementById("lightLabel"); 99 100 let verifyL10n = () => { 101 if (lightLabel.textContent == "Key 3") { 102 is(shadowLabel.textContent, "Key 2", "document.l10n content not applied to an element in the shadow DOM"); 103 window.removeEventListener("MozAfterPaint", verifyL10n); 104 this.testPart4(); 105 } 106 }; 107 window.addEventListener("MozAfterPaint", verifyL10n); 108 109 document.domLoc.setAttributes(shadowLabel, "key3"); 110 document.domLoc.setAttributes(lightLabel, "key3"); 111 } 112 testPart4() { 113 // Finally, we'll connect it back, but this time, we'll connect 114 // not the shadow root, but an element within it. 115 // This should still result in the `shadowLabel` receiving a new translation. 116 document.domLoc.connectRoot(this.shadowRoot.getElementById("shadowDiv")); 117 118 let shadowLabel = this.shadowRoot.getElementById("shadowLabel"); 119 let lightLabel = document.getElementById("lightLabel"); 120 121 // Test that mutation was applied. 122 let verifyL10n = () => { 123 if (lightLabel.textContent == "Key 4") { 124 is(shadowLabel.textContent, "Key 4", "document.l10n content applied to an element in the shadow DOM"); 125 window.removeEventListener("MozAfterPaint", verifyL10n); 126 SimpleTest.finish(); 127 } 128 }; 129 window.addEventListener("MozAfterPaint", verifyL10n); 130 131 document.domLoc.setAttributes(shadowLabel, "key4"); 132 document.domLoc.setAttributes(lightLabel, "key4"); 133 } 134 } 135 customElements.define("fluent-widget", FluentWidget); 136 </script> 137 </head> 138 <body> 139 <p id="lightLabel"></p> 140 141 <template id="fluent-widget-template"> 142 <div id="shadowDiv"> 143 <p id="shadowLabel"></p> 144 </div> 145 </template> 146 <fluent-widget id="widget1"></fluent-widget> 147 </body> 148 </html>