Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,75 @@ public function next_tag( $query = null ) {
}


/**
* Steps through each of the class names for the matched tag.
*
* This generator function is designed to be used inside a "foreach" loop.
*
* Example:
*
* $p = new WP_HTML_Tag_Processor( "<div class='free &lt;egg&lt;\tlang-en'>" );
* $p->next_tag();
* foreach ( $p->class_list() as $class_name ) {
* echo "{$class_name} ";
* }
* // Outputs: "free <egg> lang-en "
*/
public function class_list() {
$class = $this->get_attribute( 'class' );

if ( ! is_string( $class ) ) {
return;
}

$seen = array();

$at = strspn( $class, " \t\f\r\n" );
while ( $at < strlen( $class ) ) {
$length = strcspn( $class, " \t\f\r\n", $at );
if ( 0 === $length ) {
return;
}

$name = substr( $class, $at, $length );
$at = $at + $length + strspn( $class, " \t\f\r\n", $at + $length );

/*
* It's expected that the number of class names for a given tag is relatively small.
* Given this, it is probably faster overall to scan an array for a value rather
* than to use the class name as a key and check if it's a key of $seen.
*/
if ( in_array( $name, $seen, true ) ) {
continue;
}

$seen[] = $name;
yield $name;
}
}


/**
* Returns if a matched tag contains the given class name.
*
* @param string $wanted_class Look for this CSS class name.
* @return bool|null Whether the matched tag contains the given class name, or null if not matched.
*/
public function has_class( $wanted_class ) {
if ( ! $this->tag_name_starts_at ) {
return null;
}

foreach ( $this->class_list() as $class_name ) {
if ( $class_name === $wanted_class ) {
return true;
}
}

return false;
}


/**
* Sets a bookmark in the HTML document.
*
Expand Down Expand Up @@ -2256,62 +2325,7 @@ private function matches() {
}
}

$needs_class_name = null !== $this->sought_class_name;

if ( $needs_class_name && ! isset( $this->attributes['class'] ) ) {
return false;
}

/*
* Match byte-for-byte (case-sensitive and encoding-form-sensitive) on the class name.
*
* This will overlook certain classes that exist in other lexical variations
* than was supplied to the search query, but requires more complicated searching.
*/
if ( $needs_class_name ) {
$class_start = $this->attributes['class']->value_starts_at;
$class_end = $class_start + $this->attributes['class']->value_length;
$class_at = $class_start;

/*
* Ensure that boundaries surround the class name to avoid matching on
* substrings of a longer name. For example, the sequence "not-odd"
* should not match for the class "odd" even though "odd" is found
* within the class attribute text.
*
* See https://html.spec.whatwg.org/#attributes-3
* See https://html.spec.whatwg.org/#space-separated-tokens
*/
do {
$class_at = strpos( $this->html, $this->sought_class_name, $class_at );

/*
* Verify this class starts at a boundary.
*/
if ( $class_at > $class_start ) {
$character = $this->html[ $class_at - 1 ];

if ( ' ' !== $character && "\t" !== $character && "\f" !== $character && "\r" !== $character && "\n" !== $character ) {
$class_at += strlen( $this->sought_class_name );
continue;
}
}

/*
* Verify this class ends at a boundary as well.
*/
if ( $class_at + strlen( $this->sought_class_name ) < $class_end ) {
$character = $this->html[ $class_at + strlen( $this->sought_class_name ) ];

if ( ' ' !== $character && "\t" !== $character && "\f" !== $character && "\r" !== $character && "\n" !== $character ) {
$class_at += strlen( $this->sought_class_name );
continue;
}
}

return true;
} while ( false !== $class_at && $class_at < $class_end );

if ( null !== $this->sought_class_name && ! $this->has_class( $this->sought_class_name ) ) {
return false;
}

Expand Down