-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1352 from wordpress-mobile/feature/mark-formattin…
…g-improvements Improve Mark formatting
- Loading branch information
Showing
8 changed files
with
189 additions
and
12 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
47 changes: 47 additions & 0 deletions
47
...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,47 @@ | ||
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) { | ||
let styleAttribute = elementNode.attributes.first(where: { $0.name == "style" }) | ||
if let elementStyle = styleAttribute?.value.toString() { | ||
// Remove spaces between attribute name and value, and between style attributes. | ||
let styleAttributes = elementStyle.replacingOccurrences(of: ": ", with: ":").replacingOccurrences(of: "; ", with: ";") | ||
elementNode.attributes["style"] = .string(styleAttributes) | ||
} | ||
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