Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carousel: Improve DOM Parsing #13547

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions modules/carousel/jetpack-carousel.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class_exists( 'Jetpack_AMP_Support' )

if ( has_block( 'gallery', $content ) || has_block( 'jetpack/tiled-gallery', $content ) ) {
$this->enqueue_assets();
$content = $this->add_data_to_container( $content );
$content = $this->add_data_to_html( $content );
}
return $content;
}
Expand Down Expand Up @@ -511,7 +511,12 @@ class_exists( 'Jetpack_AMP_Support' )
return $attr;
}

function add_data_to_container( $html ) {
function add_data_to_container( $gallery_style ) {
$gallery_style = $this->add_data_to_html( $gallery_style );
return preg_replace( '#</div>\s*+$#', '', $gallery_style );
}

function add_data_to_html( $html ) {
global $post;
if (
class_exists( 'Jetpack_AMP_Support' )
Expand Down Expand Up @@ -547,16 +552,28 @@ class_exists( 'Jetpack_AMP_Support' )
return $html;
}

$fake_root_tag = 'jetpack' . mt_rand( 10000, 99999 );
$charset = get_option( 'blog_charset', 'utf-8' );

// Let's grab all containers from the HTML.
$dom_doc = new DOMDocument();
$dom_doc->encoding = $charset;

/*
* The @ is not enough to suppress errors when dealing with libxml,
* we have to tell it directly how we want to handle errors.
*/
$old_libxml_disable_entity_loader = libxml_disable_entity_loader( true );
$old_libxml_use_internal_errors = libxml_use_internal_errors( true );
@$dom_doc->loadHTML( $html ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@$dom_doc->loadHTML( // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
sprintf(
'<head><meta http-equiv="Content-Type" content="text/html; charset=%1$s"/></head><%2$s>%3$s</%2$s>',
esc_attr( $charset ),
tag_escape( $fake_root_tag ),
$html
)
);

libxml_use_internal_errors( $old_libxml_use_internal_errors );
libxml_disable_entity_loader( $old_libxml_disable_entity_loader );

Expand Down Expand Up @@ -592,7 +609,12 @@ class_exists( 'Jetpack_AMP_Support' )
}

// Save our updated HTML.
$html = $dom_doc->saveHTML();
$fake_root_tag_length = strlen( $fake_root_tag ) + 2;
$html = substr(
$dom_doc->saveHTML( $dom_doc->getElementsByTagName( $fake_root_tag )->item( 0 ) ),
$fake_root_tag_length,
-1 * ( $fake_root_tag_length + 1 )
);
}
}

Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@
<testsuite name="geo-location">
<directory prefix="test_" suffix=".php">tests/php/modules/geo-location</directory>
</testsuite>
<testsuite name="carousel">
<directory prefix="test_" suffix=".php">tests/php/modules/carousel</directory>
</testsuite>
<testsuite name="deprecation">
<file>tests/php/test_deprecation.php</file>
</testsuite>
Expand Down
81 changes: 81 additions & 0 deletions tests/php/modules/carousel/test_jetpack-carousel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

require_once dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) . '/modules/carousel.php';

class Test_Jetpack_Carousel extends WP_UnitTestCase {
static $post_id = 0;
static $charset = '';

var $carousel;

public static function wpSetUpBeforeClass( $factory ) {
self::$post_id = self::factory()->post->create();
self::$charset = get_option( 'blog_charset', 'utf-8' );
}

public static function wpTearDownAfterClass() {
update_option( 'blog_charset', self::$charset );
}

public function setUp() {
$GLOBALS['post'] = get_post( self::$post_id );

$this->carousel = $this->getMockBuilder( 'Jetpack_Carousel' )
->setMethods( null )
->disableOriginalConstructor()
->getMock();
}

public function test_add_data_to_container() {
$extra = 'data-carousel-extra=\'{"blog_id":1,"permalink":"http:\\/\\/example.org\\/?p=' . self::$post_id . '"}\'';

$this->assertEquals( '<div class="gallery" ' . $extra . '>', $this->carousel->add_data_to_container( '<div class="gallery">' ) );
}

public function utf_8_provider() {
// Data Providers are called prior to `::setUpBeforeClass()`, so we can't know `self::$post_id` here.
$extra = 'data-carousel-extra=\'{"blog_id":1,"permalink":"http:\\/\\/example.org\\/?p=%%%POST_ID%%%"}\'';

return [
'ascii' => [ '<div class="gallery">hello</div>', '<div class="gallery" ' . $extra . '>hello</div>' ],
'latin with diacritics' => [ '<div class="gallery">Ĵëṫṕãḉǩ</div>', '<div class="gallery" ' . $extra . '>Ĵëṫṕãḉǩ</div>' ],
'encoded' => [ '<div class="gallery">&#308;&euml;&#7787;&#7765;&atilde;&#7689;&#489;</div>', '<div class="gallery" ' . $extra . '>&#308;&euml;&#7787;&#7765;&atilde;&#7689;&#489;</div>' ],
'japanese' => [ '<div class="gallery">最高のパック</div>', '<div class="gallery" ' . $extra . '>最高のパック</div>' ],
'linear b (4-byte)' => [ '<div class="gallery">𐁂𐀐𐀷</div>', '<div class="gallery" ' . $extra . '>𐁂𐀐𐀷</div>' ],
'emoji (4-byte)' => [ '<div class="gallery">✈️🎒</div>', '<div class="gallery" ' . $extra . '>✈️🎒</div>' ],
];
}

/**
* @dataProvider utf_8_provider
*/
public function test_add_data_to_html_with_utf_8_input( $input, $expected ) {
$expected = str_replace( '%%%POST_ID%%%', self::$post_id, $expected );

update_option( 'blog_charset', 'utf-8' );

$this->assertEquals( $expected, $this->carousel->add_data_to_html( $input ) );
}

public function big_5_provider() {
// Data Providers are called prior to `::setUpBeforeClass()`, so we can't know `self::$post_id` here.
$extra = 'data-carousel-extra=\'{"blog_id":1,"permalink":"http:\\/\\/example.org\\/?p=%%%POST_ID%%%"}\'';

return [
'ascii' => [ '<div class="gallery">hello</div>', '<div class="gallery" ' . $extra . '>hello</div>' ],
'common characters' => [ "<div class=\"gallery\">\xB1\x60\xA5\xCE\xA6\x72</div>", "<div class=\"gallery\" $extra>\xB1\x60\xA5\xCE\xA6\x72</div>" ],
'graphical characters' => [ "<div class=\"gallery\">\xA1\x4B\xA1\x4B</div>", "<div class=\"gallery\" $extra>\xA1\x4B\xA1\x4B</div>" ],
];
}

/**
* @dataProvider big_5_provider
*/
public function test_add_data_to_html_with_big_5_input( $input, $expected ) {
$expected = str_replace( '%%%POST_ID%%%', self::$post_id, $expected );

update_option( 'blog_charset', 'big-5' );

$this->assertEquals( $expected, $this->carousel->add_data_to_html( $input ) );
}
}