Commit 305d8297 authored by Rémy DANIEL's avatar Rémy DANIEL

[BUGFIX] Fix wrong encoding of CDATA markers when saving the XML file

Always add CDATA markers with a CdataSupportingSimpleXMLElement,
so that CDATA markers will not be encoded when saving the file.
parent 9408f9c6
......@@ -181,23 +181,12 @@ class FileBaseXMLService extends FileBaseService {
$subNode->addAttribute('type', 'array');
$this->arrayToXml($value, $subNode, $parentTagMap, $tagName);
} else {
if (SgLib::checkForCdataInString($value)) {
$simpleSubNode = $xmlElement->addChildCData($tagName, $value);
} else {
$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);
}
// CDATA markers are stripped when reading the XML file from the disk,
// and html entities are not decoded.
// So, for simplicity and safety, when writing back the data to the file,
// we will always enclose value in CDATA, so html entities are preserved
// and XML remains valid.
$simpleSubNode = $xmlElement->addChildCData($tagName, $value);
if ($indexAttributeValue !== NULL) {
$simpleSubNode->addAttribute('index', $indexAttributeValue);
}
......@@ -230,6 +219,14 @@ class FileBaseXMLService extends FileBaseService {
$this->arrayToXml($phpArray, $xmlElement, $parentTagMap);
$formattedXml = $this->formatXml($xmlElement);
// $a = $formattedXml;
// $b = GeneralUtility::array2xml([$firstTag => $phpArray]);
// \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($phpArray);
// \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($a, 'formatXml');
// \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($b, 'array2xml');
// exit;
return $formattedXml;
}
......@@ -350,8 +347,13 @@ class FileBaseXMLService extends FileBaseService {
//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);
$xmlRaw = preg_replace_callback (
"#(&lt;\!\[CDATA\[.*\]\]&gt;)#sU",
function($matches) {
return htmlspecialchars_decode($matches[1]);
},
$xmlRaw
);
$dom->loadXML($xmlRaw);
$formattedXml = $dom->saveXML();
......
......@@ -52,7 +52,9 @@ class CdataSupportingSimpleXMLElement extends \SimpleXMLElement {
public function addChildCData($name, $cdataText) {
/** @var CdataSupportingSimpleXMLElement $child */
$child = $this->addChild($name);
$child->addCData($cdataText);
if ($cdataText !== '') {
$child->addCData($cdataText);
}
return $child;
}
}
......
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