-
Notifications
You must be signed in to change notification settings - Fork 8
Description
TL;DR: it seems that Swift's set is causing a glitch in this library in HTML2TextParser.
Problem
Sometimes the italics effect is not applied to the following text:
<b><u><i>bolded underlined and italized</i></u></b>, normal text.
Reproduction code:
struct ContentView: View {
var body: some View {
List {
ForEach(1..<100) { _ in
AttributedText("<b><u><i>bolded underlined and italized</i></u></b>, normal text")
.font(.system(size: 20))
}
}
}
}Every time the app is launched, different rows will have the glitch above.
Investigation
Initially, I thought it was a SwiftUI issue, however, this code works fine:
struct ContentView: View {
var body: some View {
List {
ForEach(1..<100) { _ in
Group {
Text("bolded underlined and italized")
.bold().italic().underline() // any sorting of this works fine in my tests.
+
Text(", normal text")
}
.font(.system(size: 20))
}
}
}
}After digging into the library code, I noticed that in HTML2TextParser's addChunkOfText(_:) the following code is executed:
...where tags is a Set<String>.
Because Swift sets are unordered, every time the for tag in tags code is run, a new sequence of tags is produced and different parses of the same text (like the one above) will produce a different output.
In fact, if we replace the code for tag in tags with for tag in tags.sorted(), for every parse of the same text the same output will be produced. This is great, we've reached determinism. Except, now the glitch above is always present:

The fix was to use another sorting order, this one works for this case: for tag in tags.sorted(by: >):

I'm not sure why having different tags orders cause the issue, if you have any idea, please let me know!
Thank you
