MDHTMLLabel icon indicating copy to clipboard operation
MDHTMLLabel copied to clipboard

problems with certain HTML entities (ex: <)

Open sspitzer opened this issue 10 years ago • 7 comments

MDHTMLLabel doesn't handle HTML entities, for example < and >

Say I want to display this exact string in MDHTMLLabel:

"1 < 2, I <3 MDHTMLLabel, 2 > 1"

label.htmlText = @"1 < 2, I <3 MDHTMLLabel, 2 > 1";

You can't. You'll end up with "1 1", see the attached screen shot.

screen shot 2014-03-12 at 12 08 34 pm

It'd expected this to work:

label.htmlText = @"1 < 2, I <3 MDHTMLLabel, 2 > 1";

But it will appear the same!

The issue is here:

https://github.com/mattdonnelly/MDHTMLLabel/blob/master/MDHTMLLabel/MDHTMLLabel.m#L1339

The code is decoding the HTML entities before it finds styles and extract links.

I think the correct fix is to move the code to decode the HTML entities to after it finds styles and extract links. But just moving the code in MDHTMLLabel to decode the HTML after we do the detection won't work, as the offsets for all the links and style components will be incorrect.

Note that RTLabel seems to have a similar bug, see https://github.com/honcheng/RTLabel/pull/47

sspitzer avatar Mar 12 '14 19:03 sspitzer

Keeping @aprato in the loop.

sspitzer avatar Mar 12 '14 19:03 sspitzer

Sorry for not acknowledging this issue until now. I've got some exams coming up soon but will be happy to take a look in to this afterwards

mattdonnelly avatar Mar 27 '14 21:03 mattdonnelly

@sspitzer @aprato @mattdonnelly I did a quick hack by replacing "<" and ">" to "≪" and "≫" before the link detection and then replacing them back later into "<" and ">".

Also in my use case i would like to disable auto link detection and let the class only detect links that i embeded in my NSString. So i added @property (nonatomic) BOOL autoDetectUrl; (default to NO in commonInit) and check if (self.autoDetectUrl) _htmlText = [self detectURLsInText:_htmlText];

yinsee avatar Jul 22 '14 12:07 yinsee

@yinsee we ended up doing the same hack, but used "〈" and "〉"

sspitzer avatar Jul 22 '14 15:07 sspitzer

@sspitzer nice :)

yinsee avatar Jul 27 '14 09:07 yinsee

@yinsee thanks for the tip about disabling auto link detection, I think we will want that, too.

sspitzer avatar Aug 27 '14 22:08 sspitzer

@yinsee created a PR with your idea, as we wanted the same thing, see https://github.com/mattdonnelly/MDHTMLLabel/pull/27

In our code, we do:

label.autoDetectUrls = NO;

sspitzer avatar Aug 29 '14 21:08 sspitzer