Commit b19cee6
committed
bug #3031 [LiveComponent] Return empty string for
This PR was squashed before being merged into the 2.x branch.
Discussion
----------
[LiveComponent] Return empty string for `data-value=""` instead of falling back to null
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no <!-- please update src/**/CHANGELOG.md files -->
| Docs? | no <!-- required for new features -->
| Issues | # <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License | MIT
## Problem
<details>
<summary><b>Demo video</b></summary>
https://github.com/user-attachments/assets/31bba6af-8690-4cd9-a5c4-20a561342626
</details>
```html
<input type="text" data-model="post.title">
<button
type="button"
data-model="post.title"
data-value="" <!-- HERE -->
data-action="live#update"
>
Clear Title
</button>
```
When using `data-value=""` on an element, the current implementation incorrectly returns `null` instead of the intended empty string value. This happens because the condition `if (element.dataset.value)` evaluates to `false` for empty strings (since empty strings are falsy in JavaScript), causing the function to fall through to the final `return null;` statement.
## Impact
This behavior prevents developers from explicitly setting empty string values via the `data-value` attribute, which is a common use case for clearing form fields or resetting values to empty states rather than `null`.
### Current behavior:
```html
<button data-model="title" data-value="" data-action="live#update">Clear</button>
<!-- Results in: null -->
```
### Expected behavior:
```html
<button data-model="title" data-value="" data-action="live#update">Clear</button>
<!-- Should result in: "" -->
```
<details>
<summary><b>After (demo video)</b></summary>
https://github.com/user-attachments/assets/bad5f5f3-1104-4102-a2bc-30264bb3c5bf
</details>
## Changes
Replace the truthiness check `if (element.dataset.value)` with an explicit attribute existence check `if (element.hasAttribute("data-value"))`. This ensures that any `data-value` attribute, including those with empty string values, are properly processed and returned.
## Manual testing done
- ✅ `data-value=""` now returns `""`
- ✅ `data-value="test"` still returns `"test"`
- ✅ Missing `data-value` attribute still falls back to other value sources
- ✅ All existing functionality remains intact
Commits
-------
d0c0670 [LiveComponent] Return empty string for `data-value=""` instead of falling back to nulldata-value="" instead of falling back to null (mercuryseries)File tree
3 files changed
+36
-2
lines changed- src/LiveComponent/assets
- dist
- src
- test/unit
3 files changed
+36
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
377 | 377 | | |
378 | 378 | | |
379 | 379 | | |
380 | | - | |
| 380 | + | |
381 | 381 | | |
382 | 382 | | |
383 | 383 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
113 | 147 | | |
114 | 148 | | |
115 | 149 | | |
| |||
0 commit comments