From a89ded05f33e1c653b9c90c7461f470dfb54fff1 Mon Sep 17 00:00:00 2001 From: jldagon Date: Wed, 15 May 2024 09:56:38 +0200 Subject: [PATCH] add support for custom attributes containing hyphens --- .../HTML/Processor/HTMLStringToParsedResultProcessor.swift | 4 ++-- .../HTML/HTMLStringToParsedResultProcessorTests.swift | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/ZMarkupParser/HTML/Processor/HTMLStringToParsedResultProcessor.swift b/Sources/ZMarkupParser/HTML/Processor/HTMLStringToParsedResultProcessor.swift index fb09851..d426d3d 100644 --- a/Sources/ZMarkupParser/HTML/Processor/HTMLStringToParsedResultProcessor.swift +++ b/Sources/ZMarkupParser/HTML/Processor/HTMLStringToParsedResultProcessor.swift @@ -26,12 +26,12 @@ final class HTMLStringToParsedResultProcessor: ParserProcessor { // e.g 3. // selfClosingTag = / // tagName = span - static let htmlTagRegexPattern: String = #"<(?:(?\/)?(?[A-Za-z0-9]+)(?(?:\s*(\w+)\s*=\s*(["|']).*?\5)*)\s*(?\/)?>)"# + static let htmlTagRegexPattern: String = #"<(?:(?\/)?(?[A-Za-z0-9]+)(?(?:\s*([\w\-]+)\s*=\s*(["|']).*?\5)*)\s*(?\/)?>)"# // e.g. href="https://zhgchg.li" // name = href // value = https://zhgchg.li - static let htmlTagAttributesRegexPattern: String = #"\s*(?(?:\w+))\s*={1}\s*(["|']){1}(?.*?)\2\s*"# + static let htmlTagAttributesRegexPattern: String = #"\s*(?(?:[\w\-]+))\s*={1}\s*(["|']){1}(?.*?)\2\s*"# // will match: // / <\!DOCTYPE html> / ` \n ` diff --git a/Tests/ZMarkupParserTests/HTML/HTMLStringToParsedResultProcessorTests.swift b/Tests/ZMarkupParserTests/HTML/HTMLStringToParsedResultProcessorTests.swift index b1f6f8a..a6687fd 100644 --- a/Tests/ZMarkupParserTests/HTML/HTMLStringToParsedResultProcessorTests.swift +++ b/Tests/ZMarkupParserTests/HTML/HTMLStringToParsedResultProcessorTests.swift @@ -11,7 +11,7 @@ import XCTest final class HTMLStringToParsedResultProcessorTests: XCTestCase { func testNormalProcess() { - let result = HTMLStringToParsedResultProcessor().process(from: NSAttributedString(string: "TestHellossssZhgchgli
.
C
B")) + let result = HTMLStringToParsedResultProcessor().process(from: NSAttributedString(string: "TestHellossssZhgchgli
.
C
B")) let items = result.items XCTAssertEqual(items.count, 15, "Should have 15 elements.") XCTAssertEqual(result.needFormatter, true, "Should have need formatter.") @@ -27,9 +27,10 @@ final class HTMLStringToParsedResultProcessorTests: XCTestCase { case 1: if case let HTMLParsedResult.start(startItem) = item { XCTAssertEqual(startItem.tagName, "a", "expected `a` tag at index:\(index).") - XCTAssertEqual(startItem.attributes?.count, 2, "expected 2 attributes in `a` tag at index:\(index).") + XCTAssertEqual(startItem.attributes?.count, 3, "expected 3 attributes in `a` tag at index:\(index).") XCTAssertEqual(startItem.attributes?["href"], "https://zhgchg.li/about?g=f#hey", "expected href attribute in `a` tag at index:\(index).") XCTAssertEqual(startItem.attributes?["style"], "color:red", "expected style attribute in `a` tag at index:\(index).") + XCTAssertEqual(startItem.attributes?["custom-attribute"], "true", "expected custom-attribute attribute in `a` tag at index:\(index).") } else{ XCTFail("expected a tag at index:\(index).") }