[wp-trac] [WordPress Trac] #64776: HTML API: set_modifiable_text() ignores leading newlines in PRE, LISTING

WordPress Trac noreply at wordpress.org
Mon Mar 2 12:56:37 UTC 2026


#64776: HTML API: set_modifiable_text() ignores leading newlines in PRE, LISTING
--------------------------+----------------------------
 Reporter:  jonsurrell    |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Future Release
Component:  HTML API      |    Version:  6.7
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+----------------------------
 The `::set_modifiable_text()` method on `WP_HTML_Tag_Processor` and
 `WP_HTML_Processor` will fail to include a leading newline in the provided
 plaintext content on the first text of `PRE` and `LISTING` elements.

 This is due to special rules for these elements that cause a single
 leading newline to be ignored immediately following the open tag.

 {{{#!php
 <?php
 $html_processor = WP_HTML_Processor::create_fragment('<pre>X</pre>');
 $html_processor->next_token();
 $html_processor->next_token(); // on the text node
 $html_processor->set_modifiable_text( "\nAFTER NEWLINE" );
 var_dump( $html_processor->get_modifiable_text() );
 // string(13) "AFTER NEWLINE"
 echo $html_processor->get_updated_html();
 /* Prints:
 <textarea>
 AFTER NEWLINE</textarea>
 */
 }}}

 Note that the newline is present in the updated HTML, however the HTML
 parsing rules cause the leading newline to be ignored.

 When rendered in the browser, this `PRE` element will report its
 `.textContent` as `AFTER NEWLINE` (with no leading newline) and the
 rendered element shows `AFTER NEWLINE` on the first line of the input box.

 In order for the `PRE` to begin with a newline in its content, an
 additional newline must be included.

 This is a follow-up to #64609 and [61754] which handled `TEXTAREA`.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/64776>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list