Skip to content

Commit 7b38c34

Browse files
authored
Allow preformatted background alpha and tidying to be set from child classes (#869)
1 parent 07d3744 commit 7b38c34

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class AztecParser @JvmOverloads constructor(val plugins: List<IAztecPlugin> = li
7575
return spanned
7676
}
7777

78-
fun fromHtml(source: String, context: Context): Spanned {
79-
val tidySource = tidy(source)
78+
fun fromHtml(source: String, context: Context, shouldSkipTidying: Boolean = false): Spanned {
79+
val tidySource = if (shouldSkipTidying) source else tidy(source)
8080

8181
val spanned = SpannableStringBuilder(Html.fromHtml(tidySource,
8282
AztecTagHandler(context, plugins), context, plugins, ignoredTags))
@@ -90,7 +90,8 @@ class AztecParser @JvmOverloads constructor(val plugins: List<IAztecPlugin> = li
9090
return spanned
9191
}
9292

93-
fun toHtml(text: Spanned, withCursor: Boolean = false): String {
93+
@JvmOverloads
94+
fun toHtml(text: Spanned, withCursor: Boolean = false, shouldSkipTidying: Boolean = false): String {
9495
val out = StringBuilder()
9596

9697
val data = SpannableStringBuilder(text)
@@ -107,7 +108,8 @@ class AztecParser @JvmOverloads constructor(val plugins: List<IAztecPlugin> = li
107108
}
108109

109110
withinHtml(out, data)
110-
val html = postprocessHtml(tidy(out.toString()))
111+
val tidyOut = if (shouldSkipTidying) out.toString() else tidy(out.toString())
112+
val html = postprocessHtml(tidyOut)
111113
return html
112114
}
113115

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import android.annotation.SuppressLint
2121
import android.content.ClipData
2222
import android.content.ClipboardManager
2323
import android.content.Context
24+
import android.content.res.TypedArray
2425
import android.graphics.Bitmap
2526
import android.graphics.Canvas
2627
import android.graphics.drawable.BitmapDrawable
@@ -417,7 +418,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
417418
BlockFormatter.HeaderStyle(verticalHeadingMargin),
418419
BlockFormatter.PreformatStyle(
419420
styles.getColor(R.styleable.AztecText_preformatBackground, 0),
420-
styles.getFraction(R.styleable.AztecText_preformatBackgroundAlpha, 1, 1, 0f),
421+
getPreformatBackgroundAlpha(styles),
421422
styles.getColor(R.styleable.AztecText_preformatColor, 0),
422423
verticalParagraphMargin)
423424
)
@@ -709,6 +710,12 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
709710
return inputConnectionRef?.get()!!
710711
}
711712

713+
// We are exposing this method in order to allow subclasses to set their own alpha value
714+
// for preformatted background
715+
open fun getPreformatBackgroundAlpha(styles: TypedArray): Float {
716+
return styles.getFraction(R.styleable.AztecText_preformatBackgroundAlpha, 1, 1, 0f)
717+
}
718+
712719
override fun onRestoreInstanceState(state: Parcelable?) {
713720
disableTextChangedListener()
714721

@@ -1144,6 +1151,10 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
11441151
}
11451152
}
11461153

1154+
open fun shouldSkipTidying(): Boolean {
1155+
return false
1156+
}
1157+
11471158
override fun afterTextChanged(text: Editable) {
11481159
if (isTextChangedListenerDisabled()) {
11491160
subWatcherNestingLevel()
@@ -1192,7 +1203,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
11921203

11931204
var cleanSource = CleaningUtils.cleanNestedBoldTags(source)
11941205
cleanSource = Format.removeSourceEditorFormatting(cleanSource, isInCalypsoMode, isInGutenbergMode)
1195-
builder.append(parser.fromHtml(cleanSource, context))
1206+
builder.append(parser.fromHtml(cleanSource, context, shouldSkipTidying()))
11961207

11971208
Format.preProcessSpannedText(builder, isInCalypsoMode)
11981209

@@ -1357,7 +1368,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
13571368

13581369
Format.postProcessSpannedText(output, isInCalypsoMode)
13591370

1360-
return EndOfBufferMarkerAdder.removeEndOfTextMarker(parser.toHtml(output, withCursorTag))
1371+
return EndOfBufferMarkerAdder.removeEndOfTextMarker(parser.toHtml(output, withCursorTag, shouldSkipTidying()))
13611372
}
13621373

13631374
// default behavior returns formatted HTML from this text

0 commit comments

Comments
 (0)