diff --git a/src/class-convertkit-api-traits.php b/src/class-convertkit-api-traits.php index 7d564be..27a0163 100644 --- a/src/class-convertkit-api-traits.php +++ b/src/class-convertkit-api-traits.php @@ -2682,6 +2682,11 @@ public function convert_relative_to_absolute_urls(\DOMNodeList $elements, string continue; } + // Skip if the attribute's value is an anchor. + if (strpos($element->getAttribute($attribute), '#') !== false) { + continue; + } + // Remove element if it's rocket-loader.min.js. Including it prevents landing page redirects from working. if (strpos($element->getAttribute($attribute), 'rocket-loader.min.js') !== false) { if ($element->parentNode instanceof \DOMNode) { diff --git a/tests/Integration/KitMethodsTest.php b/tests/Integration/KitMethodsTest.php new file mode 100644 index 0000000..2b1df4c --- /dev/null +++ b/tests/Integration/KitMethodsTest.php @@ -0,0 +1,155 @@ +api = new \ConvertKit_API_V4( + client_id: $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + redirect_uri: $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + access_token: $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + refresh_token: $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] + ); + } + + /** + * Test the convert_relative_to_absolute_urls() method. + * + * @since 2.6.2 + * + * @return void + */ + public function testConvertRelativeToAbsoluteUrls() + { + // Setup HTML in DOMDocument. + $html = new \DOMDocument(); + $html->loadHTML( + ' + + + + + + Test + Anchor + + +
Test
+ + ' + ); + + // Define URL to prepend to relative URLs. + $url_scheme_host_only = 'https://example.com'; + + // Convert relative URLs to absolute URLs for elements we want to test. + $this->api->convert_relative_to_absolute_urls( + $html->getElementsByTagName('a'), + 'href', + $url_scheme_host_only + ); + $this->api->convert_relative_to_absolute_urls( + $html->getElementsByTagName('link'), + 'href', + $url_scheme_host_only + ); + $this->api->convert_relative_to_absolute_urls( + $html->getElementsByTagName('img'), + 'src', + $url_scheme_host_only + ); + $this->api->convert_relative_to_absolute_urls( + $html->getElementsByTagName('script'), + 'src', + $url_scheme_host_only + ); + $this->api->convert_relative_to_absolute_urls( + $html->getElementsByTagName('form'), + 'action', + $url_scheme_host_only + ); + + // Fetch HTML string. + $output = $html->saveHTML(); + + // Assert string contains expected HTML elements that should not be modified. + $this->assertStringContainsString('', $output); + + // Assert string does not contain HTML elements that should be removed. + $this->assertStringNotContainsString( + '', + $output + ); + + // Assert string contains expected HTML elements that should be modified. + $this->assertStringContainsString( + 'Test', + $output + ); + $this->assertStringContainsString( + '', + $output + ); + $this->assertStringContainsString( + '', + $output + ); + $this->assertStringContainsString( + '
Test
', + $output + ); + + // Assert string contains expected HTML elements that should not be modified. + $this->assertStringContainsString( + 'Anchor', + $output + ); + } + + /** + * Test that the get_body_html() method returns the expected HTML. + * + * @since 2.6.2 + */ + public function testGetBodyHtml() + { + $content = '

Vantar þinn ungling sjálfstraust í stærðfræði?

This is a test

'; + $html = new \DOMDocument(); + $html->loadHTML( + ' + + ' . $content . ' + ' + ); + $this->assertEquals($content, $this->api->get_body_html($html)); + } +}