Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Nov 27, 2022
1 parent fc4009d commit c5f3cc0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
29 changes: 16 additions & 13 deletions lib/experimental/html/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@
*/
class WP_HTML_Tag_Processor {
/**
* The maximum number of bookmarks allowed to exist at
* any given time.
*
* @see set_bookmark();
* @since 6.2.0
* @var int
Expand Down Expand Up @@ -1175,8 +1178,8 @@ private function apply_attributes_updates() {
usort( $this->attribute_updates, array( self::class, 'sort_start_ascending' ) );

foreach ( $this->attribute_updates as $diff ) {
$this->updated_html .= substr( $this->html, $this->updated_bytes, $diff->start - $this->updated_bytes );
$this->updated_html .= $diff->text;
$this->updated_html .= substr( $this->html, $this->updated_bytes, $diff->start - $this->updated_bytes );
$this->updated_html .= $diff->text;
$this->updated_bytes = $diff->end;
}

Expand Down Expand Up @@ -1241,7 +1244,7 @@ public function seek( $bookmark_name ) {
* loop somehow.
*/
++$this->seek_counter;
if($this->seek_counter > $this->max_seek_calls) {
if ( $this->seek_counter > $this->max_seek_calls ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
throw new Exception( "Too many seek() calls ({$this->seek_counter}) – did you run into an infinite loop accidentally? If not, you may adjust this via the `seek_limit( \$new_limit )` method." );
}
Expand All @@ -1250,7 +1253,7 @@ public function seek( $bookmark_name ) {

$this->apply_html_updates_and_stay_on_current_tag();

// Point this tag processor before the sought tag opener and consume it
// Point this tag processor before the sought tag opener and consume it.
$this->parsed_bytes = $this->bookmarks[ $bookmark_name ]->start;
$this->updated_bytes = $this->parsed_bytes;
$this->updated_html = substr( $this->html, 0, $this->updated_bytes );
Expand All @@ -1266,8 +1269,8 @@ public function seek( $bookmark_name ) {
* @param int $new_limit Optional. The maximum allowed number of seek() calls.
* @return number The allowed number of seek() calls.
*/
public function seek_limit( $new_limit=null ) {
if(is_int($new_limit)) {
public function seek_limit( $new_limit = null ) {
if ( is_int( $new_limit ) ) {
$this->max_seek_calls = $new_limit;
}
return $this->max_seek_calls;
Expand Down Expand Up @@ -1593,30 +1596,30 @@ public function get_updated_html() {
* @since 6.2.0
*/
private function apply_html_updates_and_stay_on_current_tag() {
// Short-circuit if there are no new updates to apply:
// Short-circuit if there are no new updates to apply.
if ( ! count( $this->classname_updates ) && ! count( $this->attribute_updates ) ) {
return;
}

// Otherwise: apply the updates, rewind before the current tag, and parse it again:
// Otherwise: apply the updates, rewind before the current tag, and parse it again.
$delta_between_updated_html_end_and_current_tag_end = substr(
$this->html,
$this->updated_bytes,
$this->tag_name_starts_at + $this->tag_name_length - $this->updated_bytes
);
$updated_html_up_to_current_tag_name_end = $this->updated_html . $delta_between_updated_html_end_and_current_tag_end;
$updated_html_up_to_current_tag_name_end = $this->updated_html . $delta_between_updated_html_end_and_current_tag_end;

// 1. Apply the attributes updates to the original HTML
$this->class_name_updates_to_attributes_updates();
$this->apply_attributes_updates();

// 2. Replace the original HTML with the updated HTML
$this->html = $this->updated_html . substr( $this->html, $this->updated_bytes );
$this->updated_html = $updated_html_up_to_current_tag_name_end;
$this->updated_bytes = strlen($this->updated_html);
$this->html = $this->updated_html . substr( $this->html, $this->updated_bytes );
$this->updated_html = $updated_html_up_to_current_tag_name_end;
$this->updated_bytes = strlen( $this->updated_html );

// 3. Point this tag processor at the original tag opener and consume it
$this->parsed_bytes = strlen($updated_html_up_to_current_tag_name_end) - $this->tag_name_length - 2;
$this->parsed_bytes = strlen( $updated_html_up_to_current_tag_name_end ) - $this->tag_name_length - 2;
$this->next_tag();
}

Expand Down
68 changes: 34 additions & 34 deletions phpunit/html/wp-html-tag-processor-bookmark-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class WP_HTML_Tag_Processor_Bookmark_Test extends WP_UnitTestCase {
public function test_set_bookmark() {
$p = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );
$p->next_tag( 'li' );
$this->assertTrue($p->set_bookmark( 'first li' ), 'Could not allocate a "first li" bookmark.');
$this->assertTrue( $p->set_bookmark( 'first li' ), 'Could not allocate a "first li" bookmark.' );
$p->next_tag( 'li' );
$this->assertTrue($p->set_bookmark( 'second li' ), 'Could not allocate a "second li" bookmark.');
$this->assertTrue($p->set_bookmark( 'first li' ), 'Could not move the "first li" bookmark.');
$this->assertTrue( $p->set_bookmark( 'second li' ), 'Could not allocate a "second li" bookmark.' );
$this->assertTrue( $p->set_bookmark( 'first li' ), 'Could not move the "first li" bookmark.' );
}

/**
Expand All @@ -37,9 +37,9 @@ public function test_set_bookmark() {
public function test_release_bookmark() {
$p = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );
$p->next_tag( 'li' );
$this->assertFalse($p->release_bookmark( 'first li' ), 'Released a non-existing bookmark.');
$this->assertFalse( $p->release_bookmark( 'first li' ), 'Released a non-existing bookmark.' );
$p->set_bookmark( 'first li' );
$this->assertTrue($p->release_bookmark( 'first li' ), 'Could not release a bookmark.');
$this->assertTrue( $p->release_bookmark( 'first li' ), 'Could not release a bookmark.' );
}

/**
Expand Down Expand Up @@ -104,11 +104,11 @@ public function test_seek() {
* @covers set_bookmark
* @covers apply_attributes_updates
*/
public function test_removing_long_attributes_doesnt_break_seek( ) {
public function test_removing_long_attributes_doesnt_break_seek() {
$input = <<<HTML
<button twenty_one_characters 7_chars></button><button></button>
HTML;
$p = new WP_HTML_Tag_Processor( $input );
$p = new WP_HTML_Tag_Processor( $input );
$p->next_tag( 'button' );
$p->set_bookmark( 'first' );
$p->next_tag( 'button' );
Expand All @@ -118,8 +118,8 @@ public function test_removing_long_attributes_doesnt_break_seek( ) {
$p->seek( 'first' ),
'Seek() to the first button has failed'
);
$p->remove_attribute('twenty_one_characters');
$p->remove_attribute('7_chars');
$p->remove_attribute( 'twenty_one_characters' );
$p->remove_attribute( '7_chars' );

$this->assertTrue(
$p->seek( 'second' ),
Expand All @@ -133,8 +133,8 @@ public function test_removing_long_attributes_doesnt_break_seek( ) {
* @covers seek
* @covers set_bookmark
*/
public function test_bookmarks_complex_use_case( ) {
$input = <<<HTML
public function test_bookmarks_complex_use_case() {
$input = <<<HTML
<div selected class="merge-message" checked>
<div class="select-menu d-inline-block">
<div checked class="BtnGroup MixedCaseHTML position-relative" />
Expand Down Expand Up @@ -178,7 +178,7 @@ public function test_bookmarks_complex_use_case( ) {
</div>
</div>
HTML;
$p = new WP_HTML_Tag_Processor( $input );
$p = new WP_HTML_Tag_Processor( $input );
$p->next_tag( 'div' );
$p->next_tag( 'div' );
$p->next_tag( 'div' );
Expand All @@ -193,35 +193,35 @@ public function test_bookmarks_complex_use_case( ) {
$p->set_bookmark( 'fourth button' );

$p->seek( 'first button' );
$p->set_attribute('type', 'submit');
$p->set_attribute( 'type', 'submit' );

$this->assertTrue(
$p->seek( 'third button' ),
'Seek() to the third button failed'
);
$p->remove_attribute('class');
$p->remove_attribute('type');
$p->remove_attribute('aria-expanded');
$p->set_attribute('id', 'rebase-and-merge');
$p->remove_attribute('data-details-container');
$p->remove_attribute( 'class' );
$p->remove_attribute( 'type' );
$p->remove_attribute( 'aria-expanded' );
$p->set_attribute( 'id', 'rebase-and-merge' );
$p->remove_attribute( 'data-details-container' );

$this->assertTrue(
$p->seek( 'first div' ),
'Seek() to the first div failed'
);
$p->set_attribute('checked', false);
$p->set_attribute( 'checked', false );

$this->assertTrue(
$p->seek( 'fourth button' ),
'Seek() to the fourth button failed'
);
$p->set_attribute('id', 'last-button');
$p->remove_attribute('class');
$p->remove_attribute('type');
$p->remove_attribute('checked');
$p->remove_attribute('aria-label');
$p->remove_attribute('disabled');
$p->remove_attribute('data-view-component');
$p->set_attribute( 'id', 'last-button' );
$p->remove_attribute( 'class' );
$p->remove_attribute( 'type' );
$p->remove_attribute( 'checked' );
$p->remove_attribute( 'aria-label' );
$p->remove_attribute( 'disabled' );
$p->remove_attribute( 'data-view-component' );

$this->assertTrue(
$p->seek( 'second button' ),
Expand Down Expand Up @@ -340,10 +340,10 @@ public function test_updates_bookmark_for_deletions_before_both_sides() {
public function test_limits_the_number_of_bookmarks() {
$p = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );
$p->next_tag( 'li' );
for($i=0;$i<WP_HTML_Tag_Processor::MAX_BOOKMARKS;$i++) {
for ( $i = 0;$i < WP_HTML_Tag_Processor::MAX_BOOKMARKS;$i++ ) {
$this->assertTrue( $p->set_bookmark( "bookmark $i" ), "Could not allocate the bookmark #$i" );
}
$this->assertFalse( $p->set_bookmark( "final bookmark" ), "Allocated $i bookmarks, which is one above the limit." );
$this->assertFalse( $p->set_bookmark( 'final bookmark' ), "Allocated $i bookmarks, which is one above the limit." );
}

/**
Expand All @@ -354,12 +354,12 @@ public function test_limits_the_number_of_bookmarks() {
public function test_limits_the_number_of_seek_calls() {
$p = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );
$p->next_tag( 'li' );
$p->set_bookmark('bookmark');
$p->set_bookmark( 'bookmark' );

for($i=0;$i<$p->seek_limit();$i++) {
for ( $i = 0;$i < $p->seek_limit();$i++ ) {
$this->assertTrue( $p->seek( 'bookmark' ), 'Could not seek to the "bookmark"' );
}
$this->assertFalse( $p->seek( "bookmark" ), "$i-th seek() to the bookmark succeeded, even though it should exceed the allowed limit." );
$this->assertFalse( $p->seek( 'bookmark' ), "$i-th seek() to the bookmark succeeded, even though it should exceed the allowed limit." );
}
/**
* @ticket 56299
Expand All @@ -368,13 +368,13 @@ public function test_limits_the_number_of_seek_calls() {
*/
public function test_adjusing_the_number_of_seek_calls() {
$p = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );
$p->seek_limit(2);
$p->seek_limit( 2 );

$p->next_tag( 'li' );
$p->set_bookmark('bookmark');
$p->set_bookmark( 'bookmark' );
$this->assertTrue( $p->seek( 'bookmark' ), 'Could not seek to the "bookmark"' );
$this->assertTrue( $p->seek( 'bookmark' ), 'Could not seek to the "bookmark"' );
$this->assertFalse( $p->seek( "bookmark" ), "The third seek() to the bookmark succeeded, even though it should exceed the allowed limit." );
$this->assertFalse( $p->seek( 'bookmark' ), 'The third seek() to the bookmark succeeded, even though it should exceed the allowed limit.' );
}

}

0 comments on commit c5f3cc0

Please sign in to comment.