Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename to WP_HTML::tag, add non-escaping counterpart
  • Loading branch information
dmsnell committed Feb 14, 2023
commit 0c1ae3706a677aa55e5854d2282ea08e3eb2af1a
8 changes: 6 additions & 2 deletions src/wp-includes/html-api/class-wp-html.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

class WP_HTML {
public static function make_tag( $tag_name, $attributes = null, $data = '' ) {
public static function tag( $tag_name, $attributes = null, $inner_text = '' ) {
return WP_HTML::tag_with_inner_html( $tag_name, $attributes, esc_html( $inner_text ) );
}

public static function tag_with_inner_html( $tag_name, $attributes = null, $inner_html = '' ) {
$is_void = WP_HTML_Spec::is_void_element( $tag_name );
$html = $is_void ? "<{$tag_name}>" : "<{$tag_name}>{$data}</{$tag_name}>";
$html = $is_void ? "<{$tag_name}>" : "<{$tag_name}>{$inner_html}</{$tag_name}>";

$p = new WP_HTML_Tag_Processor( $html );
Copy link
Contributor

Choose a reason for hiding this comment

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

This surprised me, as I was fully expecting manual stitching of the strings, but I think it makes total sense. Tag Processor handles boolean values, escaping, and probably a few more cases we would otherwise have to reimplement here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I considered it and it might make sense to construct it. this is more of an exploration of sharing the tag processor for this utility. I think initially I was looking at returning the processor itself, which would make it possible to modify after creating. the thought of that was for conditional attributes, but here that's already possible by setting an attribute to false


Expand Down
14 changes: 12 additions & 2 deletions tests/phpunit/tests/html-api/wpHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@
* @coversDefaultClass WP_HTML
*/
class Tests_HtmlApi_wpHtml extends WP_UnitTestCase {
public function test_make_tag() {
$script = WP_HTML::make_tag(
public function test_tag() {
$script = WP_HTML::tag(
'script',
array( 'defer' => true, 'nonce' => 'HXhIuUk5lfXb' ),
'console.log("loaded");'
);

$this->assertSame( '<script defer nonce="HXhIuUk5lfXb">console.log("loaded");</script>', $script );
}

public function test_tag_with_escapable_text() {
$div = WP_HTML::tag( 'div', null, '4 < <em>9</em>. <3' );
$this->assertSame( '<div>4 &lt; &lt;em>9&lt;/em>. &lt;3</div>', $div );
}

public function test_tag_with_inner_html_does_not_escape() {
$div = WP_HTML::tag_with_inner_html( 'div', null, '4 < <em>9</em>. <3' );
$this->assertSame( '<div>4 < <em>9</em>. <3</div>', $div );
}
}