Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable collapsing of whitespaces #905

Merged
merged 2 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions aztec/src/main/java/org/wordpress/aztec/Html.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static Spanned fromHtml(String source,
Context context,
List<IAztecPlugin> plugins,
List<String> ignoredTags) {
return fromHtml(source, null, context, plugins, ignoredTags);
return fromHtml(source, null, context, plugins, ignoredTags, true);
}

/**
Expand All @@ -176,7 +176,8 @@ private static class HtmlParser {
* <p>This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.
*/
public static Spanned fromHtml(String source, TagHandler tagHandler,
Context context, List<IAztecPlugin> plugins, List<String> ignoredTags) {
Context context, List<IAztecPlugin> plugins,
List<String> ignoredTags, boolean shouldIgnoreWhitespace) {

Parser parser = new Parser();
try {
Expand All @@ -195,7 +196,13 @@ public static Spanned fromHtml(String source, TagHandler tagHandler,
source = preprocessSource(source, plugins);

HtmlToSpannedConverter converter =
new HtmlToSpannedConverter(source, tagHandler, parser, context, plugins, ignoredTags);
new HtmlToSpannedConverter(source,
tagHandler,
parser,
context,
plugins,
ignoredTags,
shouldIgnoreWhitespace);

return converter.convert();
}
Expand Down Expand Up @@ -238,18 +245,21 @@ class HtmlToSpannedConverter implements org.xml.sax.ContentHandler, LexicalHandl
private Html.TagHandler tagHandler;
private Context context;
private List<String> ignoredTags;
private boolean shouldIgnoreWhitespace;

public HtmlToSpannedConverter(
String source, Html.TagHandler tagHandler,
Parser parser,
Context context, List<IAztecPlugin> plugins, List<String> ignoredTags) {
Context context, List<IAztecPlugin> plugins,
List<String> ignoredTags, boolean shouldIgnoreWhitespace) {
this.source = source;
this.plugins = plugins;
this.spannableStringBuilder = new SpannableStringBuilder();
this.tagHandler = tagHandler;
this.reader = parser;
this.context = context;
this.ignoredTags = ignoredTags;
this.shouldIgnoreWhitespace = shouldIgnoreWhitespace;
}

public Spanned convert() {
Expand Down Expand Up @@ -718,6 +728,12 @@ public void characters(char ch[], int start, int length) throws SAXException {
char c = ch[i + start];

if (!insidePreTag && !insideCodeTag && c == ' ' || c == '\n') {

if (c == ' ' && !shouldIgnoreWhitespace) {
sb.append(c);
continue;
}

char pred;
int len = sb.length();

Expand Down
9 changes: 6 additions & 3 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,21 @@ class AztecParser @JvmOverloads constructor(val plugins: List<IAztecPlugin> = li
val tidySource = tidy(source)

val spanned = SpannableString(Html.fromHtml(tidySource,
AztecTagHandler(context, plugins), context, plugins, ignoredTags))
AztecTagHandler(context, plugins), context, plugins, ignoredTags, true))

postprocessSpans(spanned)

return spanned
}

fun fromHtml(source: String, context: Context, shouldSkipTidying: Boolean = false): Spanned {
fun fromHtml(source: String,
context: Context,
shouldSkipTidying: Boolean = false,
shouldIgnoreWhitespace: Boolean = true): Spanned {
val tidySource = if (shouldSkipTidying) source else tidy(source)

val spanned = SpannableStringBuilder(Html.fromHtml(tidySource,
AztecTagHandler(context, plugins), context, plugins, ignoredTags))
AztecTagHandler(context, plugins), context, plugins, ignoredTags, shouldIgnoreWhitespace))

addVisualNewlinesToBlockElements(spanned)
markBlockElementsAsParagraphs(spanned)
Expand Down
6 changes: 5 additions & 1 deletion aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,10 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
return false
}

open fun shouldIgnoreWhitespace(): Boolean {
return true
}

override fun afterTextChanged(text: Editable) {
if (isTextChangedListenerDisabled()) {
subWatcherNestingLevel()
Expand Down Expand Up @@ -1168,7 +1172,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown

var cleanSource = CleaningUtils.cleanNestedBoldTags(source)
cleanSource = Format.removeSourceEditorFormatting(cleanSource, isInCalypsoMode, isInGutenbergMode)
builder.append(parser.fromHtml(cleanSource, context, shouldSkipTidying()))
builder.append(parser.fromHtml(cleanSource, context, shouldSkipTidying(), shouldIgnoreWhitespace()))

Format.preProcessSpannedText(builder, isInCalypsoMode)

Expand Down
10 changes: 10 additions & 0 deletions aztec/src/test/kotlin/org/wordpress/aztec/AztecParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1260,4 +1260,14 @@ class AztecParserTest : AndroidTestCase() {
val output = mParser.toHtml(span)
Assert.assertEquals(input, output)
}

@Test
@Throws(Exception::class)
fun parseHtmlToSpanToHtmlParagraphWithMultipleWhitespace_isNotEqual() {
val input = "<p> Hello There!</p>"
val inputAfterParser = "<p>Hello There!</p>"
val span = SpannableString(mParser.fromHtml(input, RuntimeEnvironment.application.applicationContext))
val output = mParser.toHtml(span)
Assert.assertEquals(output, inputAfterParser)
}
}