From 3129565e4ebee327adb6045478b30fa06501a8e9 Mon Sep 17 00:00:00 2001 From: Maria Hutt Date: Mon, 8 Dec 2025 12:23:53 -0800 Subject: [PATCH] test(scripts): update palette query (#30842) Issue number: N/A --------- ## What is the current behavior? `high-contrast` and `high-contrast-dark` palettes were not working when requested through a URL query or hash for a test page. This was due to the `match` not accepting hyphens so it would only save `high` which is not a valid palette. ## What is the new behavior? - Updated `match` to accept hyphens - Added an error if an invalid palette is provided - Added a palette fallback if an invalid palette is provided - Added a class check for high contrast and high contrast dark ## Does this introduce a breaking change? - [ ] Yes - [x] No ## Other information How to test: 1. Verify that `?palette=high-contrast`, `?palette=high-contrast-dark`, `#palette=high-contrast`, and `#palette=high-contrast-dark` render correctly (I recommend using [button basic page](https://ionic-framework-git-scripts-ionic1.vercel.app/src/components/button/test/basic/)) --------- Co-authored-by: Brandy Smith --- core/scripts/testing/scripts.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/core/scripts/testing/scripts.js b/core/scripts/testing/scripts.js index 965bd8183e..ff8236c6a0 100644 --- a/core/scripts/testing/scripts.js +++ b/core/scripts/testing/scripts.js @@ -57,11 +57,19 @@ * Values can be `light`, `dark`, `high-contrast`, * or `high-contrast-dark`. Default to `light` for tests. */ - const paletteQuery = window.location.search.match(/palette=([a-z]+)/); - const paletteHash = window.location.hash.match(/palette=([a-z]+)/); + const validPalettes = ['light', 'dark', 'high-contrast', 'high-contrast-dark']; + const paletteQuery = window.location.search.match(/palette=([a-z-]+)/); + const paletteHash = window.location.hash.match(/palette=([a-z-]+)/); const darkClass = document.body?.classList.contains('ion-palette-dark') ? 'dark' : null; + const highContrastClass = document.body?.classList.contains('ion-palette-high-contrast') ? 'high-contrast' : null; + const highContrastDarkClass = darkClass && highContrastClass ? 'high-contrast-dark' : null; - const paletteName = paletteQuery?.[1] || paletteHash?.[1] || darkClass || 'light'; + let paletteName = paletteQuery?.[1] || paletteHash?.[1] || highContrastDarkClass || darkClass || highContrastClass || 'light'; + + if (!validPalettes.includes(paletteName)) { + console.warn(`Invalid palette name: '${paletteName}'. Falling back to 'light' palette.`); + paletteName = 'light'; + } if (paletteName !== 'light') { const linkTag = document.createElement('link');