Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-5048: Fixed handling nested lists with line feeds #237

Merged
merged 10 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@
"autoload": {
"psr-4": {
"EzSystems\\EzPlatformRichTextBundle\\": "src/bundle",
"EzSystems\\EzPlatformRichText\\": "src/lib"
"EzSystems\\EzPlatformRichText\\": "src/lib",
"Ibexa\\FieldTypeRichText\\RichText\\": "src/lib/RichText"
vidarl marked this conversation as resolved.
Show resolved Hide resolved
}
},
"autoload-dev": {
"psr-4": {
"EzSystems\\Tests\\EzPlatformRichText\\": "tests/lib",
"EzSystems\\Tests\\EzPlatformRichTextBundle\\": "tests/bundle",
"EzSystems\\IntegrationTests\\EzPlatformRichText\\": "tests/integration"
"EzSystems\\IntegrationTests\\EzPlatformRichText\\": "tests/integration",
"Ibexa\\Tests\\FieldTypeRichText\\": "tests/lib/Richtext"
}
},
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,11 @@ parameters:
count: 1
path: src/lib/Form/Type/RichTextFieldType.php

-
message: "#^Variable \\$element in PHPDoc tag @var does not match any variable in the foreach loop\\: \\$childNode$#"
count: 1
path: src/lib/RichText/Converter/ListLineBreaks.php
vidarl marked this conversation as resolved.
Show resolved Hide resolved

-
message: "#^Method EzSystems\\\\EzPlatformRichText\\\\SPI\\\\Configuration\\\\Provider\\:\\:getConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down
6 changes: 6 additions & 0 deletions src/bundle/Resources/config/fieldtype_services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ services:
tags:
- {name: ezrichtext.converter.input.xhtml5, priority: 10}

# Note: should run after xsl transformation
ezrichtext.converter.input.xhtml5.listlinebreaks:
vidarl marked this conversation as resolved.
Show resolved Hide resolved
class: Ibexa\FieldTypeRichText\RichText\Converter\ListLineBreaks
tags:
- {name: ezrichtext.converter.input.xhtml5, priority: 100}

ezrichtext.converter.edit.xhtml5:
class: EzSystems\EzPlatformRichTextBundle\eZ\RichText\Converter\Html5Edit
arguments:
Expand Down
67 changes: 67 additions & 0 deletions src/lib/RichText/Converter/ListLineBreaks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\FieldTypeRichText\RichText\Converter;

use EzSystems\EzPlatformRichText\eZ\RichText\Converter;
use DOMDocument;
use DOMXPath;

/**
* Class ListLineBreaks.
*
* Processes lists below <literallayout> tag.
*/
class ListLineBreaks implements Converter
vidarl marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* For all <itemizedList> and <orderedList> below <literallayout>, move the list after the <literallayout>, so that it is not inside it.
*
* @param \DOMDocument $document
*
* @return \DOMDocument
*/
public function convert(DOMDocument $document)
{
$xpath = new DOMXPath($document);
$xpathExpression = '//ns:literallayout [descendant::ns:orderedlist|descendant::ns:itemizedlist]';
$ns = $document->documentElement ? $document->documentElement->namespaceURI ?: '' : '';
$xpath->registerNamespace('ns', $ns);
vidarl marked this conversation as resolved.
Show resolved Hide resolved
$elements = $xpath->query($xpathExpression) ?: [];

// elements are list of <literallayout> elements
foreach ($elements as $element) {
$listItemToMove = [];
$listItemNo = 0;
/** @var \DOMElement $element */
vidarl marked this conversation as resolved.
Show resolved Hide resolved
foreach ($element->childNodes as $childNode) {
/** @var \DOMNode $childNode */
vidarl marked this conversation as resolved.
Show resolved Hide resolved
if ($childNode instanceof \DOMElement) {
vidarl marked this conversation as resolved.
Show resolved Hide resolved
if ($childNode->tagName == 'orderedlist' || $childNode->tagName == 'itemizedlist') {
alongosz marked this conversation as resolved.
Show resolved Hide resolved
$listItemToMove[$listItemNo] = $childNode;
alongosz marked this conversation as resolved.
Show resolved Hide resolved
++$listItemNo;
continue;
}
}
}

$paragraphNode = null;
foreach ($listItemToMove as $node) {
/** @var \DomNode $node */
if ($node->parentNode !== null) {
$targetNode = $node->parentNode->parentNode;
if ($targetNode !== null) {
$targetNode->appendChild($node);
}
}
}
}

return $document;
}
}
vidarl marked this conversation as resolved.
Show resolved Hide resolved
96 changes: 96 additions & 0 deletions tests/lib/Richtext/Converter/ListLineBreaksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\FieldTypeRichText\RichText\Converter;

