Make WordPress Core

Changeset 61754


Ignore:
Timestamp:
02/27/2026 12:55:51 PM (4 weeks ago)
Author:
jonsurrell
Message:

HTML API: Preserve ::set_modifiable_text() TEXTAREA leading newlines.

HTML specifies that a single newline is ignored at the start of a TEXTAREA. If ::set_modifiable_text() is called with a leading newline, ensure it is preserved in the resulting HTML.

Developed in https://github.com/WordPress/wordpress-develop/pull/11062.

Props jonsurrell, dmsnell.
See #64609.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/html-api/class-wp-html-tag-processor.php

    r61477 r61754  
    38773877                    $plaintext_content
    38783878                );
     3879
     3880                /*
     3881                 * HTML ignores a single leading newline in this context. If a leading newline
     3882                 * is intended, preserve it by adding an extra newline.
     3883                 */
     3884                if (
     3885                    'TEXTAREA' === $this->get_tag() &&
     3886                    1 === strspn( $plaintext_content, "\n\r", 0, 1 )
     3887                ) {
     3888                    $plaintext_content = "\n{$plaintext_content}";
     3889                }
    38793890
    38803891                /*
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessorModifiableText.php

    r61477 r61754  
    637637        );
    638638    }
     639
     640    /**
     641     * TEXTAREA elements ignore the first newline in their content.
     642     * Setting the modifiable text with a leading newline should ensure that the leading newline
     643     * is present in the resulting element.
     644     *
     645     * @ticket 64609
     646     */
     647    public function test_modifiable_text_special_textarea() {
     648        $processor = new WP_HTML_Tag_Processor( '<textarea></textarea>' );
     649        $processor->next_token();
     650        $processor->set_modifiable_text( "\nAFTER NEWLINE" );
     651        $this->assertSame(
     652            "\nAFTER NEWLINE",
     653            $processor->get_modifiable_text(),
     654            'Should have preserved the leading newline in the content.'
     655        );
     656        $this->assertEqualHTML(
     657            <<<'HTML'
     658            <textarea>
     659
     660            AFTER NEWLINE</textarea>
     661            HTML,
     662            $processor->get_updated_html(),
     663            '<body>',
     664            'Should have preserved the leading newline in the content.'
     665        );
     666    }
    639667}
Note: See TracChangeset for help on using the changeset viewer.