diff --git a/aztec/src/main/java/org/wordpress/aztec/Html.java b/aztec/src/main/java/org/wordpress/aztec/Html.java index 1987b7f08..67145959e 100644 --- a/aztec/src/main/java/org/wordpress/aztec/Html.java +++ b/aztec/src/main/java/org/wordpress/aztec/Html.java @@ -363,6 +363,8 @@ private void handleStartTag(String tag, Attributes attributes, int nestingLevel) start(spannableStringBuilder, AztecTextFormat.FORMAT_CODE, attributes); } else if (tag.equalsIgnoreCase("mark")) { start(spannableStringBuilder, AztecTextFormat.FORMAT_MARK, attributes); + } else if (tag.equalsIgnoreCase("highlight")) { + start(spannableStringBuilder, AztecTextFormat.FORMAT_HIGHLIGHT, attributes); } else if (!UnknownHtmlSpan.Companion.getKNOWN_TAGS().contains(tag.toLowerCase())) { // Initialize a new "Unknown" node if (contentHandlerLevel == 0) { @@ -458,6 +460,8 @@ private void handleEndTag(String tag, int nestingLevel) { end(spannableStringBuilder, AztecTextFormat.FORMAT_CODE); } else if (tag.equalsIgnoreCase("mark")) { end(spannableStringBuilder, AztecTextFormat.FORMAT_MARK); + } else if (tag.equalsIgnoreCase("highlight")) { + end(spannableStringBuilder, AztecTextFormat.FORMAT_HIGHLIGHT); } } @@ -616,6 +620,9 @@ private static void end(SpannableStringBuilder text, AztecTextFormat textFormat) case FORMAT_MARK: span = (MarkSpan) getLast(text, MarkSpan.class); break; + case FORMAT_HIGHLIGHT: + span = (HighlightSpan) getLast(text, HighlightSpan.class); + break; default: throw new IllegalArgumentException("Style not supported"); } diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecTagHandler.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecTagHandler.kt index 38468b9a7..3498c7c29 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecTagHandler.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecTagHandler.kt @@ -295,6 +295,5 @@ class AztecTagHandler(val context: Context, val plugins: List = Ar private val VIDEO = "video" private val AUDIO = "audio" private val LINE = "hr" - private val MARK = "mark" } } diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index 62ca1070f..d81fcb3f5 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -1857,6 +1857,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_CODE, start, end) inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_BACKGROUND, start, end) inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_MARK, start, end) + inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_HIGHLIGHT, start, end) } fun removeBlockStylesFromRange(start: Int, end: Int, ignoreLineBounds: Boolean = false) { diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/formatting/InlineFormatter.kt b/aztec/src/main/kotlin/org/wordpress/aztec/formatting/InlineFormatter.kt index 276fbc0e4..287ed33e5 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/formatting/InlineFormatter.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/formatting/InlineFormatter.kt @@ -442,7 +442,7 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, private val h AztecTextFormat.FORMAT_CODE -> AztecCodeSpan(codeStyle) AztecTextFormat.FORMAT_BACKGROUND -> AztecBackgroundColorSpan(backgroundSpanColor ?: R.color.background) AztecTextFormat.FORMAT_HIGHLIGHT -> { - HighlightSpan(highlightStyle = highlightStyle, context = editor.context) + HighlightSpan.create(context = editor.context, defaultStyle = highlightStyle) } AztecTextFormat.FORMAT_MARK -> MarkSpan() else -> AztecStyleSpan(Typeface.NORMAL) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/spans/HighlightSpan.kt b/aztec/src/main/kotlin/org/wordpress/aztec/spans/HighlightSpan.kt index 85a90d1bd..58237aa64 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/spans/HighlightSpan.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/spans/HighlightSpan.kt @@ -6,16 +6,40 @@ import androidx.core.content.ContextCompat import org.wordpress.aztec.AztecAttributes import org.wordpress.aztec.R import org.wordpress.aztec.formatting.InlineFormatter +import org.wordpress.aztec.source.CssStyleFormatter +import org.wordpress.aztec.util.ColorConverter class HighlightSpan( override var attributes: AztecAttributes = AztecAttributes(), - highlightStyle: InlineFormatter.HighlightStyle = InlineFormatter.HighlightStyle(R.color.grey_lighten_10), - context: Context -) : BackgroundColorSpan(ContextCompat.getColor(context, highlightStyle.color)), IAztecInlineSpan { + val colorHex: Int +) : BackgroundColorSpan(colorHex), IAztecInlineSpan { + override var TAG = HIGHLIGHT_TAG - override var TAG = "highlight" companion object { + const val HIGHLIGHT_TAG = "highlight" + @JvmStatic - fun create(attributes: AztecAttributes, context: Context) = HighlightSpan(attributes = attributes, context = context) + @JvmOverloads + fun create(attributes: AztecAttributes = AztecAttributes(), + context: Context, + defaultStyle: InlineFormatter.HighlightStyle? = null + ) = HighlightSpan(attributes = attributes, + colorHex = buildColor(context, attributes, defaultStyle)) + + private fun buildColor(context: Context, attrs: AztecAttributes, defaultStyle: InlineFormatter.HighlightStyle?): Int { + return if (CssStyleFormatter.containsStyleAttribute( + attrs, + CssStyleFormatter.CSS_BACKGROUND_COLOR_ATTRIBUTE + ) + ) { + val att = CssStyleFormatter.getStyleAttribute(attrs, + CssStyleFormatter.CSS_BACKGROUND_COLOR_ATTRIBUTE) + return ColorConverter.getColorInt(att) + } else if (defaultStyle != null) { + ContextCompat.getColor(context, defaultStyle.color) + } else { + ContextCompat.getColor(context, R.color.grey_lighten_10) + } + } } }