From 1243c2fb299c94d999c17c26058e56ccc3712360 Mon Sep 17 00:00:00 2001 From: MBriedis Date: Mon, 27 Aug 2018 09:53:07 +0300 Subject: [PATCH] VueJS DOM parsing fix. Use regex to extract script instead of DOM operations. --- src/Extractors/VueJs.php | 24 +++++++++++++++++++++--- tests/assets/vuejs/Csv.csv | 2 +- tests/assets/vuejs/CsvDictionary.csv | 2 +- tests/assets/vuejs/Jed.json | 2 +- tests/assets/vuejs/Json.json | 2 +- tests/assets/vuejs/JsonDictionary.json | 2 +- tests/assets/vuejs/PhpArray.php | 2 +- tests/assets/vuejs/Po.po | 2 +- tests/assets/vuejs/Xliff.xlf | 4 ++-- tests/assets/vuejs/Yaml.yml | 2 +- tests/assets/vuejs/YamlDictionary.yml | 2 +- tests/assets/vuejs/input.vue | 2 +- 12 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/Extractors/VueJs.php b/src/Extractors/VueJs.php index b7fdc1ef..c84b8364 100644 --- a/src/Extractors/VueJs.php +++ b/src/Extractors/VueJs.php @@ -43,14 +43,16 @@ public static function fromString($string, Translations $translations, array $op // VueJS files are valid HTML files, we will operate with the DOM here $dom = self::convertHtmlToDom($string); + $script = self::extractScriptTag($string); + // Parse the script part as a regular JS code - $script = $dom->getElementsByTagName('script')->item(0); if ($script) { + $scriptLineNumber = $dom->getElementsByTagName('script')->item(0)->getLineNo(); self::getScriptTranslationsFromString( - $script->textContent, + $script, $translations, $options, - $script->getLineNo() - 1 + $scriptLineNumber - 1 ); } @@ -67,6 +69,22 @@ public static function fromString($string, Translations $translations, array $op } } + /** + * Extracts script tag contents using regex instead of DOM operations. + * If we parse using DOM, some contents may change, for example, tags within strings will be stripped + * + * @param $string + * @return bool|string + */ + private static function extractScriptTag($string) + { + if (preg_match('#<\s*?script\b[^>]*>(.*?)]*>#s', $string, $matches)) { + return $matches[1]; + } + + return ''; + } + /** * @param string $html * @return DOMDocument diff --git a/tests/assets/vuejs/Csv.csv b/tests/assets/vuejs/Csv.csv index 81222142..fc4ad9f3 100644 --- a/tests/assets/vuejs/Csv.csv +++ b/tests/assets/vuejs/Csv.csv @@ -13,7 +13,7 @@ Report-Msgid-Bugs-To: ,js-single, ,js-obj-single, some-context,js-action, -,js-return, +,js-return
, ,t-cond-attribute, ,t-attribute, ,t-same-line-s, diff --git a/tests/assets/vuejs/CsvDictionary.csv b/tests/assets/vuejs/CsvDictionary.csv index bce713c3..3a2613d7 100644 --- a/tests/assets/vuejs/CsvDictionary.csv +++ b/tests/assets/vuejs/CsvDictionary.csv @@ -4,7 +4,7 @@ js-alert, js-single, js-obj-single, js-action, -js-return, +js-return
, t-cond-attribute, t-attribute, t-same-line-s, diff --git a/tests/assets/vuejs/Jed.json b/tests/assets/vuejs/Jed.json index def8f930..0b19d562 100644 --- a/tests/assets/vuejs/Jed.json +++ b/tests/assets/vuejs/Jed.json @@ -23,7 +23,7 @@ "some-context\\u0004js-action": [ "" ], - "js-return": [ + "js-return<\/span>
": [ "" ], "t-cond-attribute": [ diff --git a/tests/assets/vuejs/Json.json b/tests/assets/vuejs/Json.json index a52344bc..55df1ac9 100644 --- a/tests/assets/vuejs/Json.json +++ b/tests/assets/vuejs/Json.json @@ -21,7 +21,7 @@ "js-obj-single": [ "" ], - "js-return": [ + "js-return<\/span>
": [ "" ], "t-cond-attribute": [ diff --git a/tests/assets/vuejs/JsonDictionary.json b/tests/assets/vuejs/JsonDictionary.json index 5d5c1e26..6b76fdaa 100644 --- a/tests/assets/vuejs/JsonDictionary.json +++ b/tests/assets/vuejs/JsonDictionary.json @@ -5,7 +5,7 @@ "js-single": "", "js-obj-single": "", "js-action": "", - "js-return": "", + "js-return<\/span>
": "", "t-cond-attribute": "", "t-attribute": "", "t-same-line-s": "", diff --git a/tests/assets/vuejs/PhpArray.php b/tests/assets/vuejs/PhpArray.php index f0d62b12..205d0fbe 100644 --- a/tests/assets/vuejs/PhpArray.php +++ b/tests/assets/vuejs/PhpArray.php @@ -37,7 +37,7 @@ array ( 0 => '', ), - 'js-return' => + 'js-return
' => array ( 0 => '', ), diff --git a/tests/assets/vuejs/Po.po b/tests/assets/vuejs/Po.po index 7d6f0575..1454cd56 100644 --- a/tests/assets/vuejs/Po.po +++ b/tests/assets/vuejs/Po.po @@ -37,7 +37,7 @@ msgid "js-action" msgstr "" #: ./tests/assets/vuejs/input.vue:56 -msgid "js-return" +msgid "js-return
" msgstr "" #: ./tests/assets/vuejs/input.vue:4 diff --git a/tests/assets/vuejs/Xliff.xlf b/tests/assets/vuejs/Xliff.xlf index 2251bfeb..c349fbf4 100644 --- a/tests/assets/vuejs/Xliff.xlf +++ b/tests/assets/vuejs/Xliff.xlf @@ -71,13 +71,13 @@ - + ./tests/assets/vuejs/input.vue:56 - js-return + js-return

]]> diff --git a/tests/assets/vuejs/Yaml.yml b/tests/assets/vuejs/Yaml.yml index 7f21bbce..9a293cc7 100644 --- a/tests/assets/vuejs/Yaml.yml +++ b/tests/assets/vuejs/Yaml.yml @@ -9,7 +9,7 @@ messages: js-alert: '' js-single: '' js-obj-single: '' - js-return: '' + 'js-return
': '' t-cond-attribute: '' t-attribute: '' t-same-line-s: '' diff --git a/tests/assets/vuejs/YamlDictionary.yml b/tests/assets/vuejs/YamlDictionary.yml index 1fb0d527..3526d61e 100644 --- a/tests/assets/vuejs/YamlDictionary.yml +++ b/tests/assets/vuejs/YamlDictionary.yml @@ -4,7 +4,7 @@ js-alert: '' js-single: '' js-obj-single: '' js-action: '' -js-return: '' +'js-return
': '' t-cond-attribute: '' t-attribute: '' t-same-line-s: '' diff --git a/tests/assets/vuejs/input.vue b/tests/assets/vuejs/input.vue index 24d7bbf1..17cef8e8 100644 --- a/tests/assets/vuejs/input.vue +++ b/tests/assets/vuejs/input.vue @@ -53,7 +53,7 @@ var string = 'something' + this.p__('some-context', 'js-action'); - return this.gettext('js-return'); + return this.gettext('js-return
'); } }, }