ZhgChgLi / ZMarkupParser

ZMarkupParser is a pure-Swift library that helps you convert HTML strings into NSAttributedString with customized styles and tags.
https://link.zhgchg.li/
MIT License
311 stars 24 forks source link

Improve breakLine handling #45

Closed JodliDev closed 9 months ago

JodliDev commented 10 months ago

Fix1:

When evaluating container tags (like div or p) they always have a break line before them. Which in general is what we want. But when the container tag is at the start, this would lead to an empty line before it.

So for example this <div>This is one line</div> is evaluated as:


This is one line

instead of (notice that there is to empty line above):

This is one line

Or more importantly this:

<ul>
<li><div>Item</div></li>
<li><div>Item</div></li>
</ul>

would create something like this:

Fix2:

Having something like this A<br><br><br>B would be evaluated as:

A

B

instead of:

A

B

I hope this is helpful for you :)

Disclaimer 1: I tested my changes with data from the app, where the library is used and I am content that it works as expected. But I did not run (or fix) any UnitTests because my knowledge of pure library development is limited.

Disclaimer 2: In case Fix 2 breaks intended behaviour, you can just revert my change in this line: https://github.com/JodliDev/ZMarkupParser/blob/2c6a74edc37678efd888d41c07bcdab7d0500bab/Sources/ZMarkupParser/Core/Processor/MarkupNSAttributedStringVisitor.swift#L23

JodliDev commented 10 months ago

I added a fix for an unexpected source of crashes (when the HTML string is empty).

About the failing tests: At the moment I am on a tight schedule, so I dont really have the capacity to look into library development and figure out how to run tests (I assume its not that complicated, but I don't really have the head space right now đŸ˜… ). So please see this PR as a starting point for improvement. I you want to just "steal" my changes and close this PR, I am fine with that as well :)

zhgchgli0718 commented 10 months ago

will fix tests and review later, thanks.

zhgchgli0718 commented 10 months ago

I still working on it.

zhgchgli0718 commented 9 months ago

Thank you for your contribution, but in the latest version 1.8.0, I have completely refactored the line break logic and implemented it to directly match the browser's line break behavior. Therefore, this pull request is no longer needed. Thank you again for your contribution.