|
| 1 | +/** |
| 2 | + * This script is loaded in testing environments to set up the |
| 3 | + * document based on URL parameters. |
| 4 | + * |
| 5 | + * Test pages (e.g., `chip/test/basic/index.html`) are set to use |
| 6 | + * URL query parameters. |
| 7 | + * |
| 8 | + * Playwright test environments (e.g., `chip/test/basic/chip.e2e.ts`) |
| 9 | + * are set based on whether `setContent` or `goto` has been used: |
| 10 | + * - `setContent` uses URL hash parameters. Tests will break if |
| 11 | + * query parameters are used. |
| 12 | + * - `goto` uses URL query parameters. |
| 13 | + * |
| 14 | + * The following URL parameters are supported: |
| 15 | + * - `rtl`: Set to `true` to enable right-to-left directionality. |
| 16 | + * - `ionic:_testing`: Set to `true` to identify testing environments. |
| 17 | + * - `ionic:theme`: Set to `ionic`, `ios`, or `md` to load a specific |
| 18 | + * theme. Defaults to `md`. |
| 19 | + * - `palette`: Set to `light`, `dark`, `high-contrast`, or |
| 20 | + * `high-contrast-dark` to load a specific palette. Defaults to `light`. |
| 21 | + */ |
1 | 22 |
|
2 | 23 | (function() { |
3 | 24 |
|
4 | | - if (window.location.search.indexOf('rtl=true') > -1) { |
| 25 | + /** |
| 26 | + * The `rtl` param is used to set the directionality of the |
| 27 | + * document. This can be `true` or `false`. |
| 28 | + */ |
| 29 | + const isRTL = window.location.search.indexOf('rtl=true') > -1 || window.location.hash.indexOf('rtl=true') > -1; |
| 30 | + |
| 31 | + if (isRTL) { |
5 | 32 | document.documentElement.setAttribute('dir', 'rtl'); |
6 | 33 | } |
7 | 34 |
|
8 | | - if (window.location.search.indexOf('ionic:_testing=true') > -1) { |
| 35 | + /** |
| 36 | + * The `ionic:_testing` param is used to identify testing |
| 37 | + * environments. |
| 38 | + */ |
| 39 | + const isTestEnv = window.location.search.indexOf('ionic:_testing=true') > -1 || window.location.hash.indexOf('ionic:_testing=true') > -1; |
| 40 | + |
| 41 | + if (isTestEnv) { |
9 | 42 | const style = document.createElement('style'); |
10 | 43 | style.innerHTML = ` |
11 | | -* { |
12 | | - caret-color: transparent !important; |
13 | | -}`; |
| 44 | + * { |
| 45 | + caret-color: transparent !important; |
| 46 | + } |
| 47 | + `; |
14 | 48 | document.head.appendChild(style); |
15 | 49 | } |
16 | 50 |
|
17 | 51 | /** |
18 | | - * The term `palette` is used to as a param to match the |
19 | | - * Ionic docs, plus here is already a `ionic:theme` query being |
20 | | - * used for `md`, `ios`, and `ionic` themes. |
| 52 | + * The `palette` param is used to load a specific palette |
| 53 | + * for the theme. |
| 54 | + * The dark class will load the dark palette automatically |
| 55 | + * if no palette is specified through the URL. |
| 56 | + * |
| 57 | + * Values can be `light`, `dark`, `high-contrast`, |
| 58 | + * or `high-contrast-dark`. Default to `light` for tests. |
21 | 59 | */ |
22 | | - const palette = window.location.search.match(/palette=([a-z]+)/); |
23 | | - if (palette && palette[1] !== 'light') { |
| 60 | + const paletteQuery = window.location.search.match(/palette=([a-z]+)/); |
| 61 | + const paletteHash = window.location.hash.match(/palette=([a-z]+)/); |
| 62 | + const darkClass = document.body?.classList.contains('ion-palette-dark') ? 'dark' : null; |
| 63 | + |
| 64 | + const paletteName = paletteQuery?.[1] || paletteHash?.[1] || darkClass || 'light'; |
| 65 | + |
| 66 | + if (paletteName !== 'light') { |
24 | 67 | const linkTag = document.createElement('link'); |
25 | 68 | linkTag.setAttribute('rel', 'stylesheet'); |
26 | 69 | linkTag.setAttribute('type', 'text/css'); |
27 | | - linkTag.setAttribute('href', `/css/palettes/${palette[1]}.always.css`); |
| 70 | + linkTag.setAttribute('href', `/css/palettes/${paletteName}.always.css`); |
28 | 71 | document.head.appendChild(linkTag); |
29 | 72 | } |
30 | 73 |
|
|
0 commit comments