-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-5048: Fixed handling nested lists with line feeds (#237)
* IBX-5048: Richtext editor not handling sublists when parent contains line breaks * Fixed Ibexa namespace * fixup! Fixed Ibexa namespace * PHPstan * fixup! PHPstan * fixup! fixup! Fixed Ibexa namespace * Update composer.json Co-authored-by: Dawid Parafiński <[email protected]> * fixup! PHPstan * fixup! IBX-5048: Richtext editor not handling sublists when parent contains line breaks * fixup! IBX-5048: Richtext editor not handling sublists when parent contains line breaks --------- Co-authored-by: Vidar Langseid <[email protected]> Co-authored-by: Dawid Parafiński <[email protected]>
- Loading branch information
1 parent
6bff30d
commit 454e5cc
Showing
11 changed files
with
276 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
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,70 @@ | ||
<?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 DOMDocument; | ||
use DOMElement; | ||
use DOMNode; | ||
use DOMXPath; | ||
use EzSystems\EzPlatformRichText\eZ\RichText\Converter; | ||
|
||
/** | ||
* @internal | ||
* | ||
* Processes lists nested in <literallayout> tags. | ||
*/ | ||
final class LiteralLayoutNestedList implements Converter | ||
{ | ||
private const FALLBACK_NAMESPACE = 'http://docbook.org/ns/docbook'; | ||
private const ORDERED_LIST_TAG = 'orderedlist'; | ||
private const ITEMIZED_LIST_TAG = 'itemizedlist'; | ||
|
||
/** | ||
* For all <itemizedList> and <orderedList> nested in the <literallayout>, move the list after the <literallayout>, | ||
* so that it is not inside it. | ||
*/ | ||
public function convert(DOMDocument $document): DOMDocument | ||
{ | ||
$xpath = new DOMXPath($document); | ||
$xpathExpression = '//ns:literallayout [descendant::ns:orderedlist|descendant::ns:itemizedlist]'; | ||
$xpath->registerNamespace( | ||
'ns', | ||
null !== $document->documentElement && !empty($document->documentElement->namespaceURI) | ||
? $document->documentElement->namespaceURI | ||
: self::FALLBACK_NAMESPACE | ||
); | ||
$elements = $xpath->query($xpathExpression) ?: []; | ||
|
||
// elements are list of <literallayout> elements | ||
/** @var DOMElement $element */ | ||
foreach ($elements as $element) { | ||
/** @var DOMNode $childNode */ | ||
foreach ($element->childNodes as $childNode) { | ||
if ($this->isNestedListNode($childNode)) { | ||
$targetNode = $childNode->parentNode->parentNode; | ||
if ($targetNode !== null) { | ||
$targetNode->appendChild($childNode); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $document; | ||
} | ||
|
||
/** | ||
* @phpstan-assert-if-true !null $childNode->parentNode | ||
*/ | ||
private function isNestedListNode(DOMNode $childNode): bool | ||
{ | ||
return $childNode instanceof DOMElement | ||
&& ($childNode->tagName === self::ORDERED_LIST_TAG || $childNode->tagName === self::ITEMIZED_LIST_TAG) | ||
&& $childNode->parentNode !== null; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
tests/lib/Richtext/Converter/LiteralLayoutNestedListTest.php
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,94 @@ | ||
<?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 Ibexa\FieldTypeRichText\RichText\Converter\LiteralLayoutNestedList; | ||
use PHPUnit\Framework\TestCase; | ||
use DOMDocument; | ||
|
||
/** | ||
* @covers \Ibexa\FieldTypeRichText\RichText\Converter\LiteralLayoutNestedList | ||
*/ | ||
final class LiteralLayoutNestedListTest extends TestCase | ||
{ | ||
/** | ||
* @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 <li> tags which containing <br/> and <ol>/<ul> tags. | ||
* | ||
* @dataProvider providerConvert | ||
*/ | ||
public function testConvert(string $input, string $output): void | ||
{ | ||
$inputDocument = new DOMDocument(); | ||
$inputDocument->loadXML($input); | ||
|
||
$converter = new LiteralLayoutNestedList(); | ||
|
||
$outputDocument = $converter->convert($inputDocument); | ||
|
||
$expectedOutputDocument = new DOMDocument(); | ||
$expectedOutputDocument->loadXML($output); | ||
|
||
self::assertEquals($expectedOutputDocument, $outputDocument); | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
tests/lib/eZ/RichText/Converter/Xslt/_fixtures/docbook/036-orderedListWithLinefeeds.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,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> |
27 changes: 27 additions & 0 deletions
27
tests/lib/eZ/RichText/Converter/Xslt/_fixtures/docbook/037-itemizedListWithLinefeeds.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,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> |
12 changes: 12 additions & 0 deletions
12
tests/lib/eZ/RichText/Converter/Xslt/_fixtures/xhtml5/edit/036-orderedListWithLinefeeds.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,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> |
12 changes: 12 additions & 0 deletions
12
tests/lib/eZ/RichText/Converter/Xslt/_fixtures/xhtml5/edit/037-itemizedListWithLinefeeds.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,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> |
12 changes: 12 additions & 0 deletions
12
...s/lib/eZ/RichText/Converter/Xslt/_fixtures/xhtml5/output/036-orderedListWithLinefeeds.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,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> |
12 changes: 12 additions & 0 deletions
12
.../lib/eZ/RichText/Converter/Xslt/_fixtures/xhtml5/output/037-itemizedListWithLinefeeds.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,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> |