ismarslomic / angular-read-more

AngularJS Read More Directive - without jQuery dependency
BSD 3-Clause "New" or "Revised" License
66 stars 44 forks source link

How to add html attributes to hm-text #6

Open naveen305 opened 8 years ago

naveen305 commented 8 years ago

Thanks for plugin, my data is coming dynamic it is in the form of html, and it is displaying html tags in the front end.

readmore

http://codepen.io/anon/pen/zrBpMG

Thanks!

ismarslomic commented 8 years ago

HTML is not preserved in output text. This can be solved by use of ngBindHtml directive. I can fix this in three weeks or so, but if you want to raise a pull request I will gladely merge it.

ismarslomic commented 8 years ago

Fixed in 63fd429

sam2x commented 8 years ago

Hey, thx for this pull, but i spotted a pretty annoying bug. This more/less parts split the text in two, so if the limit defined fall in 'html' tags, it will break the parsing.

Example case:

Text: "<h3>My TItle</h3><span style='text-align:center'> Hey this is a long text that would not appear in less part!!!! </span>"
Limit: 20
Result of less: "<h3>My TItle</h3><span"
Result of more : " style='text-align:center'> Hey this is a long text that would not appear in less part!!!! </span>"

It need more works :/, i'm trying to figure out a fix that would not require DOMParser, and others complicated algorithm to find parent tag, etc..

sam2x commented 8 years ago

I have came to a pseudo code to solve this issue (i have to use DOMParser unfortunaly).

Let's say the initial input text (contaning html) is htmlInput:

For lessText

     DOMParser(htmlInput)  
     -> set _current_less_length_ to 0
        -> recursively parse each node and get the length (_current_node_length_)
          -> if (_current_less_length_ + _current_node_length_ > limit)
            -> truncate current node content to (limit-current_less_length) 
            -> return & close potential pending opened tag
          -> else add node to lessText and return

moreText:

Html Template:

sam2x commented 8 years ago

Please, take a look at my pull request.https://github.com/ismarslomic/angular-read-more/pull/23 and let me know if you can merge/update bower & package.

Regards

ajayreddy56 commented 8 years ago

when i have

<p ng-bind-html="articleText" data-add-ellipsis></p>

is not working can you look into this

ismarslomic commented 7 years ago

@sam2x how would this PR affect performance and could you please add few unit tests?

sam2x commented 7 years ago

Hey, as i said on my PR about benchmark I don't know how to add unit test to this kind of js stuff, sry. Will let someone else do it. I have personally not seen any perf problem in my project (i use < 5000 characters, so can't really tell the way people use this plugin...).

But, this PR need a little rework :

Apart from that, this patch works great. Will check this week-end if i have time to enhance it ;)

sam2x commented 7 years ago

Ok, good news, solved all the problems :

http://codepen.io/sam2x/pen/GmYXaj

There is 2 examples (default usage, and one with the new param 'hm-html'). I also solve the animation problem i had with html code. Here my note :

Summary

So:

Please try, and let me know what you think.

Regards