| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group formatting |
| 5 | * @ticket 45435 |
| 6 | */ |
| 7 | class Tests_Formatting_Removep extends WP_UnitTestCase { |
| 8 | public function test_remove_p() { |
| 9 | $test_data = '<p>Welcome to WordPress! This post contains important information. After you read it, you can make it private to hide it from visitors but still have the information handy for future reference.</p> |
| 10 | <p></p> |
| 11 | <p>First things first:</p> |
| 12 | '; |
| 13 | $expected = 'Welcome to WordPress! This post contains important information. After you read it, you can make it private to hide it from visitors but still have the information handy for future reference. |
| 14 | |
| 15 | First things first:'; |
| 16 | |
| 17 | // On windows environments, the EOL-style is \r\n |
| 18 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 19 | |
| 20 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 21 | } |
| 22 | |
| 23 | public function test_remove_br() { |
| 24 | $test_data = '<p>Welcome to WordPress!<br />This post contains important information.<br>After you read it, you can make it private to hide it from visitors but still have the information handy for future reference.</p> |
| 25 | <p>Test<br /> <br > <br>Test</p> |
| 26 | <p>First things first:</p> |
| 27 | '; |
| 28 | $expected = 'Welcome to WordPress! |
| 29 | This post contains important information. |
| 30 | After you read it, you can make it private to hide it from visitors but still have the information handy for future reference. |
| 31 | |
| 32 | Test |
| 33 | |
| 34 | |
| 35 | Test |
| 36 | |
| 37 | First things first:'; |
| 38 | |
| 39 | // On windows environments, the EOL-style is \r\n |
| 40 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 41 | |
| 42 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 43 | } |
| 44 | |
| 45 | public function test_line_break_around_div() { |
| 46 | $test_data = '<p>Hello</p><div>World</div><p>!!</p>'; |
| 47 | $expected = 'Hello |
| 48 | <div>World</div> |
| 49 | !!'; |
| 50 | |
| 51 | // On windows environments, the EOL-style is \r\n |
| 52 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 53 | |
| 54 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 55 | } |
| 56 | |
| 57 | public function test_line_break_around_caption() { |
| 58 | $test_data = '<p>Hello</p>[caption id="1"]whatever 1[/caption]<p>!!</p>[caption id="2"]whatever 2[/caption][caption id="3"]whatever 3[/caption]'; |
| 59 | $expected = 'Hello |
| 60 | |
| 61 | [caption id="1"]whatever 1[/caption] |
| 62 | |
| 63 | !! |
| 64 | |
| 65 | [caption id="2"]whatever 2[/caption] |
| 66 | |
| 67 | [caption id="3"]whatever 3[/caption]'; |
| 68 | |
| 69 | // On windows environments, the EOL-style is \r\n |
| 70 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 71 | |
| 72 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 73 | } |
| 74 | |
| 75 | public function test_line_break_around_block_elements() { |
| 76 | |
| 77 | //const blocklist = 'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure'; |
| 78 | //const blocklist1 = blocklist + '|div|p'; |
| 79 | //const blocklist2 = blocklist + '|pre'; |
| 80 | |
| 81 | $test_data = '<p>Hello</p> |
| 82 | |
| 83 | <blockquote cite="https://www.huxley.net/bnw/four.html"> |
| 84 | <p>Words can be like X-rays, if you use them properly – they\'ll go through anything. You read and you\'re pierced.</p> |
| 85 | </blockquote> |
| 86 | <h1>This is an UL</h1><ul> |
| 87 | <li>Milk</li><li>Cheese |
| 88 | <ul><li>Blue cheese</li><li>Feta</li> </ul> |
| 89 | </li> |
| 90 | </ul><h2>This is an OL</h2> |
| 91 | <ol> <li>Mix flour, baking powder, sugar, and salt.</li><li>In another bowl, mix eggs, milk, and oil.</li> |
| 92 | <li>Stir both mixtures together.</li> |
| 93 | <li>Fill muffin tray 3/4 full.</li><li>Bake for 20 minutes.</li> |
| 94 | </ol> |
| 95 | |
| 96 | <p>Cryptids of Cornwall:</p> |
| 97 | <h3>This is an DL</h3> |
| 98 | <dl> |
| 99 | <dt>Beast of Bodmin</dt><dd>A large feline inhabiting Bodmin Moor.</dd> |
| 100 | <dt>Morgawr</dt><dd>A sea serpent.</dd> |
| 101 | <dt>Owlman</dt><dd>A giant owl-like creature.</dd> |
| 102 | </dl> |
| 103 | <h4>This is an TABLE</h4><table> |
| 104 | <thead> |
| 105 | <tr> |
| 106 | <th colspan="2">The table header</th> |
| 107 | </tr> |
| 108 | </thead> |
| 109 | <tbody> |
| 110 | <tr> |
| 111 | <td>The table body</td> |
| 112 | <td>with two columns</td> |
| 113 | </tr> |
| 114 | </tbody> |
| 115 | </table><pre> |
| 116 | L TE |
| 117 | A A |
| 118 | C V |
| 119 | R A |
| 120 | DOU |
| 121 | LOU |
| 122 | REUSE |
| 123 | QUE TU |
| 124 | PORTES |
| 125 | ET QUI T\' |
| 126 | ORNE O CI |
| 127 | VILISÉ |
| 128 | OTE- TU VEUX |
| 129 | LA BIEN |
| 130 | SI RESPI |
| 131 | RER - Apollinaire |
| 132 | </pre> |
| 133 | |
| 134 | '; |
| 135 | $expected = 'Hello |
| 136 | <blockquote cite="https://www.huxley.net/bnw/four.html">Words can be like X-rays, if you use them properly – they\'ll go through anything. You read and you\'re pierced.</blockquote> |
| 137 | <h1>This is an UL</h1> |
| 138 | <ul> |
| 139 | <li>Milk</li> |
| 140 | <li>Cheese |
| 141 | <ul> |
| 142 | <li>Blue cheese</li> |
| 143 | <li>Feta</li> |
| 144 | </ul> |
| 145 | </li> |
| 146 | </ul> |
| 147 | <h2>This is an OL</h2> |
| 148 | <ol> |
| 149 | <li>Mix flour, baking powder, sugar, and salt.</li> |
| 150 | <li>In another bowl, mix eggs, milk, and oil.</li> |
| 151 | <li>Stir both mixtures together.</li> |
| 152 | <li>Fill muffin tray 3/4 full.</li> |
| 153 | <li>Bake for 20 minutes.</li> |
| 154 | </ol> |
| 155 | Cryptids of Cornwall: |
| 156 | <h3>This is an DL</h3> |
| 157 | <dl> |
| 158 | <dt>Beast of Bodmin</dt> |
| 159 | <dd>A large feline inhabiting Bodmin Moor.</dd> |
| 160 | <dt>Morgawr</dt> |
| 161 | <dd>A sea serpent.</dd> |
| 162 | <dt>Owlman</dt> |
| 163 | <dd>A giant owl-like creature.</dd> |
| 164 | </dl> |
| 165 | <h4>This is an TABLE</h4> |
| 166 | <table> |
| 167 | <thead> |
| 168 | <tr> |
| 169 | <th colspan="2">The table header</th> |
| 170 | </tr> |
| 171 | </thead> |
| 172 | <tbody> |
| 173 | <tr> |
| 174 | <td>The table body</td> |
| 175 | <td>with two columns</td> |
| 176 | </tr> |
| 177 | </tbody> |
| 178 | </table> |
| 179 | <pre> |
| 180 | L TE |
| 181 | A A |
| 182 | C V |
| 183 | R A |
| 184 | DOU |
| 185 | LOU |
| 186 | REUSE |
| 187 | QUE TU |
| 188 | PORTES |
| 189 | ET QUI T\' |
| 190 | ORNE O CI |
| 191 | VILISÉ |
| 192 | OTE- TU VEUX |
| 193 | LA BIEN |
| 194 | SI RESPI |
| 195 | RER - Apollinaire |
| 196 | </pre>'; |
| 197 | |
| 198 | // On windows environments, the EOL-style is \r\n |
| 199 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 200 | |
| 201 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 202 | } |
| 203 | |
| 204 | public function test_line_break_around_select() { |
| 205 | $test_data = '<p>Hello</p><select><option value="1">Value 1</option> |
| 206 | <option value="2">Value 2</option></select><p>World</p>'; |
| 207 | |
| 208 | $expected = 'Hello |
| 209 | |
| 210 | <select> |
| 211 | <option value="1">Value 1</option> |
| 212 | <option value="2">Value 2</option> |
| 213 | </select> |
| 214 | World'; |
| 215 | |
| 216 | // On windows environments, the EOL-style is \r\n |
| 217 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 218 | |
| 219 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 220 | } |
| 221 | |
| 222 | public function test_line_break_around_hr() { |
| 223 | $test_data = '<p>Hello</p><hr /><p>World</p>'; |
| 224 | |
| 225 | $expected = 'Hello |
| 226 | |
| 227 | <hr /> |
| 228 | |
| 229 | World'; |
| 230 | |
| 231 | // On windows environments, the EOL-style is \r\n |
| 232 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 233 | |
| 234 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 235 | } |
| 236 | |
| 237 | public function test_first_p_in_div() { |
| 238 | $test_data = '<p>Hello</p><div><p>World</p></div>'; |
| 239 | |
| 240 | $expected = 'Hello |
| 241 | <div> |
| 242 | |
| 243 | World |
| 244 | |
| 245 | </div>'; |
| 246 | |
| 247 | // On windows environments, the EOL-style is \r\n |
| 248 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 249 | |
| 250 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 251 | } |
| 252 | |
| 253 | public function test_remove_break_in_object() { |
| 254 | $test_data = '<p>Hello</p><object><p>World</p></object>'; |
| 255 | |
| 256 | $expected = 'Hello |
| 257 | |
| 258 | <object>World</object>'; |
| 259 | |
| 260 | // On windows environments, the EOL-style is \r\n |
| 261 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 262 | |
| 263 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 264 | } |
| 265 | |
| 266 | public function test_mark_p_with_attributes() { |
| 267 | $test_data = '<p>Welcome to WordPress! This post contains important information. After you read it, you can make it private to hide it from visitors but still have the information handy for future reference.</p><p>New P here</p> |
| 268 | <p></p><p style="color:red">First things first</p><p style="color:blue">Last things last</p> |
| 269 | '; |
| 270 | $expected = 'Welcome to WordPress! This post contains important information. After you read it, you can make it private to hide it from visitors but still have the information handy for future reference. |
| 271 | |
| 272 | New P here |
| 273 | <p style="color:red">First things first</p> |
| 274 | <p style="color:blue">Last things last</p>'; |
| 275 | |
| 276 | // On windows environments, the EOL-style is \r\n |
| 277 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 278 | |
| 279 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 280 | } |
| 281 | |
| 282 | public function test_preserve_scripts_and_styles() { |
| 283 | $test_data = '<p>Welcome to WordPress!</p><p>We test here</p> |
| 284 | <script> |
| 285 | const html = "<p>Preserve script</p>" |
| 286 | </script> |
| 287 | <style type="text/css">p.preserve { |
| 288 | content: "<p>Preserve style</p>" |
| 289 | }</style><p>End of test</p> |
| 290 | '; |
| 291 | $expected = 'Welcome to WordPress! |
| 292 | |
| 293 | We test here |
| 294 | |
| 295 | <script> |
| 296 | const html = "<p>Preserve script</p>" |
| 297 | </script> |
| 298 | <style type="text/css">p.preserve { |
| 299 | content: "<p>Preserve style</p>" |
| 300 | }</style>End of test'; |
| 301 | |
| 302 | // On windows environments, the EOL-style is \r\n |
| 303 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 304 | |
| 305 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 306 | } |
| 307 | |
| 308 | public function test_protect_pre() { |
| 309 | |
| 310 | $test_data = '<p>Hello</p> |
| 311 | <pre> |
| 312 | <p>Protect me</p> |
| 313 | <br /> |
| 314 | <p>Please</p><br /> |
| 315 | <p>Thank you.</p> |
| 316 | </pre> |
| 317 | |
| 318 | '; |
| 319 | $expected = 'Hello |
| 320 | <pre> |
| 321 | |
| 322 | Protect me |
| 323 | |
| 324 | |
| 325 | Please |
| 326 | |
| 327 | |
| 328 | Thank you. |
| 329 | </pre>'; |
| 330 | |
| 331 | // On windows environments, the EOL-style is \r\n |
| 332 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 333 | |
| 334 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 335 | } |
| 336 | |
| 337 | public function test_protect_caption_br() { |
| 338 | |
| 339 | $test_data = 'Hello<br />[caption]this<br>is<br />a caption[/caption]World'; |
| 340 | $expected = 'Hello |
| 341 | |
| 342 | [caption]this<br>is<br />a caption[/caption] |
| 343 | |
| 344 | World'; |
| 345 | |
| 346 | // On windows environments, the EOL-style is \r\n |
| 347 | $test_data = str_replace( "\r\n", "\n", $test_data ); |
| 348 | |
| 349 | $this->assertEquals( $expected, wpremovep( $test_data ) ); |
| 350 | } |
| 351 | } |