Skip to content

Commit 246ccf1

Browse files
Merge pull request #1215 from wordpress-mobile/issue/703_fix_color_styling
Issue/703 fix color styling
2 parents 9123b76 + 30f661e commit 246ccf1

6 files changed

Lines changed: 53 additions & 26 deletions

File tree

Aztec.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@
239239
FFA61EC21DF6C1C900B71BF6 /* NSAttributedString+Archive.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFA61EC11DF6C1C900B71BF6 /* NSAttributedString+Archive.swift */; };
240240
FFB5D29720BEB21A0038DCFB /* CiteFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFB5D29620BEB21A0038DCFB /* CiteFormatter.swift */; };
241241
FFD0FEB71DAE59A700430586 /* NSLayoutManager+Attachments.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFD0FEB61DAE59A700430586 /* NSLayoutManager+Attachments.swift */; };
242+
FFD3C1712344DB4E00AE8DA0 /* ForegroundColorCSSAttributeMatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFD3C1702344DB4E00AE8DA0 /* ForegroundColorCSSAttributeMatcher.swift */; };
243+
FFD3C1732344DCA900AE8DA0 /* ForegroundColorElementAttributeConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFD3C1722344DCA900AE8DA0 /* ForegroundColorElementAttributeConverter.swift */; };
242244
FFD436981E3180A500A0E26F /* FontFormatterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFD436971E3180A500A0E26F /* FontFormatterTests.swift */; };
243245
FFFEC7DB1EA7698900F4210F /* VideoAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFFEC7DA1EA7698900F4210F /* VideoAttachment.swift */; };
244246
/* End PBXBuildFile section */
@@ -516,6 +518,8 @@
516518
FFB5D29620BEB21A0038DCFB /* CiteFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CiteFormatter.swift; sourceTree = "<group>"; };
517519
FFC2BBF81F2A25CF00E404FB /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
518520
FFD0FEB61DAE59A700430586 /* NSLayoutManager+Attachments.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSLayoutManager+Attachments.swift"; sourceTree = "<group>"; };
521+
FFD3C1702344DB4E00AE8DA0 /* ForegroundColorCSSAttributeMatcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ForegroundColorCSSAttributeMatcher.swift; sourceTree = "<group>"; };
522+
FFD3C1722344DCA900AE8DA0 /* ForegroundColorElementAttributeConverter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ForegroundColorElementAttributeConverter.swift; sourceTree = "<group>"; };
519523
FFD436971E3180A500A0E26F /* FontFormatterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FontFormatterTests.swift; sourceTree = "<group>"; };
520524
FFFEC7DA1EA7698900F4210F /* VideoAttachment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VideoAttachment.swift; sourceTree = "<group>"; };
521525
/* End PBXFileReference section */
@@ -898,6 +902,7 @@
898902
F1098740214FF55F00983B6A /* BoldElementAttributeConverter.swift */,
899903
F109874421501A2F00983B6A /* ItalicElementAttributeConverter.swift */,
900904
F109874821501C8B00983B6A /* UnderlineElementAttributeConverter.swift */,
905+
FFD3C1722344DCA900AE8DA0 /* ForegroundColorElementAttributeConverter.swift */,
901906
);
902907
path = Implementations;
903908
sourceTree = "<group>";
@@ -1087,6 +1092,7 @@
10871092
F15BA610215167D200424120 /* CSSAttributeMatcher.swift */,
10881093
F1F5C9DA21516E4B00F67990 /* ItalicCSSAttributeMatcher.swift */,
10891094
F1F5C9DC2151702600F67990 /* UnderlineCSSAttributeMatcher.swift */,
1095+
FFD3C1702344DB4E00AE8DA0 /* ForegroundColorCSSAttributeMatcher.swift */,
10901096
);
10911097
path = CSS;
10921098
sourceTree = "<group>";
@@ -1524,6 +1530,8 @@
15241530
B572AC281E817CFE008948C2 /* CommentAttachment.swift in Sources */,
15251531
F1E1D5881FEC52EE0086B339 /* GenericElementConverter.swift in Sources */,
15261532
F1FA0E861E6EF514009D98EE /* Node.swift in Sources */,
1533+
FFD3C1712344DB4E00AE8DA0 /* ForegroundColorCSSAttributeMatcher.swift in Sources */,
1534+
FFD3C1732344DCA900AE8DA0 /* ForegroundColorElementAttributeConverter.swift in Sources */,
15271535
F1656FDE2152A6A6009C7E3A /* CiteStringAttributeConverter.swift in Sources */,
15281536
599F254B1D8BC9A1002871D6 /* String+RangeConversion.swift in Sources */,
15291537
599F25481D8BC9A1002871D6 /* Metrics.swift in Sources */,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Foundation
2+
import UIKit
3+
4+
class ForegroundColorElementAttributesConverter: ElementAttributeConverter {
5+
6+
let cssAttributeMatcher = ForegroundColorCSSAttributeMatcher()
7+
8+
func convert(
9+
_ attribute: Attribute,
10+
inheriting attributes: [NSAttributedString.Key: Any]) -> [NSAttributedString.Key: Any] {
11+
12+
guard let cssColor = attribute.firstCSSAttribute(ofType: .foregroundColor),
13+
let colorValue = cssColor.value,
14+
let color = UIColor(hexString: colorValue) else {
15+
return attributes
16+
}
17+
18+
var attributes = attributes
19+
20+
attributes[.foregroundColor] = color
21+
22+
return attributes
23+
}
24+
}

