-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/lib/FieldType/Converter/_fixtures/richtext/input/091-table-empty-row.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
35 changes: 35 additions & 0 deletions
35
tests/lib/FieldType/Converter/_fixtures/richtext/output/091-table-empty-row.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
|
||
|