Skip to content

Commit

Permalink
Self-closing tags support
Browse files Browse the repository at this point in the history
  • Loading branch information
psharanda committed Oct 25, 2020
1 parent a5332ad commit 61e176b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 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.9.9"
s.version = "4.9.10"
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
12 changes: 8 additions & 4 deletions Sources/String+Detection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct TagInfo {
}

public enum TagType {
case start
case start //includes self-closing tags like <img />
case end
}

Expand Down Expand Up @@ -127,9 +127,13 @@ extension String {
}) {
resultString.append(transformer.transform(tag))
}

if tagType == .start {
tagsStack.append((tag, resultTextEndIndex, (tagsStack.last?.2 ?? -1) + 1))

let nextLevel = (tagsStack.last?.2 ?? -1) + 1

if tagString.last == "/" {
tagsResult.append(TagInfoInternal(tag: tag, rangeStart: resultTextEndIndex, rangeEnd: resultTextEndIndex, level: nextLevel))
} else if tagType == .start {
tagsStack.append((tag, resultTextEndIndex, nextLevel))
} else {
for (index, (tagInStack, startIndex, level)) in tagsStack.enumerated().reversed() {
if tagInStack.name.lowercased() == tag.name.lowercased() {
Expand Down
22 changes: 22 additions & 0 deletions Tests/AtributikaTests/AtributikaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,28 @@ class AtributikaTests: XCTestCase {
XCTAssertEqual(tags[0].tag.attributes["target"], "")
XCTAssertEqual(tags[0].tag.attributes["href"], "http://foo.com")
}

func testSelfClosingTagAttributes() {
let test = "Hello <img href=\"http://foo.com/image.jpg\"/>!"

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

XCTAssertEqual(string, "Hello !")

XCTAssertEqual(tags[0].tag.name, "img")
XCTAssertEqual(tags[0].tag.attributes["href"], "http://foo.com/image.jpg")
}

func testInnerSelfClosingTagAttributes() {
let test = "Hello <b>bold<img href=\"http://foo.com/image.jpg\"/>!</b>"

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

XCTAssertEqual(string, "Hello bold!")

XCTAssertEqual(tags[0].tag.name, "img")
XCTAssertEqual(tags[0].tag.attributes["href"], "http://foo.com/image.jpg")
}

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

0 comments on commit 61e176b

Please sign in to comment.