Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interesting bug due to Swift's set indeterminism #4

Open
zntfdr opened this issue Jun 1, 2022 · 2 comments
Open

Interesting bug due to Swift's set indeterminism #4

zntfdr opened this issue Jun 1, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@zntfdr
Copy link

zntfdr commented Jun 1, 2022

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))
      }
    }
  }
}

Result:

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:

for tag in tags {
if let action = availableTags[tag] {
textChunk = action(textChunk)
}
}

...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

@Iaenhaall Iaenhaall added the bug Something isn't working label Jun 2, 2022
@Iaenhaall
Copy link
Owner

Thank you for the feedback and the research. I will try to investigate the problem and fix it as soon as I have some free time.

@LukasHakulin
Copy link

Hi guys!
I'm just looking for some simple html formatting solution with iOS 14 support (I'm new in swiftUI world) and I discover this library.
Unlikely I'm not able to use it for my purposes, but I spend a couple of hours playing with it and also checked known issues (just for sure :D).
I think what are you describing here isn't bug, it's just problem of your test, you are modifying results and this is the real reason why it's not working.
Try that again, but "simpler":

struct ContentView: View {

    var body: some View {
        VStack(spacing: 20) {
            Group {
                Text("bold, italic, underline")
                    .fontWeight(.bold)
                    .italic()
                    .underline()
            }.background(Color.green)
            Group {
                Divider()
                Text("bold, italic, underline")
                    .bold()
                    .italic()
                    .underline()
                    .font(.system(size: 20))
                Text("bold, underline, italic")
                    .bold()
                    .underline()
                    .italic()
                    .font(.system(size: 20))
            }
            .padding(8)
            Group {
                Divider()
                Text("bold, italic, underline")
                    .fontWeight(.bold)
                    .italic()
                    .underline()
                    .font(.system(size: 20))
                Text("bold, underline, italic")
                    .fontWeight(.bold)
                    .underline()
                    .italic()
                    .font(.system(size: 20))
                Divider()
                Text("italic, bold, underline")
                    .italic()
                    .fontWeight(.bold)
                    .underline()
                    .font(.system(size: 20))
                Text("italic, underline, bold")
                    .italic()
                    .underline()
                    .fontWeight(.bold)
                    .font(.system(size: 20))
                Divider()
                Text("underline, bold, italic")
                    .underline()
                    .fontWeight(.bold)
                    .italic()
                    .font(.system(size: 20))
                Text("underline, italic, bold")
                    .underline()
                    .italic()
                    .fontWeight(.bold)
                    .font(.system(size: 20))
            }
        }
        .padding()
    }
}

Screen Shot 2023-04-16 at 12 11 15

From my point of view it looks like you have modified the result with application of Font on Group with tested data(Text) in combination with using .bold() instead of .fontWeight(.bold) cause this is the real implementation of <b> tag in library.
Probably the easiest way to fix it is use .bold() as modifier.

Because ordering of modifiers matter, it's up to programmer to choose well to avoid formatting issues... or wait to iOS 15 and use AttributedString or some other solution ¯_(ツ)_/¯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants