-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mark Formatting - Fix autocorrected strings and changing the caret po…
…sition bug
- Loading branch information
Gerardo
committed
Mar 14, 2022
1 parent
b16c763
commit d655451
Showing
8 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...onverters/StringAttributesToAttributes/Implementations/MarkStringAttributeConverter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Foundation | ||
import UIKit | ||
|
||
|
||
/// Converts the mark style information from string attributes and aggregates it into an | ||
/// existing array of element nodes. | ||
/// | ||
open class MarkStringAttributeConverter: StringAttributeConverter { | ||
|
||
private let toggler = HTMLStyleToggler(defaultElement: .mark, cssAttributeMatcher: ForegroundColorCSSAttributeMatcher()) | ||
|
||
public func convert( | ||
attributes: [NSAttributedString.Key: Any], | ||
andAggregateWith elementNodes: [ElementNode]) -> [ElementNode] { | ||
|
||
var elementNodes = elementNodes | ||
|
||
// We add the representation right away, if it exists... as it could contain attributes beyond just this | ||
// style. The enable and disable methods below can modify this as necessary. | ||
// | ||
|
||
if let elementNode = attributes.storedElement(for: NSAttributedString.Key.markHtmlRepresentation) { | ||
elementNodes.append(elementNode) | ||
} | ||
|
||
if shouldEnableMarkElement(for: attributes) { | ||
return toggler.enable(in: elementNodes) | ||
} else { | ||
return toggler.disable(in: elementNodes) | ||
} | ||
} | ||
|
||
// MARK: - Style Detection | ||
|
||
func shouldEnableMarkElement(for attributes: [NSAttributedString.Key: Any]) -> Bool { | ||
return isMark(for: attributes) | ||
} | ||
|
||
func isMark(for attributes: [NSAttributedString.Key: Any]) -> Bool { | ||
return attributes[.markHtmlRepresentation] != nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters