Make WordPress Core


Ignore:
Timestamp:
12/02/2024 08:08:34 AM (12 months ago)
Author:
gziolo
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php

    r59398 r59477  
    15421542        $this->assertNull( $element );
    15431543    }
     1544
     1545    /**
     1546     * Verify behavior of .length directive access.
     1547     *
     1548     * @ticket 62582
     1549     *
     1550     * @covers ::process_directives
     1551     *
     1552     * @dataProvider data_length_directives
     1553     *
     1554     * @param mixed $value     The property value.
     1555     * @param string $expected The expected property length as a string,
     1556     *                         or "" if no length is expected.
     1557     */
     1558    public function test_process_directives_string_array_length( $value, string $expected ) {
     1559        $this->interactivity->state(
     1560            'myPlugin',
     1561            array( 'prop' => $value )
     1562        );
     1563        $html           = '<div data-wp-text="myPlugin::state.prop.length"></div>';
     1564        $processed_html = $this->interactivity->process_directives( $html );
     1565        $processor      = new WP_HTML_Tag_Processor( $processed_html );
     1566        $processor->next_tag( 'DIV' );
     1567        $processor->next_token();
     1568        $this->assertSame( $expected, $processor->get_modifiable_text() );
     1569    }
     1570
     1571    /**
     1572     * Data provider.
     1573     *
     1574     * @return array
     1575     */
     1576    public static function data_length_directives(): array {
     1577        return array(
     1578            'numeric array'     => array( array( 'a', 'b', 'c' ), '3' ),
     1579            'empty array'       => array( array(), '0' ),
     1580            'string'            => array( 'abc', '3' ),
     1581            'empty string'      => array( '', '0' ),
     1582
     1583            // Failure cases resulting in empty string.
     1584            'non-numeric array' => array( array( 'a' => 'a' ), '' ),
     1585            'object'            => array( new stdClass(), '' ),
     1586        );
     1587    }
    15441588}
Note: See TracChangeset for help on using the changeset viewer.