Skip to content

Commit 3de4024

Browse files
authored
Merge pull request #57 from jilouc/main
2 parents 62ea6a7 + a89ded0 commit 3de4024

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

Sources/ZMarkupParser/HTML/Processor/HTMLStringToParsedResultProcessor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ final class HTMLStringToParsedResultProcessor: ParserProcessor {
2626
// e.g 3. </span>
2727
// selfClosingTag = /
2828
// tagName = span
29-
static let htmlTagRegexPattern: String = #"<(?:(?<closeTag>\/)?(?<tagName>[A-Za-z0-9]+)(?<tagAttributes>(?:\s*(\w+)\s*=\s*(["|']).*?\5)*)\s*(?<selfClosingTag>\/)?>)"#
29+
static let htmlTagRegexPattern: String = #"<(?:(?<closeTag>\/)?(?<tagName>[A-Za-z0-9]+)(?<tagAttributes>(?:\s*([\w\-]+)\s*=\s*(["|']).*?\5)*)\s*(?<selfClosingTag>\/)?>)"#
3030

3131
// e.g. href="https://zhgchg.li"
3232
// name = href
3333
// value = https://zhgchg.li
34-
static let htmlTagAttributesRegexPattern: String = #"\s*(?<name>(?:\w+))\s*={1}\s*(["|']){1}(?<value>.*?)\2\s*"#
34+
static let htmlTagAttributesRegexPattern: String = #"\s*(?<name>(?:[\w\-]+))\s*={1}\s*(["|']){1}(?<value>.*?)\2\s*"#
3535

3636
// will match:
3737
// <!--Test--> / <\!DOCTYPE html> / ` \n `

Tests/ZMarkupParserTests/HTML/HTMLStringToParsedResultProcessorTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import XCTest
1111

1212
final class HTMLStringToParsedResultProcessorTests: XCTestCase {
1313
func testNormalProcess() {
14-
let result = HTMLStringToParsedResultProcessor().process(from: NSAttributedString(string: "Test<a href=\"https://zhgchg.li/about?g=f#hey\" style=\"color:red\">Hello<b>ssss</a>Zhg</b>chgli<br>.<br/>C<br />B"))
14+
let result = HTMLStringToParsedResultProcessor().process(from: NSAttributedString(string: "Test<a href=\"https://zhgchg.li/about?g=f#hey\" style=\"color:red\" custom-attribute=\"true\">Hello<b>ssss</a>Zhg</b>chgli<br>.<br/>C<br />B"))
1515
let items = result.items
1616
XCTAssertEqual(items.count, 15, "Should have 15 elements.")
1717
XCTAssertEqual(result.needFormatter, true, "Should have need formatter.")
@@ -27,9 +27,10 @@ final class HTMLStringToParsedResultProcessorTests: XCTestCase {
2727
case 1:
2828
if case let HTMLParsedResult.start(startItem) = item {
2929
XCTAssertEqual(startItem.tagName, "a", "expected `a` tag at index:\(index).")
30-
XCTAssertEqual(startItem.attributes?.count, 2, "expected 2 attributes in `a` tag at index:\(index).")
30+
XCTAssertEqual(startItem.attributes?.count, 3, "expected 3 attributes in `a` tag at index:\(index).")
3131
XCTAssertEqual(startItem.attributes?["href"], "https://zhgchg.li/about?g=f#hey", "expected href attribute in `a` tag at index:\(index).")
3232
XCTAssertEqual(startItem.attributes?["style"], "color:red", "expected style attribute in `a` tag at index:\(index).")
33+
XCTAssertEqual(startItem.attributes?["custom-attribute"], "true", "expected custom-attribute attribute in `a` tag at index:\(index).")
3334
} else{
3435
XCTFail("expected a tag at index:\(index).")
3536
}

0 commit comments

Comments
 (0)