Fix whitespace preservation in replacement strings (Issue #8)

This commit is contained in:
BRADSEC
2025-12-20 15:45:27 +10:00
parent a4bf85d9c4
commit c8b45da72e
3 changed files with 64 additions and 4 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
[project]
name = "comfyui_stringessentials"
description = "Simple string manipulation nodes for ComfyUI (strip/remove text strings, search and replace text strings, preview modified string outputs). Useful for modifying text prompts or results from LLM outputs. Nodes will be located under standard Add Node > utils menu. Node names: String Textbox, String Strip, String Multi Replace, String Conditional Append and String Preview."
version = "2.0.3"
version = "2.0.4"
license = {file = "LICENSE"}
[project.urls]
+3 -3
View File
@@ -35,13 +35,13 @@ class StringMultiReplaceNode:
# Create a list of all replacements
replacements = []
for line in replacement_pairs.splitlines():
line = line.strip()
if not line or replacement_delimiter not in line:
# Don't strip the line yet to preserve trailing whitespace in replacements (Issue #8)
if not line or line.isspace() or replacement_delimiter not in line:
continue
search_str, replace_str = line.split(replacement_delimiter, 1)
search_str = search_str.strip()
replace_str = replace_str.strip()
# Don't strip replace_str to allow intentional spaces/whitespace (Issue #8)
if not search_str:
continue
+60
View File
@@ -344,5 +344,65 @@ goodbye"""
self.assertEqual(result, "Hello world goodbye")
self.assertFalse(was_appended)
def test_replace_with_space(self):
# Test Issue #8: Replace with space (trailing space should be preserved)
input_string = "hello_world_test"
replacement_pairs = "_:: "
result = self.replace_node.string_replace(
input_string=input_string,
replacement_pairs=replacement_pairs,
replacement_delimiter="::",
match_case=False,
match_whole_string=False,
preserve_punctuation=True,
remove_extra_spaces=False
)[0]
self.assertEqual(result, "hello world test")
def test_replace_with_leading_space(self):
# Test Issue #8: Replace with leading space
input_string = "value1,value2,value3"
replacement_pairs = ",::, "
result = self.replace_node.string_replace(
input_string=input_string,
replacement_pairs=replacement_pairs,
replacement_delimiter="::",
match_case=False,
match_whole_string=False,
preserve_punctuation=True,
remove_extra_spaces=False
)[0]
self.assertEqual(result, "value1, value2, value3")
def test_replace_with_whitespace_only(self):
# Test Issue #8: Replace with only whitespace
input_string = "word1___word2"
replacement_pairs = "___:: "
result = self.replace_node.string_replace(
input_string=input_string,
replacement_pairs=replacement_pairs,
replacement_delimiter="::",
match_case=False,
match_whole_string=False,
preserve_punctuation=True,
remove_extra_spaces=False
)[0]
self.assertEqual(result, "word1 word2")
def test_replace_with_tab(self):
# Test Issue #8: Replace with tab character
input_string = "column1|column2|column3"
replacement_pairs = "|::\t"
result = self.replace_node.string_replace(
input_string=input_string,
replacement_pairs=replacement_pairs,
replacement_delimiter="::",
match_case=False,
match_whole_string=False,
preserve_punctuation=True,
remove_extra_spaces=False
)[0]
self.assertEqual(result, "column1\tcolumn2\tcolumn3")
if __name__ == '__main__':
unittest.main()