Skip to content

Commit

Permalink
Remove ::get_block_parent_name, ::get_previous_textnode,
Browse files Browse the repository at this point in the history
::get_next_textnode, ::get_last_textnode, ::get_block_parent_name
  • Loading branch information
mundschenk-at committed Mar 30, 2024
1 parent 7c8cd0e commit acdbb5d
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 192 deletions.
117 changes: 26 additions & 91 deletions src/class-dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
* Some static methods for DOM manipulation.
*
* @since 4.2.0
* @since 7.0.0 The obsolete and/or unused methods `get_block_parent_name`, `get_previous_textnode`,
* `get_next_textnode`, and `get_last_textnode` have been removed.
*/
abstract class DOM {

Expand Down Expand Up @@ -246,8 +248,14 @@ public static function get_next_chr( \DOMNode $node ) {
* @return string The character or an empty string.
*/
private static function get_adjacent_character( \DOMNode $node, $position, $length, callable $get_node ) {
$adjacent_node = $get_node( [ __CLASS__, 'is_acceptable_neighbor_node' ], $node );
$character = '';
$adjacent_node = $get_node(
// Determines if the node is a textnode or one of the acceptable elements.
function ( ?\DOMNode $n ): bool {
return $n instanceof \DOMText || ( $n instanceof \DOMElement && isset( self::ACCEPTABLE_NEIGHBOR_ELEMENTS[ $n->tagName ] ) );
},
$node
);

if ( null !== $adjacent_node ) {
if ( $adjacent_node instanceof \DOMElement ) {
Expand All @@ -270,60 +278,7 @@ private static function get_adjacent_character( \DOMNode $node, $position, $leng
}

/**
* Determines if the node is a textnode.
*
* @since 7.0.0
*
* @param ?\DOMNode $node The node to test.
*
* @return bool
*/
private static function is_textnode( ?\DOMNode $node ): bool {
return $node instanceof \DOMText;
}

/**
* Determines if the node is a textnode or one of the acceptable elements.
*
* @since 7.0.0
*
* @param ?\DOMNode $node The node to test.
*
* @return bool
*/
private static function is_acceptable_neighbor_node( ?\DOMNode $node ): bool {
return $node instanceof \DOMText || ( $node instanceof \DOMElement && isset( self::ACCEPTABLE_NEIGHBOR_ELEMENTS[ $node->tagName ] ) );
}


/**
* Retrieves the previous \DOMText sibling (if there is one).
*
* @param \DOMNode|null $node Optional. The content node. Default null.
*
* @return \DOMText|null Null if $node is a block-level element or no text sibling exists.
*/
public static function get_previous_textnode( ?\DOMNode $node ): ?\DOMText {
$result = self::get_previous_acceptable_node( [ __CLASS__, 'is_textnode' ], $node );

return $result instanceof \DOMText ? $result : null;
}

/**
* Retrieves the next \DOMText sibling (if there is one).
*
* @param \DOMNode|null $node Optional. The content node. Default null.
*
* @return \DOMText|null Null if $node is a block-level element or no text sibling exists.
*/
public static function get_next_textnode( ?\DOMNode $node ): ?\DOMText {
$result = self::get_next_acceptable_node( [ __CLASS__, 'is_textnode' ], $node );

return $result instanceof \DOMText ? $result : null;
}

/**
* Retrieves the previous \DOMText sibling (if there is one).
* Retrieves the previous acceptable sibling (if there is one).
*
* @param callable $is_acceptable Returns true if the \DOMnode is acceptable.
* @param \DOMNode|null $node Optional. The content node. Default null.
Expand All @@ -343,7 +298,7 @@ function ( callable $is_node_acceptable, \DOMNode &$another_node ): ?\DOMNode {
}

/**
* Retrieves the next \DOMText sibling (if there is one).
* Retrieves the next accceptable sibling (if there is one).
*
* @param callable $is_acceptable Returns true if the \DOMnode is acceptable.
* @param \DOMNode|null $node Optional. The content node. Default null.
Expand All @@ -363,7 +318,7 @@ function ( callable $is_node_acceptable, \DOMNode &$another_node ): ?\DOMNode {
}

/**
* Retrieves an adjacent \DOMText sibling if there is one.
* Retrieves an adjacent acceptable sibling if there is one.
*
* @since 5.0.0
* @since 7.0.0 Renamed to `get_adjacent_node` and refactored to take a callable to determine acceptable nodes.
Expand Down Expand Up @@ -416,23 +371,20 @@ private static function get_adjacent_node( callable $is_acceptable, callable $it
* @return \DOMText|null The first child of type \DOMText, the element itself if it is of type \DOMText or null.
*/
public static function get_first_textnode( \DOMNode $node = null, $recursive = false ) {
$result = self::get_first_acceptable_node( [ __CLASS__, 'is_textnode' ], $node, $recursive );

return $result instanceof \DOMText ? $result : null;
}

/**
* Retrieves the last \DOMText child of the element. Block-level child elements are ignored.
*
* @param \DOMNode|null $node Optional. Default null.
* @param bool $recursive Should be set to true on recursive calls. Optional. Default false.
*
* @return \DOMText|null The last child of type \DOMText, the element itself if it is of type \DOMText or null.
*/
public static function get_last_textnode( \DOMNode $node = null, $recursive = false ) {
$result = self::get_last_acceptable_node( [ __CLASS__, 'is_textnode' ], $node, $recursive );
/**
* We only allow textnodes.
*
* @var ?\DOMText $textnode;
*/
$textnode = self::get_first_acceptable_node(
function ( ?\DOMNode $node ): bool {
return $node instanceof \DOMText;
},
$node,
$recursive
);

return $result instanceof \DOMText ? $result : null;
return $textnode;
}

/**
Expand Down Expand Up @@ -479,7 +431,7 @@ public static function get_last_acceptable_node( callable $is_acceptable, \DOMNo
private static function get_edge_node( callable $is_acceptable, callable $get_acceptable_node, \DOMNode $node = null, $recursive = false, $reverse = false ): ?\DOMNode {
if ( $is_acceptable( $node ) ) {
return $node;
} elseif ( ! $node instanceof \DOMElement || $recursive && self::is_block_tag( $node ) ) {
} elseif ( ! $node instanceof \DOMElement || ( $recursive && self::is_block_tag( $node ) ) ) {
// Return null if $node is neither an acceptable node nor \DOMElement or
// when we are recursing and already at the block level.
return null;
Expand Down Expand Up @@ -532,23 +484,6 @@ public static function get_block_parent( \DOMNode $node ) {
return $parent;
}

/**
* Retrieves the tag name of the nearest block-level parent.
*
* @param \DOMNode $node A node.
* @return string The tag name (or the empty string).
*/
public static function get_block_parent_name( \DOMNode $node ) {
$parent = self::get_block_parent( $node );

if ( ! empty( $parent ) ) {
return $parent->tagName;
} else {
return '';
}
}

/**
* Determines if a node is a block tag.
*
Expand Down
2 changes: 1 addition & 1 deletion src/fixes/node-fixes/class-space-collapse-fix.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function apply( \DOMText $textnode, Settings $settings, $is_title ) {
);

// Remove all spacing at beginning of block level elements.
if ( null === DOM::get_previous_textnode( $textnode ) ) {
if ( DOM::get_first_textnode( $textnode ) === $textnode ) {
$node_data = (string) \preg_replace( self::COLLAPSE_SPACES_AT_START_OF_BLOCK, '', $node_data );
}

Expand Down
4 changes: 2 additions & 2 deletions src/fixes/node-fixes/class-style-initial-quotes-fix.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function __construct( $css_single, $css_double, $feed_compatible = false
* @return void
*/
protected function apply_internal( \DOMText $textnode, Settings $settings, $is_title ) {
if ( empty( $settings[ Settings::STYLE_INITIAL_QUOTES ] ) || empty( $settings[ Settings::INITIAL_QUOTE_TAGS ] ) || null !== DOM::get_previous_textnode( $textnode ) ) {
if ( empty( $settings[ Settings::STYLE_INITIAL_QUOTES ] ) || empty( $settings[ Settings::INITIAL_QUOTE_TAGS ] ) || DOM::get_first_textnode( $textnode ) !== $textnode ) {
return;
}

Expand All @@ -104,7 +104,7 @@ protected function apply_internal( \DOMText $textnode, Settings $settings, $is_t

if ( ! empty( $span_class ) ) {
// Assume page title is <h2>.
$block_level_parent = $is_title ? 'h2' : DOM::get_block_parent_name( $textnode );
$block_level_parent = $is_title ? 'h2' : DOM::get_block_parent( $textnode )->tagName ?? '';

if ( ! empty( $block_level_parent ) && isset( $settings[ Settings::INITIAL_QUOTE_TAGS ][ $block_level_parent ] ) ) {
$textnode->data = RE::escape_tags( '<span class="' . $span_class . '">' ) . $first_character . RE::escape_tags( '</span>' ) . $f['substr']( $node_data, 1, $f['strlen']( $node_data ) );
Expand Down
2 changes: 1 addition & 1 deletion src/fixes/token-fixes/class-hyphenate-fix.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function apply( array $tokens, \DOMText $textnode, Settings $settings, $i

$is_heading = false;
if ( ! empty( $textnode->parentNode ) ) {
$block_level_parent = DOM::get_block_parent_name( $textnode );
$block_level_parent = DOM::get_block_parent( $textnode )->tagName ?? '';

if ( ! empty( $block_level_parent ) && isset( $this->heading_tags[ $block_level_parent ] ) ) {
$is_heading = true;
Expand Down
Loading

0 comments on commit acdbb5d

Please sign in to comment.