-
Notifications
You must be signed in to change notification settings - Fork 439
feat: implement remaining ClassList methods #5233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
packages/@lwc/engine-server/src/__tests__/fixtures/getter-class-list-modify/config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "entry": "x/getter-class-list", | ||
| "ssrFiles": { | ||
| "error": "error-ssr.txt", | ||
| "expected": "expected-ssr.html" | ||
| } | ||
| } |
Empty file.
1 change: 1 addition & 0 deletions
1
packages/@lwc/engine-server/src/__tests__/fixtures/getter-class-list-modify/error.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| classList.forEach is not a function | ||
4 changes: 4 additions & 0 deletions
4
...ages/@lwc/engine-server/src/__tests__/fixtures/getter-class-list-modify/expected-ssr.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| <fixture-test class="a b c d-e f g h i" data-lwc-host-mutated="class"> | ||
| <template shadowrootmode="open"> | ||
| </template> | ||
| </fixture-test> |
Empty file.
25 changes: 25 additions & 0 deletions
25
...ests__/fixtures/getter-class-list-modify/modules/x/getter-class-list/getter-class-list.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { expect } from 'vitest'; | ||
| import { LightningElement } from 'lwc'; | ||
|
|
||
| export default class GetterClassList extends LightningElement { | ||
| connectedCallback() { | ||
| const { classList } = this; | ||
|
|
||
| classList.add('a', 'b', 'c', 'd-e', 'f', 'g', 'h', 'i'); | ||
| classList.forEach((value) => { | ||
| classList.remove(value); | ||
| }); | ||
| expect(this.getAttribute('class')).toBe(''); | ||
| expect(this.classList.length).toBe(0); | ||
|
|
||
| classList.add('a', 'b', 'c', 'd-e', 'f', 'g', 'h', 'i'); | ||
| while (classList.length > 0) { | ||
| classList.remove(classList.item(0)); | ||
| } | ||
| expect(this.getAttribute('class')).toBe(''); | ||
| expect(this.classList.length).toBe(0); | ||
|
|
||
| classList.add('a', 'b', 'c', 'd-e', 'f', 'g', 'h', 'i'); | ||
| expect(classList.item(3)).toBe('d-e'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -7,8 +7,6 @@ | |||
|
|
||||
| import type { LightningElement } from './lightning-element'; | ||||
|
|
||||
| const MULTI_SPACE = /\s+/g; | ||||
|
|
||||
| // Copied from lib.dom | ||||
| interface DOMTokenList { | ||||
| readonly length: number; | ||||
|
|
@@ -28,6 +26,15 @@ interface DOMTokenList { | |||
| [index: number]: string; | ||||
| } | ||||
|
|
||||
| const MULTI_SPACE = /\s+/g; | ||||
|
|
||||
| function parseClassName(className: string | null): string[] { | ||||
| return (className ?? '') | ||||
| .split(MULTI_SPACE) | ||||
| .map((item) => item.trim()) | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I don't think it's possible to split on |
||||
| .filter(Boolean); | ||||
| } | ||||
|
|
||||
| export class ClassList implements DOMTokenList { | ||||
| el: LightningElement; | ||||
|
|
||||
|
|
@@ -36,22 +43,19 @@ export class ClassList implements DOMTokenList { | |||
| } | ||||
|
|
||||
| add(...newClassNames: string[]) { | ||||
| const className = this.el.className; | ||||
| const set = new Set(className.split(MULTI_SPACE).filter(Boolean)); | ||||
| const set = new Set(parseClassName(this.el.className)); | ||||
| for (const newClassName of newClassNames) { | ||||
| set.add(newClassName); | ||||
| } | ||||
| this.el.className = Array.from(set).join(' '); | ||||
| } | ||||
|
|
||||
| contains(className: string) { | ||||
| const currentClassNameStr = this.el.className; | ||||
| return currentClassNameStr.split(MULTI_SPACE).includes(className); | ||||
| return parseClassName(this.el.className).includes(className); | ||||
| } | ||||
|
|
||||
| remove(...classNamesToRemove: string[]) { | ||||
| const className = this.el.className; | ||||
| const set = new Set(className.split(MULTI_SPACE).filter(Boolean)); | ||||
| const set = new Set(parseClassName(this.el.className)); | ||||
| for (const newClassName of classNamesToRemove) { | ||||
| set.delete(newClassName); | ||||
| } | ||||
|
|
@@ -60,8 +64,7 @@ export class ClassList implements DOMTokenList { | |||
|
|
||||
| replace(oldClassName: string, newClassName: string) { | ||||
| let classWasReplaced = false; | ||||
| const className = this.el.className; | ||||
| const listOfClasses = className.split(MULTI_SPACE).filter(Boolean) as string[]; | ||||
| const listOfClasses = parseClassName(this.el.className); | ||||
| listOfClasses.forEach((value, idx) => { | ||||
| if (value === oldClassName) { | ||||
| classWasReplaced = true; | ||||
|
|
@@ -73,8 +76,7 @@ export class ClassList implements DOMTokenList { | |||
| } | ||||
|
|
||||
| toggle(classNameToToggle: string, force?: boolean) { | ||||
| const classNameStr = this.el.className; | ||||
| const set = new Set(classNameStr.split(MULTI_SPACE).filter(Boolean)); | ||||
| const set = new Set(parseClassName(this.el.className)); | ||||
| if (!set.has(classNameToToggle) && force !== false) { | ||||
| set.add(classNameToToggle); | ||||
| } else if (set.has(classNameToToggle) && force !== true) { | ||||
|
|
@@ -93,22 +95,28 @@ export class ClassList implements DOMTokenList { | |||
| } | ||||
|
|
||||
| get length(): number { | ||||
| const currentClassNameStr = this.el.className ?? ''; | ||||
| return currentClassNameStr.split(MULTI_SPACE).length; | ||||
| return parseClassName(this.el.className).length; | ||||
| } | ||||
|
|
||||
| // Stubs to satisfy DOMTokenList interface | ||||
| [index: number]: never; // Can't implement arbitrary index getters without a proxy | ||||
| item(_index: number): string | null { | ||||
| throw new Error('Method "item" not implemented.'); | ||||
| } | ||||
| supports(_token: string): boolean { | ||||
| throw new Error('Method "supports" not implemented.'); | ||||
|
|
||||
| item(index: number): string | null { | ||||
| return parseClassName(this.el.className ?? '')[index] ?? null; | ||||
| } | ||||
|
|
||||
| forEach( | ||||
| _callbackfn: (value: string, key: number, parent: DOMTokenList) => void, | ||||
| _thisArg?: any | ||||
| callbackFn: (value: string, key: number, parent: DOMTokenList) => void, | ||||
| thisArg?: any | ||||
| ): void { | ||||
| throw new Error('Method "forEach" not implemented.'); | ||||
| parseClassName(this.el.className).forEach((value, index) => | ||||
| callbackFn.call(thisArg, value, index, this) | ||||
| ); | ||||
| } | ||||
|
|
||||
| // This method is present on DOMTokenList but throws an error in the browser when used | ||||
| // in connection with Element#classList. | ||||
| supports(_token: string): boolean { | ||||
| throw new TypeError('DOMTokenList has no supported tokens.'); | ||||
| } | ||||
| } | ||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR introduces functionality that is not present in SSRv1. It is unclear to me how the (internal) components that are failing with SSRv2 ever rendered with SSRv1, unless the codepath just wasn't hit for some reason.