Skip to content

Commit 7f475a8

Browse files
authored
Merge pull request #696 from wordpress-mobile/issue/695-fix-video-tag-through-plugin
Fix video tag shortcode & start/end tags through plugin
2 parents 5a5eb92 + 8468d75 commit 7f475a8

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.wordpress.aztec.plugins.shortcodes
33
import org.wordpress.aztec.plugins.shortcodes.extensions.ATTRIBUTE_VIDEOPRESS_HIDDEN_ID
44
import org.wordpress.aztec.plugins.shortcodes.extensions.ATTRIBUTE_VIDEOPRESS_HIDDEN_SRC
55
import org.wordpress.aztec.plugins.html2visual.IHtmlPreprocessor
6+
import org.wordpress.aztec.plugins.shortcodes.utils.GutenbergUtils
67
import org.wordpress.aztec.plugins.visual2html.IHtmlPostprocessor
78

89
class VideoShortcodePlugin : IHtmlPreprocessor, IHtmlPostprocessor {
@@ -11,15 +12,23 @@ class VideoShortcodePlugin : IHtmlPreprocessor, IHtmlPostprocessor {
1112
private val TAG_VIDEOPRESS_SHORTCODE = "wpvideo"
1213

1314
override fun beforeHtmlProcessed(source: String): String {
15+
if (GutenbergUtils.contentContainsGutenbergBlocks(source)) return source
1416
var newSource = source.replace(Regex("(?<!\\[)\\[$TAG([^\\]]*)\\](?!\\])"), "<$TAG$1 />")
1517
newSource = newSource.replace(Regex("(?<!\\[)\\[$TAG_VIDEOPRESS_SHORTCODE([^\\]]*)\\](?!\\])"), { it -> fromVideoPressShortCodeToHTML(it) })
1618
return newSource
1719
}
1820

1921
override fun onHtmlProcessed(source: String): String {
22+
if (GutenbergUtils.contentContainsGutenbergBlocks(source)) {
23+
// From https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video
24+
// > Tag omission None, both the starting and ending tag are mandatory.
25+
return StringBuilder(source)
26+
.replace(Regex("(<$TAG[^>]*?)(\\s*/>)"), "\$1></$TAG>")
27+
}
28+
2029
return StringBuilder(source)
2130
.replace(Regex("<$TAG([^>]*(?<! )) */>"), { it -> fromHTMLToShortcode(it) })
22-
.replace(Regex("<$TAG([^>]*(?<! )) */>"), { it -> fromHTMLToShortcode(it) })
31+
.replace(Regex("<$TAG([^>]*(?<! )) *></$TAG>"), { it -> fromHTMLToShortcode(it) })
2332
}
2433

2534
/**

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
@@ -14,7 +14,7 @@ import org.wordpress.aztec.AztecText
1414
import org.wordpress.aztec.plugins.shortcodes.TestUtils.safeEmpty
1515

1616
/**
17-
* Tests for the caption shortcode plugin
17+
* Tests for the audio shortcode plugin
1818
*/
1919
@RunWith(RobolectricTestRunner::class)
2020
@Config(constants = BuildConfig::class, sdk = intArrayOf(25))
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
@file:Suppress("DEPRECATION")
2+
3+
package org.wordpress.aztec.plugins.shortcodes
4+
5+
import android.app.Activity
6+
import org.junit.Assert
7+
import org.junit.Before
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
import org.robolectric.Robolectric
11+
import org.robolectric.RobolectricTestRunner
12+
import org.robolectric.annotation.Config
13+
import org.wordpress.aztec.AztecText
14+
import org.wordpress.aztec.plugins.shortcodes.TestUtils.safeEmpty
15+
16+
/**
17+
* Tests for the video shortcode plugin
18+
*/
19+
@RunWith(RobolectricTestRunner::class)
20+
@Config(constants = BuildConfig::class, sdk = intArrayOf(25))
21+
class VideoShortcodeTest {
22+
lateinit var editText: AztecText
23+
24+
private val htmlGutenbergVideoBlock =
25+
"<!-- wp:video {\"id\":25} --><figure class=\"wp-block-video\"><video controls src=\"https://somedomain.com/wp-content/uploads/2017/05/VID-20170508-WA0011-1.mp4\"></video></figure><!-- /wp:video -->"
26+
27+
private val htmlNormalVideoTag =
28+
"<figure class=\"wp-block-video\"><video controls src=\"https://somedomain.com/wp-content/uploads/2017/05/VID-20170508-WA0011-1.mp4\"></video></figure>"
29+
30+
/**
31+
* Initialize variables.
32+
*/
33+
@Before
34+
fun init() {
35+
val activity = Robolectric.buildActivity(Activity::class.java).create().visible().get()
36+
37+
editText = AztecText(activity)
38+
editText.setCalypsoMode(false)
39+
40+
activity.setContentView(editText)
41+
42+
editText.plugins.add(VideoShortcodePlugin())
43+
}
44+
45+
@Test
46+
@Throws(Exception::class)
47+
fun testGutenbergVideoBlockDoesntConvertToShortcode() {
48+
Assert.assertTrue(safeEmpty(editText))
49+
50+
editText.fromHtml(htmlGutenbergVideoBlock)
51+
52+
// expected result: the <video> tag does not get converted to shortcode when it's found within a Gutenberg block
53+
// Note: the slight difference of <video controls> being converted to <video controls="controls"> should be equivalent
54+
val htmlWithoutShortcode = "<!-- wp:video {\"id\":25} --><figure class=\"wp-block-video\"><video controls=\"controls\" src=\"https://somedomain.com/wp-content/uploads/2017/05/VID-20170508-WA0011-1.mp4\"></video></figure><!-- /wp:video -->"
55+
56+
Assert.assertEquals(htmlWithoutShortcode, editText.toPlainHtml())
57+
}
58+
59+
@Test
60+
@Throws(Exception::class)
61+
fun testVideoTagConvertsToShortcode() {
62+
Assert.assertTrue(safeEmpty(editText))
63+
64+
editText.fromHtml(htmlNormalVideoTag)
65+
66+
// expected result: the <video> tag gets converted to shortcode when it's found in free HTML
67+
val htmlWithShortcode = "<figure class=\"wp-block-video\">[video controls=\"controls\" src=\"https://somedomain.com/wp-content/uploads/2017/05/VID-20170508-WA0011-1.mp4\"]</figure>"
68+
69+
Assert.assertEquals(htmlWithShortcode, editText.toPlainHtml())
70+
}
71+
}

0 commit comments

Comments
 (0)