diff --git a/.travis.yml b/.travis.yml index 038c22ce3f1..66fc489ed79 100644 --- a/.travis.yml +++ b/.travis.yml @@ -42,10 +42,6 @@ cache: matrix: include: - - php: "5.2" - env: WP_VERSION=latest DEV_LIB_SKIP=phpcs - - php: "5.2" - env: WP_VERSION=4.7 DEV_LIB_SKIP=phpcs - php: "5.3" env: WP_VERSION=latest DEV_LIB_SKIP=phpcs - php: "5.3" diff --git a/amp.php b/amp.php index e84f62c5f5f..8e8558f888a 100644 --- a/amp.php +++ b/amp.php @@ -13,6 +13,23 @@ * @package AMP */ +/** + * Print admin notice regarding having an old version of PHP. + * + * @since 0.7 + */ +function _amp_print_php_version_admin_notice() { + ?> +
+

+
+ get_blacklisted_tags(); $blacklisted_attributes = $this->get_blacklisted_attributes(); $blacklisted_protocols = $this->get_blacklisted_protocols(); diff --git a/includes/utils/class-amp-dom-utils.php b/includes/utils/class-amp-dom-utils.php index 9492dd4cee5..8ffd3653e2c 100644 --- a/includes/utils/class-amp-dom-utils.php +++ b/includes/utils/class-amp-dom-utils.php @@ -160,23 +160,15 @@ public static function get_content_from_dom_node( $dom, $node ) { */ static $self_closing_tags_regex; - /* - * Most AMP elements need closing tags. To force them, we cannot use - * saveHTML (node support is 5.3+) and LIBXML_NOEMPTYTAG results in - * issues with self-closing tags like `br` and `hr`. So, we're manually - * forcing closing tags. - */ - self::recursive_force_closing_tags( $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::$self_closing_tags ); - $self_closing_tags_regex = "#>#i"; + $self_closing_tags_regex = "##i"; } - $html = $dom->saveXML( $node ); + $html = $dom->saveHTML( $node ); // Whitespace just causes unit tests to fail... so whitespace begone. if ( '' === trim( $html ) ) { @@ -189,10 +181,9 @@ public static function get_content_from_dom_node( $dom, $node ) { * Seems like LIBXML_NOEMPTYTAG was passed, but as you can see it was not. * This does not happen in my (@mikeschinkel) local testing, btw. */ - $html = preg_replace( $self_closing_tags_regex, '/>', $html ); + $html = preg_replace( $self_closing_tags_regex, '', $html ); return $html; - } /** @@ -268,12 +259,14 @@ public static function is_node_empty( $node ) { * Forces HTML element closing tags given a DOMDocument and optional DOMElement * * @since 0.2 + * @deprecated * * @param DOMDocument $dom Represents HTML document on which to force closing tags. * @param DOMElement $node Represents HTML element to start closing tags on. * If not passed, defaults to first child of body. */ public static function recursive_force_closing_tags( $dom, $node = null ) { + _deprecated_function( __METHOD__, '0.7' ); if ( is_null( $node ) ) { $node = $dom->getElementsByTagName( 'body' )->item( 0 ); diff --git a/readme.md b/readme.md index 98afc1c400e..f6219724678 100644 --- a/readme.md +++ b/readme.md @@ -10,7 +10,7 @@ Enable Accelerated Mobile Pages (AMP) on your WordPress site. **Tested up to:** 4.9 **Stable tag:** 0.6.0 **License:** [GPLv2 or later](http://www.gnu.org/licenses/gpl-2.0.html) -**Requires PHP:** 5.2 +**Requires PHP:** 5.3 [![Build Status](https://travis-ci.org/Automattic/amp-wp.svg?branch=master)](https://travis-ci.org/Automattic/amp-wp) [![Built with Grunt](https://cdn.gruntjs.com/builtwith.svg)](http://gruntjs.com) diff --git a/readme.txt b/readme.txt index eaaeee8d0f2..0a50cb8ccaf 100644 --- a/readme.txt +++ b/readme.txt @@ -6,7 +6,7 @@ Tested up to: 4.9 Stable tag: 0.6.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html -Requires PHP: 5.2 +Requires PHP: 5.3 Enable Accelerated Mobile Pages (AMP) on your WordPress site. diff --git a/tests/amp-tag-and-attribute-sanitizer-private-methods-tests.php b/tests/amp-tag-and-attribute-sanitizer-private-methods-tests.php index 1b9df941109..66b133c8c1e 100644 --- a/tests/amp-tag-and-attribute-sanitizer-private-methods-tests.php +++ b/tests/amp-tag-and-attribute-sanitizer-private-methods-tests.php @@ -619,10 +619,10 @@ public function get_remove_node_data() { ), 'remove_bad_tag_leave_empty_siblings_and_parent' => array( array( - 'source' => '

', + 'source' => '

', 'tag_name' => 'bad-tag', ), - '

', + '

', ), 'remove_single_bad_tag_with_non-empty_parent' => array( array( @@ -759,10 +759,10 @@ public function get_replace_node_with_children_data() { ), 'no_children_leave_empty_siblings_and_parent' => array( array( - 'source' => '

', + 'source' => '

', 'tag_name' => 'bad-tag', ), - '

', + '

', ), 'no_childreng_with_non-empty_parent' => array( array( @@ -780,7 +780,7 @@ public function get_replace_node_with_children_data() { ), 'no_children_and_empty_grandparent_leave_non-empty_greatgrandparent' => array( array( - 'source' => '

Good Data

', + 'source' => '

Good Data

', 'tag_name' => 'bad-tag', ), '

Good Data

', @@ -800,6 +800,7 @@ public function test_replace_node_with_children( $data, $expected ) { $this->invoke_method( $sanitizer, 'replace_node_with_children', array( $node ) ); $got = AMP_DOM_Utils::get_content_from_dom( $dom ); + $got = preg_replace( '/(?<=>)\s+(?=<)/', '', $got ); if ( $expected != $got ) { printf( 'using source: %s' . PHP_EOL, $data['source'] ); diff --git a/tests/test-amp-audio-converter.php b/tests/test-amp-audio-converter.php index 6fab1cce26c..53d5cb246b1 100644 --- a/tests/test-amp-audio-converter.php +++ b/tests/test-amp-audio-converter.php @@ -54,7 +54,7 @@ public function get_data() { '', - '', + '', ), 'multiple_same_audio' => array( @@ -67,7 +67,7 @@ public function get_data() { ', - '', + '', ), 'multiple_different_audio' => array( @@ -78,7 +78,7 @@ public function get_data() { ', - '', + '', ), 'https_not_required' => array( @@ -96,6 +96,7 @@ public function test_converter( $source, $expected ) { $sanitizer = new AMP_Audio_Sanitizer( $dom ); $sanitizer->sanitize(); $content = AMP_DOM_Utils::get_content_from_dom( $dom ); + $content = preg_replace( '/(?<=>)\s+(?=<)/', '', $content ); $this->assertEquals( $expected, $content ); } diff --git a/tests/test-amp-blacklist-sanitizer.php b/tests/test-amp-blacklist-sanitizer.php deleted file mode 100644 index a278501dc5e..00000000000 --- a/tests/test-amp-blacklist-sanitizer.php +++ /dev/null @@ -1,194 +0,0 @@ - array( - '', - '', - ), - - 'blacklisted_tag_with_innertext' => array( - '', - '', - ), - - 'multiple_blacklisted_tags_only' => array( - '', - '', - ), - - 'multiple_blacklisted_tags_only_in_child' => array( - '

', - '', - ), - - 'whitelisted_tag_only' => array( - '

Text

', - '

Text

', // LIBXML_NOEMPTYTAG - ), - - 'blacklisted_attributes' => array( - 'Link', - 'Link', - ), - - 'onclick_attribute' => array( - 'Link', - 'Link', - ), - - 'on_attribute' => array( - '', - '', - ), - - 'multiple_blacklisted_attributes' => array( - 'Link', - 'Link', - ), - - 'javascript_protocol' => array( - 'Click', - 'Click', - ), - - 'attribute_recursive' => array( - '
Hello World
', - '
Hello World
', - ), - - 'mixed_tags' => array( - '

Text

', - '

Text

', - ), - - 'no_strip_amp_tags' => array( - '', - '', - ), - - 'a_with_attachment_rel' => array( - 'Link', - 'Link', - ), - - 'a_with_attachment_rel_plus_another_valid_value' => array( - 'Link', - 'Link', - ), - - 'a_with_rev' => array( - 'Link', - 'Link', - ), - - 'a_with_target_blank' => array( - 'Link', - 'Link', - ), - - 'a_with_target_uppercase_blank' => array( - 'Link', - 'Link', - ), - - 'a_with_target_new' => array( - 'Link', - 'Link', - ), - - 'a_with_target_self' => array( - 'Link', - 'Link', - ), - - 'a_with_target_invalid' => array( - 'Link', - 'Link', - ), - - 'a_with_href_invalid' => array( - 'Link', - 'Link', - ), - - 'a_with_href_scheme_invalid' => array( - 'Link', - 'Link', - ), - - 'a_with_href_scheme_tel' => array( - 'Call Me, Maybe', - 'Call Me, Maybe', - ), - - 'a_with_href_scheme_sms' => array( - 'SMS Me, Maybe', - 'SMS Me, Maybe', - ), - - 'a_with_href_scheme_mailto' => array( - 'Email Me, Maybe', - 'Email Me, Maybe', - ), - - 'a_with_href_relative' => array( - 'Home', - 'Home', - ), - - 'a_with_anchor' => array( - 'Home', - 'Home', - ), - - 'a_is_anchor' => array( - '', - '', - ), - - 'a_is_achor_with_id' => array( - '', - '', - ), - - 'a_empty' => array( - 'Hello World', - 'Hello World', - ), - - 'a_empty_with_children_with_restricted_attributes' => array( - 'Red&Orange', - 'Red&Orange', - ), - - 'h1_with_size' => array( - '

Headline

', - '

Headline

', - ), - - 'font' => array( - 'Headline', - 'Headline', - ), - - // font is removed so we should check that other elements are checked as well - 'font_with_other_bad_elements' => array( - 'HeadlineSpan', - 'HeadlineSpan', - ), - ); - } - - /** - * @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 ); - } -} diff --git a/tests/test-amp-iframe-sanitizer.php b/tests/test-amp-iframe-sanitizer.php index bb6b3e3d961..7748462ca4a 100644 --- a/tests/test-amp-iframe-sanitizer.php +++ b/tests/test-amp-iframe-sanitizer.php @@ -103,6 +103,7 @@ public function test_converter( $source, $expected ) { $sanitizer = new AMP_Iframe_Sanitizer( $dom ); $sanitizer->sanitize(); $content = AMP_DOM_Utils::get_content_from_dom( $dom ); + $content = preg_replace( '/(?<=>)\s+(?=<)/', '', $content ); $this->assertEquals( $expected, $content ); } diff --git a/tests/test-amp-video-sanitizer.php b/tests/test-amp-video-sanitizer.php index cf0f781922d..140b3584025 100644 --- a/tests/test-amp-video-sanitizer.php +++ b/tests/test-amp-video-sanitizer.php @@ -50,10 +50,10 @@ public function get_data() { 'video_with_children' => array( '', - '', + '', ), 'multiple_same_video' => array( diff --git a/tests/test-class-amp-dom-utils.php b/tests/test-class-amp-dom-utils.php index afc838f80a7..261e77f6596 100644 --- a/tests/test-class-amp-dom-utils.php +++ b/tests/test-class-amp-dom-utils.php @@ -82,71 +82,12 @@ public function test__is_node_empty__no__has_child() { } public function test__get_content_from_dom__br_no_closing_tag() { - $source = '
'; - $expected = '
'; + $source = '
'; + $expected = '
'; $dom = AMP_DOM_Utils::get_dom_from_content( $source ); $actual = AMP_DOM_Utils::get_content_from_dom( $dom ); $this->assertEquals( $expected, $actual ); } - - public function test__recursive_force_closing_tags__ignore_non_elements() { - $dom = new DOMDocument; - $node = $dom->createAttribute( 'src' ); - $expected = ' src=""'; - - $actual = AMP_DOM_Utils::get_content_from_dom_node( $dom, $node ); - $this->assertEquals( $expected, $actual ); - } - - public function test__recursive_force_closing_tags__ignore_self_closing() { - $dom = new DOMDocument; - $node = $dom->createElement( 'br' ); - $expected = '
'; - - $actual = AMP_DOM_Utils::get_content_from_dom_node( $dom, $node ); - - $this->assertEquals( $expected, $actual ); - } - - public function test__recursive_force_closing_tags__ignore_non_empty() { - $dom = new DOMDocument; - $node = $dom->createElement( 'p' ); - $text = $dom->createTextNode( 'Hello' ); - $node->appendChild( $text ); - $expected = '

Hello

'; - - $actual = AMP_DOM_Utils::get_content_from_dom_node( $dom, $node ); - - $this->assertEquals( $expected, $actual ); - - } - - public function test__recursive_force_closing_tags__force_close() { - $dom = new DOMDocument; - $node = $dom->createElement( 'amp-img' ); - $expected = ''; - - AMP_DOM_Utils::recursive_force_closing_tags( $dom, $node ); - - $this->assertEquals( $expected, $dom->saveXML( $node ) ); - // Extra test to confirm we added the child node - $this->assertTrue( $node->hasChildNodes() ); - $this->assertEquals( '', $node->firstChild->nodeValue ); - } - - public function test__recursive_force_closing_tags__force_close_with_children() { - $dom = new DOMDocument; - $node = $dom->createElement( 'div' ); - $child_with_closing = $dom->createElement( 'amp-img' ); - $child_self_closing = $dom->createElement( 'br' ); - $node->appendChild( $child_with_closing ); - $node->appendChild( $child_self_closing ); - $expected = '

'; - - $actual = AMP_DOM_Utils::get_content_from_dom_node( $dom, $node ); - - $this->assertEquals( $expected, $actual ); - } } diff --git a/tests/test-tag-and-attribute-sanitizer.php b/tests/test-tag-and-attribute-sanitizer.php index 304cd7a8b87..d19395a8a82 100644 --- a/tests/test-tag-and-attribute-sanitizer.php +++ b/tests/test-tag-and-attribute-sanitizer.php @@ -202,8 +202,8 @@ public function get_data() { ), 'form' => array( - '
', - '
', + '
', + '
', ), 'gfycat' => array( @@ -216,7 +216,7 @@ public function get_data() { ), 'empty_element' => array( - '
', + '
', ), 'merge_two_attr_specs' => array( @@ -370,7 +370,7 @@ public function get_data() { 'leave_attribute_on_node_with_present_mandatory_parent' => array( '
This is a test.
', - '
This is a test.
', + '
This is a test.
', ), 'disallowed_empty_attr_removed' => array( @@ -397,7 +397,7 @@ public function get_data() { ), 'allowed_tag_only' => array( - '

Text

', + '

Text

', '

Text

', ), @@ -465,7 +465,7 @@ public function get_data() { ), 'a_with_href_invalid' => array( - 'Link', + 'Link', ), 'a_with_href_scheme_tel' => array( @@ -568,6 +568,7 @@ public function test_sanitizer( $source, $expected = null ) { $sanitizer = new AMP_Tag_And_Attribute_Sanitizer( $dom ); $sanitizer->sanitize(); $content = AMP_DOM_Utils::get_content_from_dom( $dom ); + $content = preg_replace( '/(?<=>)\s+(?=<)/', '', $content ); $this->assertEquals( $expected, $content ); } }