Aztec/Classes/Libxml2/DOM/Data/CSSAttributeType.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extension CSSAttributeType {
2323
public static let fontStyle = CSSAttributeType("font-style")
2424
public static let fontWeight = CSSAttributeType("font-weight")
2525
public static let textDecoration = CSSAttributeType("text-decoration")
26+
public static let foregroundColor = CSSAttributeType("color")
2627

2728
}
2829

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Foundation
2+
3+
open class ForegroundColorCSSAttributeMatcher: CSSAttributeMatcher {
4+
5+
public func check(_ cssAttribute: CSSAttribute) -> Bool {
6+
guard let value = cssAttribute.value else {
7+
return false
8+
}
9+
10+
return cssAttribute.type == .foregroundColor && !value.isEmpty
11+
}
12+
}

Aztec/Classes/NSAttributedString/Conversions/AttributedStringSerializer.swift

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class AttributedStringSerializer {
2929
BoldElementAttributesConverter(),
3030
ItalicElementAttributesConverter(),
3131
UnderlineElementAttributesConverter(),
32+
ForegroundColorElementAttributesConverter(),
3233
]
3334
)
3435

@@ -181,32 +182,6 @@ class AttributedStringSerializer {
181182

182183
return content
183184
}
184-
185-
// MARK: - Formatter Maps
186-
187-
public lazy var attributeFormattersMap: [String: AttributeFormatter] = {
188-
return [:]
189-
}()
190-
191-
public let styleToFormattersMap: [String: (AttributeFormatter, (String)->Any?)] = [
192-
"color": (ColorFormatter(), {(value) in return UIColor(hexString: value)}),
193-
"text-decoration": (UnderlineFormatter(), { (value) in return value == "underline" ? NSUnderlineStyle.single.rawValue : nil})
194-
]
195-
196-
func parseStyle(style: String) -> [String: String] {
197-
var stylesDictionary = [String: String]()
198-
let styleAttributes = style.components(separatedBy: ";")
199-
for sytleAttribute in styleAttributes {
200-
let keyValue = sytleAttribute.components(separatedBy: ":")
201-
guard keyValue.count == 2,
202-
let key = keyValue.first?.trimmingCharacters(in: CharacterSet.whitespaces),
203-
let value = keyValue.last?.trimmingCharacters(in: CharacterSet.whitespaces) else {
204-
continue
205-
}
206-
stylesDictionary[key] = value
207-
}
208-
return stylesDictionary
209-
}
210185
}
211186

212187
private extension AttributedStringSerializer {

AztecTests/TextKit/TextViewTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,13 @@ class TextViewTests: XCTestCase {
377377
XCTAssert(!textView.formattingIdentifiersAtIndex(1).contains(.blockquote))
378378
}
379379

380+
func testColorAttributeAtPosition() {
381+
let textView = TextViewStub(withHTML: "foo<span style=\"color: #FF0000\">bar</span>baz")
382+
383+
XCTAssert(textView.attributedText!.attributes(at: 4, effectiveRange: nil).keys.contains(.foregroundColor))
384+
let color = textView.attributedText!.attributes(at: 4, effectiveRange: nil)[.foregroundColor] as! UIColor
385+
XCTAssertEqual(color, UIColor(hexString: "#FF0000"))
386+
}
380387

381388
// MARK: - Adding newlines
382389

0 commit comments

Comments
 (0)