Skip to content

Add list slice#706

Merged
annevk merged 5 commits intowhatwg:mainfrom
jakearchibald:slice
Apr 14, 2026
Merged

Add list slice#706
annevk merged 5 commits intowhatwg:mainfrom
jakearchibald:slice

Conversation

@jakearchibald
Copy link
Copy Markdown
Contributor

For whatwg/html#12345.

I also changed the definition of clone, since it's just a slice with default args.

Comment thread infra.bs Outdated
<dfn export for=list/slice><var>to</var></dfn> (default |list|'s <a for=list>size</a>), is to create a new
<a>list</a> |slice|, of the same designation, and <a for=list>append</a> each
<a for=list>item</a> of |list|, where the index is greater than or equal to |from| and less than
|to|, to |slice|, maintaining order.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make this a list of steps for clarity. That way we can also assert the obvious things about from and to.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's fair.

Are you ok with clone being defined in terms of slice?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that seems good. Always nice to have fewer fundamental primitives.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@jakearchibald jakearchibald requested a review from annevk April 10, 2026 10:27
Comment thread infra.bs Outdated
Comment on lines +1673 to +1675
<li><p>If |from| is less than 0, then set |from| to 0.

<li><p>If |to| is greater than |list|'s <a for=list>size</a>, then set |to| to |list|'s <a for=list>size</a>.
Copy link
Copy Markdown
Contributor Author

@jakearchibald jakearchibald Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt this was preferable to asserting. It means, like in JS, you can slice from 0 to 100 to 'crop' a list to maximum of 100 items.

['foo'].slice(0, 100); // does not throw

Comment thread infra.bs Outdated
Comment on lines +1673 to +1675
<li><p>If |from| is less than 0, then set |from| to 0.

<li><p>If |to| is greater than |list|'s <a for=list>size</a>, then set |to| to |list|'s <a for=list>size</a>.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed for specifications? I might not be considering all scenarios that well, but I was assuming we'd just want to require both of these to be within bounds of the list.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind turning these into asserts. I was just copying how JS array.slice() works.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's start out with a very strict contract then until we hit cases where it would make sense to not be so strict.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@jakearchibald jakearchibald requested a review from annevk April 14, 2026 08:23
@annevk annevk merged commit 994009c into whatwg:main Apr 14, 2026
2 checks passed
@jakearchibald jakearchibald deleted the slice branch April 14, 2026 12:26
@jakearchibald
Copy link
Copy Markdown
Contributor Author

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants