diff --git a/lib/FieldType/XmlText/Converter/RichText.php b/lib/FieldType/XmlText/Converter/RichText.php index 312ec137..db985104 100644 --- a/lib/FieldType/XmlText/Converter/RichText.php +++ b/lib/FieldType/XmlText/Converter/RichText.php @@ -115,7 +115,7 @@ protected function getConverter() if ($this->converter === null) { $this->converter = new Aggregate( [ - new ToRichTextPreNormalize([new ExpandingToRichText(), new ExpandingList(), new EmbedLinking()]), + new ToRichTextPreNormalize([new ExpandingToRichText(), new ExpandingList(), new EmbedLinking(), new TableToRichText()]), new Xslt( __DIR__ . '/../Input/Resources/stylesheets/eZXml2Docbook.xsl', $this->styleSheets diff --git a/lib/FieldType/XmlText/Converter/TableToRichText.php b/lib/FieldType/XmlText/Converter/TableToRichText.php new file mode 100644 index 00000000..3122551b --- /dev/null +++ b/lib/FieldType/XmlText/Converter/TableToRichText.php @@ -0,0 +1,74 @@ + + * + * + * In richtext, such rows needs to include the tags too + * + * + * + * + * + * + * + */ +class TableToRichText implements Converter +{ + /** + * Attribute used for storing number of table columns. + * + * @const string + */ + const ATTRIBUTE_COLUMNS = 'ez-columns'; + + protected function getNumberOfColumns(DOMElement $tableElement) + { + // Let's first check if we have already calculated number of columns for this table + if ($tableElement->hasAttribute(self::ATTRIBUTE_COLUMNS)) { + $numberOfColumns = $tableElement->getAttribute(self::ATTRIBUTE_COLUMNS); + } else { + $numberOfColumns = 1; + foreach ($tableElement->childNodes as $tableRow) { + if ($tableRow->childNodes->length > $numberOfColumns) { + $numberOfColumns = $tableRow->childNodes->length; + } + } + $tableElement->setAttribute(self::ATTRIBUTE_COLUMNS, $numberOfColumns); + } + + return $numberOfColumns; + } + + public function convert(DOMDocument $document) + { + $xpath = new DOMXPath($document); + + // Get all empty table rows + $xpathExpression = '//table/tr[count(*) = 0]'; + + $emptyRows = $xpath->query($xpathExpression); + foreach ($emptyRows as $row) { + $tableElement = $row->parentNode; + $numberOfColumns = $this->getNumberOfColumns($tableElement); + for ($i = 0; $i < $numberOfColumns; ++$i) { + $row->appendChild($document->createElement('td')); + } + } + } +} diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/input/091-table-empty-row.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/input/091-table-empty-row.xml new file mode 100644 index 00000000..aa984ed6 --- /dev/null +++ b/tests/lib/FieldType/Converter/_fixtures/richtext/input/091-table-empty-row.xml @@ -0,0 +1,30 @@ + + +
+ + + + + + + + +
+ + Foobar1 + + + + Foobar2 + + + + Foobar3 + +
+
+
diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/output/091-table-empty-row.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/output/091-table-empty-row.xml new file mode 100644 index 00000000..ed1eb062 --- /dev/null +++ b/tests/lib/FieldType/Converter/_fixtures/richtext/output/091-table-empty-row.xml @@ -0,0 +1,35 @@ + +
+ + + + + + + + + + + Foobar1 + + + + + Foobar2 + + + + + Foobar3 + + + + + +
+ +