Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
Stable Diffusion Webui
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
novelai-storage
Stable Diffusion Webui
Commits
e847df7e
Commit
e847df7e
authored
Apr 29, 2023
by
AUTOMATIC1111
Committed by
GitHub
Apr 29, 2023
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9609 from akx/bracket-fix
prompt-bracket-checker: Simplify code + improve error reporting
parents
d6a3988b
fcc194af
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
91 deletions
+30
-91
extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js
...ompt-bracket-checker/javascript/prompt-bracket-checker.js
+30
-91
No files found.
extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js
View file @
e847df7e
// Stable Diffusion WebUI - Bracket checker
// Stable Diffusion WebUI - Bracket checker
// Version 1.0
// By Hingashi no Florin/Bwin4L & @akx
// By Hingashi no Florin/Bwin4L
// Counts open and closed brackets (round, square, curly) in the prompt and negative prompt text boxes in the txt2img and img2img tabs.
// 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
(
evt
,
textArea
,
counterElt
)
{
function
checkBrackets
(
textArea
,
counterElt
)
{
errorStringParen
=
'
(...) - Different number of opening and closing parentheses detected.
\n
'
;
var
counts
=
{};
errorStringSquare
=
'
[...] - Different number of opening and closing square brackets detected.
\n
'
;
(
textArea
.
value
.
match
(
/
[
(){}
\[\]]
/g
)
||
[]).
forEach
(
bracket
=>
{
errorStringCurly
=
'
{...} - Different number of opening and closing curly brackets detected.
\n
'
;
counts
[
bracket
]
=
(
counts
[
bracket
]
||
0
)
+
1
;
});
openBracketRegExp
=
/
\(
/g
;
var
errors
=
[];
closeBracketRegExp
=
/
\)
/g
;
function
checkPair
(
open
,
close
,
kind
)
{
openSquareBracketRegExp
=
/
\[
/g
;
if
(
counts
[
open
]
!==
counts
[
close
])
{
closeSquareBracketRegExp
=
/
\]
/g
;
errors
.
push
(
`
${
open
}
...
${
close
}
- Detected
${
counts
[
open
]
||
0
}
opening and
${
counts
[
close
]
||
0
}
closing
${
kind
}
.`
openCurlyBracketRegExp
=
/
\{
/g
;
);
closeCurlyBracketRegExp
=
/
\}
/g
;
totalOpenBracketMatches
=
0
;
totalCloseBracketMatches
=
0
;
totalOpenSquareBracketMatches
=
0
;
totalCloseSquareBracketMatches
=
0
;
totalOpenCurlyBracketMatches
=
0
;
totalCloseCurlyBracketMatches
=
0
;
openBracketMatches
=
textArea
.
value
.
match
(
openBracketRegExp
);
if
(
openBracketMatches
)
{
totalOpenBracketMatches
=
openBracketMatches
.
length
;
}
closeBracketMatches
=
textArea
.
value
.
match
(
closeBracketRegExp
);
if
(
closeBracketMatches
)
{
totalCloseBracketMatches
=
closeBracketMatches
.
length
;
}
openSquareBracketMatches
=
textArea
.
value
.
match
(
openSquareBracketRegExp
);
if
(
openSquareBracketMatches
)
{
totalOpenSquareBracketMatches
=
openSquareBracketMatches
.
length
;
}
closeSquareBracketMatches
=
textArea
.
value
.
match
(
closeSquareBracketRegExp
);
if
(
closeSquareBracketMatches
)
{
totalCloseSquareBracketMatches
=
closeSquareBracketMatches
.
length
;
}
openCurlyBracketMatches
=
textArea
.
value
.
match
(
openCurlyBracketRegExp
);
if
(
openCurlyBracketMatches
)
{
totalOpenCurlyBracketMatches
=
openCurlyBracketMatches
.
length
;
}
closeCurlyBracketMatches
=
textArea
.
value
.
match
(
closeCurlyBracketRegExp
);
if
(
closeCurlyBracketMatches
)
{
totalCloseCurlyBracketMatches
=
closeCurlyBracketMatches
.
length
;
}
if
(
totalOpenBracketMatches
!=
totalCloseBracketMatches
)
{
if
(
!
counterElt
.
title
.
includes
(
errorStringParen
))
{
counterElt
.
title
+=
errorStringParen
;
}
}
else
{
counterElt
.
title
=
counterElt
.
title
.
replace
(
errorStringParen
,
''
);
}
if
(
totalOpenSquareBracketMatches
!=
totalCloseSquareBracketMatches
)
{
if
(
!
counterElt
.
title
.
includes
(
errorStringSquare
))
{
counterElt
.
title
+=
errorStringSquare
;
}
}
else
{
counterElt
.
title
=
counterElt
.
title
.
replace
(
errorStringSquare
,
''
);
}
if
(
totalOpenCurlyBracketMatches
!=
totalCloseCurlyBracketMatches
)
{
if
(
!
counterElt
.
title
.
includes
(
errorStringCurly
))
{
counterElt
.
title
+=
errorStringCurly
;
}
}
}
else
{
counterElt
.
title
=
counterElt
.
title
.
replace
(
errorStringCurly
,
''
);
}
}
if
(
counterElt
.
title
!=
''
)
{
checkPair
(
'
(
'
,
'
)
'
,
'
round brackets
'
);
counterElt
.
classList
.
add
(
'
error
'
);
checkPair
(
'
[
'
,
'
]
'
,
'
square brackets
'
);
}
else
{
checkPair
(
'
{
'
,
'
}
'
,
'
curly brackets
'
);
counterElt
.
classList
.
remove
(
'
error
'
);
counterElt
.
title
=
errors
.
join
(
'
\n
'
);
}
counterElt
.
classList
.
toggle
(
'
error
'
,
errors
.
length
!==
0
);
}
}
function
setupBracketChecking
(
id_prompt
,
id_counter
){
function
setupBracketChecking
(
id_prompt
,
id_counter
)
{
var
textarea
=
gradioApp
().
querySelector
(
"
#
"
+
id_prompt
+
"
> label > textarea
"
);
var
textarea
=
gradioApp
().
querySelector
(
"
#
"
+
id_prompt
+
"
> label > textarea
"
);
var
counter
=
gradioApp
().
getElementById
(
id_counter
)
var
counter
=
gradioApp
().
getElementById
(
id_counter
)
textarea
.
addEventListener
(
"
input
"
,
function
(
evt
)
{
if
(
textarea
&&
counter
)
{
checkBrackets
(
evt
,
textarea
,
counter
)
textarea
.
addEventListener
(
"
input
"
,
()
=>
checkBrackets
(
textarea
,
counter
));
});
}
}
}
onUiLoaded
(
function
(){
onUiLoaded
(
function
()
{
setupBracketChecking
(
'
txt2img_prompt
'
,
'
txt2img_token_counter
'
)
setupBracketChecking
(
'
txt2img_prompt
'
,
'
txt2img_token_counter
'
);
setupBracketChecking
(
'
txt2img_neg_prompt
'
,
'
txt2img_negative_token_counter
'
)
setupBracketChecking
(
'
txt2img_neg_prompt
'
,
'
txt2img_negative_token_counter
'
);
setupBracketChecking
(
'
img2img_prompt
'
,
'
img2img_token_counter
'
)
setupBracketChecking
(
'
img2img_prompt
'
,
'
img2img_token_counter
'
);
setupBracketChecking
(
'
img2img_neg_prompt
'
,
'
img2img_negative_token_counter
'
)
setupBracketChecking
(
'
img2img_neg_prompt
'
,
'
img2img_negative_token_counter
'
);
})
});
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment