Skip to content

Commit cb08ec5

Browse files
briedisoscarotero
authored andcommitted
VueJS DOM parsing fix. Use regex to extract script instead of DOM operations. (#188)
1 parent cae84af commit cb08ec5

12 files changed

Lines changed: 33 additions & 15 deletions

File tree

src/Extractors/VueJs.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ public static function fromString($string, Translations $translations, array $op
4343
// VueJS files are valid HTML files, we will operate with the DOM here
4444
$dom = self::convertHtmlToDom($string);
4545

46+
$script = self::extractScriptTag($string);
47+
4648
// Parse the script part as a regular JS code
47-
$script = $dom->getElementsByTagName('script')->item(0);
4849
if ($script) {
50+
$scriptLineNumber = $dom->getElementsByTagName('script')->item(0)->getLineNo();
4951
self::getScriptTranslationsFromString(
50-
$script->textContent,
52+
$script,
5153
$translations,
5254
$options,
53-
$script->getLineNo() - 1
55+
$scriptLineNumber - 1
5456
);
5557
}
5658

@@ -67,6 +69,22 @@ public static function fromString($string, Translations $translations, array $op
6769
}
6870
}
6971

72+
/**
73+
* Extracts script tag contents using regex instead of DOM operations.
74+
* If we parse using DOM, some contents may change, for example, tags within strings will be stripped
75+
*
76+
* @param $string
77+
* @return bool|string
78+
*/
79+
private static function extractScriptTag($string)
80+
{
81+
if (preg_match('#<\s*?script\b[^>]*>(.*?)</script\b[^>]*>#s', $string, $matches)) {
82+
return $matches[1];
83+
}
84+
85+
return '';
86+
}
87+
7088
/**
7189
* @param string $html
7290
* @return DOMDocument

tests/assets/vuejs/Csv.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Report-Msgid-Bugs-To:
1313
,js-single,
1414
,js-obj-single,
1515
some-context,js-action,
16-
,js-return,
16+
,<span>js-return</span><br>,
1717
,t-cond-attribute,
1818
,t-attribute,
1919
,t-same-line-s,

tests/assets/vuejs/CsvDictionary.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ js-alert,
44
js-single,
55
js-obj-single,
66
js-action,
7-
js-return,
7+
<span>js-return</span><br>,
88
t-cond-attribute,
99
t-attribute,
1010
t-same-line-s,

tests/assets/vuejs/Jed.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"some-context\\u0004js-action": [
2424
""
2525
],
26-
"js-return": [
26+
"<span>js-return<\/span><br>": [
2727
""
2828
],
2929
"t-cond-attribute": [

tests/assets/vuejs/Json.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"js-obj-single": [
2222
""
2323
],
24-
"js-return": [
24+
"<span>js-return<\/span><br>": [
2525
""
2626
],
2727
"t-cond-attribute": [

tests/assets/vuejs/JsonDictionary.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"js-single": "",
66
"js-obj-single": "",
77
"js-action": "",
8-
"js-return": "",
8+
"<span>js-return<\/span><br>": "",
99
"t-cond-attribute": "",
1010
"t-attribute": "",
1111
"t-same-line-s": "",

tests/assets/vuejs/PhpArray.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
array (
3838
0 => '',
3939
),
40-
'js-return' =>
40+
'<span>js-return</span><br>' =>
4141
array (
4242
0 => '',
4343
),

tests/assets/vuejs/Po.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ msgid "js-action"
3737
msgstr ""
3838

3939
#: ./tests/assets/vuejs/input.vue:56
40-
msgid "js-return"
40+
msgid "<span>js-return</span><br>"
4141
msgstr ""
4242

4343
#: ./tests/assets/vuejs/input.vue:4

tests/assets/vuejs/Xliff.xlf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@
7171
<target></target>
7272
</segment>
7373
</unit>
74-
<unit id="526e26ae7627d2587e7217f4cad5ff2a">
74+
<unit id="165738b03fc41d793cf080ad7f3bf5e3">
7575
<notes>
7676
<note category="context"></note>
7777
<note category="reference">./tests/assets/vuejs/input.vue:56</note>
7878
</notes>
7979
<segment>
80-
<source>js-return</source>
80+
<source><![CDATA[<span>js-return</span><br>]]></source>
8181
<target></target>
8282
</segment>
8383
</unit>

tests/assets/vuejs/Yaml.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ messages:
99
js-alert: ''
1010
js-single: ''
1111
js-obj-single: ''
12-
js-return: ''
12+
'<span>js-return</span><br>': ''
1313
t-cond-attribute: ''
1414
t-attribute: ''
1515
t-same-line-s: ''

0 commit comments

Comments
 (0)