Skip to content

Commit fb0c3a1

Browse files
committed
[Fix] style inherit bug
1 parent 127af55 commit fb0c3a1

File tree

5 files changed

+119
-31
lines changed

5 files changed

+119
-31
lines changed

Sources/ZMarkupParser/HTML/Processor/HTMLElementMarkupComponentMarkupStyleVisitor.swift

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -144,41 +144,44 @@ extension HTMLElementMarkupComponentMarkupStyleVisitor {
144144
}
145145

146146
func defaultVisit(_ htmlElement: HTMLElementMarkupComponent.HTMLElement?, defaultStyle: MarkupStyle? = nil) -> Result {
147-
var markupStyle: MarkupStyle? = customStyle(htmlElement) ?? defaultStyle
148-
guard let styleString = htmlElement?.attributes?["style"],
149-
styleAttributes.count > 0 else {
150-
return markupStyle
151-
}
152147

153-
let styles = styleString.split(separator: ";").filter { $0.trimmingCharacters(in: .whitespacesAndNewlines) != "" }.map { $0.split(separator: ":") }
148+
var markupStyle: MarkupStyle? = nil
149+
if let customStyle = customStyle(htmlElement) {
150+
// has custom style
151+
markupStyle = customStyle
152+
}
154153

155-
for style in styles {
156-
guard style.count == 2 else {
157-
continue
158-
}
154+
if let styleString = htmlElement?.attributes?["style"],
155+
styleAttributes.count > 0 {
156+
let styles = styleString.split(separator: ";").filter { $0.trimmingCharacters(in: .whitespacesAndNewlines) != "" }.map { $0.split(separator: ":") }
159157

160-
let key = style[0].trimmingCharacters(in: .whitespacesAndNewlines)
161-
let value = style[1].trimmingCharacters(in: .whitespacesAndNewlines)
162-
163-
if let styleAttribute = styleAttributes.first(where: { $0.isEqualTo(styleName: key) }) {
164-
let visitor = HTMLTagStyleAttributeToMarkupStyleVisitor(value: value)
165-
if var thisMarkupStyle = visitor.visit(styleAttribute: styleAttribute) {
166-
thisMarkupStyle.fillIfNil(from: markupStyle)
167-
markupStyle = thisMarkupStyle
158+
for style in styles {
159+
guard style.count == 2 else {
160+
continue
161+
}
162+
163+
let key = style[0].trimmingCharacters(in: .whitespacesAndNewlines)
164+
let value = style[1].trimmingCharacters(in: .whitespacesAndNewlines)
165+
166+
if let styleAttribute = styleAttributes.first(where: { $0.isEqualTo(styleName: key) }) {
167+
let visitor = HTMLTagStyleAttributeToMarkupStyleVisitor(value: value)
168+
if var thisMarkupStyle = visitor.visit(styleAttribute: styleAttribute) {
169+
switch policy {
170+
case .respectMarkupStyleFromCode:
171+
if var markupStyle = markupStyle {
172+
markupStyle.fillIfNil(from: thisMarkupStyle)
173+
} else {
174+
markupStyle = thisMarkupStyle
175+
}
176+
case .respectMarkupStyleFromHTMLStyleAttribute:
177+
thisMarkupStyle.fillIfNil(from: markupStyle ?? defaultStyle)
178+
markupStyle = thisMarkupStyle
179+
}
180+
}
168181
}
169182
}
170183
}
171184

172-
if var defaultStyle = defaultStyle {
173-
switch policy {
174-
case .respectMarkupStyleFromHTMLStyleAttribute:
175-
markupStyle?.fillIfNil(from: defaultStyle)
176-
case .respectMarkupStyleFromCode:
177-
defaultStyle.fillIfNil(from: markupStyle)
178-
markupStyle = defaultStyle
179-
}
180-
}
181-
182-
return markupStyle
185+
return markupStyle ?? defaultStyle
183186
}
184187
}

Sources/ZMarkupParser/HTML/Processor/HTMLTagAttributeToMarkupStyleVisitor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct HTMLTagStyleAttributeToMarkupStyleVisitor: HTMLTagStyleAttributeVisitor {
7474
}
7575

7676
func convert(fromPX string: String) -> Int? {
77-
guard let regex = try? NSRegularExpression(pattern: "([0-9]+)px"),
77+
guard let regex = try? NSRegularExpression(pattern: "([0-9]+)p(x|t)"),
7878
let firstMatch = regex.firstMatch(in: string, options: [], range: NSRange(location: 0, length: string.count)),
7979
firstMatch.range(at: 1).location != NSNotFound,
8080
let range = Range(firstMatch.range(at: 1), in: string),

Sources/ZMarkupParser/HTML/ZHTMLParserBuilder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public final class ZHTMLParserBuilder {
1212
private(set) var htmlTags: [HTMLTag] = []
1313
private(set) var styleAttributes: [HTMLTagStyleAttribute] = []
1414
private(set) var rootStyle: MarkupStyle? = .default
15-
private(set) var policy: MarkupStylePolicy = .respectMarkupStyleFromCode
15+
private(set) var policy: MarkupStylePolicy = .respectMarkupStyleFromHTMLStyleAttribute
1616

1717
public init() {
1818

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// HTMLElementMarkupComponentMarkupStyleVisitorTests.swift
3+
//
4+
//
5+
// Created by zhgchgli on 2023/7/23.
6+
//
7+
8+
import Foundation
9+
@testable import ZMarkupParser
10+
import XCTest
11+
12+
final class HTMLElementMarkupComponentMarkupStyleVisitorTests: XCTestCase {
13+
14+
func testDefaultStyleByDefault() {
15+
let visitor = HTMLElementMarkupComponentMarkupStyleVisitor(policy: .respectMarkupStyleFromCode, components: [], styleAttributes: [])
16+
17+
let result = visitor.visit(markup: HeadMarkup(levle: .h1))
18+
XCTAssertEqual(result?.font.size, MarkupStyle.h1.font.size)
19+
}
20+
21+
func testDefaultStyleByCustomStyle() {
22+
let markup = InlineMarkup()
23+
let customStyle = MarkupStyle(font: .init(size: 99))
24+
let visitor = HTMLElementMarkupComponentMarkupStyleVisitor(policy: .respectMarkupStyleFromCode, components: [.init(markup: markup, value: .init(tag: .init(tagName: H1_HTMLTagName(), customStyle: customStyle), tagAttributedString: NSAttributedString(string: "<span>test</span>"), attributes: [:]))], styleAttributes: [])
25+
26+
let result = visitor.visit(markup: markup)
27+
XCTAssertEqual(result?.font.size, customStyle.font.size)
28+
}
29+
30+
func testDefaultStyleShouldOverrideByCustomStyle() {
31+
let markup = HeadMarkup(levle: .h1)
32+
let customStyle = MarkupStyle(font: .init(size: 99))
33+
let visitor = HTMLElementMarkupComponentMarkupStyleVisitor(policy: .respectMarkupStyleFromCode, components: [.init(markup: markup, value: .init(tag: .init(tagName: H1_HTMLTagName(), customStyle: customStyle), tagAttributedString: NSAttributedString(string: "<h1>test</h1>"), attributes: [:]))], styleAttributes: [])
34+
35+
let result = visitor.visit(markup: markup)
36+
XCTAssertEqual(result?.font.size, customStyle.font.size)
37+
}
38+
39+
func testDefaultStyleShouldOverrideByStyleAttributed() {
40+
let markup = HeadMarkup(levle: .h1)
41+
let visitor = HTMLElementMarkupComponentMarkupStyleVisitor(policy: .respectMarkupStyleFromCode, components: [.init(markup: markup, value: .init(tag: .init(tagName: H1_HTMLTagName()), tagAttributedString: NSAttributedString(string: "<h1>test</h1>"), attributes: ["style": "font-size:99pt"]))], styleAttributes: [FontSizeHTMLTagStyleAttribute()])
42+
43+
let result = visitor.visit(markup: markup)
44+
XCTAssertEqual(result?.font.size, 99)
45+
}
46+
47+
func testDefaultStylePolicyRespectMarkupStyleFromCode() {
48+
let markup = HeadMarkup(levle: .h1)
49+
let customStyle = MarkupStyle(font: .init(size: 109))
50+
let visitor = HTMLElementMarkupComponentMarkupStyleVisitor(policy: .respectMarkupStyleFromCode, components: [.init(markup: markup, value: .init(tag: .init(tagName: H1_HTMLTagName(), customStyle: customStyle), tagAttributedString: NSAttributedString(string: "<h1>test</h1>"), attributes: ["style": "font-size:99pt"]))], styleAttributes: [FontSizeHTMLTagStyleAttribute()])
51+
52+
let result = visitor.visit(markup: markup)
53+
XCTAssertEqual(result?.font.size, customStyle.font.size)
54+
}
55+
56+
func testDefaultStylePolicyRespectMarkupStyleFromHTMLStyleAttribute() {
57+
let markup = HeadMarkup(levle: .h1)
58+
let customStyle = MarkupStyle(font: .init(size: 109))
59+
let visitor = HTMLElementMarkupComponentMarkupStyleVisitor(policy: .respectMarkupStyleFromHTMLStyleAttribute, components: [.init(markup: markup, value: .init(tag: .init(tagName: H1_HTMLTagName(), customStyle: customStyle), tagAttributedString: NSAttributedString(string: "<h1>test</h1>"), attributes: ["style": "font-size:99pt"]))], styleAttributes: [FontSizeHTMLTagStyleAttribute()])
60+
61+
let result = visitor.visit(markup: markup)
62+
XCTAssertEqual(result?.font.size, 99)
63+
}
64+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// HTMLTagStyleAttributeToMarkupStyleVisitorTests.swift
3+
//
4+
//
5+
// Created by zhgchgli on 2023/7/23.
6+
//
7+
8+
import Foundation
9+
@testable import ZMarkupParser
10+
import XCTest
11+
12+
final class HTMLTagStyleAttributeToMarkupStyleVisitorTests: XCTestCase {
13+
func testConvert() {
14+
let visitor = HTMLTagStyleAttributeToMarkupStyleVisitor(value: "test")
15+
16+
17+
XCTAssertEqual(visitor.convert(fromPX: "14px"), 14)
18+
XCTAssertEqual(visitor.convert(fromPX: "14pt"), 14)
19+
20+
}
21+
}

0 commit comments

Comments
 (0)