Skip to content

Commit 14c88d3

Browse files
authored
Add option to disable collapsing of whitespaces (#905)
1 parent 8402a2d commit 14c88d3

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

aztec/src/main/java/org/wordpress/aztec/Html.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public static Spanned fromHtml(String source,
154154
Context context,
155155
List<IAztecPlugin> plugins,
156156
List<String> ignoredTags) {
157-
return fromHtml(source, null, context, plugins, ignoredTags);
157+
return fromHtml(source, null, context, plugins, ignoredTags, true);
158158
}
159159

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

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

197198
HtmlToSpannedConverter converter =
198-
new HtmlToSpannedConverter(source, tagHandler, parser, context, plugins, ignoredTags);
199+
new HtmlToSpannedConverter(source,
200+
tagHandler,
201+
parser,
202+
context,
203+
plugins,
204+
ignoredTags,
205+
shouldIgnoreWhitespace);
199206

200207
return converter.convert();
201208
}
@@ -238,18 +245,21 @@ class HtmlToSpannedConverter implements org.xml.sax.ContentHandler, LexicalHandl
238245
private Html.TagHandler tagHandler;
239246
private Context context;
240247
private List<String> ignoredTags;
248+
private boolean shouldIgnoreWhitespace;
241249

242250
public HtmlToSpannedConverter(
243251
String source, Html.TagHandler tagHandler,
244252
Parser parser,
245-
Context context, List<IAztecPlugin> plugins, List<String> ignoredTags) {
253+
Context context, List<IAztecPlugin> plugins,
254+
List<String> ignoredTags, boolean shouldIgnoreWhitespace) {
246255
this.source = source;
247256
this.plugins = plugins;
248257
this.spannableStringBuilder = new SpannableStringBuilder();
249258
this.tagHandler = tagHandler;
250259
this.reader = parser;
251260
this.context = context;
252261
this.ignoredTags = ignoredTags;
262+
this.shouldIgnoreWhitespace = shouldIgnoreWhitespace;
253263
}
254264

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

720730
if (!insidePreTag && !insideCodeTag && c == ' ' || c == '\n') {
731+
732+
if (c == ' ' && !shouldIgnoreWhitespace) {
733+
sb.append(c);
734+
continue;
735+
}
736+
721737
char pred;
722738
int len = sb.length();
723739

aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,21 @@ class AztecParser @JvmOverloads constructor(val plugins: List<IAztecPlugin> = li
6868
val tidySource = tidy(source)
6969

7070
val spanned = SpannableString(Html.fromHtml(tidySource,
71-
AztecTagHandler(context, plugins), context, plugins, ignoredTags))
71+
AztecTagHandler(context, plugins), context, plugins, ignoredTags, true))
7272

7373
postprocessSpans(spanned)
7474

7575
return spanned
7676
}
7777

78-
fun fromHtml(source: String, context: Context, shouldSkipTidying: Boolean = false): Spanned {
78+
fun fromHtml(source: String,
79+
context: Context,
80+
shouldSkipTidying: Boolean = false,
81+
shouldIgnoreWhitespace: Boolean = true): Spanned {
7982
val tidySource = if (shouldSkipTidying) source else tidy(source)
8083

8184
val spanned = SpannableStringBuilder(Html.fromHtml(tidySource,
82-
AztecTagHandler(context, plugins), context, plugins, ignoredTags))
85+
AztecTagHandler(context, plugins), context, plugins, ignoredTags, shouldIgnoreWhitespace))
8386

8487
addVisualNewlinesToBlockElements(spanned)
8588
markBlockElementsAsParagraphs(spanned)

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,10 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
11181118
return false
11191119
}
11201120

1121+
open fun shouldIgnoreWhitespace(): Boolean {
1122+
return true
1123+
}
1124+
11211125
override fun afterTextChanged(text: Editable) {
11221126
if (isTextChangedListenerDisabled()) {
11231127
subWatcherNestingLevel()
@@ -1168,7 +1172,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
11681172

11691173
var cleanSource = CleaningUtils.cleanNestedBoldTags(source)
11701174
cleanSource = Format.removeSourceEditorFormatting(cleanSource, isInCalypsoMode, isInGutenbergMode)
1171-
builder.append(parser.fromHtml(cleanSource, context, shouldSkipTidying()))
1175+
builder.append(parser.fromHtml(cleanSource, context, shouldSkipTidying(), shouldIgnoreWhitespace()))
11721176

11731177
Format.preProcessSpannedText(builder, isInCalypsoMode)
11741178

aztec/src/test/kotlin/org/wordpress/aztec/AztecParserTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,4 +1260,14 @@ class AztecParserTest : AndroidTestCase() {
12601260
val output = mParser.toHtml(span)
12611261
Assert.assertEquals(input, output)
12621262
}
1263+
1264+
@Test
1265+
@Throws(Exception::class)
1266+
fun parseHtmlToSpanToHtmlParagraphWithMultipleWhitespace_isNotEqual() {
1267+
val input = "<p> Hello There!</p>"
1268+
val inputAfterParser = "<p>Hello There!</p>"
1269+
val span = SpannableString(mParser.fromHtml(input, RuntimeEnvironment.application.applicationContext))
1270+
val output = mParser.toHtml(span)
1271+
Assert.assertEquals(output, inputAfterParser)
1272+
}
12631273
}

0 commit comments

Comments
 (0)