From cb5661909bfc12f29d21d7de00a249614c2bbc76 Mon Sep 17 00:00:00 2001 From: Vidar Date: Tue, 3 Jul 2018 10:49:18 +0200 Subject: [PATCH] EZP-29365: ezxmltext -> ricktext conversion : empty rows (#54) * EZP-29365: ezxmltext -> ricktext conversion : empty rows * fixup! EZP-29365: ezxmltext -> ricktext conversion : empty rows * fixup! fixup! EZP-29365: ezxmltext -> ricktext conversion : empty rows --- lib/FieldType/XmlText/Converter/RichText.php | 2 +- .../XmlText/Converter/TableToRichText.php | 74 +++++++++++++++++++ .../richtext/input/091-table-empty-row.xml | 30 ++++++++ .../richtext/output/091-table-empty-row.xml | 35 +++++++++ 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 lib/FieldType/XmlText/Converter/TableToRichText.php create mode 100644 tests/lib/FieldType/Converter/_fixtures/richtext/input/091-table-empty-row.xml create mode 100644 tests/lib/FieldType/Converter/_fixtures/richtext/output/091-table-empty-row.xml 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 + + + + + +
+ +