ZhgChgLi/ZMarkupParser

Support parse `<table>` like `NSAttributedString.DocumentType.html` (by using NSTextTableBlock)

zhgchgli0718 opened this issue · 0 comments

After researching how the 'NSAttributedString.DocumentType.html' renders HTML <table> tag,

I found something interesting. It turns out that 'NSAttributedString.DocumentType.html' uses 'NSTextTableBlock' in 'NSParagraphStyle' to format table results. However, 'NSTextTableBlock' is not a publicly accessible API in iOS, so there isn't anything we can do about it.

ref: https://stackoverflow.com/a/25537864/1093917

sample code:

let data = "what<table><tr><td>1</td><td>2</td></tr><tr><td>a</td><td>b</td></tr></table>test".data(using: String.Encoding.utf8)!
let attributedOptions:[NSAttributedString.DocumentReadingOptionKey: Any] = [
      .documentType :NSAttributedString.DocumentType.html,
      .characterEncoding: String.Encoding.utf8.rawValue
]
let result = try! NSAttributedString(data: data, options: attributedOptions, documentAttributes: nil)

output:

"Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n    \"<NSTextTableBlock: 0x60000392d6e0>\"\n), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0 PresentationIntents (\n) ListIntentOrdinal 0 CodeBlockIntentLanguageHint \'(null)\'"

You can see the displayed text above: Blocks (\n "<NSTextTableBlock: 0x60000392d6e0>"\n), but we can't access it through NSParagraphStyle.

I have no idea...