Commit 007789c9 authored by Philipp Nowinski's avatar Philipp Nowinski
Browse files

[BUGFIX] fix problems with CDATA-blocks

parent 079fbdf9
......@@ -83,7 +83,17 @@ class FileBaseXLFService extends FileBaseService {
}
// read xml into array
$xmlContent = simplexml_load_file($file);
// $xmlContent = simplexml_load_file($file);
// $xmlContent = json_decode(json_encode($xmlContent), TRUE);
$contents = file_get_contents($file);
// Remove CDATA tags and encode html entities
$contents = preg_replace_callback ("#<\!\[CDATA\[(.*)\]\]>#sU", function($matches) {
return htmlspecialchars($matches[1]);
}, $contents);
$xmlContent = simplexml_load_string($contents,'SimpleXMLElement', LIBXML_NOCDATA);
$xmlContent = json_decode(json_encode($xmlContent), TRUE);
// check data
......@@ -265,8 +275,28 @@ class FileBaseXLFService extends FileBaseService {
$body = '<body>' . "\n";
foreach ($phpArray['data'] as $constant => $value) {
$approved = ($targetLanguage !== 'default' ? ' approved="yes"' : '');
$enValue = SgLib::htmlSpecialCharsIgnoringCdata($enLanguage[$constant]);
$value = SgLib::htmlSpecialCharsIgnoringCdata($value);
$enValue = $enLanguage[$constant];
$startCDATA = "<![CDATA[";
$endCDATA = "]]>";
//Detects if html is in the value and add CDATA tags
if(preg_match("#<(\w+).*>.*</(\w)>#isU", $value) ||
preg_match("#(\w+).*/>#isU", $value) ||
preg_match("#<!--.*-->#isU", $value)) {
$value = $startCDATA.$value.$endCDATA;
} else {
$value = htmlspecialchars($value);
}
//Detects if html is in the enValue and add CDATA tags
if(preg_match("#<(\w+).*>.*</(\w)>#isU", $enValue) ||
preg_match("#(\w+).*/>#isU", $value) ||
preg_match("#<!--.*-->#isU", $value)) {
$enValue = $startCDATA.$enValue.$endCDATA;
} else {
$enValue = htmlspecialchars($enLanguage[$constant]);
}
$body .= "\t\t" . '<trans-unit id="' . htmlspecialchars(
$constant
) . '"' . $approved . $this->addPreserveSpaceAttribute(
......@@ -402,4 +432,4 @@ class FileBaseXLFService extends FileBaseService {
}
}
?>
\ No newline at end of file
?>
......@@ -201,7 +201,19 @@ class FileBaseXMLService extends FileBaseService {
if (SgLib::checkForCdataInString($value)) {
$simpleSubNode = $xmlElement->addChildCData($tagName, $value);
} else {
$simpleSubNode = $xmlElement->addChild($tagName, htmlspecialchars($value));
$startCDATA = "<![CDATA[";
$endCDATA = "]]>";
//Detects if html is in the value and add CDATA tags
if(preg_match("#<(\w+).*>.*<\/(\w+)>#isU", $value) ||
preg_match("#(\w+).*/>#isU", $value) ||
preg_match("#<!--.*-->#isU", $value)) {
$value = $startCDATA.$value.$endCDATA;
} else {
$value = htmlspecialchars($value);
}
$simpleSubNode = $xmlElement->addChild($tagName, $value);
}
if ($indexAttributeValue !== NULL) {
$simpleSubNode->addAttribute('index', $indexAttributeValue);
......@@ -351,10 +363,18 @@ class FileBaseXMLService extends FileBaseService {
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = FALSE;
$dom->formatOutput = TRUE;
$dom->loadXML($xmlElement->asXML());
$xmlRaw = $xmlElement->asXML();
//Search CDATA tags to decode the content inside
//Because $xmlElement->asXML() encode html entities
$xmlRaw = preg_replace_callback ("#(&lt;\!\[CDATA\[.*\]\]&gt;)#sU", function($matches) {
return htmlspecialchars_decode($matches[1]);}, $xmlRaw);
$dom->loadXML($xmlRaw);
$formattedXml = $dom->saveXML();
return $formattedXml;
}
}
?>
\ No newline at end of file
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment