-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
078d9de
commit 3830c4c
Showing
6 changed files
with
156 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Foundation | ||
|
||
struct HTMLTag { | ||
let name: String | ||
} | ||
|
||
extension HTMLTag { | ||
private enum Constants { | ||
static let tagExpression = try! NSRegularExpression(pattern: "<\\/?([a-zA-Z0-9]+)[^>]*>") | ||
} | ||
|
||
init?(_ description: String) { | ||
guard | ||
let match = Constants.tagExpression.firstMatch( | ||
in: description, | ||
range: NSRange(description.startIndex..., in: description) | ||
), | ||
let nameRange = Range(match.range(at: 1), in: description) | ||
else { | ||
return nil | ||
} | ||
|
||
self.name = String(description[nameRange]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Foundation | ||
import XCTest | ||
|
||
@testable import MarkdownUI | ||
|
||
final class HTMLTagTests: XCTestCase { | ||
func testInvalidTag() { | ||
XCTAssertNil(HTMLTag("")) | ||
XCTAssertNil(HTMLTag("foo")) | ||
XCTAssertNil(HTMLTag("<")) | ||
XCTAssertNil(HTMLTag("<>")) | ||
} | ||
|
||
func testOpeningTag() { | ||
// given | ||
let tag = HTMLTag("<sub>") | ||
|
||
// then | ||
XCTAssertEqual("sub", tag?.name) | ||
} | ||
|
||
func testOpeningTagWithAttributes() { | ||
// given | ||
let tag = HTMLTag( | ||
"<img src=\"img_girl.jpg\" alt=\"Girl in a jacket\" width=\"500\" height=\"600\">" | ||
) | ||
|
||
// then | ||
XCTAssertEqual("img", tag?.name) | ||
} | ||
|
||
func testClosingTag() { | ||
let tag = HTMLTag("</sub>") | ||
XCTAssertEqual(tag?.name, "sub") | ||
} | ||
|
||
func testSelfClosingTag() { | ||
XCTAssertEqual("br", HTMLTag("<br />")?.name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+11 KB
(110%)
Tests/MarkdownUITests/__Snapshots__/MarkdownTests/testInlines.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.