Skip to content

Commit

Permalink
Merge pull request #484 from dhiaspaner/add-dir-text-direction-html-a…
Browse files Browse the repository at this point in the history
…ttribute-support

Add dir text direction html attribute support
  • Loading branch information
MohamedRejeb authored Jan 7, 2025
2 parents 2846548 + ecdb801 commit 57b9a91
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,14 @@ internal object CssEncoder {
* @param cssStyleMap the CSS style map to convert.
* @return the converted [ParagraphStyle].
*/
internal fun parseCssStyleMapToParagraphStyle(cssStyleMap: Map<String, String>): ParagraphStyle {
internal fun parseCssStyleMapToParagraphStyle(
cssStyleMap: Map<String, String>,
attributes: Map<String,String>
): 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) },
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ internal object RichTextStateHtmlParser : RichTextStateParser<String> {
val paragraphType = encodeHtmlElementToRichParagraphType(lastOpenedTag)
currentRichParagraph.type = paragraphType

val cssParagraphStyle = CssEncoder.parseCssStyleMapToParagraphStyle(cssStyleMap)
val cssParagraphStyle = CssEncoder.parseCssStyleMapToParagraphStyle(cssStyleMap, attributes)
currentRichParagraph.paragraphStyle = currentRichParagraph.paragraphStyle.merge(cssParagraphStyle)
}

Expand All @@ -120,7 +120,7 @@ internal object RichTextStateHtmlParser : RichTextStateParser<String> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}

0 comments on commit 57b9a91

Please sign in to comment.