pc035860 / angular-highlightjs

AngularJS directive for syntax highlighting with highlight.js
http://pc035860.github.io/angular-highlightjs/example/
MIT License
294 stars 53 forks source link

C++ "<" and ">" not rendering properly #59

Closed alexgarces closed 8 years ago

alexgarces commented 8 years ago

Hello,

I have this code snippet inside the directive:

<div hljs hljs-language="cpp">
    std::vector<cv::Point> init_positions;
</div>

It is rendered as an HTML tag, so at the end of the code it appears a weird closing tag:

</cv::point>

I tried with &lt; and &gt; and they still show as &lt; and &gt;. Also when I put just a < character, it renders like &lt;, even when there's no hljs-no-escape set at the directive.

It's funny because when working with HTML code I didn't have any problem.

pc035860 commented 8 years ago

Hi @alexgarces ,

The weird closing tag is added by browser since the HTML inside hljs directive element will get parsed by the browser even before AngularJS bootraped.

I think the reason that the same issue doesn't happen on HTML codes is usually you'll have a well-formed HTML code inside hljs. And in the C++ case, <cv::Point> is a tag that browser doesn't recognize and there's no closing tag for it.

To deal with most of these issues, use hljs-source or hljs-include rather than putting the code inside hljs directive directly.

<div hljs hljs-language="cpp" hljs-include="'cpp-example'"></div>

<script type="text/ng-template" id="cpp-example">
std::vector<cv::Point> init_positions;
</script>
alexgarces commented 8 years ago

It worked! Thanks a lot.