get_dom_from_content() public static method

public static get_dom_from_content ( $content )
Example #1
0
 public function test__is_node_empty__no__has_child()
 {
     $source = '<p><b></b></p>';
     $dom = AMP_DOM_Utils::get_dom_from_content($source);
     $node = $dom->getElementsByTagName('p')->item(0);
     $this->assertFalse(AMP_DOM_Utils::is_node_empty($node));
 }
 public function test__get_content_from_dom__br_no_closing_tag()
 {
     $source = '<br/>';
     $expected = '<br/>';
     $dom = AMP_DOM_Utils::get_dom_from_content($source);
     $actual = AMP_DOM_Utils::get_content_from_dom($dom);
     $this->assertEquals($expected, $actual);
 }
 /**
  * @dataProvider get_data
  */
 public function test_sanitizer($source, $expected)
 {
     $dom = AMP_DOM_Utils::get_dom_from_content($source);
     $sanitizer = new AMP_Blacklist_Sanitizer($dom);
     $sanitizer->sanitize();
     $content = AMP_DOM_Utils::get_content_from_dom($dom);
     $this->assertEquals($expected, $content);
 }
 public function test_add_attributes_to_node__attribute_with_value()
 {
     $dom = AMP_DOM_Utils::get_dom_from_content('<p>Hello World</p>');
     $node = $dom->createElement('div');
     $attributes = array('class' => 'myClass', 'id' => 'myId');
     AMP_DOM_Utils::add_attributes_to_node($node, $attributes);
     $this->assertTrue($node->hasAttributes());
     $this->check_node_has_attributes($node, $attributes);
 }
 public function test__args__placeholder()
 {
     $source = '<iframe src="https://example.com/video/132886713" width="500" height="281"></iframe>';
     $expected = '<amp-iframe src="https://example.com/video/132886713" width="500" height="281" sandbox="allow-scripts allow-same-origin" sizes="(min-width: 500px) 500px, 100vw" class="amp-wp-enforced-sizes"><div placeholder="" class="amp-wp-iframe-placeholder"></div></amp-iframe>';
     $dom = AMP_DOM_Utils::get_dom_from_content($source);
     $sanitizer = new AMP_Iframe_Sanitizer($dom, array('add_placeholder' => true));
     $sanitizer->sanitize();
     $content = AMP_DOM_Utils::get_content_from_dom($dom);
     $this->assertEquals($expected, $content);
 }
 public function test_get_scripts__empty()
 {
     $source = '<video width="300" height="300" src="https://archive.org/download/WebmVp8Vorbis/webmvp8_512kb.mp4"></video>';
     $expected = array();
     $dom = AMP_DOM_Utils::get_dom_from_content($source);
     $sanitizer = new AMP_Video_Sanitizer($dom);
     $sanitizer->sanitize();
     $scripts = $sanitizer->get_scripts();
     $this->assertEquals($expected, $scripts);
 }
Example #7
0
 public function test_get_scripts__did_convert()
 {
     $source = '<audio width="400" height="300" src="https://example.com/audio/file.ogg"></audio>';
     $expected = array('amp-audio' => 'https://cdn.ampproject.org/v0/amp-audio-0.1.js');
     $dom = AMP_DOM_Utils::get_dom_from_content($source);
     $sanitizer = new AMP_Audio_Sanitizer($dom);
     $sanitizer->sanitize();
     $scripts = $sanitizer->get_scripts();
     $this->assertEquals($expected, $scripts);
 }
 public function test_get_scripts__did_convert()
 {
     $source = '<iframe src="https://player.vimeo.com/video/132886713" width="500" height="281"></iframe>';
     $expected = array('amp-iframe' => 'https://cdn.ampproject.org/v0/amp-iframe-0.1.js');
     $dom = AMP_DOM_Utils::get_dom_from_content($source);
     $sanitizer = new AMP_Iframe_Sanitizer($dom);
     $sanitizer->sanitize();
     $scripts = $sanitizer->get_scripts();
     $this->assertEquals($expected, $scripts);
 }
