|
| 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 | +} |
0 commit comments