Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VueJS DOM parsing fix #188

Merged
merged 1 commit into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/Extractors/VueJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand All @@ -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[^>]*>(.*?)</script\b[^>]*>#s', $string, $matches)) {
return $matches[1];
}

return '';
}

/**
* @param string $html
* @return DOMDocument
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/vuejs/Csv.csv
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Report-Msgid-Bugs-To:
,js-single,
,js-obj-single,
some-context,js-action,
,js-return,
,<span>js-return</span><br>,
,t-cond-attribute,
,t-attribute,
,t-same-line-s,
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/vuejs/CsvDictionary.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ js-alert,
js-single,
js-obj-single,
js-action,
js-return,
<span>js-return</span><br>,
t-cond-attribute,
t-attribute,
t-same-line-s,
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/vuejs/Jed.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"some-context\\u0004js-action": [
""
],
"js-return": [
"<span>js-return<\/span><br>": [
""
],
"t-cond-attribute": [
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/vuejs/Json.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"js-obj-single": [
""
],
"js-return": [
"<span>js-return<\/span><br>": [
""
],
"t-cond-attribute": [
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/vuejs/JsonDictionary.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"js-single": "",
"js-obj-single": "",
"js-action": "",
"js-return": "",
"<span>js-return<\/span><br>": "",
"t-cond-attribute": "",
"t-attribute": "",
"t-same-line-s": "",
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/vuejs/PhpArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
array (
0 => '',
),
'js-return' =>
'<span>js-return</span><br>' =>
array (
0 => '',
),
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/vuejs/Po.po
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ msgid "js-action"
msgstr ""

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

#: ./tests/assets/vuejs/input.vue:4
Expand Down
4 changes: 2 additions & 2 deletions tests/assets/vuejs/Xliff.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@
<target></target>
</segment>
</unit>
<unit id="526e26ae7627d2587e7217f4cad5ff2a">
<unit id="165738b03fc41d793cf080ad7f3bf5e3">
<notes>
<note category="context"></note>
<note category="reference">./tests/assets/vuejs/input.vue:56</note>
</notes>
<segment>
<source>js-return</source>
<source><![CDATA[<span>js-return</span><br>]]></source>
<target></target>
</segment>
</unit>
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/vuejs/Yaml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ messages:
js-alert: ''
js-single: ''
js-obj-single: ''
js-return: ''
'<span>js-return</span><br>': ''
t-cond-attribute: ''
t-attribute: ''
t-same-line-s: ''
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/vuejs/YamlDictionary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ js-alert: ''
js-single: ''
js-obj-single: ''
js-action: ''
js-return: ''
'<span>js-return</span><br>': ''
t-cond-attribute: ''
t-attribute: ''
t-same-line-s: ''
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/vuejs/input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

var string = 'something' + this.p__('some-context', 'js-action');

return this.gettext('js-return');
return this.gettext('<span>js-return</span><br>');
}
},
}
Expand Down