-
Notifications
You must be signed in to change notification settings - Fork 3.2k
WIP: HTML API: Introduce make_tag helper for creating HTML tags
#4065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
b7d2cd9
0c1ae37
2664112
76a09ca
81e5e4e
6a3080c
4edab74
73c81d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| class WP_HTML_Spec { | ||
| /** | ||
| * @see https://html.spec.whatwg.org/#elements-2 | ||
| */ | ||
| public static function is_void_element( $tag_name ) { | ||
| switch ( strtoupper( $tag_name ) ) { | ||
| case 'AREA': | ||
| case 'BASE': | ||
| case 'BR': | ||
| case 'COL': | ||
| case 'EMBED': | ||
| case 'HR': | ||
| case 'IMG': | ||
| case 'INPUT': | ||
| case 'LINK': | ||
| case 'META': | ||
| case 'SOURCE': | ||
| case 'TRACK': | ||
| case 'WBR': | ||
| return true; | ||
|
|
||
| default: | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| class WP_HTML { | ||
| public static function make_tag( $tag_name, $attributes = null, $data = '' ) { | ||
|
||
| $is_void = WP_HTML_Spec::is_void_element( $tag_name ); | ||
| $html = $is_void ? "<{$tag_name}>" : "<{$tag_name}>{$data}</{$tag_name}>"; | ||
|
|
||
| $p = new WP_HTML_Tag_Processor( $html ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| if ( is_array( $attributes ) ) { | ||
| $p->next_tag(); | ||
| foreach ( $attributes as $name => $value ) { | ||
| $p->set_attribute( $name, $value ); | ||
| } | ||
| } | ||
|
|
||
| return $p->get_updated_html(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
| /** | ||
| * Unit tests covering WP_HTML functionality. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage HTML-API | ||
| */ | ||
|
|
||
| /** | ||
| * @group html-api | ||
| * | ||
| * @coversDefaultClass WP_HTML | ||
| */ | ||
| class Tests_HtmlApi_wpHtml extends WP_UnitTestCase { | ||
| public function test_make_tag() { | ||
| $script = WP_HTML::make_tag( | ||
| 'script', | ||
| array( 'defer' => true, 'nonce' => 'HXhIuUk5lfXb' ), | ||
| 'console.log("loaded");' | ||
| ); | ||
|
|
||
| $this->assertSame( '<script defer nonce="HXhIuUk5lfXb">console.log("loaded");</script>', $script ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is something I've meant to create in the HTML processor branches I've been working in. the idea is to move utilities which related to HTML-defined terminology into here.
that would be:
is_void_element()is_html_element()is_format_element()is_html_element()will be important for HTML parsing because foreign elements may be self-closing, but HTML elements may not be, and therefore we have to ignore the trailing/in something like an<img />tag and look for a closing tag if it's not a void element, such as with<div />(because while it looks like a self-closer it isn't), but<wp-self-closer />is due to it being a foreign element.