Skip to content

Commit

Permalink
EZP-30166 : RichText Link converter emits warning during custom tag c…
Browse files Browse the repository at this point in the history
…onversion (#32)

* Added test case for EZP-30166

* EZP-30166: Added http://www.w3.org/1999/xlink namespace for the xpath expression used for extracting links from richtext

* EZP-30166: Added section as root node in the temporary documents with the content of custom tag
  • Loading branch information
vidarl authored and Łukasz Serwatka committed Apr 4, 2019
1 parent acce142 commit a76975e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/lib/eZ/RichText/Converter/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ public function __construct(LocationService $locationService, ContentService $co
public function convert(DOMDocument $document)
{
$document = clone $document;

$xpath = new DOMXPath($document);
$xpath->registerNamespace('docbook', 'http://docbook.org/ns/docbook');
$xpath->registerNamespace('xlink', 'http://www.w3.org/1999/xlink');

$linkAttributeExpression = "starts-with( @xlink:href, 'ezlocation://' ) or starts-with( @xlink:href, 'ezcontent://' )";
$xpathExpression = "//docbook:link[{$linkAttributeExpression}]|//docbook:ezlink";

Expand Down
10 changes: 6 additions & 4 deletions src/lib/eZ/RichText/Converter/Render/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ protected function getCustomTemplateContent(DOMNode $node)
{
$innerDoc = new DOMDocument();

$rootNode = $innerDoc->createElementNS('http://docbook.org/ns/docbook', 'section');
$innerDoc->appendChild($rootNode);

/** @var \DOMNode $child */
foreach ($node->childNodes as $child) {
$newNode = $innerDoc->importNode($child, true);
Expand All @@ -194,12 +197,11 @@ protected function getCustomTemplateContent(DOMNode $node)
"Failed to import Custom Style content of node '{$child->getNodePath()}'"
);
}
$innerDoc->appendChild($newNode);
}

$convertedInnerDoc = $this->richTextConverter->convert($innerDoc);
$rootNode->appendChild($newNode);
}

return trim($convertedInnerDoc->saveHTML());
return trim($this->richTextConverter->convert($innerDoc)->saveHTML());
}

/**
Expand Down
104 changes: 104 additions & 0 deletions tests/lib/eZ/RichText/Converter/AggregateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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 EzSystems\Tests\EzPlatformRichText\eZ\RichText\Converter;

use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter;
use EzSystems\EzPlatformRichText\eZ\RichText\Converter\Aggregate;
use EzSystems\EzPlatformRichText\eZ\RichText\Converter\Link;
use EzSystems\EzPlatformRichText\eZ\RichText\Converter\Render\Template;
use EzSystems\EzPlatformRichText\eZ\RichText\RendererInterface;
use PHPUnit\Framework\TestCase;

class AggregateTest extends TestCase
{
/**
* @param $input
* @param $expectedWarningMessage
*
* @dataProvider providerConvertWithLinkInCustomTag
*
* @see https://jira.ez.no/browse/EZP-30166
*/
public function testConvertWithLinkInCustomTag(
$input,
$expectedOutput
) {
$xmlDocument = new \DOMDocument();
$xmlDocument->loadXML($input);

$locationService = $this->createMock(LocationService::class);
$contentService = $this->createMock(ContentService::class);
$urlAliasRouter = $this->createMock(UrlAliasRouter::class);
$renderer = $this->createMock(RendererInterface::class);

$linkConverter = new Link(
$locationService,
$contentService,
$urlAliasRouter
);

$aggregate = new Aggregate([$linkConverter]);

$templateConverter = new Template(
$renderer,
$aggregate
);

$aggregate = new Aggregate([$templateConverter, $linkConverter]);
$output = $aggregate->convert($xmlDocument);

$expectedOutputDocument = new \DOMDocument();
$expectedOutputDocument->loadXML($expectedOutput);
$this->assertEquals($expectedOutputDocument, $output, 'Xml is not converted as expected');
}

