From 12b2062945429a48011d7aee76e84578b8c08e63 Mon Sep 17 00:00:00 2001 From: dhiaspaner <78903066+dhiaspaner@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:44:01 +0100 Subject: [PATCH 1/2] Add dir text direction html attribute support --- .../com/mohamedrejeb/richeditor/parser/html/CssEncoder.kt | 5 +++-- .../richeditor/parser/html/RichTextStateHtmlParser.kt | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/CssEncoder.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/CssEncoder.kt index 01b3e85f..7b601b77 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/CssEncoder.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/CssEncoder.kt @@ -140,10 +140,11 @@ internal object CssEncoder { * @param cssStyleMap the CSS style map to convert. * @return the converted [ParagraphStyle]. */ - internal fun parseCssStyleMapToParagraphStyle(cssStyleMap: Map): ParagraphStyle { + internal fun parseCssStyleMapToParagraphStyle(cssStyleMap: Map,attributes: Map): ParagraphStyle { + val direction = cssStyleMap["direction"] ?: attributes["dir"] return ParagraphStyle( textAlign = cssStyleMap["text-align"]?.let { parseCssTextAlign(it) } ?: TextAlign.Unspecified, - textDirection = cssStyleMap["direction"]?.let { parseCssTextDirection(it) } ?: TextDirection.Unspecified, + textDirection = direction?.let { parseCssTextDirection(it) } ?: TextDirection.Unspecified, lineHeight = cssStyleMap["line-height"]?.let { parseCssLineHeight(it) } ?: TextUnit.Unspecified, textIndent = cssStyleMap["text-indent"]?.let { parseCssTextIndent(it) }, ) diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/RichTextStateHtmlParser.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/RichTextStateHtmlParser.kt index a497a635..b3ef8027 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/RichTextStateHtmlParser.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/RichTextStateHtmlParser.kt @@ -105,7 +105,7 @@ internal object RichTextStateHtmlParser : RichTextStateParser { val paragraphType = encodeHtmlElementToRichParagraphType(lastOpenedTag) currentRichParagraph.type = paragraphType - val cssParagraphStyle = CssEncoder.parseCssStyleMapToParagraphStyle(cssStyleMap) + val cssParagraphStyle = CssEncoder.parseCssStyleMapToParagraphStyle(cssStyleMap,attributes) currentRichParagraph.paragraphStyle = currentRichParagraph.paragraphStyle.merge(cssParagraphStyle) } @@ -120,7 +120,7 @@ internal object RichTextStateHtmlParser : RichTextStateParser { if (name == "li" && lastOpenedTag != null) { paragraphType = encodeHtmlElementToRichParagraphType(lastOpenedTag) } - val cssParagraphStyle = CssEncoder.parseCssStyleMapToParagraphStyle(cssStyleMap) + val cssParagraphStyle = CssEncoder.parseCssStyleMapToParagraphStyle(cssStyleMap,attributes) newRichParagraph.paragraphStyle = newRichParagraph.paragraphStyle.merge(cssParagraphStyle) newRichParagraph.type = paragraphType From ecdb8018f9e366464a2d4b35d61d2cda2b01e71f Mon Sep 17 00:00:00 2001 From: dhiaspaner <78903066+dhiaspaner@users.noreply.github.com> Date: Tue, 7 Jan 2025 09:15:11 +0100 Subject: [PATCH 2/2] add test to parseCssStyleMapToParagraphStyle function to that check text direction --- .../mohamedrejeb/richeditor/parser/html/CssEncoder.kt | 5 ++++- .../richeditor/parser/html/RichTextStateHtmlParser.kt | 4 ++-- .../parser/html/CssEncoderTest.kt | 11 +++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/CssEncoder.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/CssEncoder.kt index 7b601b77..db31a716 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/CssEncoder.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/CssEncoder.kt @@ -140,7 +140,10 @@ internal object CssEncoder { * @param cssStyleMap the CSS style map to convert. * @return the converted [ParagraphStyle]. */ - internal fun parseCssStyleMapToParagraphStyle(cssStyleMap: Map,attributes: Map): ParagraphStyle { + internal fun parseCssStyleMapToParagraphStyle( + cssStyleMap: Map, + attributes: Map + ): ParagraphStyle { val direction = cssStyleMap["direction"] ?: attributes["dir"] return ParagraphStyle( textAlign = cssStyleMap["text-align"]?.let { parseCssTextAlign(it) } ?: TextAlign.Unspecified, diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/RichTextStateHtmlParser.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/RichTextStateHtmlParser.kt index b3ef8027..5c4328e1 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/RichTextStateHtmlParser.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/html/RichTextStateHtmlParser.kt @@ -105,7 +105,7 @@ internal object RichTextStateHtmlParser : RichTextStateParser { val paragraphType = encodeHtmlElementToRichParagraphType(lastOpenedTag) currentRichParagraph.type = paragraphType - val cssParagraphStyle = CssEncoder.parseCssStyleMapToParagraphStyle(cssStyleMap,attributes) + val cssParagraphStyle = CssEncoder.parseCssStyleMapToParagraphStyle(cssStyleMap, attributes) currentRichParagraph.paragraphStyle = currentRichParagraph.paragraphStyle.merge(cssParagraphStyle) } @@ -120,7 +120,7 @@ internal object RichTextStateHtmlParser : RichTextStateParser { if (name == "li" && lastOpenedTag != null) { paragraphType = encodeHtmlElementToRichParagraphType(lastOpenedTag) } - val cssParagraphStyle = CssEncoder.parseCssStyleMapToParagraphStyle(cssStyleMap,attributes) + val cssParagraphStyle = CssEncoder.parseCssStyleMapToParagraphStyle(cssStyleMap, attributes) newRichParagraph.paragraphStyle = newRichParagraph.paragraphStyle.merge(cssParagraphStyle) newRichParagraph.type = paragraphType diff --git a/richeditor-compose/src/commonTest/kotlin/com.mohamedrejeb.richeditor/parser/html/CssEncoderTest.kt b/richeditor-compose/src/commonTest/kotlin/com.mohamedrejeb.richeditor/parser/html/CssEncoderTest.kt index 29104b5f..ecc30036 100644 --- a/richeditor-compose/src/commonTest/kotlin/com.mohamedrejeb.richeditor/parser/html/CssEncoderTest.kt +++ b/richeditor-compose/src/commonTest/kotlin/com.mohamedrejeb.richeditor/parser/html/CssEncoderTest.kt @@ -382,4 +382,15 @@ class CssEncoderTest { CssEncoder.parseCssTextIndent(textIndent4), ) } + + @Test + fun testParseCssStyleMapToParagraphStyle() { + val paragraphStyle = CssEncoder.parseCssStyleMapToParagraphStyle( + cssStyleMap = mapOf("direction" to "ltr"), + attributes = mapOf("dir" to "rtl") + ) + + assertEquals(TextDirection.Ltr, paragraphStyle.textDirection) + } + } \ No newline at end of file