Skip to content

Commit

Permalink
EZP-29365: ezxmltext -> ricktext conversion : empty rows (ezsystems#54)
Browse files Browse the repository at this point in the history
* EZP-29365: ezxmltext -> ricktext conversion : empty rows

* fixup! EZP-29365: ezxmltext -> ricktext conversion : empty rows

* fixup! fixup! EZP-29365: ezxmltext -> ricktext conversion : empty rows
  • Loading branch information
vidarl authored and andrerom committed Jul 3, 2018
1 parent ade2f31 commit cb56619
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/FieldType/XmlText/Converter/RichText.php
Expand Up @@ -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
Expand Down
74 changes: 74 additions & 0 deletions lib/FieldType/XmlText/Converter/TableToRichText.php
@@ -0,0 +1,74 @@
<?php
/**
* This file is part of the eZ Platform XmlText Field Type package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\FieldType\XmlText\Converter;

use eZ\Publish\Core\FieldType\XmlText\Converter;
use DOMDocument;
use DOMElement;
use DOMXPath;

/**
* Class TableToRichText.
*
* In ezxmltext we may have empty rows like this:
* <table class="list" width="90%" border="0" ez-colums="3">
* <tr/>
*
* In richtext, such rows needs to include the <td> tags too
* <informaltable class="list" width="90%">
* <tbody>
* <tr>
* <td/>
* <td/>
* <td/>
* </tr>
*/
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'));
}
}
}
}
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>

<section
xmlns:image="http://ez.no/namespaces/ezpublish3/image/"
xmlns:xhtml="http://ez.no/namespaces/ezpublish3/xhtml/"
xmlns:custom="http://ez.no/namespaces/ezpublish3/custom/">
<paragraph
xmlns:tmp="http://ez.no/namespaces/ezpublish3/temporary/" ez-temporary="1">
<table class="list" width="90%" border="0" ez-colums="3">
<tr/>
<tr>
<td xhtml:width="0">
<paragraph>
<strong>Foobar1</strong>
</paragraph>
</td>
<td xhtml:width="0">
<paragraph>
<strong>Foobar2</strong>
</paragraph>
</td>
<td xhtml:width="0">
<paragraph>
<strong>Foobar3</strong>
</paragraph>
</td>
</tr>
</table>
</paragraph>
</section>
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<section
xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<informaltable class="list" width="90%">
<tbody>
<tr>
<td/>
<td/>
<td/>
</tr>
<tr>
<td ezxhtml:width="0">
<para>
<emphasis role="strong">Foobar1</emphasis>
</para>
</td>
<td ezxhtml:width="0">
<para>
<emphasis role="strong">Foobar2</emphasis>
</para>
</td>
<td ezxhtml:width="0">
<para>
<emphasis role="strong">Foobar3</emphasis>
</para>
</td>
</tr>
</tbody>
</informaltable>
</section>


0 comments on commit cb56619

Please sign in to comment.