Skip to content

Commit dae950f

Browse files
authored
Merge pull request #710 from wordpress-mobile/issue/709-first-class-support-for-strong
First class support for strong
2 parents 41bf0b2 + d6779c3 commit dae950f

File tree

16 files changed

+217
-96
lines changed

16 files changed

+217
-96
lines changed

app/src/androidTest/kotlin/org/wordpress/aztec/demo/tests/FormattingHistoryTests.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class FormattingHistoryTests : BaseHistoryTest() {
1818
@Test
1919
fun testAddBoldUndoRedo() {
2020
val word = "Testing"
21-
val html = "<b>$word</b>"
21+
val html = "<strong>$word</strong>"
2222
val editorPage = EditorPage()
2323

2424
// Add bold text, verify
@@ -56,8 +56,8 @@ class FormattingHistoryTests : BaseHistoryTest() {
5656
val word1 = "Testing"
5757
val word2 = " Bolder"
5858
val word3 = " History"
59-
val htmlSecond = "$word1<b>$word2</b>"
60-
val htmlFinal = "$word1<b>$word2</b>$word3"
59+
val htmlSecond = "$word1<strong>$word2</strong>"
60+
val htmlFinal = "$word1<strong>$word2</strong>$word3"
6161
val editorPage = EditorPage()
6262

6363
// Add first word - regular
@@ -109,7 +109,7 @@ class FormattingHistoryTests : BaseHistoryTest() {
109109
@Test
110110
fun testSelectToMakeBoldUndoRedo() {
111111
val text = "There's no crying in baseball!"
112-
val html = "<b>$text</b>"
112+
val html = "<strong>$text</strong>"
113113
val editorPage = EditorPage()
114114

115115
// Insert text snippet
@@ -307,4 +307,4 @@ class FormattingHistoryTests : BaseHistoryTest() {
307307
.toggleHtml()
308308
.verifyHTML(html)
309309
}
310-
}
310+
}

app/src/androidTest/kotlin/org/wordpress/aztec/demo/tests/MixedTextFormattingTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class MixedTextFormattingTests : BaseTest() {
2222
val text1 = "so"
2323
val text2 = "me "
2424
val text3 = "text "
25-
val regex = Regex("<b>$text1</b><i>$text2</i><[bi]><[bi]>$text3</[bi]></[bi]>")
25+
val regex = Regex("<strong>$text1</strong><i>$text2</i><(strong|i)><(strong|i)>$text3</(strong|i)></(strong|i)>")
2626

2727
EditorPage()
2828
.toggleBold()
@@ -43,7 +43,7 @@ class MixedTextFormattingTests : BaseTest() {
4343
val text1 = "a"
4444
val text2 = "b"
4545
val text3 = " "
46-
val html = "a <b>b</b>"
46+
val html = "a <strong>b</strong>"
4747

4848
EditorPage()
4949
.insertText(text1)
@@ -60,7 +60,7 @@ class MixedTextFormattingTests : BaseTest() {
6060
val text1 = "some"
6161
val text2 = "more"
6262
val text3 = "text"
63-
val html = "$text1<b>$text2</b>$text3"
63+
val html = "$text1<strong>$text2</strong>$text3"
6464

6565
EditorPage()
6666
.insertText(text1)

app/src/androidTest/kotlin/org/wordpress/aztec/demo/tests/SimpleTextFormattingTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class SimpleTextFormattingTests : BaseTest() {
1818
fun testSimpleBoldFormatting() {
1919
val text1 = "some"
2020
val text2 = "text"
21-
val html = "$text1<b>$text2</b>"
21+
val html = "$text1<strong>$text2</strong>"
2222

2323
EditorPage()
2424
.insertText(text1)

app/src/androidTest/kotlin/org/wordpress/aztec/demo/tests/ToolbarHighlightingTests.kt

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ToolbarHighlightingTests : BaseTest() {
7070
.insertText(text2)
7171
.checkBold(isNotChecked())
7272
.toggleHtml()
73-
.verifyHTML("<b>$text1</b>$text2")
73+
.verifyHTML("<strong>$text1</strong>$text2")
7474
}
7575

7676
// make sure that selected toolbar style in empty editor remains when soft keyboard is displayed
@@ -87,6 +87,36 @@ class ToolbarHighlightingTests : BaseTest() {
8787
.checkBold(isChecked())
8888
.insertText(text)
8989
.toggleHtml()
90-
.verifyHTML("<b>$text</b>")
90+
.verifyHTML("<strong>$text</strong>")
91+
}
92+
93+
@Test
94+
fun testHasBoldAndStrongFormatting() {
95+
val input = "<b>bold</b> normal strong"
96+
val html = "<b>bold</b> normal <strong>strong</strong>"
97+
98+
EditorPage()
99+
.toggleHtml()
100+
.insertHTML(input)
101+
.toggleHtml()
102+
.selectText(12, 18)
103+
.toggleBold()
104+
.toggleHtml()
105+
.verifyHTML(html)
106+
}
107+
108+
@Test
109+
fun testExpandBoldAndSetToStrongFormatting() {
110+
val input = "<b>bold</b> normal strong"
111+
val html = "<strong>bold normal strong</strong>"
112+
113+
EditorPage()
114+
.toggleHtml()
115+
.insertHTML(input)
116+
.toggleHtml()
117+
.selectAllText()
118+
.toggleBold()
119+
.toggleHtml()
120+
.verifyHTML(html)
91121
}
92122
}

app/src/main/kotlin/org/wordpress/aztec/demo/MediaToolbarAction.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.wordpress.aztec.toolbar.IToolbarAction
66
import org.wordpress.aztec.toolbar.ToolbarActionType
77

88
enum class MediaToolbarAction constructor(override val buttonId: Int, override val actionType: ToolbarActionType,
9-
override val textFormat: ITextFormat) : IToolbarAction {
10-
GALLERY(R.id.media_bar_button_gallery, ToolbarActionType.OTHER, AztecTextFormat.FORMAT_NONE),
11-
CAMERA(R.id.media_bar_button_camera, ToolbarActionType.OTHER, AztecTextFormat.FORMAT_NONE)
12-
}
9+
override val textFormats: Set<ITextFormat> = setOf()) : IToolbarAction {
10+
GALLERY(R.id.media_bar_button_gallery, ToolbarActionType.OTHER, setOf(AztecTextFormat.FORMAT_NONE)),
11+
CAMERA(R.id.media_bar_button_camera, ToolbarActionType.OTHER, setOf(AztecTextFormat.FORMAT_NONE))
12+
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.wordpress.aztec.spans.AztecStyleBoldSpan;
4747
import org.wordpress.aztec.spans.AztecStyleCiteSpan;
4848
import org.wordpress.aztec.spans.AztecStyleItalicSpan;
49+
import org.wordpress.aztec.spans.AztecStyleStrongSpan;
4950
import org.wordpress.aztec.spans.AztecSubscriptSpan;
5051
import org.wordpress.aztec.spans.AztecSuperscriptSpan;
5152
import org.wordpress.aztec.spans.AztecTypefaceMonospaceSpan;
@@ -310,7 +311,7 @@ private void handleStartTag(String tag, Attributes attributes, int nestingLevel)
310311
} else if (tag.equalsIgnoreCase("aztec_cursor")) {
311312
handleCursor(spannableStringBuilder);
312313
} else if (tag.equalsIgnoreCase("strong")) {
313-
start(spannableStringBuilder, AztecTextFormat.FORMAT_BOLD, attributes);
314+
start(spannableStringBuilder, AztecTextFormat.FORMAT_STRONG, attributes);
314315
} else if (tag.equalsIgnoreCase("b")) {
315316
start(spannableStringBuilder, AztecTextFormat.FORMAT_BOLD, attributes);
316317
} else if (tag.equalsIgnoreCase("em")) {
@@ -403,7 +404,7 @@ private void handleEndTag(String tag, int nestingLevel) {
403404
if (tag.equalsIgnoreCase("br")) {
404405
handleBr(spannableStringBuilder);
405406
} else if (tag.equalsIgnoreCase("strong")) {
406-
end(spannableStringBuilder, AztecTextFormat.FORMAT_BOLD);
407+
end(spannableStringBuilder, AztecTextFormat.FORMAT_STRONG);
407408
} else if (tag.equalsIgnoreCase("b")) {
408409
end(spannableStringBuilder, AztecTextFormat.FORMAT_BOLD);
409410
} else if (tag.equalsIgnoreCase("em")) {
@@ -488,6 +489,9 @@ private static void start(SpannableStringBuilder text, AztecTextFormat textForma
488489
case FORMAT_BOLD:
489490
newSpan = new AztecStyleBoldSpan(attributes);
490491
break;
492+
case FORMAT_STRONG:
493+
newSpan = new AztecStyleStrongSpan(attributes);
494+
break;
491495
case FORMAT_ITALIC:
492496
newSpan = new AztecStyleItalicSpan(attributes);
493497
break;
@@ -537,6 +541,9 @@ private static void end(SpannableStringBuilder text, AztecTextFormat textFormat)
537541
case FORMAT_BOLD:
538542
span = (AztecStyleBoldSpan) getLast(text, AztecStyleBoldSpan.class);
539543
break;
544+
case FORMAT_STRONG:
545+
span = (AztecStyleStrongSpan) getLast(text, AztecStyleStrongSpan.class);
546+
break;
540547
case FORMAT_ITALIC:
541548
span = (AztecStyleItalicSpan) getLast(text, AztecStyleItalicSpan.class);
542549
break;

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import org.wordpress.aztec.spans.IAztecBlockSpan
8585
import org.wordpress.aztec.spans.UnknownClickableSpan
8686
import org.wordpress.aztec.spans.UnknownHtmlSpan
8787
import org.wordpress.aztec.toolbar.IAztecToolbar
88+
import org.wordpress.aztec.toolbar.ToolbarAction
8889
import org.wordpress.aztec.util.AztecLog
8990
import org.wordpress.aztec.util.InstanceStateUtils
9091
import org.wordpress.aztec.util.SpanWrapper
@@ -826,7 +827,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
826827
}
827828

828829
plugins.filter { it is IToolbarButton }
829-
.map { (it as IToolbarButton).action.textFormat }
830+
.flatMap { (it as IToolbarButton).action.textFormats }
830831
.forEach {
831832
if (contains(it, newSelStart, newSelEnd)) {
832833
styles.add(it)
@@ -868,12 +869,13 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
868869
AztecTextFormat.FORMAT_HEADING_5,
869870
AztecTextFormat.FORMAT_HEADING_6,
870871
AztecTextFormat.FORMAT_PREFORMAT -> blockFormatter.toggleHeading(textFormat)
871-
AztecTextFormat.FORMAT_BOLD,
872872
AztecTextFormat.FORMAT_ITALIC,
873873
AztecTextFormat.FORMAT_CITE,
874874
AztecTextFormat.FORMAT_UNDERLINE,
875875
AztecTextFormat.FORMAT_STRIKETHROUGH,
876876
AztecTextFormat.FORMAT_CODE -> inlineFormatter.toggle(textFormat)
877+
AztecTextFormat.FORMAT_BOLD,
878+
AztecTextFormat.FORMAT_STRONG -> inlineFormatter.toggleAny(ToolbarAction.BOLD.textFormats)
877879
AztecTextFormat.FORMAT_UNORDERED_LIST -> blockFormatter.toggleUnorderedList()
878880
AztecTextFormat.FORMAT_ORDERED_LIST -> blockFormatter.toggleOrderedList()
879881
AztecTextFormat.FORMAT_ALIGN_LEFT,
@@ -882,7 +884,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
882884
AztecTextFormat.FORMAT_QUOTE -> blockFormatter.toggleQuote()
883885
AztecTextFormat.FORMAT_HORIZONTAL_RULE -> lineBlockFormatter.applyHorizontalRule()
884886
else -> {
885-
plugins.filter { it is IToolbarButton && textFormat == it.action.textFormat }
887+
plugins.filter { it is IToolbarButton && it.action.textFormats.contains(textFormat) }
886888
.map { it as IToolbarButton }
887889
.forEach { it.toggle() }
888890
}
@@ -898,6 +900,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
898900
AztecTextFormat.FORMAT_HEADING_5,
899901
AztecTextFormat.FORMAT_HEADING_6 -> return lineBlockFormatter.containsHeading(format, selStart, selEnd)
900902
AztecTextFormat.FORMAT_BOLD,
903+
AztecTextFormat.FORMAT_STRONG,
901904
AztecTextFormat.FORMAT_ITALIC,
902905
AztecTextFormat.FORMAT_CITE,
903906
AztecTextFormat.FORMAT_UNDERLINE,
@@ -1282,6 +1285,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
12821285

12831286
fun removeInlineStylesFromRange(start: Int, end: Int) {
12841287
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_BOLD, start, end)
1288+
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_STRONG, start, end)
12851289
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_ITALIC, start, end)
12861290
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_CITE, start, end)
12871291
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_STRIKETHROUGH, start, end)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum class AztecTextFormat : ITextFormat {
1414
FORMAT_UNORDERED_LIST,
1515
FORMAT_ORDERED_LIST,
1616
FORMAT_BOLD,
17+
FORMAT_STRONG,
1718
FORMAT_ITALIC,
1819
FORMAT_CITE,
1920
FORMAT_UNDERLINE,

aztec/src/main/kotlin/org/wordpress/aztec/formatting/InlineFormatter.kt

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.wordpress.aztec.spans.AztecStrikethroughSpan
1616
import org.wordpress.aztec.spans.AztecStyleBoldSpan
1717
import org.wordpress.aztec.spans.AztecStyleCiteSpan
1818
import org.wordpress.aztec.spans.AztecStyleItalicSpan
19+
import org.wordpress.aztec.spans.AztecStyleStrongSpan
1920
import org.wordpress.aztec.spans.AztecStyleSpan
2021
import org.wordpress.aztec.spans.AztecUnderlineSpan
2122
import org.wordpress.aztec.spans.IAztecInlineSpan
@@ -38,6 +39,17 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle) : AztecFormat
3839
}
3940
}
4041

42+
/**
43+
* Removes all formats in the list but if none found, applies the first one
44+
*/
45+
fun toggleAny(textFormats: Set<ITextFormat>) {
46+
if (!textFormats
47+
.filter { containsInlineStyle(it) }
48+
.fold(false, { found, containedTextFormat -> removeInlineStyle(containedTextFormat); true })) {
49+
applyInlineStyle(textFormats.first())
50+
}
51+
}
52+
4153
fun handleInlineStyling(textChangedEvent: TextChangedEvent) {
4254
if (textChangedEvent.isEndOfBufferMarker()) return
4355

@@ -51,6 +63,7 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle) : AztecFormat
5163
for (item in editor.selectedStyles) {
5264
when (item) {
5365
AztecTextFormat.FORMAT_BOLD,
66+
AztecTextFormat.FORMAT_STRONG,
5467
AztecTextFormat.FORMAT_ITALIC,
5568
AztecTextFormat.FORMAT_CITE,
5669
AztecTextFormat.FORMAT_STRIKETHROUGH,
@@ -149,14 +162,13 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle) : AztecFormat
149162
}
150163
}
151164

152-
// if we already have same span within selection - reuse it by changing it's bounds
165+
// if we already have same span within selection - reuse its attributes
153166
if (existingSpanOfSameStyle != null) {
154167
editableText.removeSpan(existingSpanOfSameStyle)
155-
(existingSpanOfSameStyle as IAztecInlineSpan).attributes = attrs
156-
applySpan(existingSpanOfSameStyle as IAztecInlineSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
157-
} else {
158-
applySpan(spanToApply, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
168+
spanToApply.attributes = attrs
159169
}
170+
171+
applySpan(spanToApply, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
160172
}
161173

162174
joinStyleSpans(start, end)
@@ -184,6 +196,7 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle) : AztecFormat
184196
fun spanToTextFormat(span: IAztecInlineSpan): ITextFormat? {
185197
when (span::class.java) {
186198
AztecStyleBoldSpan::class.java -> return AztecTextFormat.FORMAT_BOLD
199+
AztecStyleStrongSpan::class.java -> return AztecTextFormat.FORMAT_STRONG
187200
AztecStyleItalicSpan::class.java -> return AztecTextFormat.FORMAT_ITALIC
188201
AztecStyleCiteSpan::class.java -> return AztecTextFormat.FORMAT_CITE
189202
AztecStrikethroughSpan::class.java -> return AztecTextFormat.FORMAT_STRIKETHROUGH
@@ -234,16 +247,12 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle) : AztecFormat
234247
}
235248

236249
fun isSameInlineSpanType(firstSpan: IAztecInlineSpan, secondSpan: IAztecInlineSpan): Boolean {
237-
if (firstSpan.javaClass == secondSpan.javaClass) {
238-
// special check for StyleSpan
239-
if (firstSpan is StyleSpan && secondSpan is StyleSpan) {
240-
return firstSpan.style == secondSpan.style
241-
} else {
242-
return true
243-
}
250+
// special check for StyleSpans
251+
if (firstSpan is StyleSpan && secondSpan is StyleSpan) {
252+
return firstSpan.style == secondSpan.style
244253
}
245254

246-
return false
255+
return firstSpan.javaClass == secondSpan.javaClass
247256
}
248257

249258
// TODO: Check if there is more efficient way to tidy spans
@@ -330,6 +339,7 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle) : AztecFormat
330339
fun makeInlineSpan(textFormat: ITextFormat): IAztecInlineSpan {
331340
when (textFormat) {
332341
AztecTextFormat.FORMAT_BOLD -> return AztecStyleBoldSpan()
342+
AztecTextFormat.FORMAT_STRONG -> return AztecStyleStrongSpan()
333343
AztecTextFormat.FORMAT_ITALIC -> return AztecStyleItalicSpan()
334344
AztecTextFormat.FORMAT_CITE -> return AztecStyleCiteSpan()
335345
AztecTextFormat.FORMAT_STRIKETHROUGH -> return AztecStrikethroughSpan()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.wordpress.aztec.spans
2+
3+
import android.graphics.Typeface
4+
import org.wordpress.aztec.AztecAttributes
5+
6+
class AztecStyleStrongSpan(attributes: AztecAttributes = AztecAttributes())
7+
: AztecStyleSpan(Typeface.BOLD, attributes) {
8+
9+
override val TAG by lazy {
10+
when (style) {
11+
Typeface.BOLD -> {
12+
return@lazy "strong"
13+
}
14+
}
15+
throw IllegalArgumentException()
16+
}
17+
}

0 commit comments

Comments
 (0)