Skip to content
This repository was archived by the owner on Nov 21, 2019. It is now read-only.

Commit ce02ed8

Browse files
nlisgothewilkybarkid
authored andcommitted
Add support for sub in JatsContentBundle (#42)
* Add support for sub in JatsContentBundle * Update patterns * Introduce patterns at the moment that sub template was merged * Update vendor-extra/JatsContentBundle/src/Resources/config/services.xml Co-Authored-By: nlisgo <[email protected]>
1 parent 0a20377 commit ce02ed8

File tree

6 files changed

+176
-2
lines changed

6 files changed

+176
-2
lines changed

vendor-extra/JatsContentBundle/src/Resources/config/services.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@
100100
<tag name="libero.view_converter"/>
101101
</service>
102102

103+
<service id="Libero\JatsContentBundle\ViewConverter\SubVisitor"
104+
class="Libero\JatsContentBundle\ViewConverter\SubVisitor">
105+
<argument type="service" id="Libero\ViewsBundle\Views\ViewConverter"/>
106+
<tag name="libero.view_converter"/>
107+
</service>
108+
103109
</services>
104110

105111
</container>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Libero\JatsContentBundle\ViewConverter;
6+
7+
use FluentDOM\DOM\Element;
8+
use Libero\ViewsBundle\Views\ConvertsChildren;
9+
use Libero\ViewsBundle\Views\SimplifiedChildVisitor;
10+
use Libero\ViewsBundle\Views\View;
11+
use Libero\ViewsBundle\Views\ViewConverter;
12+
use Libero\ViewsBundle\Views\ViewConverterVisitor;
13+
14+
final class SubVisitor implements ViewConverterVisitor
15+
{
16+
use ConvertsChildren;
17+
use SimplifiedChildVisitor;
18+
19+
public function __construct(ViewConverter $converter)
20+
{
21+
$this->converter = $converter;
22+
}
23+
24+
protected function doVisit(Element $object, View $view, array &$context = []) : View
25+
{
26+
return $view->withArgument('text', $this->convertChildren($object, $context));
27+
}
28+
29+
protected function possibleTemplate() : string
30+
{
31+
return '@LiberoPatterns/sub.html.twig';
32+
}
33+
34+
protected function expectedElement() : string
35+
{
36+
return '{http://jats.nlm.nih.gov}sub';
37+
}
38+
39+
protected function unexpectedArguments() : array
40+
{
41+
return ['text'];
42+
}
43+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace tests\Libero\JatsContentBundle\ViewConverter;
6+
7+
use Libero\JatsContentBundle\ViewConverter\SubVisitor;
8+
use Libero\ViewsBundle\Views\View;
9+
use PHPUnit\Framework\TestCase;
10+
use tests\Libero\ContentPageBundle\ViewConvertingTestCase;
11+
use tests\Libero\ContentPageBundle\XmlTestCase;
12+
13+
final class SubVisitorTest extends TestCase
14+
{
15+
use ViewConvertingTestCase;
16+
use XmlTestCase;
17+
18+
/**
19+
* @test
20+
* @dataProvider nodeProvider
21+
*/
22+
public function it_does_nothing_if_it_is_not_a_jats_sub_element(string $xml) : void
23+
{
24+
$visitor = new SubVisitor($this->createFailingConverter());
25+
26+
$element = $this->loadElement($xml);
27+
28+
$newContext = [];
29+
$view = $visitor->visit($element, new View(null), $newContext);
30+
31+
$this->assertNull($view->getTemplate());
32+
$this->assertEmpty($view->getArguments());
33+
$this->assertEmpty($newContext);
34+
}
35+
36+
public function nodeProvider() : iterable
37+
{
38+
yield 'different namespace' => ['<sub xmlns="http://example.com">foo</sub>'];
39+
yield 'different element' => ['<bold xmlns="http://jats.nlm.nih.gov">foo</bold>'];
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
public function it_does_nothing_if_is_not_the_sub_template() : void
46+
{
47+
$visitor = new SubVisitor($this->createFailingConverter());
48+
49+
$element = $this->loadElement('<sub xmlns="http://jats.nlm.nih.gov">foo</sub>');
50+
51+
$newContext = [];
52+
$view = $visitor->visit($element, new View('template'), $newContext);
53+
54+
$this->assertSame('template', $view->getTemplate());
55+
$this->assertEmpty($view->getArguments());
56+
$this->assertEmpty($newContext);
57+
}
58+
59+
/**
60+
* @test
61+
*/
62+
public function it_does_nothing_if_there_is_already_text_set() : void
63+
{
64+
$visitor = new SubVisitor($this->createFailingConverter());
65+
66+
$element = $this->loadElement('<sub xmlns="http://jats.nlm.nih.gov">foo</sub>');
67+
68+
$newContext = [];
69+
$view = $visitor->visit($element, new View(null, ['text' => 'bar']), $newContext);
70+
71+
$this->assertNull($view->getTemplate());
72+
$this->assertSame(['text' => 'bar'], $view->getArguments());
73+
$this->assertEmpty($newContext);
74+
}
75+
76+
/**
77+
* @test
78+
*/
79+
public function it_sets_the_template_and_text_argument() : void
80+
{
81+
$visitor = new SubVisitor($this->createDumpingConverter());
82+
83+
$element = $this->loadElement(
84+
<<<XML
85+
<jats:sub xmlns:jats="http://jats.nlm.nih.gov">
86+
foo <jats:bold>bar</jats:bold> baz
87+
</jats:sub>
88+
XML
89+
);
90+
91+
$newContext = ['qux' => 'quux'];
92+
$view = $visitor->visit($element, new View(null), $newContext);
93+
94+
$this->assertSame('@LiberoPatterns/sub.html.twig', $view->getTemplate());
95+
$this->assertEquals(
96+
[
97+
'text' => [
98+
new View(
99+
null,
100+
['node' => '/jats:sub/text()[1]', 'template' => null, 'context' => ['qux' => 'quux']]
101+
),
102+
new View(
103+
null,
104+
['node' => '/jats:sub/jats:bold', 'template' => null, 'context' => ['qux' => 'quux']]
105+
),
106+
new View(
107+
null,
108+
['node' => '/jats:sub/text()[2]', 'template' => null, 'context' => ['qux' => 'quux']]
109+
),
110+
],
111+
],
112+
$view->getArguments()
113+
);
114+
$this->assertSame(['qux' => 'quux'], $newContext);
115+
}
116+
}

vendor-extra/LiberoPatternsBundle/src/Resources/public/css/all.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor-extra/LiberoPatternsBundle/src/Resources/public/css/all.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% spaceless %}
2+
3+
<sub {%- include '@LiberoPatterns/attributes.html.twig' %}>
4+
5+
{%- include '@LiberoPatterns/text.html.twig' with {nodes: text} only -%}
6+
7+
</sub>
8+
9+
{% endspaceless %}

0 commit comments

Comments
 (0)