Skip to content

Commit 38f5860

Browse files
committed
Stop string call method arguments from getting spaces removed.
1 parent c738226 commit 38f5860

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

django_unicorn/call_method_parser.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,14 @@ def parse_args(args: str) -> List[Any]:
8181
"""
8282
found_args = []
8383
arg = ""
84+
8485
curly_bracket_count = 0
8586
square_bracket_count = 0
8687
parenthesis_count = 0
8788

89+
in_single_quote = False
90+
in_double_quote = False
91+
8892
def _eval_arg(_arg):
8993
try:
9094
_arg = literal_eval(_arg)
@@ -114,7 +118,7 @@ def _parse_arg(_arg):
114118
return _arg
115119

116120
for c in args:
117-
if not c.strip():
121+
if not in_single_quote and not in_double_quote and not c.strip():
118122
continue
119123

120124
if (
@@ -147,6 +151,10 @@ def _parse_arg(_arg):
147151
elif c == ")":
148152
parenthesis_count -= 1
149153
arg = _parse_arg(arg)
154+
elif c == "'":
155+
in_single_quote = not in_single_quote
156+
elif c == '"':
157+
in_double_quote = not in_double_quote
150158

151159
if arg:
152160
arg = _eval_arg(arg)

django_unicorn/views.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99

1010
import orjson
1111

12-
from .call_method_parser import (
13-
InvalidKwarg,
14-
parse_args,
15-
parse_call_method_name,
16-
parse_kwarg,
17-
)
12+
from .call_method_parser import InvalidKwarg, parse_call_method_name, parse_kwarg
1813
from .components import UnicornField, UnicornView
1914
from .errors import UnicornViewError
2015
from .utils import generate_checksum

example/unicorn/templates/unicorn/text-inputs.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@
3737
<button unicorn:click="set_name">set_name</button>
3838
<button unicorn:click="set_name()">set_name()</button>
3939
<button unicorn:click="set_name('')">set_name('')</button>
40-
<button unicorn:click="set_name('blob')">set_name('blob')</button>
40+
<button unicorn:click="set_name('blob 2')">set_name('blob 2')</button>
4141
<button unicorn:click="name='human'">name='human'</button>
4242

4343
<br />
4444
Hello {{ name|title }} 👋
45+
46+
<br />
47+
<button unicorn:click='set_name($event.target.value.trim())' value=' button value '>set_name($event.target.value.trim())</button>
4548
</div>

tests/call_method_parser/test_parse_args.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ def test_single_quote_str_arg():
2020
assert isinstance(actual[0], str)
2121

2222

23+
def test_str_with_space_arg():
24+
expected = ["django unicorn"]
25+
actual = parse_args("'django unicorn'")
26+
27+
assert actual == expected
28+
assert isinstance(actual[0], str)
29+
30+
31+
def test_complicated_str_with_space_arg():
32+
expected = ["django's \" ' unicorn"]
33+
actual = parse_args("django's \" ' unicorn")
34+
35+
assert actual == expected
36+
assert isinstance(actual[0], str)
37+
38+
2339
def test_double_quote_str_arg():
2440
expected = ["1"]
2541
actual = parse_args('"1"')

0 commit comments

Comments
 (0)