Make WordPress Core

Opened 5 months ago

Closed 5 months ago

Last modified 4 weeks ago

#62582 closed enhancement (fixed)

Support length property on strings and arrays on the server

Reported by: jonsurrell's profile jonsurrell Owned by: gziolo's profile gziolo
Milestone: 6.8 Priority: normal
Severity: normal Version: 6.5
Component: Interactivity API Keywords: has-patch has-unit-tests has-dev-note
Focuses: Cc:

Description (last modified by jonsurrell)

The Interactivity API does not support the .length property on numeric arrays or strings on the server. This is one place where the behavior of server rendering is markedly different from client side rendering. The Interactivity API tries to align client and server rendering so that the behavior is the same.

String and array have a .length property in JavaScript that the directives processor can provide in order to support directives like the following:

<div data-wp-interactive="example">
  <div data-wp-bind--hidden="!state.list.length">
    <input type="range" min="1" data-wp-bind--max="state.list.length">
  </div>
  <div data-wp-bind--hidden="!state.string.length">
    <h1 data-wp-text="state.string"></h1>
  </div>
</div>

Testing for falsy .length is a common JavaScript idiom.

Change History (9)

This ticket was mentioned in PR #7751 on WordPress/wordpress-develop by @jonsurrell.


5 months ago
#1

  • Keywords has-patch added

Add .length to directives processing on the server on strings and numeric arrays.

The Interactivity API does not support the .length property on numeric arrays or strings on the server. This is one place where the behavior of server rendering is markedly different from client side rendering. The Interactivity API tries to align client and server rendering so that the behavior is the same.

String and array have a .length property in JavaScript that the directives processor can provide in order to support directives like the following:

<div data-wp-interactive="example">
  <div data-wp-bind--hidden="!state.list.length">
    <input type="range" min="1" data-wp-bind--max="state.list.length">
  </div>
  <div data-wp-bind--hidden="!state.string.length">
    <h1 data-wp-text="state.string"></h1>
  </div>
</div>

Testing for falsy .length is a common JavaScript idiom.

Trac ticket: https://core.trac.wordpress.org/ticket/62582

@jonsurrell commented on PR #7751:


5 months ago
#2

This is ready for review @luisherranz @michalczaplinski @darerodz @cbravobernal.

This ticket was mentioned in Slack in #meta-tracdev by jonsurrell. View the logs.


5 months ago

#4 @jonsurrell
5 months ago

  • Description modified (diff)

#5 @gziolo
5 months ago

  • Keywords has-unit-tests added
  • Milestone changed from Awaiting Review to 6.8

#6 @gziolo
5 months ago

  • Owner set to gziolo
  • Resolution set to fixed
  • Status changed from new to closed

In 59477:

Interactivity API: Support length property on strings and arrays on the server

The Interactivity API tries to align client and server rendering so that the behavior is the same. Adds missing handling for .length to directives processing on the server on strings and numeric arrays which is inherently supported through JavaScript language on the client.

Props jonsurrell, gziolo, luisherranz.
Fixes #62582.

#7 @gziolo
5 months ago

  • Keywords needs-dev-note added

#9 @JeffPaul
4 weeks ago

  • Keywords has-dev-note added; needs-dev-note removed

Note that the dev note has been published as Interactivity API best practices in 6.8.

Note: See TracTickets for help on using tickets.