Example #9
0
 public function test_get_scripts__empty()
 {
     $source = '<video width="300" height="300" src="https://example.com/video.mp4"></video>';
     $expected = array();
     $dom = AMP_DOM_Utils::get_dom_from_content($source);
     $sanitizer = new AMP_Video_Sanitizer($dom);
     $sanitizer->sanitize();
     $scripts = $sanitizer->get_scripts();
     $this->assertEquals($expected, $scripts);
 }
 public function test_no_gif_image_scripts()
 {
     $source = '<img src="http://placehold.it/350x150.gif" width="350" height="150" alt="Placeholder!" />';
     $expected = array('amp-anim' => 'https://cdn.ampproject.org/v0/amp-anim-0.1.js');
     $dom = AMP_DOM_Utils::get_dom_from_content($source);
     $sanitizer = new AMP_Img_Sanitizer($dom);
     $sanitizer->sanitize();
     $scripts = $sanitizer->get_scripts();
     $this->assertEquals($expected, $scripts);
 }
 /**
  * @dataProvider get_data
  */
 public function test_sanitizer($source, $expected_content, $expected_stylesheet)
 {
     $dom = AMP_DOM_Utils::get_dom_from_content($source);
     $sanitizer = new AMP_Style_Sanitizer($dom);
     $sanitizer->sanitize();
     // Test content
     $content = AMP_DOM_Utils::get_content_from_dom($dom);
     $this->assertEquals($expected_content, $content);
     // Test stylesheet
     $stylesheet = $sanitizer->get_styles();
     $this->assertEquals($expected_stylesheet, $stylesheet);
 }
Example #12
0
 private function sanitize($content)
 {
     $dom = AMP_DOM_Utils::get_dom_from_content($content);
     foreach ($this->sanitizer_classes as $sanitizer_class => $args) {
         $sanitizer = new $sanitizer_class($dom, array_merge($this->args, $args));
         if (!is_subclass_of($sanitizer, 'AMP_Base_Sanitizer')) {
             _doing_it_wrong(__METHOD__, sprintf(__('Sanitizer (%s) must extend `AMP_Base_Sanitizer`', 'amp'), esc_html($sanitizer_class)), '0.1');
             continue;
         }
         $sanitizer->sanitize();
         $this->add_scripts($sanitizer->get_scripts());
     }
     return AMP_DOM_Utils::get_content_from_dom($dom);
 }
Example #13
0
 public function transform()
 {
     $content = $this->original_content;
     $twitter_embed = new AMP_Twitter_Embed_Handler();
     $youtube_embed = new AMP_YouTube_Embed_Handler();
     $gallery_embed = new AMP_Gallery_Embed_Handler();
     $instagram_embed = new AMP_Instagram_Embed_Handler();
     $content = apply_filters('the_content', $content);
     $this->add_scripts($twitter_embed->get_scripts());
     $this->add_scripts($youtube_embed->get_scripts());
     $this->add_scripts($gallery_embed->get_scripts());
     $this->add_scripts($instagram_embed->get_scripts());
     $dom = AMP_DOM_Utils::get_dom_from_content($content);
     $this->sanitize(new AMP_Blacklist_Sanitizer($dom, $this->args));
     $this->sanitize(new AMP_Img_Sanitizer($dom, $this->args));
     $this->sanitize(new AMP_Video_Sanitizer($dom, $this->args));
     $this->sanitize(new AMP_Iframe_Sanitizer($dom, $this->args));
     $this->sanitize(new AMP_Audio_Sanitizer($dom, $this->args));
     $content = AMP_DOM_Utils::get_content_from_dom($dom);
     return $content;
 }
Example #14
0
 public static function sanitize($content, $sanitizer_classes, $args = array())
 {
     $scripts = array();
     $styles = array();
     $dom = AMP_DOM_Utils::get_dom_from_content($content);
     foreach ($sanitizer_classes as $sanitizer_class => $args) {
         if (!class_exists($sanitizer_class)) {
             _doing_it_wrong(__METHOD__, sprintf(__('Sanitizer (%s) class does not exist', 'amp'), esc_html($sanitizer_class)), '0.4.1');
             continue;
         }
         $sanitizer = new $sanitizer_class($dom, array_merge($args, $args));
         if (!is_subclass_of($sanitizer, 'AMP_Base_Sanitizer')) {
             _doing_it_wrong(__METHOD__, sprintf(__('Sanitizer (%s) must extend `AMP_Base_Sanitizer`', 'amp'), esc_html($sanitizer_class)), '0.1');
             continue;
         }
         $sanitizer->sanitize();
         $scripts = array_merge($scripts, $sanitizer->get_scripts());
         $styles = array_merge($styles, $sanitizer->get_styles());
     }
     $sanitized_content = AMP_DOM_Utils::get_content_from_dom($dom);
     return array($sanitized_content, $scripts, $styles);
 }