From b9fa5a697f302fe73d2e59bcb62c178a86131037 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Wed, 10 Aug 2022 12:58:35 +0200 Subject: [PATCH] Improve the behavior of the `` name attribute --- ts/WoltLabSuite/WebComponent/fa-icon.ts | 23 ++++++++----------- .../js/WoltLabSuite/WebComponent/fa-icon.js | 20 +++++++--------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/ts/WoltLabSuite/WebComponent/fa-icon.ts b/ts/WoltLabSuite/WebComponent/fa-icon.ts index 2df931ed6d..39509326a6 100644 --- a/ts/WoltLabSuite/WebComponent/fa-icon.ts +++ b/ts/WoltLabSuite/WebComponent/fa-icon.ts @@ -69,6 +69,10 @@ if (!isSolid && isFontAwesome6Free()) { const [, styles] = window.getFontAwesome6IconMetadata(name)!; if (!styles.includes("regular")) { + // Font Awesome 6 Free only includes solid icons with the + // the exception to some special icons that use the weight + // to differentiate two related icons. One such example is + // the `bell` icon that comes in `solid` and `regular` flavor. return false; } } @@ -92,16 +96,6 @@ root.append(codepoint); } - attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void { - switch (name) { - case "name": - if (newValue !== null && this.isValidIconName(newValue)) { - this.updateIcon(); - } - break; - } - } - get solid(): boolean { return this.hasAttribute("solid"); } @@ -119,7 +113,12 @@ } set name(name: string) { + if (!this.isValidIconName(name)) { + throw new Error(`Refused to set the unknown icon name '${name}'.`); + } + this.setAttribute("name", name); + this.updateIcon(); } get size(): IconSize { @@ -138,10 +137,6 @@ this.setAttribute("size", size.toString()); } - - static get observedAttributes() { - return ["name"]; - } } window.customElements.define("fa-icon", FaIcon); diff --git a/wcfsetup/install/files/js/WoltLabSuite/WebComponent/fa-icon.js b/wcfsetup/install/files/js/WoltLabSuite/WebComponent/fa-icon.js index 044de90923..ceceabcc46 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/WebComponent/fa-icon.js +++ b/wcfsetup/install/files/js/WoltLabSuite/WebComponent/fa-icon.js @@ -58,6 +58,10 @@ if (!isSolid && isFontAwesome6Free()) { const [, styles] = window.getFontAwesome6IconMetadata(name); if (!styles.includes("regular")) { + // Font Awesome 6 Free only includes solid icons with the + // the exception to some special icons that use the weight + // to differentiate two related icons. One such example is + // the `bell` icon that comes in `solid` and `regular` flavor. return false; } } @@ -76,15 +80,6 @@ const [codepoint] = window.getFontAwesome6IconMetadata(this.name); root.append(codepoint); } - attributeChangedCallback(name, oldValue, newValue) { - switch (name) { - case "name": - if (newValue !== null && this.isValidIconName(newValue)) { - this.updateIcon(); - } - break; - } - } get solid() { return this.hasAttribute("solid"); } @@ -100,7 +95,11 @@ return this.getAttribute("name") || ""; } set name(name) { + if (!this.isValidIconName(name)) { + throw new Error(`Refused to set the unknown icon name '${name}'.`); + } this.setAttribute("name", name); + this.updateIcon(); } get size() { const size = this.getAttribute("size"); @@ -115,9 +114,6 @@ } this.setAttribute("size", size.toString()); } - static get observedAttributes() { - return ["name"]; - } } window.customElements.define("fa-icon", FaIcon); })(); -- 2.20.1