mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTML API: Add functions to read inner and outer HTML.
- Loading branch information
Showing
3 changed files
with
305 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
tests/phpunit/tests/html-api/wpHtmlProcessorGetInnerMarkup.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* Unit tests covering WP_HTML_Processor::get_inner_markup() | ||
* | ||
* @package WordPress | ||
* @subpackage HTML-API | ||
* | ||
* @since 6.4.0 | ||
* | ||
* @group html-api | ||
* | ||
* @coversDefaultClass WP_HTML_Processor | ||
*/ | ||
class Tests_HtmlApi_WpHtmlProcessorGetInnerMarkup extends WP_UnitTestCase { | ||
/** | ||
* @ticket {TICKET_NUMBER} | ||
* | ||
* @covers WP_HTML_Processor::get_inner_markup | ||
* | ||
* @since 6.4.0 | ||
*/ | ||
public function test_returns_null_when_not_on_a_matching_tag() { | ||
$p = WP_HTML_Processor::createFragment( '<p><div><span></span></div>' ); | ||
|
||
$this->assertNull( $p->get_inner_markup() ); | ||
|
||
$this->assertFalse( $p->next_tag( 'BUTTON' ), "Should not have found a BUTTON tag but stopped at {$p->get_tag()}." ); | ||
$this->assertNull( $p->get_inner_markup() ); | ||
} | ||
|
||
/** | ||
* @ticket {TICKET_NUMBER} | ||
* | ||
* @covers WP_HTML_Processor::get_inner_markup | ||
* | ||
* @dataProvider data_html_with_inner_markup | ||
* | ||
* @since 6.4.0 | ||
* | ||
* @param string $html_with_target_node HTML containing a node with the `target` attribute set. | ||
* @param string $expected_inner_markup Inner markup of target node. | ||
*/ | ||
public function test_returns_appropriate_inner_markup( $html_with_target_node, $expected_inner_markup ) { | ||
$p = WP_HTML_Processor::createFragment( $html_with_target_node ); | ||
|
||
while ( $p->next_tag() && null === $p->get_attribute( 'target' ) ) { | ||
continue; | ||
} | ||
|
||
$this->assertSame( $expected_inner_markup, $p->get_inner_markup(), 'Failed to return appropriate inner markup.' ); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array[] | ||
*/ | ||
public function data_html_with_inner_markup() { | ||
$data = array( | ||
'Empty elements' => array( '<div target></div>', '' ), | ||
'Element containing only text' => array( '<div target>inside</div>', 'inside' ), | ||
'Element with nested tags' => array( '<div target>inside <span>the</span> div</div>', 'inside <span>the</span> div' ), | ||
'Unclosed element' => array( '<div target>This is <em>all</em> inside the DIV', 'This is <em>all</em> inside the DIV' ), | ||
'Partially-closed element' => array( '<div target>This is <em>all</em> inside the DIV</div', 'This is <em>all</em> inside the DIV</div' ), | ||
'Implicitly-closed element' => array( '<div><p target>Inside the P</div>Outside the P</p>', 'Inside the P' ), | ||
); | ||
|
||
$inner_html = <<<HTML | ||
<p>This is inside the <strong>Match</strong></p> | ||
<p><img></p> | ||
<div> | ||
<figure> | ||
<img> | ||
<figcaption>Look at the <strike>picture</strike> photograph.</figcaption> | ||
</figure> | ||
</div> | ||
HTML; | ||
|
||
$html = <<<HTML | ||
<div> | ||
<p>This is not in the match. | ||
<p>This is another paragraph not <a href="#">in</a> the match. | ||
</div> | ||
<div target>{$inner_html}</div> | ||
<div> | ||
<p>This is also note in the match.</p> | ||
</div> | ||
HTML; | ||
$data['Complicated inner nesting'] = array( $html, $inner_html ); | ||
|
||
return $data; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
tests/phpunit/tests/html-api/wpHtmlProcessorGetOuterMarkup.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* Unit tests covering WP_HTML_Processor::get_outer_html() | ||
* | ||
* @package WordPress | ||
* @subpackage HTML-API | ||
* | ||
* @since 6.4.0 | ||
* | ||
* @group html-api | ||
* | ||
* @coversDefaultClass WP_HTML_Processor | ||
*/ | ||
class Tests_HtmlApi_WpHtmlProcessorGetOuterMarkup extends WP_UnitTestCase { | ||
/** | ||
* Ensures that it's not possible to get inner contents when not stopped at a tag in the HTML. | ||
* | ||
* @ticket {TICKET_NUMBER} | ||
* | ||
* @covers WP_HTML_Processor::get_outer_markup | ||
* | ||
* @since 6.4.0 | ||
*/ | ||
public function test_returns_null_when_not_on_a_matching_tag() { | ||
$p = WP_HTML_Processor::createFragment( '<p><div><span></span></div>' ); | ||
|
||
$this->assertNull( $p->get_outer_markup() ); | ||
|
||
$this->assertFalse( $p->next_tag( 'BUTTON' ), "Should not have found a BUTTON tag but stopped at {$p->get_tag()}." ); | ||
$this->assertNull( $p->get_outer_markup() ); | ||
} | ||
|
||
/** | ||
* @ticket {TICKET_NUMBER} | ||
* | ||
* @covers WP_HTML_Processor::get_outer_markup | ||
* | ||
* @dataProvider data_html_with_outer_markup | ||
* | ||
* @since 6.4.0 | ||
* | ||
* @param string $html_with_target_node HTML containing a node with the `target` attribute set. | ||
* @param string $expected_outer_markup Outer markup of target node. | ||
*/ | ||
public function test_returns_appropriate_outer_markup( $html_with_target_node, $expected_outer_markup ) { | ||
$p = WP_HTML_Processor::createFragment( $html_with_target_node ); | ||
|
||
while ( $p->next_tag() && null === $p->get_attribute( 'target' ) ) { | ||
continue; | ||
} | ||
|
||
$this->assertSame( $expected_outer_markup, $p->get_outer_markup(), 'Failed to return appropriate inner markup.' ); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array[] | ||
*/ | ||
public function data_html_with_outer_markup() { | ||
$data = array( | ||
'Empty elements' => array( '<div target></div>', '<div target></div>' ), | ||
'Element containing only text' => array( '<div target>inside</div>', '<div target>inside</div>' ), | ||
'Element with nested tags' => array( '<div target>inside <span>the</span> div</div>', '<div target>inside <span>the</span> div</div>' ), | ||
'Unclosed element' => array( '<div target>This is <em>all</em> inside the DIV', '<div target>This is <em>all</em> inside the DIV' ), | ||
'Partially-closed element' => array( '<div target>This is <em>all</em> inside the DIV</div', '<div target>This is <em>all</em> inside the DIV</div' ), | ||
'Implicitly-closed element' => array( '<div><p target>Inside the P</div>Outside the P</p>', '<p target>Inside the P' ), | ||
); | ||
|
||
$inner_html = <<<HTML | ||
<p>This is inside the <strong>Match</strong></p> | ||
<p><img></p> | ||
<div> | ||
<figure> | ||
<img> | ||
<figcaption>Look at the <strike>picture</strike> photograph.</figcaption> | ||
</figure> | ||
</div> | ||
HTML; | ||
|
||
$html = <<<HTML | ||
<div> | ||
<p>This is not in the match. | ||
<p>This is another paragraph not <a href="#">in</a> the match. | ||
</div> | ||
<div target>{$inner_html}</div> | ||
<div> | ||
<p>This is also note in the match.</p> | ||
</div> | ||
HTML; | ||
$data['Complicated inner nesting'] = array( $html, "<div target>{$inner_html}</div>" ); | ||
|
||
return $data; | ||
} | ||
} |