Skip to content

Commit 67f101e

Browse files
committed
Formatting: Treat math elements as block-level elements.
The `math` element can be displayed either as a block or inline element. If `wpautop` only treats it as an inline element, it will break multiline elements by inserting `br` elements. Treating the element as a block element means that the editor won't break common normative usages of the `math` element. Prevent `math` elements from having internal elements split up with `br` elements, disrupting formatting. Props nicholaswilson, wojtek.szkutnik, hakre, conner_bw, ericlewis, hughie.molloy, SteelWagstaff, ryokuhi, joedolson, bgoewert, adamsilverstein, joedolson. Fixes #13340. git-svn-id: https://develop.svn.wordpress.org/trunk@55272 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d499513 commit 67f101e

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/wp-includes/formatting.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ function wpautop( $text, $br = true ) {
480480
// Change multiple <br>'s into two line breaks, which will turn into paragraphs.
481481
$text = preg_replace( '|<br\s*/?>\s*<br\s*/?>|', "\n\n", $text );
482482

483-
$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
483+
$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
484484

485485
// Add a double line break above block-level opening tags.
486486
$text = preg_replace( '!(<' . $allblocks . '[\s/>])!', "\n\n$1", $text );
@@ -568,7 +568,7 @@ function wpautop( $text, $br = true ) {
568568
// Optionally insert line breaks.
569569
if ( $br ) {
570570
// Replace newlines that shouldn't be touched with a placeholder.
571-
$text = preg_replace_callback( '/<(script|style|svg).*?<\/\\1>/s', '_autop_newline_preservation_helper', $text );
571+
$text = preg_replace_callback( '/<(script|style|svg|math).*?<\/\\1>/s', '_autop_newline_preservation_helper', $text );
572572

573573
// Normalize <br>
574574
$text = str_replace( array( '<br>', '<br/>' ), '<br />', $text );

tests/phpunit/tests/formatting/wpAutop.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,63 @@ public function test_skip_input_elements() {
104104
$this->assertSame( "<p>$str</p>", trim( wpautop( $str ) ) );
105105
}
106106

107+
/**
108+
* wpautop() Should add <p> around inline "<math>" elements.
109+
*
110+
* @ticket 13340
111+
*/
112+
public function test_wrap_inline_math_elements() {
113+
$str = '<math><mrow><msup><mi>a</mi><mn>2</mn></msup><mo>+</mo><msup><mi>b</mi><mn>2</mn></msup><mo>=</mo><msup><mi>c</mi><mn>2</mn></msup></mrow></math>';
114+
115+
$this->assertSame( "<p>$str</p>", trim( wpautop( $str ) ) );
116+
}
117+
118+
/**
119+
* wpautop() Should not add <br> inside block "<math>" elements.
120+
*
121+
* @ticket 13340
122+
*/
123+
public function test_skip_block_math_elements() {
124+
$str = '<math display="block">
125+
<mtable>
126+
<mtr>
127+
<mtd>
128+
<msup><mrow><mo>(</mo><mi>a</mi><mo>+</mo><mi>b</mi><mo>)</mo></mrow><mn>2</mn></msup>
129+
</mtd>
130+
<mtd>
131+
<mo>=</mo>
132+
</mtd>
133+
<mtd>
134+
<msup><mi>c</mi><mn>2</mn></msup>
135+
<mo>+</mo><mn>4</mn><mo>⋅</mo>
136+
<mo>(</mo><mfrac><mn>1</mn><mn>2</mn></mfrac><mi>a</mi><mi>b</mi><mo>)</mo>
137+
</mtd>
138+
</mtr>
139+
<mtr>
140+
<mtd>
141+
<msup><mi>a</mi><mn>2</mn></msup>
142+
<mo>+</mo><mn>2</mn><mi>a</mi><mi>b</mi><mo>+</mo>
143+
<msup><mi>b</mi><mn>2</mn></msup>
144+
</mtd>
145+
<mtd>
146+
<mo>=</mo>
147+
</mtd>
148+
<mtd>
149+
<msup><mi>c</mi><mn>2</mn></msup>
150+
<mo>+</mo><mn>2</mn><mi>a</mi><mi>b</mi>
151+
</mtd>
152+
</mtr>
153+
<mtr>
154+
<mtd><msup><mi>a</mi><mn>2</mn></msup><mo>+</mo><msup><mi>b</mi><mn>2</mn></msup></mtd>
155+
<mtd><mo>=</mo></mtd>
156+
<mtd><msup><mi>c</mi><mn>2</mn></msup></mtd>
157+
</mtr>
158+
</mtable>
159+
</math>';
160+
161+
$this->assertSame( "<p>$str</p>", trim( wpautop( $str ) ) );
162+
}
163+
107164
/**
108165
* wpautop() Should not add <p> and <br/> around <source> and <track>
109166
*
@@ -308,7 +365,6 @@ public function test_that_wpautop_treats_block_level_elements_as_blocks() {
308365
'map',
309366
'area',
310367
'address',
311-
'math',
312368
'style',
313369
'p',
314370
'h1',

0 commit comments

Comments
 (0)