diff --git a/composer.json b/composer.json index 2d32b371..98a04bec 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "smartling/wordpress-connector", "license": "GPL-2.0-or-later", - "version": "5.5.0", + "version": "5.5.1", "description": "", "type": "wordpress-plugin", "repositories": [ diff --git a/inc/Smartling/WP/View/ContentEditJob.php b/inc/Smartling/WP/View/ContentEditJob.php index 20d57e44..92585d6f 100644 --- a/inc/Smartling/WP/View/ContentEditJob.php +++ b/inc/Smartling/WP/View/ContentEditJob.php @@ -91,150 +91,135 @@ let globalButton; - -
-

Translate content

-
- - -
-
Please wait...
-
- - resolveTemplate(true); + $this->assertDivBalanced($html, 'taxonomy edit screen ($needWrapper=true)'); + } + + public function testHtmlDivTagsBalanceOnPostEditScreen(): void + { + $html = $this->resolveTemplate(false); + $this->assertDivBalanced($html, 'post edit screen ($needWrapper=false)'); + } + + public function testHtmlDivTagsBalanceOnBulkSubmitScreen(): void + { + $html = $this->resolveTemplate(false, true); + $this->assertDivBalanced($html, 'bulk submit screen ($isBulkSubmitPage=true)'); + } + + public function testHiddenWrapperHasClosingTag(): void + { + $html = $this->resolveTemplate(false); + + $this->assertSame( + 1, + preg_match( + '##i', + $html, + $matches, + PREG_OFFSET_CAPTURE + ), + 'Hidden display:none wrapper opening
should exist' + ); + + $tail = substr($html, $matches[0][1]); + $opens = preg_match_all('##i', $tail); + + $this->assertSame( + $opens, + $closes, + 'Hidden display:none wrapper must have a matching closing
' . + '(an unclosed wrapper here is what caused WP-1004).' + ); + } + + private function resolveTemplate(bool $needWrapper, bool $isBulkSubmitPage = false): string + { + $source = $this->stripNonHtml(file_get_contents(self::VIEW_FILE)); + + $source = preg_replace_callback( + '#<\?php\s+if\s*\(\s*\$needWrapper\s*\)\s*:\s*\?>((?:(?!<\?php\s+endif).)*)<\?php\s+endif\s*;\s*\?>#s', + static fn(array $m): string => $needWrapper ? $m[1] : '', + $source + ); + + $source = preg_replace_callback( + '#<\?php\s.*?if\s*\(\s*!\$isBulkSubmitPage\s*\)\s*:\s*\?>((?:(?!<\?php\s+endif).)*)<\?php\s+endif\s*;\s*\?>#s', + static fn(array $m): string => $isBulkSubmitPage ? '' : $m[1], + $source + ); + + $source = preg_replace('#<\?(?:php|=).*?\?>#s', '', $source); + + return $source; + } + + private function stripNonHtml(string $source): string + { + $source = preg_replace('#]*>.*?#is', '', $source); + $source = preg_replace('#]*>.*?#is', '', $source); + + return $source; + } + + private function assertDivBalanced(string $html, string $context): void + { + $opens = preg_match_all('##i', $html); + + $this->assertSame( + $opens, + $closes, + sprintf( + 'Unbalanced
tags on %s: %d opens vs %d closes. ' . + 'A mismatch here is what caused WP-1004 (postboxes below the Smartling box refusing to open).', + $context, + $opens, + $closes + ) + ); + } +}