public function sanitize($amp_attributes = array()) { $nodes = $this->dom->getElementsByTagName(self::$tag); $num_nodes = $nodes->length; if (0 === $num_nodes) { return; } for ($i = $num_nodes - 1; $i >= 0; $i--) { $node = $nodes->item($i); $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array($node); if (!array_key_exists('src', $old_attributes)) { $node->parentNode->removeChild($node); continue; } $new_attributes = $this->filter_attributes($old_attributes); if (!isset($new_attributes['width']) || !isset($new_attributes['height'])) { $dimensions = AMP_Image_Dimension_Extractor::extract($new_attributes['src']); if ($dimensions) { $new_attributes['width'] = $dimensions[0]; $new_attributes['height'] = $dimensions[1]; } } $new_attributes = $this->enforce_sizes_attribute($new_attributes); $new_attributes = array_merge($new_attributes, $amp_attributes); if ($this->is_gif_url($new_attributes['src'])) { $this->did_convert_elements = true; $new_tag = 'amp-anim'; } else { $new_tag = 'amp-img'; } $new_node = AMP_DOM_Utils::create_node($this->dom, $new_tag, $new_attributes); $node->parentNode->replaceChild($new_node, $node); } }
/** * Figure out width and height attribute values for images that don't have them by * attempting to determine actual dimensions and setting reasonable defaults otherwise. * * @param array $need_dimensions List of Img src url to node mappings corresponding to images that need dimensions. */ private function determine_dimensions($need_dimensions) { $dimensions_by_url = AMP_Image_Dimension_Extractor::extract(array_keys($need_dimensions)); foreach ($dimensions_by_url as $url => $dimensions) { foreach ($need_dimensions[$url] as $node) { // Provide default dimensions for images whose dimensions we couldn't fetch. if (false === $dimensions) { $width = isset($this->args['content_max_width']) ? $this->args['content_max_width'] : self::FALLBACK_WIDTH; $height = self::FALLBACK_HEIGHT; $node->setAttribute('width', $width); $node->setAttribute('height', $height); $class = $node->hasAttribute('class') ? $node->getAttribute('class') . ' amp-wp-unknown-size' : 'amp-wp-unknown-size'; $node->setAttribute('class', $class); } else { $node->setAttribute('width', $dimensions['width']); $node->setAttribute('height', $dimensions['height']); } } } }
public function sanitize() { $nodes = $this->dom->getElementsByTagName(self::$tag); $num_nodes = $nodes->length; if (0 === $num_nodes) { return; } for ($i = $num_nodes - 1; $i >= 0; $i--) { $node = $nodes->item($i); $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array($node); // Added data-src for lazy-loaded imgs. if (!array_key_exists('src', $old_attributes) && !array_key_exists('data-src', $old_attributes)) { $node->parentNode->removeChild($node); continue; } $new_attributes = $this->filter_attributes($old_attributes); // Try to extract dimensions for the image, if not set. if (!isset($new_attributes['width']) || !isset($new_attributes['height'])) { $dimensions = AMP_Image_Dimension_Extractor::extract($new_attributes['src']); if (is_array($dimensions)) { $new_attributes['width'] = $dimensions[0]; $new_attributes['height'] = $dimensions[1]; } } // Final fallback when we have no dimensions. if (!isset($new_attributes['width']) || !isset($new_attributes['height'])) { $new_attributes['width'] = isset($this->args['content_max_width']) ? $this->args['content_max_width'] : self::FALLBACK_WIDTH; $new_attributes['height'] = self::FALLBACK_HEIGHT; $this->add_or_append_attribute($new_attributes, 'class', 'amp-wp-unknown-size'); } $new_attributes = $this->enforce_sizes_attribute($new_attributes); if ($this->is_gif_url($new_attributes['src'])) { $this->did_convert_elements = true; $new_tag = 'amp-anim'; } else { $new_tag = 'amp-img'; } $new_node = AMP_DOM_Utils::create_node($this->dom, $new_tag, $new_attributes); $node->parentNode->replaceChild($new_node, $node); } }