-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathHRElementConverter.swift
46 lines (30 loc) · 1.58 KB
/
HRElementConverter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import UIKit
/// Returns a specialised representation for a `<hr>` element.
///
class HRElementConverter: AttachmentElementConverter {
// MARK: - AttachmentElementConverter
typealias T = NSTextAttachment
func convert(
_ element: ElementNode,
inheriting attributes: [NSAttributedString.Key: Any],
contentSerializer serialize: ContentSerializer) -> (attachment: NSTextAttachment, string: NSAttributedString) {
precondition(element.type == .hr)
let elementRepresentation = HTMLElementRepresentation(element)
let representation = HTMLRepresentation(for: .element(elementRepresentation))
let attributes = combine(attributes, with: representation)
let attachment = self.attachment(for: element)
let intrinsicRepresentation = NSAttributedString(attachment: attachment, attributes: attributes, aztec: ())
let serialization = serialize(element, intrinsicRepresentation, attributes, false)
return (attachment, serialization)
}
// MARK: - Attachment Creation
private func attachment(for element: ElementNode) -> NSTextAttachment {
return LineAttachment()
}
// MARK: - Additional HTMLRepresentation Logic
private func combine(_ attributes: [NSAttributedString.Key: Any], with representation: HTMLRepresentation) -> [NSAttributedString.Key : Any] {
var combinedAttributes = attributes
combinedAttributes[.hrHtmlRepresentation] = representation
return combinedAttributes
}
}