$html, 'applied' => 0, 'skipped' => count( $active ), ); } $css_rules = array(); $attr_rules = array(); foreach ( $active as $rule ) { if ( 'css' === $rule['action']['type'] ) { $css_rules[] = $rule; } else { $attr_rules[] = $rule; } } $applied = 0; $skipped = 0; // Attribute mutations via DOMDocument. Guarded: on any failure the // original markup is preserved. if ( ! empty( $attr_rules ) ) { $rewritten = self::apply_attribute_rules( $html, $attr_rules, $applied, $skipped ); if ( null !== $rewritten ) { $html = $rewritten; } else { $skipped += count( $attr_rules ); } } // The stylesheet itself is not written into the page here: WordPress prints it // through the enqueue API (see equalsites_enqueue_fixes_css), built from the same // rules by css_body(). Counting still happens here, so a rule counts as applied // or skipped for the same reason it always did. if ( ! empty( $css_rules ) ) { $built = self::css_body( $css_rules ); $applied += $built['applied']; $skipped += $built['refused']; } return array( 'html' => $html, 'applied' => $applied, 'skipped' => $skipped, ); } /** * The stylesheet body for a request's rules: the safe CSS rules, each validated * whole (EqualSites_Fix_Safety::css_block) and an unsafe one dropped, never emitted * partially. Nothing parses these values on our behalf once they reach a style * element, so a " '', 'applied' => 0, 'refused' => $refused + count( $blocks ), ); } return array( 'body' => $body, 'applied' => count( $blocks ), 'refused' => $refused, ); } private static function apply_attribute_rules( $html, $rules, &$applied, &$skipped ) { if ( ! class_exists( 'DOMDocument' ) ) { return null; } $previous = libxml_use_internal_errors( true ); try { $doc = new DOMDocument(); // Hint UTF-8 to the HTML4-era parser without mutating the markup. $loaded = $doc->loadHTML( '' . $html, LIBXML_HTML_NODEFDTD | LIBXML_NOERROR | LIBXML_NOWARNING ); if ( ! $loaded ) { return null; } $xpath = new DOMXPath( $doc ); foreach ( $rules as $rule ) { $action = $rule['action']; $matched = false; foreach ( $rule['selectors'] as $selector ) { $query = EqualSites_Selector_Xpath::convert( $selector ); if ( null === $query ) { continue; } $nodes = $xpath->query( $query ); if ( false === $nodes || 0 === $nodes->length ) { continue; } $limit = ! empty( $rule['firstMatchOnly'] ) ? 1 : $nodes->length; for ( $i = 0; $i < $limit; $i ++ ) { $el = $nodes->item( $i ); if ( ! $el instanceof DOMElement ) { continue; } // Fail closed, per element: part of the policy depends on // the target ("content" only on the viewport , so // the element's own name attribute is part of the // question), so this cannot be hoisted out of the loop. // A rule refused on every node it matched sets no // $matched and is counted as skipped, not applied. $refusal = EqualSites_Fix_Safety::attribute_refusal_reason( isset( $action['name'] ) ? $action['name'] : null, isset( $action['value'] ) ? $action['value'] : null, $el->tagName, $el->getAttribute( 'http-equiv' ), $el->getAttribute( 'name' ) ); if ( null !== $refusal ) { continue; } $existing = $el->getAttribute( $action['name'] ); $only_if_missing = ! isset( $action['onlyIfMissing'] ) || false !== $action['onlyIfMissing']; if ( $only_if_missing && '' !== trim( $existing ) ) { continue; } $el->setAttribute( $action['name'], $action['value'] ); $matched = true; } if ( $matched && ! empty( $rule['firstMatchOnly'] ) ) { break; } } if ( $matched ) { $applied ++; } else { $skipped ++; } } $output = $doc->saveHTML(); if ( false === $output || '' === $output ) { return null; } // Strip the encoding processing instruction we prepended. return str_replace( '', '', $output ); } catch ( Throwable $e ) { return null; } finally { libxml_clear_errors(); libxml_use_internal_errors( $previous ); } } }