use EzSystems\EzPlatformRichText\eZ\RichText\Converter\Link;
use Ibexa\FieldTypeRichText\RichText\Converter\ListLineBreaks;
use PHPUnit\Framework\TestCase;
use DOMDocument;

/**
* Tests the Link converter
* Class LinkTest.
*/
vidarl marked this conversation as resolved.
Show resolved Hide resolved
class ListLineBreaksTest extends TestCase
vidarl marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @return array<int, array<int, string>>
*/
public function providerConvert(): array
{
return [
[
'<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0-variant ezpublish-1.0">
<para>This is a p</para>
<para> </para>
<itemizedlist>
<listitem>
<para>item 1</para>
</listitem>
<listitem>
<para>
<literallayout class="normal">item 2
this is a line 2
this is line 3<itemizedlist><listitem><para>item 3</para></listitem></itemizedlist></literallayout>
</para>
</listitem>
</itemizedlist>
<itemizedlist>
<listitem>
<para> </para>
</listitem>
</itemizedlist>
</section>',
'<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0-variant ezpublish-1.0">
<para>This is a p</para>
<para> </para>
<itemizedlist>
<listitem>
<para>item 1</para>
</listitem>
<listitem>
<para>
<literallayout class="normal">item 2
this is a line 2
this is line 3</literallayout>
<itemizedlist><listitem><para>item 3</para></listitem></itemizedlist></para>
</listitem>
</itemizedlist>
<itemizedlist>
<listitem>
<para> </para>
</listitem>
</itemizedlist>
</section> ',
],
];
}

/**
* Test conversion of ezurl://<id> links.
*
* @dataProvider providerConvert
*/
public function testConvert(string $input, string $output): void
{
$inputDocument = new DOMDocument();
$inputDocument->loadXML($input);

$converter = new ListLineBreaks();

$outputDocument = $converter->convert($inputDocument);

$expectedOutputDocument = new DOMDocument();
$expectedOutputDocument->loadXML($output);

$this->assertEquals($expectedOutputDocument, $outputDocument);
vidarl marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 2 additions & 0 deletions tests/lib/eZ/RichText/Converter/Xslt/Xhtml5ToDocbookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace EzSystems\Tests\EzPlatformRichText\eZ\RichText\Converter\Xslt;

use EzSystems\EzPlatformRichText\eZ\RichText\Converter\Aggregate;
use Ibexa\FieldTypeRichText\RichText\Converter\ListLineBreaks;
use EzSystems\EzPlatformRichText\eZ\RichText\Converter\ProgramListing;
use EzSystems\EzPlatformRichText\eZ\RichText\Converter\Xslt;

Expand Down Expand Up @@ -110,6 +111,7 @@ protected function getConverter()
$this->getConversionTransformationStylesheet(),
$this->getCustomConversionTransformationStylesheets()
),
new ListLineBreaks(),
]
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0-variant ezpublish-1.0">
<para>This is a p</para>
<para> </para>
<orderedlist>
<listitem>
<para>item 1</para>
</listitem>
<listitem>
<para>
<literallayout class="normal">item 2
this is a line 2
this is line 3</literallayout>
<orderedlist>
<listitem>
<para>item 3</para>
</listitem>
</orderedlist>
</para>
</listitem>
</orderedlist>
<orderedlist>
<listitem>
<para> </para>
</listitem>
</orderedlist>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0-variant ezpublish-1.0">
<para>This is a p</para>
<para> </para>
<itemizedlist>
<listitem>
<para>item 1</para>
</listitem>
<listitem>
<para>
<literallayout class="normal">item 2
this is a line 2
this is line 3</literallayout>
<itemizedlist>
<listitem>
<para>item 3</para>
</listitem>
</itemizedlist>
</para>
</listitem>
</itemizedlist>
<itemizedlist>
<listitem>
<para> </para>
</listitem>
</itemizedlist>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5/edit">
<p>This is a p</p>
<p> </p>
<ol>
<li>item 1</li>
<li>item 2<br/>this is a line 2<br/>this is line 3<ol><li>item 3</li></ol></li>
</ol>
<ol>
<li> </li>
</ol>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5/edit">
<p>This is a p</p>
<p> </p>
<ul>
<li>item 1</li>
<li>item 2<br/>this is a line 2<br/>this is line 3<ul><li>item 3</li></ul></li>
</ul>
<ul>
<li> </li>
</ul>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5">
<p>This is a p</p>
<p> </p>
<ol>
<li>item 1</li>
<li>item 2<br/>this is a line 2<br/>this is line 3<ol><li>item 3</li></ol></li>
</ol>
<ol>
<li> </li>
</ol>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5">
<p>This is a p</p>
<p> </p>
<ul>
<li>item 1</li>
<li>item 2<br/>this is a line 2<br/>this is line 3<ul><li>item 3</li></ul></li>
</ul>
<ul>
<li> </li>
</ul>
</section>