Skip to content

Commit

Permalink
Lower-case tag param in is_self_closing_tag check; replace static met…
Browse files Browse the repository at this point in the history
…hod w/ static return with a plain static variable
  • Loading branch information
westonruter committed Jan 23, 2018
1 parent 70a444e commit eededf8
Showing 1 changed file with 32 additions and 44 deletions.
76 changes: 32 additions & 44 deletions includes/utils/class-amp-dom-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@
*/
class AMP_DOM_Utils {

/**
* HTML elements that are self-closing.
*
* Not all are valid AMP, but we include them for completeness.
*
* @since 0.6
* @link https://www.w3.org/TR/html5/syntax.html#serializing-html-fragments
* @var array
*/
private static $self_closing_tags = array(
'area',
'base',
'basefont',
'bgsound',
'br',
'col',
'embed',
'frame',
'hr',
'img',
'input',
'keygen',
'link',
'meta',
'param',
'source',
'track',
'wbr',
);

/**
* Return a valid DOMDocument representing HTML document passed as a parameter.
*
Expand Down Expand Up @@ -142,7 +172,7 @@ public static function get_content_from_dom_node( $dom, $node ) {
* Cache this regex so we don't have to recreate it every call.
*/
if ( ! isset( $self_closing_tags_regex ) ) {
$self_closing_tags = implode( '|', self::get_self_closing_tags() );
$self_closing_tags = implode( '|', self::$self_closing_tags );
$self_closing_tags_regex = "#></({$self_closing_tags})>#i";
}

Expand Down Expand Up @@ -285,48 +315,6 @@ public static function recursive_force_closing_tags( $dom, $node = null ) {
* @return bool Returns true if a valid self-closing tag, false if not.
*/
private static function is_self_closing_tag( $tag ) {
return in_array( $tag, self::get_self_closing_tags(), true );
}

/**
* Returns array of self closing tags
*
* @since 0.6
*
* @return string[]
*/
private static function get_self_closing_tags() {
/*
* As this function is called a lot the static var
* prevents having to re-create the array every time.
*/
static $self_closing_tags;
if ( ! isset( $self_closing_tags ) ) {
/*
* https://www.w3.org/TR/html5/syntax.html#serializing-html-fragments
* Not all are valid AMP, but we include them for completeness.
*/
$self_closing_tags = array(
'area',
'base',
'basefont',
'bgsound',
'br',
'col',
'embed',
'frame',
'hr',
'img',
'input',
'keygen',
'link',
'meta',
'param',
'source',
'track',
'wbr',
);
}
return $self_closing_tags;
return in_array( strtolower( $tag ), self::$self_closing_tags, true );
}
}

0 comments on commit eededf8

Please sign in to comment.