From eea147ab420be10c0fd73926c86f322467c604c1 Mon Sep 17 00:00:00 2001 From: Mauro Cassani Date: Wed, 9 Jun 2021 16:34:21 +0200 Subject: [PATCH] Handling ph tags with dataRef attribute without any correspondence on dataRef map --- src/Filters/DataRefReplace.php | 60 +++++++++++++++++++++++++++++++ tests/MateCatSubFilteringTest.php | 19 ++++++++++ 2 files changed, 79 insertions(+) diff --git a/src/Filters/DataRefReplace.php b/src/Filters/DataRefReplace.php index fc067b7..c3cab1a 100644 --- a/src/Filters/DataRefReplace.php +++ b/src/Filters/DataRefReplace.php @@ -3,6 +3,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; +use Matecat\XliffParser\Utils\HtmlParser; use Matecat\XliffParser\XliffUtils\DataRefReplacer; class DataRefReplace extends AbstractHandler { @@ -30,15 +31,74 @@ public function transform( $segment ) { // dataRefMap is present only in xliff 2.0 files if ( empty( $this->dataRefMap ) ) { + $segment = $this->replaceXliffPhTagsWithNoDataRefToMatecatPhTags($segment); + return $this->replaceXliffPcTagsToMatecatPhTags($segment); } $dataRefReplacer = new DataRefReplacer( $this->dataRefMap ); $segment = $dataRefReplacer->replace( $segment ); + $segment = $this->replaceXliffPhTagsWithNoDataRefToMatecatPhTags($segment); return $this->replaceXliffPcTagsToMatecatPhTags($segment); } + /** + * This function replace encoded ph tags (from Xliff 2.0) without any dataRef correspondence + * to regular Matecat tag for UI presentation + * + * Example: + * + * We can control who sees content when with Visibility Constraints. + * + * is transformed to: + * + * We can control who sees content when with <ph id="mtc_ph_u_1" equiv-text="base64:Jmx0O3BoIGlkPSJzb3VyY2UxIiBkYXRhUmVmPSJzb3VyY2UxIi8mZ3Q7"/>Visibility Constraints. + * + * @param $segment + * + * @return string|string[] + */ + private function replaceXliffPhTagsWithNoDataRefToMatecatPhTags($segment) + { + preg_match_all('/<(ph .*?)>/iu', $segment, $phTags); + + if(count($phTags[0]) === 0) { + return $segment; + } + + $phIndex = 1; + + foreach ($phTags[0] as $phTag){ + // check if phTag has not any correspondence on dataRef map + if($this->isAPhTagWithNoDataRefCorrespondence($phTag)){ + $phMatecat = '<ph id="mtc_ph_u_'.$phIndex.'" equiv-text="base64:'.base64_encode($phTag).'"/>'; + $segment = str_replace($phTag, $phMatecat, $segment); + $phIndex++; + } + } + + return $segment; + } + + /** + * This function checks if a ph tag with dataRef attribute a correspondence on dataRef map + * + * @param string $phTag + * + * @return bool + */ + private function isAPhTagWithNoDataRefCorrespondence($phTag) + { + $parsed = HtmlParser::parse($phTag); + + return ( + isset($parsed[0]) and + isset($parsed[0]->attributes['dataRef']) and + !array_key_exists($parsed[0]->attributes['dataRef'], $this->dataRefMap + )); + } + /** * This function replace encoded pc tags (from Xliff 2.0) without any dataRef correspondence * to regular Matecat tag for UI presentation diff --git a/tests/MateCatSubFilteringTest.php b/tests/MateCatSubFilteringTest.php index 84a1168..4c0ba6c 100644 --- a/tests/MateCatSubFilteringTest.php +++ b/tests/MateCatSubFilteringTest.php @@ -271,6 +271,25 @@ public function testTwigUngreedy() ************************** */ + public function testPhWithoutDataRef() + { + $db_segment = 'We can control who sees content when with Visibility Constraints.'; + $Filter = MateCatFilter::getInstance( new FeatureSet(), 'en-EN','et-ET', [] ); + + $expected_l1_segment = 'We can control who sees content when with Visibility Constraints.'; + $expected_l2_segment = 'We can control who sees content when with <ph id="mtc_ph_u_1" equiv-text="base64:Jmx0O3BoIGlkPSJzb3VyY2UxIiBkYXRhUmVmPSJzb3VyY2UxIi8mZ3Q7"/>Visibility Constraints.'; + + $l1_segment = $Filter->fromLayer0ToLayer1( $db_segment ); + $l2_segment = $Filter->fromLayer1ToLayer2( $l1_segment ); + + $this->assertEquals($l1_segment, $expected_l1_segment); + $this->assertEquals($l2_segment, $expected_l2_segment); + + $back_to_db_segment =$Filter->fromLayer1ToLayer0($l1_segment); + + $this->assertEquals($back_to_db_segment, $db_segment); + } + /** * @throws \Exception */