Skip to content

Commit

Permalink
Fix parseAttribute Function to Handle Multiple Nodes (#243)
Browse files Browse the repository at this point in the history
* bug(DOMHelpers): fix parseAttributes to search on all nodes for matching values

* chore(changeset): add changeset

* style(PostTypeBlockInterface): phpcs fix
  • Loading branch information
theodesp authored Jun 17, 2024
1 parent 8d4ab6c commit 39e8181
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-colts-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpengine/wp-graphql-content-blocks": patch
---

Bug fix: CoreTable column alignment returns null
3 changes: 2 additions & 1 deletion includes/Type/InterfaceType/PostTypeBlockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public static function register_type( string $post_type, array $block_names = []
],
'args' => [
'flat' => [
'type' => 'Boolean',
'description' => __( 'Returns the list of blocks as a flat list if true', 'wp-graphql-content-blocks' ),
'type' => 'Boolean',
],
],
'description' => sprintf(
Expand Down
9 changes: 7 additions & 2 deletions includes/Utilities/DOMHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ public static function parseAttribute( $html, $selector, $attribute, $default_va
if ( empty( $selector ) ) {
$selector = '*';
}
$node = $doc->find( $selector );
$nodes = $doc->find( $selector );
$default_value = isset( $default_value ) ? $default_value : null;
return ( ! empty( $node ) && isset( $node[0] ) ) ? $node[0]->getAttribute( $attribute ) : $default_value;
foreach ( $nodes as $node ) {
if ( $node->hasAttribute( $attribute ) ) {
return $node->getAttribute( $attribute );
}
}
return $default_value;
}

/**
Expand Down
46 changes: 30 additions & 16 deletions tests/unit/DOMHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@

final class DOMHelpersTest extends PluginTestCase {
public function testParseAttribute(): void {
$html = '<p id="foo-id" class="foo-class" data="foo-data"><span >Bar</span></p>';
$html = '<p id="foo-id" class="foo-class" data="foo-data"><span >Bar</span></p>';
$html2 = '<td class="has-text-align-center" data-align="center">Content 1</td>
<td class="has-text-align-right" data-align="right">Content 2</td>';
$html3 = '<div class="container"><span data-align="left">Left</span><span data-align="right">Right</span></div>';
$no_existent_selector = '#foo';
$id_selector = '#foo-id';
$class_selector = '.foo-class';
$element_selector = 'p';
$data_attribute = 'data';
$class_attribute = 'class';
$id_attribute = 'id';
$id_selector = '#foo-id';
$class_selector = '.foo-class';
$element_selector = 'p';
$data_attribute = 'data';
$class_attribute = 'class';
$id_attribute = 'id';

// $html
$this->assertNull( DOMHelpers::parseAttribute( $html, $no_existent_selector, $data_attribute ) );
$this->assertEquals( DOMHelpers::parseAttribute( $html, $no_existent_selector, $data_attribute, 'Bar' ), 'Bar' );
$this->assertEquals( DOMHelpers::parseAttribute( $html, $id_selector, $data_attribute ), 'foo-data' );
Expand All @@ -26,14 +30,24 @@ public function testParseAttribute(): void {
$this->assertEquals( DOMHelpers::parseAttribute( $html, $element_selector, $data_attribute ), 'foo-data' );
$this->assertEquals( DOMHelpers::parseAttribute( $html, $element_selector, $class_attribute ), 'foo-class' );
$this->assertEquals( DOMHelpers::parseAttribute( $html, $element_selector, $id_attribute ), 'foo-id' );

// $html2
$this->assertEquals( 'center', DOMHelpers::parseAttribute( $html2, '*', 'data-align' ) );
$this->assertEquals( 'right', DOMHelpers::parseAttribute( $html2, '.has-text-align-right', 'data-align' ) );
$this->assertNull( DOMHelpers::parseAttribute( $html2, '.non-existent-class', 'data-align' ) );
$this->assertEquals( 'default', DOMHelpers::parseAttribute( $html2, '.non-existent-class', 'data-align', 'default' ) );

// $htm3
$this->assertEquals( 'left', DOMHelpers::parseAttribute( $html3, 'span', 'data-align' ) );
$this->assertEquals( 'left', DOMHelpers::parseAttribute( $html3, '*', 'data-align' ) );
}

public function testParseHTML(): void {
$html = '<p id="foo-id" class="foo-class" data="foo-data"><span >Bar</span></p>';
$html = '<p id="foo-id" class="foo-class" data="foo-data"><span >Bar</span></p>';
$no_existent_selector = '#foo';
$id_selector = '#foo-id';
$class_selector = '.foo-class';
$element_selector = 'p';
$id_selector = '#foo-id';
$class_selector = '.foo-class';
$element_selector = 'p';

$this->assertEmpty( DOMHelpers::parseHTML( $html, $no_existent_selector ) );
$this->assertEquals( DOMHelpers::parseHTML( $html, $no_existent_selector, 'Bar' ), 'Bar' );
Expand All @@ -43,8 +57,8 @@ public function testParseHTML(): void {
}

public function testGetElementsFromHTML(): void {
$html = '<blockquote><p>First paragraph</p><div>My div</div><p>Second paragraph</p></blockquote>';
$element_selector = 'p';
$html = '<blockquote><p>First paragraph</p><div>My div</div><p>Second paragraph</p></blockquote>';
$element_selector = 'p';
$no_existent_selector = 'span';

$this->assertEquals( DOMHelpers::getElementsFromHTML( $html, $element_selector ), '<p>First paragraph</p><p>Second paragraph</p>' );
Expand All @@ -54,9 +68,9 @@ public function testGetElementsFromHTML(): void {
public function getTextFromSelector(): void {
$html = '<blockquote><p>First paragraph</p><div>My div</div><p>Second paragraph</p></blockquote>';

$blockquote_element = 'blockquote';
$p_element = 'p';
$div_element = 'div';
$blockquote_element = 'blockquote';
$p_element = 'p';
$div_element = 'div';
$no_existent_selector = 'span';

// getTextFromSelector should get all text (even descendents) according to "textContent"
Expand Down

0 comments on commit 39e8181

Please sign in to comment.