From 043b70cc339fce6a9b898b99adea7aae81940ee8 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 8 Feb 2023 13:00:34 +0100 Subject: [PATCH] [TASK] Move html utility function --- Classes/Hooks/TranslateHook.php | 31 ++++--------------------------- Classes/Utility/HtmlUtility.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 Classes/Utility/HtmlUtility.php diff --git a/Classes/Hooks/TranslateHook.php b/Classes/Hooks/TranslateHook.php index a6406ba5..cfadc911 100644 --- a/Classes/Hooks/TranslateHook.php +++ b/Classes/Hooks/TranslateHook.php @@ -41,6 +41,7 @@ use WebVision\WvDeepltranslate\Domain\Repository\SettingsRepository; use WebVision\WvDeepltranslate\Service\DeeplService; use WebVision\WvDeepltranslate\Service\GoogleTranslateService; +use WebVision\WvDeepltranslate\Utility\HtmlUtility; class TranslateHook { @@ -128,8 +129,8 @@ public function processTranslateTo_copyAction(string &$content, array $languageR $sourceLanguageIso = $sourceLanguage['language_isocode']; $deeplSourceIso = $sourceLanguageIso; } - if ($this->isHtml($content)) { - $content = $this->stripSpecificTags(['br'], $content); + if (HtmlUtility::isHtml($content)) { + $content = HtmlUtility::stripSpecificTags(['br'], $content); } // mode deepl @@ -153,7 +154,7 @@ public function processTranslateTo_copyAction(string &$content, array $languageR $response = $this->googleService->translate($deeplSourceIso, $targetLanguageIso, $content); if (!empty($response)) { - if ($this->isHtml($response)) { + if (HtmlUtility::isHtml($response)) { $content = preg_replace('/\/\s/', '/', $response); $content = preg_replace('/\>\s+/', '>', $content); } else { @@ -165,28 +166,4 @@ public function processTranslateTo_copyAction(string &$content, array $languageR return $content; } - - /** - * check whether the string contains html - * - * @param string $string - */ - public function isHtml(string $string): bool - { - return preg_match('/<[^<]+>/', $string, $m) != 0; - } - - /** - * stripoff the tags provided - * - * @param string[] $tags - */ - public function stripSpecificTags(array $tags, string $content): string - { - foreach ($tags as $tag) { - $content = preg_replace('/<\\/?' . $tag . '(.|\\s)*?>/', '', $content); - } - - return $content; - } } diff --git a/Classes/Utility/HtmlUtility.php b/Classes/Utility/HtmlUtility.php new file mode 100644 index 00000000..8b67abc7 --- /dev/null +++ b/Classes/Utility/HtmlUtility.php @@ -0,0 +1,32 @@ +/', $string, $m) != 0; + } + + /** + * stripoff the tags provided + * + * @param string[] $tags + */ + public static function stripSpecificTags(array $tags, string $content): string + { + foreach ($tags as $tag) { + $content = preg_replace('/<\\/?' . $tag . '(.|\\s)*?>/', '', $content); + } + + return $content; + } +}