1414use Doctrine \RST \Environment ;
1515use Doctrine \RST \HTML \Renderers \SpanNodeRenderer as BaseSpanNodeRenderer ;
1616use Doctrine \RST \Nodes \SpanNode ;
17- use Doctrine \RST \Templates \TemplateRenderer ;
17+ use Doctrine \RST \References \ResolvedReference ;
18+ use Doctrine \RST \Renderers \SpanNodeRenderer as AbstractSpanNodeRenderer ;
1819use SymfonyDocsBuilder \CI \UrlChecker ;
1920use function Symfony \Component \String \u ;
2021
21- class SpanNodeRenderer extends BaseSpanNodeRenderer
22+ class SpanNodeRenderer extends AbstractSpanNodeRenderer
2223{
23- /** @var TemplateRenderer */
24- private $ templateRenderer ;
24+ /** @var BaseSpanNodeRenderer */
25+ private $ decoratedSpanNodeRenderer ;
2526 /** @var UrlChecker|null */
2627 private $ urlChecker ;
2728
2829 public function __construct (
2930 Environment $ environment ,
3031 SpanNode $ span ,
31- TemplateRenderer $ templateRenderer ,
32+ BaseSpanNodeRenderer $ decoratedSpanNodeRenderer ,
3233 ?UrlChecker $ urlChecker = null
3334 ) {
34- parent ::__construct ($ environment , $ span, $ templateRenderer );
35+ parent ::__construct ($ environment , $ span );
3536
36- $ this ->templateRenderer = $ templateRenderer ;
37+ $ this ->decoratedSpanNodeRenderer = $ decoratedSpanNodeRenderer ;
3738 $ this ->urlChecker = $ urlChecker ;
3839 }
3940
40- /**
41- * @param mixed[] $attributes
42- */
41+ /** @inheritDoc */
4342 public function link (?string $ url , string $ title , array $ attributes = []): string
4443 {
45- $ url = (string ) $ url ;
44+ $ url = (string )$ url ;
4645
4746 if (
4847 $ this ->urlChecker &&
@@ -53,18 +52,25 @@ public function link(?string $url, string $title, array $attributes = []): strin
5352 }
5453
5554 if (!$ this ->isSafeUrl ($ url )) {
56- $ attributes ['rel ' ] = 'external noopener noreferrer ' ;
57- $ attributes ['target ' ] = '_blank ' ;
55+ $ attributes = $ this ->addAttributesForUnsafeUrl ($ attributes );
5856 }
5957
60- return $ this ->templateRenderer ->render (
61- 'link.html.twig ' ,
62- [
63- 'url ' => $ this ->environment ->generateUrl ($ url ),
64- 'title ' => $ title ,
65- 'attributes ' => $ attributes ,
66- ]
67- );
58+ return $ this ->decoratedSpanNodeRenderer ->link ($ url , $ title , $ attributes );
59+ }
60+
61+ public function reference (ResolvedReference $ reference , array $ value ): string
62+ {
63+ if (!$ this ->isSafeUrl ($ reference ->getUrl ())) {
64+ $ reference = new ResolvedReference (
65+ $ reference ->getFile (),
66+ $ reference ->getTitle (),
67+ $ reference ->getUrl (),
68+ $ reference ->getTitles (),
69+ $ this ->addAttributesForUnsafeUrl ($ reference ->getAttributes ())
70+ );
71+ }
72+
73+ return $ this ->decoratedSpanNodeRenderer ->reference ($ reference , $ value );
6874 }
6975
7076 public function literal (string $ text ): string
@@ -78,7 +84,32 @@ public function literal(string $text): string
7884 $ text = str_replace ('\\' , '<wbr> \\' , $ text );
7985 }
8086
81- return $ this ->templateRenderer ->render ('literal.html.twig ' , ['text ' => $ text ]);
87+ return $ this ->decoratedSpanNodeRenderer ->literal ($ text );
88+ }
89+
90+ public function emphasis (string $ text ): string
91+ {
92+ return $ this ->decoratedSpanNodeRenderer ->emphasis ($ text );
93+ }
94+
95+ public function strongEmphasis (string $ text ): string
96+ {
97+ return $ this ->decoratedSpanNodeRenderer ->strongEmphasis ($ text );
98+ }
99+
100+ public function nbsp (): string
101+ {
102+ return $ this ->decoratedSpanNodeRenderer ->nbsp ();
103+ }
104+
105+ public function br (): string
106+ {
107+ return $ this ->decoratedSpanNodeRenderer ->br ();
108+ }
109+
110+ public function escape (string $ span ): string
111+ {
112+ return $ this ->decoratedSpanNodeRenderer ->escape ($ span );
82113 }
83114
84115 private function isExternalUrl ($ url ): bool
@@ -101,4 +132,12 @@ private function isSafeUrl(string $url): bool
101132
102133 return $ isSymfonyUrl || $ isRelativeUrl ;
103134 }
135+
136+ private function addAttributesForUnsafeUrl (array $ attributes ): array
137+ {
138+ return array_merge (
139+ $ attributes ,
140+ ['rel ' => 'external noopener noreferrer ' , 'target ' => '_blank ' ]
141+ );
142+ }
104143}
0 commit comments