Skip to content

Commit

Permalink
minor API updates
Browse files Browse the repository at this point in the history
  • Loading branch information
psharanda committed May 9, 2016
1 parent 0c31dc8 commit 18c669d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 31 deletions.
24 changes: 17 additions & 7 deletions Atributika/Atributika/Atributika.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,16 @@ public enum Attribute {
}
}

private struct TagInfo {
public struct TagInfo : Equatable {
let name: String
let attributes: [String: String]
var range: Range<Int>
}

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]]
Expand All @@ -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))

Expand All @@ -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":"\"",
Expand Down Expand Up @@ -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
Expand Down
71 changes: 49 additions & 22 deletions Atributika/AtributikaTests/AtributikaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<a href=\"http://google.com\">Hello</a> 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() {
Expand All @@ -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: "He<b>llo</b> <b>World</b>!!!",
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: "He<red>llo <b>World</b>!!!</red>",
let test = Atributika(text: "Hello <b>W<red>orld</b>!!!</red>",
tags: [
"b" : [
.Font(UIFont.boldSystemFontOfSize(45)),
Expand All @@ -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<br>World!!!").attributedText
let test = Atributika(text: "Hello<br>World!!!").buildAttributedString()

let reference = NSMutableAttributedString(string: "Hello\nWorld!!!")

XCTAssert(test == reference)
XCTAssertEqual(test, reference)
}

func testNotClosedTag() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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)
}

}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let str = Atributika(text: "Hello <bold>\(name)</bold>!!!",
"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.
Expand Down Expand Up @@ -58,7 +58,7 @@ label = UILabel()
.UnderlineStyle(.StyleSingle)
]

]).attributedText
]).buildAttributedString()
view.addSubview(label)
```

Expand Down

0 comments on commit 18c669d

Please sign in to comment.