From 7025a2c4a5c27329ecb0ea526b94c6baa93f5840 Mon Sep 17 00:00:00 2001 From: Haoming Date: Thu, 12 Dec 2024 16:08:15 +0800 Subject: [PATCH 1/3] check-for-order --- .../javascript/prompt-bracket-checker.js | 115 ++++++++++++------ 1 file changed, 81 insertions(+), 34 deletions(-) diff --git a/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js b/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js index a59e584c26d..bb78acabdb1 100644 --- a/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js +++ b/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js @@ -1,42 +1,89 @@ -// Stable Diffusion WebUI - Bracket checker -// By Hingashi no Florin/Bwin4L & @akx +// Stable Diffusion WebUI - Bracket Checker +// By @Bwin4L, @akx, @w-e-w, @Haoming02 // Counts open and closed brackets (round, square, curly) in the prompt and negative prompt text boxes in the txt2img and img2img tabs. -// If there's a mismatch, the keyword counter turns red and if you hover on it, a tooltip tells you what's wrong. +// If there's a mismatch, the keyword counter turns red, and if you hover on it, a tooltip tells you what's wrong. -function checkBrackets(textArea, counterElt) { - const counts = {}; - textArea.value.matchAll(/(? { - counts[bracket[1]] = (counts[bracket[1]] || 0) + 1; - }); - const errors = []; +(function() { + const pairs = [ + ['(', ')', 'round brackets'], + ['[', ']', 'square brackets'], + ['{', '}', 'curly brackets'] + ]; + + function checkBrackets(textArea, counterElem) { + const counts = {}; + const errors = new Set(); + let i = 0; + + while (i < textArea.value.length) { + let char = textArea.value[i]; + let escaped = false; + while (char === '\\' && i + 1 < textArea.value.length) { + escaped = !escaped; + i++; + char = textArea.value[i]; + } + + for (const [open, close, label] of pairs) { + const lb = escaped ? `escaped ${label}` : label; - function checkPair(open, close, kind) { - if (counts[open] !== counts[close]) { - errors.push( - `${open}...${close} - Detected ${counts[open] || 0} opening and ${counts[close] || 0} closing ${kind}.` - ); + if (char === open) { + counts[lb] = (counts[lb] || 0) + 1; + } else if (char === close) { + counts[lb] = (counts[lb] || 0) - 1; + if (counts[lb] < 0) { + errors.add(`Incorrect order of ${lb}.`); + } + } + } + + i++; + } + + for (const [open, close, label] of pairs) { + if (counts[label] == undefined) { + continue; + } + + if (counts[label] > 0) { + errors.add(`${open} ... ${close} - Detected ${counts[label]} more opening than closing ${label}.`); + } else if (counts[label] < 0) { + errors.add(`${open} ... ${close} - Detected ${-counts[label]} more closing than opening ${label}.`); + } + } + + for (const [open, close, label] of pairs) { + const lb = `escaped ${label}`; + if (counts[lb] == undefined) { + continue; + } + + const op = `\\${open}`; + const cl = `\\${close}`; + if (counts[lb] > 0) { + errors.add(`${op} ... ${cl} - Detected ${counts[lb]} more opening than closing ${lb}.`); + } else if (counts[lb] < 0) { + errors.add(`${op} ... ${cl} - Detected ${-counts[lb]} more closing than opening ${lb}.`); + } } - } - checkPair('(', ')', 'round brackets'); - checkPair('[', ']', 'square brackets'); - checkPair('{', '}', 'curly brackets'); - counterElt.title = errors.join('\n'); - counterElt.classList.toggle('error', errors.length !== 0); -} + counterElem.title = [...errors].join('\n'); + counterElem.classList.toggle('error', errors.size !== 0); + } -function setupBracketChecking(id_prompt, id_counter) { - var textarea = gradioApp().querySelector("#" + id_prompt + " > label > textarea"); - var counter = gradioApp().getElementById(id_counter); + function setupBracketChecking(id_prompt, id_counter) { + const textarea = gradioApp().querySelector(`#${id_prompt} > label > textarea`); + const counter = gradioApp().getElementById(id_counter); - if (textarea && counter) { - textarea.addEventListener("input", () => checkBrackets(textarea, counter)); + if (textarea && counter) { + onEdit(`${id_prompt}_BracketChecking`, textarea, 400, () => checkBrackets(textarea, counter)); + } } -} - -onUiLoaded(function() { - setupBracketChecking('txt2img_prompt', 'txt2img_token_counter'); - setupBracketChecking('txt2img_neg_prompt', 'txt2img_negative_token_counter'); - setupBracketChecking('img2img_prompt', 'img2img_token_counter'); - setupBracketChecking('img2img_neg_prompt', 'img2img_negative_token_counter'); -}); + + onUiLoaded(function() { + setupBracketChecking('txt2img_prompt', 'txt2img_token_counter'); + setupBracketChecking('txt2img_neg_prompt', 'txt2img_negative_token_counter'); + setupBracketChecking('img2img_prompt', 'img2img_token_counter'); + setupBracketChecking('img2img_neg_prompt', 'img2img_negative_token_counter'); + }); +})(); From fbc51fa210bf689f300135e84a89c4ca9fcb4b5c Mon Sep 17 00:00:00 2001 From: Haoming Date: Mon, 16 Dec 2024 09:47:38 +0800 Subject: [PATCH 2/3] skip escaped --- .../javascript/prompt-bracket-checker.js | 30 ++++++------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js b/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js index bb78acabdb1..3014424807b 100644 --- a/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js +++ b/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js @@ -24,15 +24,18 @@ char = textArea.value[i]; } - for (const [open, close, label] of pairs) { - const lb = escaped ? `escaped ${label}` : label; + if (escaped) { + i++; + continue; + } + for (const [open, close, label] of pairs) { if (char === open) { - counts[lb] = (counts[lb] || 0) + 1; + counts[label] = (counts[label] || 0) + 1; } else if (char === close) { - counts[lb] = (counts[lb] || 0) - 1; - if (counts[lb] < 0) { - errors.add(`Incorrect order of ${lb}.`); + counts[label] = (counts[label] || 0) - 1; + if (counts[label] < 0) { + errors.add(`Incorrect order of ${label}.`); } } } @@ -52,21 +55,6 @@ } } - for (const [open, close, label] of pairs) { - const lb = `escaped ${label}`; - if (counts[lb] == undefined) { - continue; - } - - const op = `\\${open}`; - const cl = `\\${close}`; - if (counts[lb] > 0) { - errors.add(`${op} ... ${cl} - Detected ${counts[lb]} more opening than closing ${lb}.`); - } else if (counts[lb] < 0) { - errors.add(`${op} ... ${cl} - Detected ${-counts[lb]} more closing than opening ${lb}.`); - } - } - counterElem.title = [...errors].join('\n'); counterElem.classList.toggle('error', errors.size !== 0); } From 8bf30e3c42b39a43166e7022865c2c62af9eff96 Mon Sep 17 00:00:00 2001 From: Haoming Date: Wed, 18 Dec 2024 01:02:40 +0800 Subject: [PATCH 3/3] revert IIFE --- .../javascript/prompt-bracket-checker.js | 100 +++++++++--------- 1 file changed, 49 insertions(+), 51 deletions(-) diff --git a/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js b/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js index 3014424807b..67ff7fe22d2 100644 --- a/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js +++ b/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js @@ -3,75 +3,73 @@ // Counts open and closed brackets (round, square, curly) in the prompt and negative prompt text boxes in the txt2img and img2img tabs. // If there's a mismatch, the keyword counter turns red, and if you hover on it, a tooltip tells you what's wrong. -(function() { +function checkBrackets(textArea, counterElem) { const pairs = [ ['(', ')', 'round brackets'], ['[', ']', 'square brackets'], ['{', '}', 'curly brackets'] ]; - function checkBrackets(textArea, counterElem) { - const counts = {}; - const errors = new Set(); - let i = 0; + const counts = {}; + const errors = new Set(); + let i = 0; - while (i < textArea.value.length) { - let char = textArea.value[i]; - let escaped = false; - while (char === '\\' && i + 1 < textArea.value.length) { - escaped = !escaped; - i++; - char = textArea.value[i]; - } - - if (escaped) { - i++; - continue; - } - - for (const [open, close, label] of pairs) { - if (char === open) { - counts[label] = (counts[label] || 0) + 1; - } else if (char === close) { - counts[label] = (counts[label] || 0) - 1; - if (counts[label] < 0) { - errors.add(`Incorrect order of ${label}.`); - } - } - } + while (i < textArea.value.length) { + let char = textArea.value[i]; + let escaped = false; + while (char === '\\' && i + 1 < textArea.value.length) { + escaped = !escaped; + i++; + char = textArea.value[i]; + } + if (escaped) { i++; + continue; } for (const [open, close, label] of pairs) { - if (counts[label] == undefined) { - continue; - } - - if (counts[label] > 0) { - errors.add(`${open} ... ${close} - Detected ${counts[label]} more opening than closing ${label}.`); - } else if (counts[label] < 0) { - errors.add(`${open} ... ${close} - Detected ${-counts[label]} more closing than opening ${label}.`); + if (char === open) { + counts[label] = (counts[label] || 0) + 1; + } else if (char === close) { + counts[label] = (counts[label] || 0) - 1; + if (counts[label] < 0) { + errors.add(`Incorrect order of ${label}.`); + } } } - counterElem.title = [...errors].join('\n'); - counterElem.classList.toggle('error', errors.size !== 0); + i++; } - function setupBracketChecking(id_prompt, id_counter) { - const textarea = gradioApp().querySelector(`#${id_prompt} > label > textarea`); - const counter = gradioApp().getElementById(id_counter); + for (const [open, close, label] of pairs) { + if (counts[label] == undefined) { + continue; + } - if (textarea && counter) { - onEdit(`${id_prompt}_BracketChecking`, textarea, 400, () => checkBrackets(textarea, counter)); + if (counts[label] > 0) { + errors.add(`${open} ... ${close} - Detected ${counts[label]} more opening than closing ${label}.`); + } else if (counts[label] < 0) { + errors.add(`${open} ... ${close} - Detected ${-counts[label]} more closing than opening ${label}.`); } } - onUiLoaded(function() { - setupBracketChecking('txt2img_prompt', 'txt2img_token_counter'); - setupBracketChecking('txt2img_neg_prompt', 'txt2img_negative_token_counter'); - setupBracketChecking('img2img_prompt', 'img2img_token_counter'); - setupBracketChecking('img2img_neg_prompt', 'img2img_negative_token_counter'); - }); -})(); + counterElem.title = [...errors].join('\n'); + counterElem.classList.toggle('error', errors.size !== 0); +} + +function setupBracketChecking(id_prompt, id_counter) { + const textarea = gradioApp().querySelector(`#${id_prompt} > label > textarea`); + const counter = gradioApp().getElementById(id_counter); + + if (textarea && counter) { + onEdit(`${id_prompt}_BracketChecking`, textarea, 400, () => checkBrackets(textarea, counter)); + } +} + +onUiLoaded(function() { + setupBracketChecking('txt2img_prompt', 'txt2img_token_counter'); + setupBracketChecking('txt2img_neg_prompt', 'txt2img_negative_token_counter'); + setupBracketChecking('img2img_prompt', 'img2img_token_counter'); + setupBracketChecking('img2img_neg_prompt', 'img2img_negative_token_counter'); +});