From fb84ca5616cd1eb4bf442cd62cf93d7c6d9947b7 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Wed, 4 Oct 2023 18:59:46 +0000 Subject: [PATCH] fix search regexp escape, wrap chartjs with custom element --- account.html | 7 +--- scripts/account.js | 81 ++++++++++++++++++++------------------------ scripts/chart.js | 35 +++++++++++++++++++ scripts/draggable.js | 2 +- scripts/instance.js | 6 ++-- 5 files changed, 78 insertions(+), 53 deletions(-) create mode 100644 scripts/chart.js diff --git a/account.html b/account.html index 2d67fcb..3daa866 100644 --- a/account.html +++ b/account.html @@ -9,6 +9,7 @@ + +
+ +
+ `; + this.canvas = this.shadowRoot.querySelector("canvas"); + } + + set data (data) { + this.canvas.role = "img"; + this.canvas.ariaLabel = data.ariaLabel; + createChart(this.canvas, data.chart); + } + + get data () { + return null; + } +} + +function createChart (ctx, data) { + return new Chart(ctx, data); +} + +customElements.define("custom-chart", CustomChart); diff --git a/scripts/draggable.js b/scripts/draggable.js index 215b944..765ce12 100644 --- a/scripts/draggable.js +++ b/scripts/draggable.js @@ -5,7 +5,7 @@ const draggableItemUUIDs = {}; * Get the data transfer source object by parsing its types. Valid draggable-item events have one type of the format `application/json/${uuid}`. * The function takes the entire type list from event.dataTransfer.types and returns the source object if valid, or null if invalid. * @param {*} typesList from event.dataTransfer.types - * @returns {Object} Object containing the type, uuid, and element of the dataTransfer source or null + * @returns {Object} Object containing the type, uuid, and element of the dataTransfer source or null */ function getDragSource (typesList) { if (typesList.length !== 1) { diff --git a/scripts/instance.js b/scripts/instance.js index c3425a9..2ba792d 100644 --- a/scripts/instance.js +++ b/scripts/instance.js @@ -77,8 +77,10 @@ class InstanceCard extends HTMLElement { const nameParagraph = this.shadowRoot.querySelector("#instance-name"); if (this.searchQuery) { - const regEscape = v => v.replace("[-[\]{}()*+?.,\\^$|#\s]", "\\$&"); - const nameParts = this.name.split(new RegExp(regEscape(`(${this.searchQuery})`), "ig")); + const regExpEscape = v => v.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + const escapedQuery = regExpEscape(this.searchQuery); + const searchRegExp = new RegExp(`(${escapedQuery})`, "gi"); + const nameParts = this.name.split(searchRegExp); for (let i = 0; i < nameParts.length; i++) { const part = document.createElement("span"); part.innerText = nameParts[i];