Skip to content

Commit

Permalink
Merge pull request #273 from wordpress-mobile/issue/260-span-tag-space
Browse files Browse the repository at this point in the history
Issue/260 span tag space
  • Loading branch information
khaykov authored Feb 26, 2017
2 parents 9069cf5 + 4ff3ae8 commit 701950a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
11 changes: 2 additions & 9 deletions aztec/src/main/kotlin/org/wordpress/aztec/source/Format.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package org.wordpress.aztec.source

import android.support.v4.util.ArrayMap
import org.apache.commons.lang.StringEscapeUtils
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import java.util.regex.Pattern
import org.jsoup.nodes.Entities.EscapeMode
import org.jsoup.safety.Cleaner
import org.jsoup.safety.Whitelist



object Format {

// list of block elements
private val block = "div|span|br|blockquote|ul|ol|li|p|h1|h2|h3|h4|h5|h6|iframe"
private val block = "div|br|blockquote|ul|ol|li|p|h1|h2|h3|h4|h5|h6|iframe"

private val iframePlaceholder = "iframe-replacement-0x0"

Expand All @@ -25,7 +18,7 @@ object Format {
html = replaceAll(html, iframePlaceholder, "iframe")

//remove newline around all non block elements
val newlineToTheLeft = replaceAll(html, "(?<!</?($block)>)\n\\s*?<((?!/?($block)).*?)>", "<$2>")
val newlineToTheLeft = replaceAll(html, "(?<!</?($block)>)\n<((?!/?($block)).*?)>", "<$2>")
val newlineToTheRight = replaceAll(newlineToTheLeft, "<(/?(?!$block).)>\n(?!</?($block)>)", "<$1>")
var fixBrNewlines = replaceAll(newlineToTheRight, "([\t ]*)(<br>)(?!\n)", "$1$2\n$1")
fixBrNewlines = replaceAll(fixBrNewlines, ">([\t ]*)(<br>)", ">\n$1$2")
Expand Down
34 changes: 25 additions & 9 deletions aztec/src/test/kotlin/org/wordpress/aztec/HtmlFormattingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import org.wordpress.aztec.source.Format

@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class, sdk = intArrayOf(23))
class HtmlFormattingTest() : AndroidTestCase() {
class HtmlFormattingTest : AndroidTestCase() {

private var parser = AztecParser()


private val HTML_LINE_BREAKS = "HI<br><br><br><br><br><br>BYE"

private val HTML_NESTED =
Expand All @@ -31,11 +30,11 @@ class HtmlFormattingTest() : AndroidTestCase() {
"<div class=\"fifth\"></div>" +
"</div>" +
"<span class=\"second last\"></span>" +
"<span></span><div><div><div><span></span></div></div></div><div></div>" +
"<div><span></span><div><div><span></span></div></div></div><div></div>" +
"</div>" +
"<br><br>"

private val HTML_MIXED =
private val HTML_MIXED_WITH_NEWLINES =
"\n\n<span><i>Italic</i></span>\n\n" +
"<b>Bold</b><br>" +
"\t<div class=\"first\">" +
Expand All @@ -52,9 +51,9 @@ class HtmlFormattingTest() : AndroidTestCase() {
"</div>" +
"<br>"

private val HTML_MIXED_NO_WS =
private val HTML_MIXED_WITHOUT_NEWLINES =
"<span><i>Italic</i></span>" +
"<b>Bold</b><br>" +
" <b>Bold</b><br>" +
"<div class=\"first\">" +
"<a href=\"https://github.com/wordpress-mobile/WordPress-Aztec-Android\">Link</a>" +
"<div class=\"second\">" +
Expand All @@ -69,6 +68,9 @@ class HtmlFormattingTest() : AndroidTestCase() {
"</div>" +
"<br>"

private val HTML_BLOCK_WITH_NEWLINES = "\n\n<div>Division</div>\n\n"
private val HTML_BLOCK_WITHOUT_NEWLINES = "<div>Division</div>"

/**
* Initialize variables.
*/
Expand Down Expand Up @@ -113,9 +115,23 @@ class HtmlFormattingTest() : AndroidTestCase() {
@Test
@Throws(Exception::class)
fun formatMixedHtml() {
val input = HTML_MIXED
val input = HTML_MIXED_WITH_NEWLINES
val span = SpannableString(parser.fromHtml(input, null, null, context))
val output = Format.clearFormatting(Format.addFormatting(parser.toHtml(span)))
Assert.assertEquals(HTML_MIXED_WITHOUT_NEWLINES, output)
}

/**
* Test block conversion from HTML to visual mode with newlines.
*
* @throws Exception
*/
@Test
@Throws(Exception::class)
fun formatNewlines() {
val input = HTML_BLOCK_WITH_NEWLINES
val span = SpannableString(parser.fromHtml(input, null, null, context))
val output = Format.clearFormatting(Format.addFormatting(parser.toHtml(span)))
Assert.assertEquals(HTML_MIXED_NO_WS, output)
Assert.assertEquals(HTML_BLOCK_WITHOUT_NEWLINES, output)
}
}
}

0 comments on commit 701950a

Please sign in to comment.