From 6caad844d3413a5403e926645bd351036675c5d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20=28Netux=29=20Rodr=C3=ADguez?= Date: Mon, 8 Jan 2024 15:11:34 -0300 Subject: [PATCH] fix(editor script importer): Reimplement `findAllRules()` to use `RegExp.exec()` and `getClosingBracket()` Now we create items for each rule, from where the rule starts being defined ("rule(...) {...") to the closing bracket. While the old implementation was more lenient on what it included, it also always skipped over disabled tags, meaning the rules were always enabled when imported. This fixes that issue :^) --- .../editor/Modals/ScriptImporterModal.svelte | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/app/javascript/src/components/editor/Modals/ScriptImporterModal.svelte b/app/javascript/src/components/editor/Modals/ScriptImporterModal.svelte index ae832cc92..4365592cb 100644 --- a/app/javascript/src/components/editor/Modals/ScriptImporterModal.svelte +++ b/app/javascript/src/components/editor/Modals/ScriptImporterModal.svelte @@ -2,7 +2,7 @@ import Modal from "./Modal.svelte" import { items, modal } from "../../../stores/editor" import { createNewItem } from "../../../utils/editor" - import { getSettings } from "../../../utils/parse" + import { getClosingBracket, getSettings } from "../../../utils/parse" import { submittable } from "../../actions/submittable" import { onMount } from "svelte" @@ -25,30 +25,28 @@ } function findAllRules() { - const rules = value.split(/(?=(?:disabled\s+)?rule\s*\()/g) - const newItems = [] + const regex = /(?:disabled\s+)?rule\s*\("(.*)"\)\s*{/g + const newItems = [] let counter = 0 - rules.forEach(rule => { - const name = getTypeName(rule) - if (!name) return + let match = null + while ((match = regex.exec(value)) != null) { + const name = match[1] + + const openBracket = match.index + match[0].length - 1 + let end = getClosingBracket(value, "{", "}", openBracket - 1) + if (end === -1) end = value.length + + const rule = value.substring(match.index, end + 1) + const newItem = createNewItem(name, rule, $items.length + counter) newItems.push(newItem) counter++ - }) + } return newItems } - function getTypeName(content) { - const regex = new RegExp(/rule\s*\("(.*)"\)/g) - const match = regex.exec(content) - - if (!match) return - - return match[1] - } - function submit() { if (disallowSubmit) return