From 18c669ddac06cc10820d55b2242dbc5098f7815a Mon Sep 17 00:00:00 2001 From: Pavel Sharanda Date: Mon, 9 May 2016 10:06:40 +0300 Subject: [PATCH] minor API updates --- Atributika/Atributika/Atributika.swift | 24 +++++-- .../AtributikaTests/AtributikaTests.swift | 71 +++++++++++++------ README.md | 4 +- 3 files changed, 68 insertions(+), 31 deletions(-) diff --git a/Atributika/Atributika/Atributika.swift b/Atributika/Atributika/Atributika.swift index 3cc060b..7882cc2 100644 --- a/Atributika/Atributika/Atributika.swift +++ b/Atributika/Atributika/Atributika.swift @@ -134,12 +134,16 @@ public enum Attribute { } } -private struct TagInfo { +public struct TagInfo : Equatable { let name: String let attributes: [String: String] var range: Range } +public func ==(lhs: TagInfo, rhs: TagInfo) -> Bool { + return lhs.name == rhs.name && lhs.attributes == rhs.attributes && lhs.range == rhs.range +} + public struct Atributika { let text: String let tags: [String: [Attribute]] @@ -161,11 +165,9 @@ public struct Atributika { return attrs } - var attributedText: NSAttributedString { + private func buildAttributedStringInternal() -> (NSAttributedString, [TagInfo]) { - guard let (parsedText, tagsInfo) = parseText(text) else { - return NSAttributedString() - } + let (parsedText, tagsInfo) = parseText(text) let attributedString = NSMutableAttributedString(string: parsedText, attributes: attributesListToAttributes(baseAttributes)) @@ -176,7 +178,15 @@ public struct Atributika { } } - return attributedString + return (attributedString, tagsInfo) + } + + func buildAttributedString() -> NSAttributedString { + return buildAttributedStringInternal().0 + } + + func buildAttributedStringAndTagsInfo() -> (NSAttributedString, [TagInfo]) { + return buildAttributedStringInternal() } private let specials = ["quot":"\"", @@ -222,7 +232,7 @@ public struct Atributika { return (tagName, attrubutes) } - private func parseText(text: String) -> (String, [TagInfo])? { + private func parseText(text: String) -> (String, [TagInfo]) { let scanner = NSScanner(string: text) scanner.charactersToBeSkipped = nil diff --git a/Atributika/AtributikaTests/AtributikaTests.swift b/Atributika/AtributikaTests/AtributikaTests.swift index 670aeed..cad0899 100644 --- a/Atributika/AtributikaTests/AtributikaTests.swift +++ b/Atributika/AtributikaTests/AtributikaTests.swift @@ -19,28 +19,39 @@ class AtributikaTests: XCTestCase { .Font(UIFont.boldSystemFontOfSize(45)), ] - ]).attributedText + ]).buildAttributedString() let reference = NSMutableAttributedString(string: "Hello World!!!") reference.addAttributes([NSFontAttributeName: UIFont.boldSystemFontOfSize(45)], range: NSMakeRange(6, 5)) - XCTAssert(test == reference) + XCTAssertEqual(test,reference) } func testEmpty() { - let test = Atributika(text: "Hello World!!!", tags: [:]).attributedText + let test = Atributika(text: "Hello World!!!").buildAttributedString() let reference = NSMutableAttributedString(string: "Hello World!!!") - XCTAssert(test == reference) + XCTAssertEqual(test, reference) + } + + func testParams() { + let (test, tags) = Atributika(text: "Hello World!!!").buildAttributedStringAndTagsInfo() + + let reference = NSMutableAttributedString(string: "Hello World!!!") + + XCTAssertEqual(test, reference) + + let referenceTags = [TagInfo(name: "a", attributes: ["href":"http://google.com"], range: 0..<5)] + XCTAssertEqual(tags, referenceTags) } func testBase() { - let test = Atributika(text: "Hello World!!!", tags: [:], baseAttributes: [.Font(UIFont.boldSystemFontOfSize(45))]).attributedText + let test = Atributika(text: "Hello World!!!", tags: [:], baseAttributes: [.Font(UIFont.boldSystemFontOfSize(45))]).buildAttributedString() let reference = NSMutableAttributedString(string: "Hello World!!!", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(45)]) - XCTAssert(test == reference) + XCTAssertEqual(test, reference) } func testManyTags() { @@ -53,17 +64,33 @@ class AtributikaTests: XCTestCase { .Font(UIFont.italicSystemFontOfSize(12)), ] - ]).attributedText + ]).buildAttributedString() let reference = NSMutableAttributedString(string: "Hello World!!!") reference.addAttributes([NSFontAttributeName: UIFont.italicSystemFontOfSize(12)], range: NSMakeRange(2, 3)) reference.addAttributes([NSFontAttributeName: UIFont.boldSystemFontOfSize(45)], range: NSMakeRange(6, 5)) - XCTAssert(test == reference) + XCTAssertEqual(test, reference) + } + + func testManySameTags() { + let test = Atributika(text: "Hello World!!!", + tags: [ + "b" : [ + .Font(UIFont.boldSystemFontOfSize(45)), + ] + + ]).buildAttributedString() + + let reference = NSMutableAttributedString(string: "Hello World!!!") + reference.addAttributes([NSFontAttributeName: UIFont.boldSystemFontOfSize(45)], range: NSMakeRange(2, 3)) + reference.addAttributes([NSFontAttributeName: UIFont.boldSystemFontOfSize(45)], range: NSMakeRange(6, 5)) + + XCTAssertEqual(test, reference) } func testTagsOverlap() { - let test = Atributika(text: "Hello World!!!", + let test = Atributika(text: "Hello World!!!", tags: [ "b" : [ .Font(UIFont.boldSystemFontOfSize(45)), @@ -72,21 +99,21 @@ class AtributikaTests: XCTestCase { .ForegroundColor(UIColor.redColor()), ] - ]).attributedText + ]).buildAttributedString() let reference = NSMutableAttributedString(string: "Hello World!!!") reference.addAttributes([NSFontAttributeName: UIFont.boldSystemFontOfSize(45)], range: NSMakeRange(6, 5)) - reference.addAttributes([NSForegroundColorAttributeName: UIColor.redColor()], range: NSMakeRange(2, 12)) + reference.addAttributes([NSForegroundColorAttributeName: UIColor.redColor()], range: NSMakeRange(7, 7)) - XCTAssert(test == reference) + XCTAssertEqual(test, reference) } func testBr() { - let test = Atributika(text: "Hello
World!!!").attributedText + let test = Atributika(text: "Hello
World!!!").buildAttributedString() let reference = NSMutableAttributedString(string: "Hello\nWorld!!!") - XCTAssert(test == reference) + XCTAssertEqual(test, reference) } func testNotClosedTag() { @@ -96,11 +123,11 @@ class AtributikaTests: XCTestCase { .Font(UIFont.boldSystemFontOfSize(45)), ] - ]).attributedText + ]).buildAttributedString() let reference = NSMutableAttributedString(string: "Hello World!!!") - XCTAssert(test == reference) + XCTAssertEqual(test, reference) } func testNotOpenedTag() { @@ -110,11 +137,11 @@ class AtributikaTests: XCTestCase { .Font(UIFont.boldSystemFontOfSize(45)), ] - ]).attributedText + ]).buildAttributedString() let reference = NSMutableAttributedString(string: "Hello World!!!") - XCTAssert(test == reference) + XCTAssertEqual(test, reference) } func testBadTag() { @@ -126,11 +153,11 @@ class AtributikaTests: XCTestCase { .Font(UIFont.boldSystemFontOfSize(45)), ] - ]).attributedText + ]).buildAttributedString() let reference = NSMutableAttributedString(string: "Hello ") - XCTAssert(test == reference) + XCTAssertEqual(test, reference) } func testTagsStack() { @@ -146,14 +173,14 @@ class AtributikaTests: XCTestCase { .UnderlineStyle(.StyleSingle) ] - ]).attributedText + ]).buildAttributedString() let reference = NSMutableAttributedString(string: "Hello World!!!") reference.addAttributes([NSFontAttributeName: UIFont.boldSystemFontOfSize(45)], range: NSMakeRange(6, 5)) reference.addAttributes([NSForegroundColorAttributeName: UIColor.redColor()], range: NSMakeRange(8, 3)) reference.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue], range: NSMakeRange(10, 1)) - XCTAssert(test == reference) + XCTAssertEqual(test, reference) } } diff --git a/README.md b/README.md index 7677d4c..5a9273a 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ let str = Atributika(text: "Hello \(name)!!!", "bold" : [ .Font(UIFont.boldSystemFontOfSize(45)), ] - ]).attributedText + ]).buildAttributedString() ``` Yeah, that's much better. Atributika is easy, declarative, flexible and covers all the raw edges for you. @@ -58,7 +58,7 @@ label = UILabel() .UnderlineStyle(.StyleSingle) ] - ]).attributedText + ]).buildAttributedString() view.addSubview(label) ```