Skip to content

Commit

Permalink
More test cases, a couple new failures
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Jan 6, 2023
1 parent f415bdb commit 6103139
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/experimental/html/class-wp-html-attribute-sourcer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @TODO:
* - [ ] Handle multiple joined constraints for classes and attributes
* e.g. ".locale-en-US.localized[data-translation-id][data-translate]"
* - [ ] Handle comma-separated selector sequences; apparently we only grab the first right now
*/

/**
Expand Down Expand Up @@ -158,14 +159,18 @@ public static function select_match( $tags, $s ) {
return $tags;
}

/**
* @TODO: This needs to be able to continue to the next match
* Pass in $tags? Pass in a bookmark?
*/
public static function select( $selectors, $html ) {
$selector_index = 0;

selector_choice:
$tags = new WP_HTML_Processor( $html );
$outer_state = $tags->new_state();

$next = $selectors[$selector_index];
$next = $selectors[ $selector_index ];

loop:
while ( $tags->balanced_next( $outer_state ) ) {
Expand Down
5 changes: 5 additions & 0 deletions lib/experimental/html/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @TODO: Handle self-closing foreign elements.
* @TODO: Detect non-normative HTML input.
* @TODO: Consider parsing non-normative HTML input, support adoption agency algorithm.
* @TODO: Figure out how multiple external states can conflict.
*
* If we support non-normative HTML we can probably handle significantly more
* HTML without introducing unexpected results, but I'm not sure yet if we can
Expand Down Expand Up @@ -65,6 +66,10 @@ public function balanced_next( WP_HTML_Processor_Scan_State $state, $query = nul
* need to separately track those, but their behavior matches
* this case. The self-closing flag is ignored for HTML5 tags.
*/
if ( 0 === $state->relative_depth() ) {
return false;
}

break;

case 'opener':
Expand Down
50 changes: 50 additions & 0 deletions phpunit/html/wp-html-attribute-sourcer-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,56 @@ class WP_UnitTestCase extends PHPUnit\Framework\TestCase {}
* @coversDefaultClass WP_HTML_Attribute_Sourcer
*/
class WP_HTML_Attribute_Sourcer_Test extends WP_UnitTestCase {
/**
* @dataProvider data_ids_and_their_selectors
*/
public function test_selects_proper_html_from_selector( $wanted_ids, $selector ) {
$html = <<<HTML
<main id="main">
<section id="main-section">
<header id="header"><h2 id="page-title">It's a post!</h2></header>
</section>
<section id="post-section" class="wp-post post-15 post">
<h2 id="post-title" class="post-title">The antics of ants with antlers</h2>
<p>
<img id="ant-logo" href="ant.jpg">
<strong id="strong-ants">Ants</strong>
<em id="funny-stuff">with antlers can be funny</em>.
</p>
<ul id="ul-list">
<li id="li-1">Decorations</li>
<li id="li-2">Cleanup crew</li>
<li id="li-3">Spooky visitors</li>
</ul>
</section>
</main>
HTML;

list( $selectors ) = WP_HTML_Attribute_Sourcer::parse_selector( $selector );
$this->assertIsArray( $selectors );

$found_ids = array();
if ( $tags = WP_HTML_Attribute_Sourcer::select( [ $selectors ], $html ) ) {
$found_ids[] = $tags->get_attribute( 'id' );
}

$this->assertEqualsCanonicalizing( $wanted_ids, $found_ids );
}

public function data_ids_and_their_selectors() {
return array(
array( array( 'li-1' ), 'li' ),
array( array(), 'section > p img + em' ),
array( array( 'strong-ants' ), 'section > p img + strong' ),
array( array( 'funny-stuff' ), 'section > p strong + em' ),
array( array( 'page-title' ), 'section h2' ),
array( array( 'post-title' ), 'section > h2' ),
array( array( 'ant-logo' ), '[href]' ),
array( array(), '.non-existent' ),
);
}

/**
* @dataProvider data_single_combinators
*/
Expand Down

0 comments on commit 6103139

Please sign in to comment.