Skip to content

Commit 83f9850

Browse files
bijela-gorajanosh
andauthored
Prevent empty style attributes on ul.selected > li and ul.options > li (#304)
* Prevent style attribute to be equal to ' null' string * migrate test to svelte 5 and fix expect by using hasAttribute() * bump eslint --------- Co-authored-by: Janosh Riebesell <[email protected]>
1 parent 816865f commit 83f9850

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ repos:
3838
exclude: changelog\.md
3939

4040
- repo: https://github.com/pre-commit/mirrors-eslint
41-
rev: v9.24.0
41+
rev: v9.26.0
4242
hooks:
4343
- id: eslint
4444
types: [file]

src/lib/MultiSelect.svelte

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,9 @@
546546
style={ulSelectedStyle}
547547
>
548548
{#each selected as option, idx (duplicates ? [key(option), idx] : key(option))}
549+
{@const selectedOptionStyle =
550+
[get_style(option, `selected`), liSelectedStyle].filter(Boolean).join(` `) ||
551+
null}
549552
<li
550553
class={liSelectedClass}
551554
role="option"
@@ -556,7 +559,7 @@
556559
ondrop={drop(idx)}
557560
ondragenter={() => (drag_idx = idx)}
558561
class:active={drag_idx === idx}
559-
style="{get_style(option, `selected`)} {liSelectedStyle}"
562+
style={selectedOptionStyle}
560563
>
561564
{#if selectedItem}
562565
{@render selectedItem({
@@ -689,6 +692,9 @@
689692
disabledTitle = defaultDisabledTitle,
690693
} = optionItem instanceof Object ? optionItem : { label: optionItem }}
691694
{@const active = activeIndex === idx}
695+
{@const optionStyle =
696+
[get_style(optionItem, `option`), liOptionStyle].filter(Boolean).join(` `) ||
697+
null}
692698
<li
693699
onclick={(event) => {
694700
if (!disabled) add(optionItem, event)
@@ -708,7 +714,7 @@
708714
}}
709715
role="option"
710716
aria-selected="false"
711-
style="{get_style(optionItem, `option`)} {liOptionStyle}"
717+
style={optionStyle}
712718
onkeydown={(event) => {
713719
if (!disabled && (event.key === `Enter` || event.code === `Space`)) {
714720
event.preventDefault()

tests/unit/MultiSelect.svelte.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,27 @@ test.each([
14781478
},
14791479
)
14801480

1481+
test.each([
1482+
{ prop: `liSelectedStyle`, css_selector: `ul.selected > li` },
1483+
{ prop: `liOptionStyle`, css_selector: `ul.options > li` },
1484+
])(
1485+
`MultiSelect doesn't add style attribute to element '$css_selector' if '$prop' prop not passed`,
1486+
async ({ prop, css_selector }) => {
1487+
mount(MultiSelect, {
1488+
target: document.body,
1489+
props: { options: [1, 2, 3], selected: [1] },
1490+
})
1491+
1492+
await tick()
1493+
1494+
const elem = doc_query(css_selector)
1495+
await tick()
1496+
1497+
const err_msg = `style attribute should be absent when '${prop}' not passed, but hasAttribute('style') is ${elem.hasAttribute(`style`)}`
1498+
expect(elem.hasAttribute(`style`), err_msg).toBe(false)
1499+
},
1500+
)
1501+
14811502
test.each([true, false, `desktop`] as const)(
14821503
`closeDropdownOnSelect=%s controls input focus and dropdown closing`,
14831504
async (closeDropdownOnSelect) => {

0 commit comments

Comments
 (0)