Confusion rendering unescaped HTML and newlines - angularjs

I have a forum like site, where people should be able to add strings like:
<<<<<
>.<
etc.
Also I want to preserve the new lines. Besides I replace many newlines in the server with 1.
For first feature I found the solution is to use ng-bind-html="myText"
that works.
But I have a problem with the newlines, not matter what I do, they are not displayed.
If I don't do anything (also no replacements in the server), they are rendered as newlines in the source and not displayed.
If I replace them with <br> or <br/> before rendering, they show as source -> <br> or <br/>.
If I don't use ng-bind-html anymore, and render the text as normal expression, I get escaped html: <br> (besides, in this case, the strings mentioned first also don't work).
What do I have to do? Thanks in advance!

It's more of a CSS issue. Use: white-space: pre;.

Related

Angular template variable replace markup on the fly

I am working on a heavy dataset.
so I am placing a nested variable inside a table
{{entry.text}}
but this can have several names like
Clare Butterfield /n/r Barry Burton
--
really I want to replace the carriage returns with a br tag
so each name goes on another line
{{(entry.text).replace("/n", "<br>")}}
You'll need two pieces to complete this:
1) A filter that will take in the text and replace /n with <br/>, and
2) The ng-bind-html directive to get angular to use that <br/> instead of just displaying Clare Butterfield <br/> Barry Burton
in the end your html will look something like this:
<span ng-bind-html="::entry.text | newLineFilter"/>
For an example of a similar filter to what you need, see this SO question here:
Angular filter to replace all underscores to spaces
For the ngBindHTML directive, see here:
https://docs.angularjs.org/api/ng/directive/ngBindHtml
any questions?
p.s. since your data-set is large you may notice that I added 2 colons in the example above to indicate that it's using one-time binding. This will prevent angular from recalculating the value too often and save on performance. Those aren't strictly necessary, so if you find your text is not updating when you expect it to, just remove them.

How to escape single quotes in ng-init

Let's say we are passing data from apache velocity to angularjs and the data is some string that contain quotes,
the error on screen:
Error: [$parse:lexerr] http://errors.angularjs.org/1.2.22/$parse/lexerr?
p0=Unterminated%20quote&p1=s%20327-
my code :
<span ng-init='draftDemands=$draftDemands;'>
how to solve this problem
When i use $esc.xml($draftDemands), it work my last example,
Are you escaping characters in Velocit's generated code? https://velocity.apache.org/tools/2.0/apidocs/org/apache/velocity/tools/generic/EscapeTool.html
Check also how angular does escaping via: Strict Contextual Escaping. So there is no need to render characters as HTML explicitly.
This discussion could put some more light on the case.
This should also work: how to pass special characters into ng-init in angularjs from python

Sublime Text 2: Different language highlighting based on context? (a la Webstorm)

I was watching some videos on Egghead.io about AngularJS. The creator of the videos uses Webstorm (and, I believe, works for them). One feature I noticed is that he can set different syntax highlighting within different scopes or quotation marks. So, in code like the following (from an AngularJS directive)
return {
template: '<div>something</div>',
// ^^^ these guys ^^^
}
...he can get the inside of the quotation marks to highlight as HTML.
I use Sublime Text 2, and am fairly wedded to it. Is there an existing feature/plugin for Sublime that could handle a case like this? If not, is something like this technically possible using the Sublime Text 2 API?
I don't think it's built in, but it's certainly possible. I've been doing some work with graphviz and wanted to do something similar. Labels can be generated with html like syntax. Anyways, I played around with the .tmLanguage file and added a new pattern to match the context where html like entries were valid (I look for label = <). The patterns I used for the captures aren't that good, but it works for fine for me. This give me the following, which I think is similar to what you are looking for.
I don't know anything about AngularJS, so I can't help you with anything specific to that, but it is certainly possible. Note that in the image below, the last <table></table> are just to show that highlighting doesn't occur there.
Edit:
Forgot to include this in the original post, but here is my updated tmLangauage file. That first pattern is what I added(link). I used PlistJsonConverter to go from JSON to plist, then saved the file as .tmLanguage. Hope this helps.
#skuroda is right, I implemented #skuroda's code with an additional plugin to easily edit HTML within an AngularJS directive JS file. The result is HTML syntax highlighting within a directive JS file and additional functionality to remove string related delimiters while editing templates.... Sublime AngularJS HTML Template Plugin

HTMLPurifier: how to escape broken tags instead of removing?

I am using HTMLPurifier for cleaning the post input but I'd like it to escape (html encode) all broken tags or suspicious symbols instead of removing them completely. I have searched through it's docs and this site but without any luck. Still hope that I have missed something.
Opening tag is the most irritating. If someone tries to post a formula or comparison, writes "param1<param2" and does not put space in between, the purifier gets it as a wrong tag opening and completely discards everything on the right side.
I am using htmlspecialchars inside [code] tags, but I want to allow some html outside and cannot encode everything.. That is why I'm filtering it with HTMLPurifier.
Your advice would be appreciated.
Try %Core.AggressivelyFixLt or using %Core.LexerImpl set to DirectLex. I don't know offhand if this will work, it may not.
A partial solution is to set %Core.EscapeInvalidTags; but it's a pretty imperfect fix, and it may mangle some text.

Why are Paragraph spaces being omitted

I have a form which accepts varchar2(5000). It is basically a description field. Now when users enter spces after paragraphs, they are all combined into on paragraph, and not multiple as it is entered.
Why? Ex - This is one paragraph.
This is another paragraph.
Here is what is happening -
This is one paragraph.This is another paragraph.
Are you displaying this data on a webpage or via HTML in some way? If so, then white-space is not handled in a straightforward fashion and that might be causing confusion.
Update - it appears that this is being displayed on a webpage.
You need to do one of several things:
Display your text inside a <pre> element on your page
OR
Replace carriage returns in your text with <br/> chars before sending to the webpage
(You might also need to do something with spaces too, if you need to have multiple of them displayed accurately).
This is nothing to do with databases and only to do with how HTML is displayed.
Have a look at this answer Rendering Plaintext as HTML maintaining whitespace – without <pre>

Resources