gonzalezreal / swift-markdown-ui

Display and customize Markdown text in SwiftUI
MIT License
2.26k stars 267 forks source link

Crash when text has html tags #45

Closed ppoh71 closed 3 years ago

ppoh71 commented 3 years ago

When i try to display a markup text which contains html tags (also has some malformed tags in it) the app crashes when using MarkdownUI

Tested with this text

**CC-Attribution-NonCommercial**\n\nBased on a scan of a replica of the \"Mayflower\" ship, processed by @spogna and available [here](https://skfb.ly/6u8T6).\n\nLike in the [Mosquito in Amber](https://skfb.ly/6xWCO), most of my attention was spent on normal maps, roughness and refraction settings to try to make this model as realistic as possible.\n\nI actually had to create a secondary internal surface for the bottle, and also relied on clear coat options for the glass material in order to try to give the model a feeling of thickness. \n\nTo try to get the opacity right, I used refraction on the outer layer (normals offset checked), and alpha-blending on the inner one.\n\nAll of the modeling was done with Blender, but the critical part for the model aesthetics relies on Sketchfab materials and post-processing.\n\n<br>\n>\n> Another ship in a bottle\n> \n> Has got no place to sail\n> \n> To see the waves getting darker every hour\n> \n> The stars have lost their power\n> \n> In the sky\n> \n\n<br>\n**Beck**, *Ship in a bottle*

Screenshot from Xcode

I solved this by stripping out html tags.

func deleteHTMLTags() -> String { return self.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil) }

The problem is that when you get markdown text from an api and which can contain malformed Markdown that includes html tags or other user created malformed tags, it could crash your app.

Questions if this this should or could go into MarkdownUI ?

gonzalezreal commented 3 years ago

I can't reproduce the crash with the text you provided, neither in the simulator nor in my device. Tested with Xcode 12.4 / iOS 14.4.2.

Here's a couple of screenshots:

IMG_1750 IMG_1751

ppoh71 commented 3 years ago

i had another look and you're right. The problem was on my site and how i structured my views. I used a state var to show/hide the text and also a function to get the text. The problem lies somewhere there.

`if self.showMoreDescription {

Markdown(Document(getDescrption(showMore: true)) ).markdownStyle( DefaultMarkdownStyle( font: .system(size: 12, weight: .regular), foregroundColor: .white, codeFontSizeMultiple: 1 ) ).accentColor(Constants.yellow) }`

I'm closing the issue. Sorry for the trouble...

jiangdi0924 commented 3 years ago

i had another look and you're right. The problem was on my site and how i structured my views. I used a state var to show/hide the text and also a function to get the text. The problem lies somewhere there.

`if self.showMoreDescription {

Markdown(Document(getDescrption(showMore: true)) ).markdownStyle( DefaultMarkdownStyle( font: .system(size: 12, weight: .regular), foregroundColor: .white, codeFontSizeMultiple: 1 ) ).accentColor(Constants.yellow) }`

I'm closing the issue. Sorry for the trouble...

@ppoh71 Hi, i find the same issue, when i use html render the markdown, page reopen crashed. Do you fix it ?

kevoniori commented 2 years ago

How do you render HTML markdown using Markdown UI? I just get the plain Html tags and text