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); } }
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); $new_attributes = $this->filter_attributes($old_attributes); if (!isset($new_attributes['width'], $new_attributes['height'])) { $new_attributes['height'] = self::FALLBACK_HEIGHT; $new_attributes['layout'] = 'fixed-height'; } $new_attributes = $this->enforce_sizes_attribute($new_attributes); $new_node = AMP_DOM_Utils::create_node($this->dom, 'amp-video', $new_attributes); // TODO: limit child nodes too (only allowed: `source`; move rest to div+fallback) // TODO: `source` does not have closing tag, and DOMDocument doesn't handle it well. foreach ($node->childNodes as $child_node) { $new_child_node = $child_node->cloneNode(true); $new_node->appendChild($new_child_node); } $node->parentNode->replaceChild($new_node, $node); } }
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); $new_attributes = $this->filter_attributes($old_attributes); $new_node = AMP_DOM_Utils::create_node($this->dom, 'amp-audio', $new_attributes); // TODO: `source` does not have closing tag, and DOMDocument doesn't handle it well. foreach ($node->childNodes as $child_node) { $new_child_node = $child_node->cloneNode(true); $old_child_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array($new_child_node); $new_child_attributes = $this->filter_attributes($old_child_attributes); // Only append source tags with a valid src attribute if (!empty($new_child_attributes['src']) && 'source' === $new_child_node->tagName) { $new_node->appendChild($new_child_node); } } // If the node has at least one valid source, replace the old node with it. // Otherwise, just remove the node. // // TODO: Add a fallback handler. // See: https://github.com/ampproject/amphtml/issues/2261 if (0 === $new_node->childNodes->length && empty($new_attributes['src'])) { $node->parentNode->removeChild($node); } else { $node->parentNode->replaceChild($new_node, $node); } $this->did_convert_elements = true; } }
/** * Make final modifications to DOMNode * * @param DOMNode $node The DOMNode to adjust and replace */ private function adjust_and_replace_node($node) { $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array($node); $new_attributes = $this->filter_attributes($old_attributes); $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); }
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); } }
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); $new_attributes = $this->filter_attributes($old_attributes); $new_node = AMP_DOM_Utils::create_node($this->dom, 'amp-audio', $new_attributes); // TODO: limit child nodes too (only allowed: `source`; move rest to div+fallback) // TODO: `source` does not have closing tag, and DOMDocument doesn't handle it well. foreach ($node->childNodes as $child_node) { $new_child_node = $child_node->cloneNode(true); $new_node->appendChild($new_child_node); } $node->parentNode->replaceChild($new_node, $node); $this->did_convert_elements = true; } }
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; } $this->did_convert_elements = true; $new_attributes = $this->filter_attributes($old_attributes); $new_attributes = $this->enforce_sizes_attribute($new_attributes); $new_attributes = array_merge($new_attributes, $amp_attributes); $new_node = AMP_DOM_Utils::create_node($this->dom, 'amp-iframe', $new_attributes); $node->parentNode->replaceChild($new_node, $node); } }
private function build_placeholder($parent_attributes) { $placeholder_node = AMP_DOM_Utils::create_node($this->dom, 'div', array('layout' => 'fill', 'placeholder' => '', 'class' => 'amp-wp-iframe-placeholder')); return $placeholder_node; }