Skip to content

Commit

Permalink
Add single quote support for tag attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
psharanda committed Aug 9, 2018
1 parent f9bc6ed commit a6af81a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Atributika.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Atributika"
s.version = "4.6.0"
s.version = "4.6.1"
s.summary = "Convert text with HTML tags, hashtags, mentions, links into NSAttributedString. Make them clickable with UILabel drop-in replacement."
s.description = <<-DESC
`Atributika` is an easy and painless way to build NSAttributedString. It is able to detect HTML-like tags, links, phone numbers, hashtags, any regex or even standard ios data detectors and style them with various attributes like font, color, etc. `Atributika` comes with drop-in label replacement `AttributedLabel` which is able to make any detection clickable.
Expand Down
14 changes: 9 additions & 5 deletions Sources/String+Detection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public struct TagTransformer {
extension String {

private func parseTag(_ tagString: String, parseAttributes: Bool) -> Tag? {

let tagScanner = Scanner(string: tagString)

guard let tagName = tagScanner.scanCharacters(from: CharacterSet.alphanumerics) else {
Expand All @@ -68,13 +67,18 @@ extension String {
break
}

guard tagScanner.scanString("\"") != nil else {
break
let startsFromSingleQuote = (tagScanner.scanString("'") != nil)
if !startsFromSingleQuote {
guard tagScanner.scanString("\"") != nil else {
break
}
}

let value = tagScanner.scanUpTo("\"") ?? ""
let quote = startsFromSingleQuote ? "'" : "\""

let value = tagScanner.scanUpTo(quote) ?? ""

guard tagScanner.scanString("\"") != nil else {
guard tagScanner.scanString(quote) != nil else {
break
}

Expand Down
12 changes: 12 additions & 0 deletions Tests/AtributikaTests/AtributikaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,18 @@ class AtributikaTests: XCTestCase {
XCTAssertEqual(tags[0].tag.attributes["href"], "http://foo.com")
}

func testTagAttributesWithSingleQuote() {
let test = "Hello <a class='big' target='' href=\"http://foo.com\">world</a>!"

let (string, tags) = test.detectTags()


XCTAssertEqual(string, "Hello world!")
XCTAssertEqual(tags[0].tag.attributes["class"], "big")
XCTAssertEqual(tags[0].tag.attributes["target"], "")
XCTAssertEqual(tags[0].tag.attributes["href"], "http://foo.com")
}

func testSpecials() {
XCTAssertEqual("Hello&amp;World!!!".detectTags().string , "Hello&World!!!")
XCTAssertEqual("Hello&".detectTags().string , "Hello&")
Expand Down

0 comments on commit a6af81a

Please sign in to comment.