Skip to content

Commit c38bf3e

Browse files
tooonidamienalexandre
authored andcommitted
fixed attribution replacement (#78)
* only match chars until next </x-trans occurence * match when used in special links if a translation is used as a link (localization)`<a href="email:*localizedEmail*">` the regex wouldn't match. * added EditInPlace tests * leave additional strings in attribute * try to add encoding to DomDocument * cs * convert encoding
1 parent b05aaa6 commit c38bf3e

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

EventListener/EditInPlaceResponseListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function onKernelResponse(FilterResponseEvent $event)
7474
$content = $event->getResponse()->getContent();
7575

7676
// Clean the content for malformed tags in attributes or encoded tags
77-
$content = preg_replace("@=\\s*[\"']\\s*(<x-trans.+<\\/x-trans>)\\s*[\"']@mi", "=\"🚫 Can't be translated here. 🚫\"", $content);
77+
$content = preg_replace("@=\\s*[\"']\\s*(.[a-zA-Z]+:|)(<x-trans.+?(?=<\\/x-trans)<\\/x-trans>)\\s*[\"']@mi", "=\"$1🚫 Can't be translated here. 🚫\"", $content);
7878
$content = preg_replace('@&lt;x-trans.+data-key=&quot;([^&]+)&quot;.+&lt;\\/x-trans&gt;@mi', '🚫 $1 🚫', $content);
7979

8080
$html = sprintf(

Tests/Functional/EditInPlaceTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,21 @@ public function testActivatedTest()
4242
self::assertSame(200, $response->getStatusCode());
4343
self::assertContains('<!-- TranslationBundle -->', $response->getContent());
4444

45-
$dom = new \DOMDocument();
46-
@$dom->loadHTML($response->getContent());
45+
$dom = new \DOMDocument('1.0', 'utf-8');
46+
@$dom->loadHTML(mb_convert_encoding($response->getContent(), 'HTML-ENTITIES', 'UTF-8'));
4747
$xpath = new \DomXpath($dom);
4848

49+
// Check number of x-trans tags
4950
$xtrans = $xpath->query('//x-trans');
51+
self::assertEquals(6, $xtrans->length);
5052

51-
self::assertEquals(5, $xtrans->length);
53+
// Check attribute with prefix (href="mailto:...")
54+
$emailTag = $dom->getElementById('email');
55+
self::assertEquals('mailto:'.'🚫 Can\'t be translated here. 🚫', $emailTag->getAttribute('href'));
56+
self::assertEquals('localized.email', $emailTag->textContent);
57+
58+
// Check attribute
59+
$attributeDiv = $dom->getElementById('attribute-div');
60+
self::assertEquals('🚫 Can\'t be translated here. 🚫', $attributeDiv->getAttribute('data-value'));
5261
}
5362
}

Tests/Functional/app/Resources/views/translated.html.twig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
</head>
77
<body>
88
<h1>{{ 'translated.heading'|transchoice(12) }}</h1>
9-
<p>{{ 'translated.paragraph0'|trans }}</p>
10-
<p>{% trans %}translated.paragraph1{% endtrans %}</p>
11-
<p>{% transchoice 21 %}translated.paragraph2{% endtranschoice %}</p>
9+
<p>{{ 'translated.paragraph0'|trans }}</p><p>{% trans %}translated.paragraph1{% endtrans %}</p><!-- two translation in same html line -->
10+
<p>
11+
{% transchoice 21 %}translated.paragraph2{% endtranschoice %}
12+
<a id="email" href="mailto:{{ 'localized.email'|trans }}">{{ 'localized.email'|trans }}</a><!-- translation inside attribute with additional content -->
13+
</p>
14+
<div id="attribute-div" data-value="{{ 'translated.attribute'|trans }}"></div> <!-- translation inside html attribute -->
1215
</body>
1316
</html>

0 commit comments

Comments
 (0)