From 0d4160ee488afe82a3c19e58b37cf9a35ffc0258 Mon Sep 17 00:00:00 2001 From: Toni Sevener Date: Wed, 6 Dec 2023 11:08:52 -0600 Subject: [PATCH 1/3] Bug fix - final character selection state before closing bold or italic --- .../WKSourceEditorFormatterBoldItalics.m | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterBoldItalics.m b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterBoldItalics.m index 763e2947cd0..38d636876ac 100644 --- a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterBoldItalics.m +++ b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterBoldItalics.m @@ -282,10 +282,19 @@ - (BOOL)attributedString:(NSMutableAttributedString *)attributedString isBoldInR if (range.length == 0) { if (attributedString.length > range.location) { + NSLog(@"%@", [attributedString attributesAtIndex:range.location effectiveRange:nil]); NSDictionary *attrs = [attributedString attributesAtIndex:range.location effectiveRange:nil]; if (attrs[WKSourceEditorCustomKeyFontBoldItalics] != nil || attrs[WKSourceEditorCustomKeyFontBold] != nil) { isBold = YES; + } else { + // Edge case, check previous character if we are up against a closing bold or italic + if (attrs[WKSourceEditorCustomKeyColorOrange]) { + attrs = [attributedString attributesAtIndex:range.location - 1 effectiveRange:nil]; + if (attrs[WKSourceEditorCustomKeyFontBoldItalics] != nil || attrs[WKSourceEditorCustomKeyFontBold] != nil) { + isBold = YES; + } + } } } @@ -312,6 +321,14 @@ - (BOOL)attributedString:(NSMutableAttributedString *)attributedString isItalics if (attrs[WKSourceEditorCustomKeyFontBoldItalics] != nil || attrs[WKSourceEditorCustomKeyFontItalics] != nil) { isItalics = YES; + } else { + // Edge case, check previous character if we are up against a closing bold or italic + if (attrs[WKSourceEditorCustomKeyColorOrange]) { + attrs = [attributedString attributesAtIndex:range.location - 1 effectiveRange:nil]; + if (attrs[WKSourceEditorCustomKeyFontBoldItalics] != nil || attrs[WKSourceEditorCustomKeyFontItalics] != nil) { + isItalics = YES; + } + } } } From 5fe71e6af1e9b7ed714fb8fba057f3753703d72d Mon Sep 17 00:00:00 2001 From: Toni Sevener Date: Wed, 6 Dec 2023 11:23:58 -0600 Subject: [PATCH 2/3] Add tests --- ...KSourceEditorTextFrameworkMediatorTests.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Components/Tests/ComponentsTests/WKSourceEditorTextFrameworkMediatorTests.swift b/Components/Tests/ComponentsTests/WKSourceEditorTextFrameworkMediatorTests.swift index 5f01b4b4c71..b473f2c83f3 100644 --- a/Components/Tests/ComponentsTests/WKSourceEditorTextFrameworkMediatorTests.swift +++ b/Components/Tests/ComponentsTests/WKSourceEditorTextFrameworkMediatorTests.swift @@ -61,6 +61,22 @@ final class WKSourceEditorTextFrameworkMediatorTests: XCTestCase { XCTAssertFalse(selectionStates9.isItalics) } + func testClosingBoldSelectionStateCursor() throws { + let text = "One '''Two''' Three" + mediator.textView.attributedText = NSAttributedString(string: text) + + let selectionStates = mediator.selectionState(selectedDocumentRange: NSRange(location: 10, length: 0)) + XCTAssertTrue(selectionStates.isBold) + } + + func testClosingItalicsSelectionStateCursor() throws { + let text = "One ''Two'' Three" + mediator.textView.attributedText = NSAttributedString(string: text) + + let selectionStates = mediator.selectionState(selectedDocumentRange: NSRange(location: 9, length: 0)) + XCTAssertTrue(selectionStates.isItalics) + } + func testHorizontalTemplateButtonSelectionStateCursor() throws { let text = "Testing simple {{Currentdate}} template example." mediator.textView.attributedText = NSAttributedString(string: text) From af177ee8eaa4e151ce9dbba9383e17290af548d2 Mon Sep 17 00:00:00 2001 From: Toni Sevener Date: Thu, 7 Dec 2023 09:53:36 -0600 Subject: [PATCH 3/3] Remove NSLog --- .../Sources/ComponentsObjC/WKSourceEditorFormatterBoldItalics.m | 1 - 1 file changed, 1 deletion(-) diff --git a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterBoldItalics.m b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterBoldItalics.m index 38d636876ac..f376e5eec50 100644 --- a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterBoldItalics.m +++ b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterBoldItalics.m @@ -282,7 +282,6 @@ - (BOOL)attributedString:(NSMutableAttributedString *)attributedString isBoldInR if (range.length == 0) { if (attributedString.length > range.location) { - NSLog(@"%@", [attributedString attributesAtIndex:range.location effectiveRange:nil]); NSDictionary *attrs = [attributedString attributesAtIndex:range.location effectiveRange:nil]; if (attrs[WKSourceEditorCustomKeyFontBoldItalics] != nil || attrs[WKSourceEditorCustomKeyFontBold] != nil) {