diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
index 51a1388dfd..bde523af0f 100644
--- a/src/wp-includes/formatting.php
+++ b/src/wp-includes/formatting.php
@@ -2465,17 +2465,63 @@ function force_balance_tags( $text ) {
 	// WP bug fix for LOVE <3 (and other situations with '<' before a number)
 	$text = preg_replace( '#<([0-9]{1})#', '&lt;$1', $text );
 
-	while ( preg_match( '/<(\/?[\w:]*)\s*([^>]*)>/', $text, $regex ) ) {
+	/**
+	 * Matches supported tags
+     *
+     * To get the pattern as a string without the comments paste into a PHP
+     * REPL like `php -a`
+     *
+     * @see https://html.spec.whatwg.org/#elements-2
+     * @see https://w3c.github.io/webcomponents/spec/custom/#valid-custom-element-name
+     *
+     * @example
+     * ~# php -a
+     * php > $s = [paste copied contents of expression below including parentheses];
+     * php > echo $s;
+     *
+     * @type string
+	 */
+	$tag_pattern = (
+		'#<' . // Start with an opening bracket.
+		'(/?)' . // Group 1 - If it's a closing tag it'll have a leading slash.
+		'(' . // Group 2 - tag name
+			// Custom element tags have more lenient rules than HTML tag names.
+			'(?:[a-z](?:[a-z0-9._]*)-(?:[a-z0-9._-]+)+)' .
+				'|' .
+			// Traditional tag rules approximate HTML tag names.
+			'(?:[\w:]+)' .
+		')' .
+		'(?:' .
+			// We _either_ immediately close the tag with its '>' and have nothing here.
+			'\s*' .
+			'(/?)' . // Group 3 - "attributes" for empty tag
+				'|' .
+			// _or_ We must start with space characters to separate the tag name from the attributes (or whitespace).
+			'(\s+)' . // Group 4 - pre-attribute whitespace
+			'([^>]*)' . // Group 5 - attributes
+		')' .
+		'>#' // End with a closing bracket.
+	);
+
+	while ( preg_match( $tag_pattern, $text, $regex ) ) {
+		$full_match        = $regex[0];
+		$has_leading_slash = ! empty( $regex[1] );
+		$tag_name          = $regex[2];
+		$tag               = strtolower( $tag_name );
+		$is_single_tag     = in_array( $tag, $single_tags, true );
+		$pre_attribute_ws  = isset( $regex[4] ) ? $regex[4] : '';
+		$attributes        = trim( isset( $regex[5] ) ? $regex[5] : $regex[3] );
+		$has_self_closer   = '/' === substr( $attributes, -1 );
+
 		$newtext .= $tagqueue;
 
-		$i = strpos( $text, $regex[0] );
-		$l = strlen( $regex[0] );
+		$i = strpos( $text, $full_match );
+		$l = strlen( $full_match );
 
 		// clear the shifter
 		$tagqueue = '';
 		// Pop or Push
-		if ( isset( $regex[1][0] ) && '/' == $regex[1][0] ) { // End Tag
-			$tag = strtolower( substr( $regex[1], 1 ) );
+		if ( $has_leading_slash ) { // End Tag
 			// if too many closing tags
 			if ( $stacksize <= 0 ) {
 				$tag = '';
@@ -2501,21 +2547,16 @@ function force_balance_tags( $text ) {
 				$tag = '';
 			}
 		} else { // Begin Tag
-			$tag = strtolower( $regex[1] );
-
 			// Tag Cleaning
-
-			// If it's an empty tag "< >", do nothing
-			if ( '' == $tag ) {
-				// do nothing
-			} elseif ( substr( $regex[2], -1 ) == '/' ) { // ElseIf it presents itself as a self-closing tag...
+			if ( $has_self_closer ) { // If it presents itself as a self-closing tag...
 				// ...but it isn't a known single-entity self-closing tag, then don't let it be treated as such and
 				// immediately close it with a closing tag (the tag will encapsulate no text as a result)
-				if ( ! in_array( $tag, $single_tags ) ) {
-					$regex[2] = trim( substr( $regex[2], 0, -1 ) ) . "></$tag";
+				if ( ! $is_single_tag ) {
+					$attributes = trim( substr( $attributes, 0, -1 ) ) . "></$tag";
 				}
-			} elseif ( in_array( $tag, $single_tags ) ) { // ElseIf it's a known single-entity tag but it doesn't close itself, do so
-				$regex[2] .= '/';
+			} elseif ( $is_single_tag ) { // ElseIf it's a known single-entity tag but it doesn't close itself, do so
+				$pre_attribute_ws = ' ';
+				$attributes .= '/';
 			} else { // Else it's not a single-entity tag
 				// If the top of the stack is the same as the tag we want to push, close previous tag
 				if ( $stacksize > 0 && ! in_array( $tag, $nestable_tags ) && $tagstack[ $stacksize - 1 ] == $tag ) {
@@ -2526,12 +2567,12 @@ function force_balance_tags( $text ) {
 			}
 
 			// Attributes
-			$attributes = $regex[2];
-			if ( ! empty( $attributes ) && $attributes[0] != '>' ) {
-				$attributes = ' ' . $attributes;
+			if ( $has_self_closer && $is_single_tag ) {
+			    // we need some space - avoid <br/> and prefer <br />
+				$pre_attribute_ws = ' ';
 			}
 
-			$tag = '<' . $tag . $attributes . '>';
+			$tag = '<' . $tag . $pre_attribute_ws . $attributes . '>';
 			//If already queuing a close tag, then put this tag on, too
 			if ( ! empty( $tagqueue ) ) {
 				$tagqueue .= $tag;
diff --git a/tests/phpunit/tests/formatting/balanceTags.php b/tests/phpunit/tests/formatting/balanceTags.php
index 783c320780..a533edbe36 100644
--- a/tests/phpunit/tests/formatting/balanceTags.php
+++ b/tests/phpunit/tests/formatting/balanceTags.php
@@ -37,6 +37,146 @@ class Tests_Formatting_BalanceTags extends WP_UnitTestCase {
 		);
 	}
 
+	function supported_traditional_tag_names() {
+		return array(
+			array( 'a' ),
+			array( 'div' ),
+			array( 'blockquote' ),
+			// HTML tag names can be CAPITALIZED and are case-insensitive
+			array( 'A' ),
+			array( 'dIv' ),
+			array( 'BLOCKQUOTE' ),
+		);
+	}
+
+	function supported_custom_element_tag_names() {
+		return array(
+			array( 'custom-element' ),
+			array( 'my-custom-element' ),
+			array( 'weekday-5-item' ),
+			array( 'a-big-old-tag-name' ),
+			array( 'with_underscores-and_the_dash' ),
+		);
+	}
+
+	function invalid_tag_names() {
+		return array(
+			array( '<0-day>inside', '&lt;0-day>inside' ), // can't start with a number - handled by the "<3" "fix"
+			array( '<UPPERCASE-TAG>inside', '<UPPERCASE-TAG>inside' ), // custom elements cannot be uppercase
+		);
+	}
+
+	// These are valid custom elements but we don't support them yet.
+	// see https://w3c.github.io/webcomponents/spec/custom/#valid-custom-element-name
+	function unsupported_valid_tag_names() {
+		return array(
+			// we don't allow ending in a dash
+			array( '<what->inside' ),
+			// examples from the spec working document
+			array( 'math-α' ),
+			array( 'emotion-😍' ),
+			// UNICODE ranges
+			// middle dot
+			// 0x00b7
+			array( 'b-·' ),
+			// latin characters with accents/modifiers
+			// 0x00c0-0x00d6
+			// 0x00d8-0x00f6
+			array( 'a-À-Ó-Ý' ),
+			// 0x00f8-0x037d
+			array( 'a-ͳ' ),
+			// no 0x037e, which is a Greek semicolon
+			// 0x037f-0x1fff
+			array( 'a-Ფ' ),
+			// zero-width characters, probably never supported
+			// 0x200c-0x200d
+			array( 'a-‌to-my-left-is-a-zero-width-non-joiner-do-not-delete-it' ),
+			array( 'a-‍to-my-left-is-a-zero-width-joiner-do-not-delete-it' ),
+			// ties
+			// 0x203f-0x2040
+			array( 'under-‿-tie' ),
+			array( 'over-⁀-tie' ),
+			// 0x2170-0x218f
+			array( 'a-⁰' ),
+			array( 'a-⅀' ),
+			array( 'tag-ↀ-it' ),
+			// 0x2c00-0x2fef
+			array( 'a-Ⰰ' ),
+			array( 'b-ⴓ-c' ),
+			array( 'd-⽗' ),
+			// 0x3001-0xd7ff
+			array( 'a-、' ),
+			array( 'z-态' ),
+			array( 'a-送-䠺-ퟱ-퟿' ),
+			// 0xf900-0xfdcf
+			array( 'a-豈' ),
+			array( 'my-切' ),
+			array( 'aﴀ-tag' ),
+			array( 'my-﷌' ),
+			// 0xfdf0-0xfffd
+			array( 'a-ﷰ' ),
+			array( 'a-￰-￸-�' ), // warning; blank characters are in there
+			// extended ranges
+			// 0x10000-0xeffff
+			array( 'a-𐀀' ),
+			array( 'my-𝀀' ),
+			array( 'a𞀀-𜿐' ),
+		);
+	}
+
+	// These are invalid custom elements but we support them right now in order to keep the parser simpler
+	// see https://w3c.github.io/webcomponents/spec/custom/#valid-custom-element-name
+	function supported_invalid_tag_names() {
+		return array(
+			// reserved names for custom element
+			array( 'annotation-xml' ),
+			array( 'color-profile' ),
+			array( 'font-face' ),
+			array( 'font-face-src' ),
+			array( 'font-face-uri' ),
+			array( 'font-face-format' ),
+			array( 'font-face-name' ),
+			array( 'missing-glyph' ),
+		);
+	}
+
+	/**
+	 * @dataProvider supported_traditional_tag_names
+	 */
+	function test_detects_traditional_tag_names( $tag ) {
+		$normalized = strtolower( $tag );
+
+		$this->assertEquals( "<$normalized>inside</$normalized>", balanceTags( "<$tag>inside", true ) );
+	}
+
+	/**
+	 * @dataProvider supported_custom_element_tag_names
+	 */
+	function test_detects_supported_custom_element_tag_names( $tag ) {
+		$this->assertEquals( "<$tag>inside</$tag>", balanceTags( "<$tag>inside", true ) );
+	}
+
+	/**
+	 * @dataProvider invalid_tag_names
+	 */
+	function test_ignores_invalid_tag_names( $input, $output ) {
+		$this->assertEquals( $output, balanceTags( $input, true ) );
+	}
+
+	/**
+	 * @dataProvider unsupported_valid_tag_names
+	 */
+	function test_ignores_unsupported_custom_tag_names( $tag ) {
+		$this->assertEquals( "<$tag>inside", balanceTags( "<$tag>inside", true ) );
+	}
+
+	/**
+	 * @dataProvider supported_invalid_tag_names
+	 */
+	function test_detects_supported_invalid_tag_names( $tag ) {
+		$this->assertEquals( "<$tag>inside</$tag>", balanceTags( "<$tag>inside", true ) );
+	}
+
 	/**
 	 * If a recognized valid single tag appears unclosed, it should get self-closed
 	 *
@@ -68,12 +208,15 @@ class Tests_Formatting_BalanceTags extends WP_UnitTestCase {
 			'<em />',
 			'<p class="main1"/>',
 			'<p class="main2" />',
+			'<STRONG/>',
 		);
 		$expected = array(
 			'<strong></strong>',
 			'<em></em>',
 			'<p class="main1"></p>',
 			'<p class="main2"></p>',
+			// Valid tags are transformed to lowercase.
+			'<strong></strong>',
 		);
 
 		foreach ( $inputs as $key => $input ) {
@@ -221,4 +364,68 @@ class Tests_Formatting_BalanceTags extends WP_UnitTestCase {
 		}
 	}
 
+	/**
+	 * Get custom element data.
+	 *
+	 * @return array Data.
+	 */
+	public function data_custom_elements() {
+		return array(
+			// Valid custom element tags.
+			array(
+				'<my-custom-element data-attribute="value"/>',
+				'<my-custom-element data-attribute="value"></my-custom-element>',
+			),
+			array(
+				'<my-custom-element>Test</my-custom-element>',
+				'<my-custom-element>Test</my-custom-element>',
+			),
+			array(
+				'<my-custom-element>Test',
+				'<my-custom-element>Test</my-custom-element>',
+			),
+			array(
+				'Test</my-custom-element>',
+				'Test',
+			),
+			array(
+				'</my-custom-element>Test',
+				'Test',
+			),
+			array(
+				'<my-custom-element/>',
+				'<my-custom-element></my-custom-element>',
+			),
+			array(
+				'<my-custom-element />',
+				'<my-custom-element></my-custom-element>',
+			),
+			// Invalid (or at least temporarily unsupported) custom element tags.
+			array(
+				'<MY-CUSTOM-ELEMENT>Test',
+				'<MY-CUSTOM-ELEMENT>Test',
+			),
+			array(
+				'<my->Test',
+				'<my->Test',
+			),
+			array(
+				'<--->Test',
+				'<--->Test',
+			),
+		);
+	}
+
+	/**
+	 * Test custom elements.
+	 *
+	 * @ticket 47014
+	 * @dataProvider data_custom_elements
+	 *
+	 * @param string $source   Source.
+	 * @param string $expected Expected.
+	 */
+	public function test_custom_elements( $source, $expected ) {
+		$this->assertEquals( $expected, balanceTags( $source, true ) );
+	}
 }
