Skip to content

Commit

Permalink
DataRefReplacer do not ancode nor expect encoded xliff
Browse files Browse the repository at this point in the history
  • Loading branch information
Ostico committed Sep 27, 2023
1 parent 1a7841e commit e8c8e2b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
43 changes: 13 additions & 30 deletions src/XliffUtils/DataRefReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,16 @@ public function replace( $string ) {
}

// 2. Replace <pc> tags
$toBeEscaped = Strings::isAnEscapedHTML( $string );

if ( $this->stringContainsPcTags( $string, $toBeEscaped ) ) {
if ( $this->stringContainsPcTags( $string ) ) {

// replace self-closed <pc />
$string = $this->replaceSelfClosedPcTags( $string, $toBeEscaped );
$string = $this->replaceSelfClosedPcTags( $string );

// create a dataRefEnd map
// (needed for correct handling of </pc> closing tags)
$dataRefEndMap = $this->buildDataRefEndMap( $html );
$string = $this->replaceOpeningPcTags( $string, $toBeEscaped );
$string = $this->replaceClosingPcTags( $string, $toBeEscaped, $dataRefEndMap );
$string = ( $toBeEscaped ) ? Strings::escapeOnlyHTMLTags( $string ) : $string;
$string = $this->replaceOpeningPcTags( $string );
$string = $this->replaceClosingPcTags( $string, $dataRefEndMap );
}

return $string;
Expand Down Expand Up @@ -146,7 +143,7 @@ private function recursiveAddEquivTextToPhTag( $node, $string ) {
$value = $this->map[ $b ];
$base64EncodedValue = base64_encode( $value );

if ( empty( $base64EncodedValue ) || $base64EncodedValue === '' ) {
if ( empty( $base64EncodedValue ) ) {
return $string;
}

Expand Down Expand Up @@ -176,27 +173,21 @@ private function recursiveAddEquivTextToPhTag( $node, $string ) {

/**
* @param $string
* @param $toBeEscaped
*
* @return bool
*/
private function stringContainsPcTags( $string, $toBeEscaped ) {
$regex = ( $toBeEscaped ) ? '/&lt;pc (.*?)&gt;/iu' : '/<pc (.*?)>/iu';
preg_match_all( $regex, $string, $openingPcMatches );
private function stringContainsPcTags( $string ) {
preg_match_all( '/<pc (.*?)>/iu', $string, $openingPcMatches );

return ( isset( $openingPcMatches[ 0 ] ) && count( $openingPcMatches[ 0 ] ) > 0 );
}

/**
* @param $string
* @param $toBeEscaped
*
* @return mixed
*/
private function replaceSelfClosedPcTags( $string, $toBeEscaped ) {
if ( $toBeEscaped ) {
$string = str_replace( [ '&lt;', '&gt;' ], [ '<', '>' ], $string );
}
private function replaceSelfClosedPcTags( $string ) {

$regex = '/<pc[^>]+?\/>/iu';
preg_match_all( $regex, $string, $selfClosedPcMatches );
Expand All @@ -213,10 +204,6 @@ private function replaceSelfClosedPcTags( $string, $toBeEscaped ) {
}
}

if ( $toBeEscaped ) {
$string = str_replace( [ '<', '>' ], [ '&lt;', '&gt;' ], $string );
}

return $string;
}

Expand Down Expand Up @@ -295,13 +282,11 @@ private function recursiveCleanFromEquivText( $node, $string ) {
* Replace opening <pc> tags with correct reference in the $string
*
* @param string $string
* @param bool $toBeEscaped
*
* @return string
*/
private function replaceOpeningPcTags( $string, $toBeEscaped ) {
$regex = ( $toBeEscaped ) ? '/&lt;pc (.*?)&gt;/iu' : '/<pc (.*?)>/iu';
preg_match_all( $regex, $string, $openingPcMatches );
private function replaceOpeningPcTags( $string ) {
preg_match_all( '/<pc (.*?)>/iu', $string, $openingPcMatches );

foreach ( $openingPcMatches[ 0 ] as $index => $match ) {
$attr = HtmlParser::getAttributes( $openingPcMatches[ 1 ][ $index ] );
Expand All @@ -318,7 +303,7 @@ private function replaceOpeningPcTags( $string, $toBeEscaped ) {

if ( isset( $attr[ 'dataRefStart' ] ) ) {
$startOriginalData = $match; // opening <pc>
$startValue = $this->map[ $attr[ 'dataRefStart' ] ] ? $this->map[ $attr[ 'dataRefStart' ] ] : 'NULL'; //handling null values in original data map
$startValue = $this->map[ $attr[ 'dataRefStart' ] ] ?: 'NULL'; //handling null values in original data map
$base64EncodedStartValue = base64_encode( $startValue );
$base64StartOriginalData = base64_encode( $startOriginalData );

Expand All @@ -339,14 +324,12 @@ private function replaceOpeningPcTags( $string, $toBeEscaped ) {
* thanks to $dataRefEndMap
*
* @param string $string
* @param bool $toBeEscaped
* @param array $dataRefEndMap
*
* @return string
*/
private function replaceClosingPcTags( $string, $toBeEscaped, $dataRefEndMap = [] ) {
$regex = ( $toBeEscaped ) ? '/&lt;\/pc&gt;/iu' : '/<\/pc>/iu';
preg_match_all( $regex, $string, $closingPcMatches, PREG_OFFSET_CAPTURE );
private function replaceClosingPcTags( $string, $dataRefEndMap = [] ) {
preg_match_all( '|</pc>|iu', $string, $closingPcMatches, PREG_OFFSET_CAPTURE );
$delta = 0;

foreach ( $closingPcMatches[ 0 ] as $index => $match ) {
Expand Down
Loading

0 comments on commit e8c8e2b

Please sign in to comment.