public function providerConvertWithLinkInCustomTag(): array
{
return [
[
'<?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">
<para></para>
<eztemplate name="bulbo" ezxhtml:class="ez-custom-tag ez-custom-tag--attributes-visible">

This comment has been minimized.

Copy link
@vidarl

vidarl Sep 16, 2019

Author Member

It just hit me.. Why will custom tags be decorated with these ezxhtml:class="ez-custom-tag ez-custom-tag--attributes-visible". In this case it is in a test ( which I comitted ), but it is cut&paste of the what is produced via editor.

Is this something the editor uses internally, and is leaked down to the richtext markup?
@alongosz

This comment has been minimized.

Copy link
@alongosz

alongosz Sep 16, 2019

Member

It just hit me.. Why will custom tags be decorated with these ezxhtml:class="ez-custom-tag ez-custom-tag--attributes-visible". In this case it is in a test ( which I comitted ), but it is cut&paste of the what is produced via editor.

Is this something the editor uses internally, and is leaked down to the richtext markup?
@alongosz

Hmm, it's possible it is indeed used by OE internally, but more of a question for @dew326 :)

This comment has been minimized.

Copy link
@dew326

dew326 Sep 16, 2019

Member

The intention here was to store what "tab" of a custom tag is opened, the attribute or the content and open proper tab in the edit view.

This comment has been minimized.

Copy link
@vidarl

vidarl Sep 24, 2019

Author Member

Okay.. I found a bug related to this : https://jira.ez.no/browse/EZP-30964. Seems like ezxhtml:class="custom-tag--attributes-visible" is required in order for the editor to work properly

<ezcontent>
<para>Just a regular text</para>
<para>
<link xlink:href="ezlocation://2" xlink:show="none">ezlocation URL</link>
</para>
<para>
<link xlink:href="ezurl://1" xlink:show="none" xlink:title="">ezurl URL </link>
</para>
</ezcontent>
<ezconfig><ezvalue key="title">Bulbo</ezvalue></ezconfig>
</eztemplate>
</section>',
'<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">
<para/>
<eztemplate name="bulbo" ezxhtml:class="ez-custom-tag ez-custom-tag--attributes-visible">
<ezcontent>
<para>Just a regular text</para>
<para>
<link xlink:href="" xlink:show="none">ezlocation URL</link>
</para>
<para>
<link xlink:href="ezurl://1" xlink:show="none" xlink:title="">ezurl URL </link>
</para>
</ezcontent>
<ezconfig><ezvalue key="title">Bulbo</ezvalue></ezconfig>
</eztemplate>
</section>',
],
];
}
}
14 changes: 12 additions & 2 deletions tests/lib/eZ/RichText/Converter/Render/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,25 @@ public function testConvert(DOMDocument $inputDocument, DOMDocument $expectedOut
if (!empty($params['params']['content'])) {
// mock simple converter
$contentDoc = new DOMDocument();

$xml = '<section xmlns="http://docbook.org/ns/docbook">';
$xml .= $params['params']['content'];
$xml .= '</section>';

$params['params']['content'] = $xml;

$fragment = $contentDoc->createDocumentFragment();
$fragment->appendXML($params['params']['content']);
$fragment->appendXML($xml);

$contentDoc->appendChild($fragment);

$this->converterMock
->expects($this->at($convertIndex++))
->method('convert')
->with($contentDoc)
->will($this->returnValue($contentDoc));
}

$this->rendererMock
->expects($this->at($index))
->method('renderTemplate')
Expand Down Expand Up @@ -239,7 +249,7 @@ protected function getConverterMock()
'is_inline' => false,
'params' => [
'name' => 'custom_tag',
'content' => '<para xmlns="http://docbook.org/ns/docbook">Param: value</para>',
'content' => '<para>Param: value</para>',
'params' => [
'param' => 'value',
],
Expand Down

0 comments on commit a76975e

Please sign in to comment.