Skip to content

Commit

Permalink
Fix indexes when working with characters containing multiple unicode …
Browse files Browse the repository at this point in the history
…scalars
  • Loading branch information
kean committed Apr 5, 2022
1 parent ab2800b commit 0804f33
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Source/Formatting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ private final class Parser: NSObject, XMLParserDelegate {
let output = NSMutableAttributedString(string: text)
// Apply tags in reverse, more specific tags are applied last.
for (range, attributes) in attributes.reversed() {
let lb = text.index(text.startIndex, offsetBy: range.lowerBound)
let ub = text.index(text.startIndex, offsetBy: range.upperBound)
let range = NSRange(lb..<ub, in: text)
output.addAttributes(attributes, range: range)
}
return output
Expand Down
3 changes: 2 additions & 1 deletion Tests/FormattingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ class FormattingsTests: XCTestCase {
])

// WHEN
let input = "⚠ Text with <t>emoji</t>"
// important: ⚠️ contains two unicode scalars
let input = "⚠️ Text with <t>emoji</t>"
let output = NSAttributedString(formatting: input, style: style)

// THEN
Expand Down

0 comments on commit 0804f33

Please sign in to comment.