Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Wt2Html/DOM/Processors/PWrapTest.php

raw
1 <?php
2
3 namespace Test\Parsoid\Wt2Html\DOM\Processors;
4
5 use PHPUnit\Framework\TestCase;
6 use Wikimedia\Parsoid\Mocks\MockEnv;
7 use Wikimedia\Parsoid\Utils\ContentUtils;
8 use Wikimedia\Parsoid\Utils\DOMCompat;
9 use Wikimedia\Parsoid\Utils\DOMDataUtils;
10 use Wikimedia\Parsoid\Wt2Html\DOM\Processors\PWrap;
11
12 // phpcs:disable Generic.Files.LineLength.TooLong
13
14 /**
15 * based on tests/mocha/pwrap.js
16 * @coversDefaultClass \Wikimedia\Parsoid\Wt2Html\DOM\Processors\PWrap
17 */
18 class PWrapTest extends TestCase {
19
20 private function verifyPWrap( string $html, string $expected ): void {
21 $mockEnv = new MockEnv( [] );
22 $doc = ContentUtils::createAndLoadDocument( $html );
23 $body = DOMCompat::getBody( $doc );
24 $pwrap = new PWrap();
25 $pwrap->run( $mockEnv, $body );
26
27 $innerHtml = DOMCompat::getInnerHTML( $body );
28 $pattern = '/ ' . DOMDataUtils::DATA_OBJECT_ATTR_NAME . '="\d+"/';
29 $actual = preg_replace( $pattern, '', $innerHtml );
30 $this->assertEquals( $expected, $actual );
31 }
32
33 /**
34 * @covers ::run
35 * @dataProvider provideNoPWrapper
36 * @param string $html
37 * @param string $expected
38 */
39 public function testNoPWrapper( string $html, string $expected ): void {
40 $this->verifyPWrap( $html, $expected );
41 }
42
43 public function provideNoPWrapper() {
44 // NOTE: verifyPWrap doesn't store data attribs. Hence no data-parsoid in output.
45 return [
46 [ '', '' ],
47 [ ' ', ' ' ],
48 [ ' <!--c--> ', ' <!--c--> ' ],
49 [
50 // "empty" span gets no p-wrapper
51 '<span about="#mwt1" data-parsoid=\'{"tmp":{"tagId":null,"bits":16}}\'><!--x--></span>',
52 '<span about="#mwt1"><!--x--></span>'
53 ],
54 [
55 // "empty" span gets no p-wrapper
56 '<style>p{}</style><span about="#mwt1" data-parsoid=\'{"tmp":{"tagId":null,"bits":16}}\'><!--x--></span>',
57 '<style>p{}</style><span about="#mwt1"><!--x--></span>'
58 ],
59 [ '<div>a</div>', '<div>a</div>' ],
60 [ '<div>a</div> <div>b</div>', '<div>a</div> <div>b</div>' ],
61 [ '<i><div>a</div></i>', '<i><div>a</div></i>' ],
62 // <span> is not a splittable tag
63 [ '<span>x<div>a</div>y</span>', '<span>x<div>a</div>y</span>' ],
64 [ '<span>x<div></div>y</span>', '<span>x<div></div>y</span>' ],
65 ];
66 }
67
68 /**
69 * @covers ::run
70 * @dataProvider provideSimplePWrapper
71 * @param string $html
72 * @param string $expected
73 */
74 public function testSimplePWrapper( string $html, string $expected ): void {
75 $this->verifyPWrap( $html, $expected );
76 }
77
78 public function provideSimplePWrapper(): array {
79 return [
80 [ 'a', '<p>a</p>' ],
81 // <span> is not a splittable tag, but gets p-wrapped in simple wrapping scenarios
82 [ '<span>a</span>', '<p><span>a</span></p>' ],
83 [
84 'x <div>a</div> <div>b</div> y',
85 '<p>x </p><div>a</div> <div>b</div><p> y</p>',
86 ],
87 [
88 'x<!--c--> <div>a</div> <div>b</div> <!--c-->y',
89 '<p>x<!--c--> </p><div>a</div> <div>b</div> <!--c--><p>y</p>',
90 ],
91 ];
92 }
93
94 /**
95 * @covers ::run
96 * @dataProvider provideComplexPWrapper
97 * @param string $html
98 * @param string $expected
99 */
100 public function testComplexPWrapper( string $html, string $expected ): void {
101 $this->verifyPWrap( $html, $expected );
102 }
103
104 public function provideComplexPWrapper(): array {
105 return [
106 [
107 '<i>x<div>a</div>y</i>',
108 '<p><i>x</i></p><i><div>a</div></i><p><i>y</i></p>',
109 ],
110 [
111 'a<small>b</small><i>c<div>d</div>e</i>f',
112 '<p>a<small>b</small><i>c</i></p><i><div>d</div></i><p><i>e</i>f</p>',
113 ],
114 [
115 'a<small>b<i>c<div>d</div></i>e</small>',
116 '<p>a<small>b<i>c</i></small></p><small><i><div>d</div></i></small><p><small>e</small></p>',
117 ],
118 [
119 'x<small><div>y</div></small>',
120 '<p>x</p><small><div>y</div></small>',
121 ],
122 [
123 'a<small><i><div>d</div></i>e</small>',
124 '<p>a</p><small><i><div>d</div></i></small><p><small>e</small></p>',
125 ],
126 [
127 '<i>a<div>b</div>c<b>d<div>e</div>f</b>g</i>',
128 '<p><i>a</i></p><i><div>b</div></i><p><i>c<b>d</b></i></p>' .
129 '<i><b><div>e</div></b></i><p><i><b>f</b>g</i></p>',
130 ],
131 [
132 '<i><b><font><div>x</div></font></b><div>y</div><b><font><div>z</div></font></b></i>',
133 '<i><b><font><div>x</div></font></b><div>y</div><b><font><div>z</div></font></b></i>',
134 ],
135 [
136 // Wikitext: "<div>foo</div> {{1x|a}}</span>"
137 // NOTE: Simplified the strippedTag meta tag by removing data-parsoid since that is immaterial to the test
138 '<div>foo</div> <meta typeof="mw:Transclusion" about="#mwt1"/>a<meta typeof="mw:Transclusion/End" about="#mwt1"/><meta typeof="mw:Placeholder/StrippedTag"/>',
139 '<div>foo</div> <meta typeof="mw:Transclusion" about="#mwt1"/><p>a</p><meta typeof="mw:Transclusion/End" about="#mwt1"/><meta typeof="mw:Placeholder/StrippedTag"/>',
140 ],
141 [
142 // Wikitext: "<div>foo</div> {{1x|a}} <div>bar</div>"
143 '<div>foo</div> <meta typeof="mw:Transclusion" about="#mwt1"/>a<meta typeof="mw:Transclusion/End" about="#mwt1"/> <div>bar</div>',
144 '<div>foo</div> <meta typeof="mw:Transclusion" about="#mwt1"/><p>a</p><meta typeof="mw:Transclusion/End" about="#mwt1"/> <div>bar</div>',
145 ],
146 [
147 // Wikitext: "<div>foo</div> a {{1x|b}} <div>bar</div>
148 '<div>foo</div> a <meta typeof="mw:Transclusion" about="#mwt2"/>b<meta typeof="mw:Transclusion/End" about="#mwt2"/> <div>bar</div>',
149 '<div>foo</div><p> a <meta typeof="mw:Transclusion" about="#mwt2"/>b<meta typeof="mw:Transclusion/End" about="#mwt2"/></p> <div>bar</div>',
150 ],
151 [
152 // This is an example where ideally the opening meta tag will be pushed into the <p> tag
153 // but the algorithm isn't smart enough for doing that.
154 // Wikitext: "<div>foo</div> {{1x|a}} b <div>bar</div>"
155 '<div>foo</div> <meta typeof="mw:Transclusion" about="#mwt1"/>a<meta typeof="mw:Transclusion/End" about="#mwt1"/> b <div>bar</div>',
156 '<div>foo</div> <meta typeof="mw:Transclusion" about="#mwt1"/><p>a<meta typeof="mw:Transclusion/End" about="#mwt1"/> b </p><div>bar</div>',
157 ],
158 [
159 // Wikitext: "<div>foo</div> a {{1x|b}} {{1x|<div>bar</div>}}"
160 '<div>foo</div> a <meta typeof="mw:Transclusion" about="#mwt1"/>b<meta typeof="mw:Transclusion/End" about="#mwt1"/> <meta typeof="mw:Transclusion" about="#mwt2"/><div>bar</div><meta typeof="mw:Transclusion/End" about="#mwt2"/>',
161 '<div>foo</div><p> a <meta typeof="mw:Transclusion" about="#mwt1"/>b<meta typeof="mw:Transclusion/End" about="#mwt1"/></p> <meta typeof="mw:Transclusion" about="#mwt2"/><div>bar</div><meta typeof="mw:Transclusion/End" about="#mwt2"/>',
162 ],
163 ];
164 }
165 }