Skip to content

Commit cb32a8d

Browse files
committed
adds a Regex to make sure audio tag gets both start and end tags
1 parent 5cbafff commit cb32a8d

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

wordpress-shortcodes/src/main/java/org/wordpress/aztec/plugins/shortcodes/AudioShortcodePlugin.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ class AudioShortcodePlugin : IHtmlPreprocessor, IHtmlPostprocessor {
1414
}
1515

1616
override fun onHtmlProcessed(source: String): String {
17-
if (GutenbergUtils.contentContainsGutenbergBlocks(source)) return source
17+
if (GutenbergUtils.contentContainsGutenbergBlocks(source)) {
18+
// From https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio
19+
// > Tag omission None, both the starting and ending tag are mandatory.
20+
return StringBuilder(source)
21+
.replace(Regex("(<$TAG[^>]*?)(\\s*/>)"), "\$1></$TAG>")
22+
}
23+
1824
return StringBuilder(source)
1925
.replace(Regex("<$TAG([^>]*(?<! )) */>"), "[$TAG$1]")
2026
.replace(Regex("<$TAG([^>]*(?<! )) *></$TAG>"), "[$TAG$1]")

wordpress-shortcodes/src/test/java/org/wordpress/aztec/plugins/shortcodes/AudioShortcodeTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class AudioShortcodeTest {
5151

5252
// expected result: the <audio> tag does not get converted to shortcode when it's found within a Gutenberg block
5353
// Note: the slight difference of <audio controls> being converted to <audio controls="controls"> should be equivalent
54-
val htmlWithoutShortcode = "<!-- wp:audio {\"id\":435} --><figure class=\"wp-block-audio\"><audio controls=\"controls\" src=\"https://selfhostedmario.mystagingwebsite.com/wp-content/uploads/2018/05/ArgentinaAnthem.mp3\" /><figcaption>a caption</figcaption></figure><!-- /wp:audio -->"
54+
val htmlWithoutShortcode = "<!-- wp:audio {\"id\":435} --><figure class=\"wp-block-audio\"><audio controls=\"controls\" src=\"https://selfhostedmario.mystagingwebsite.com/wp-content/uploads/2018/05/ArgentinaAnthem.mp3\"></audio><figcaption>a caption</figcaption></figure><!-- /wp:audio -->"
5555

5656
Assert.assertEquals(htmlWithoutShortcode, editText.toPlainHtml())
5757
}

0 commit comments

Comments
 (0)