-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Render unescaped HTML #2
Comments
I would just use something like: {! myHtml !} Where the |
How about double |
@tipiirai it will be a problem for the users using other template delimiters. What should use someone using |
Couldn't we just improve the |
Raw tag is actually harder to implement in above way since there is no (easy) way to access the HTML. I prefer a custom delimiters for the job. Not sure why checking I think @aMarCruz is best to know. |
Hi @tipiirai , you are right, and checking for In the new version of tmpl (I'm testing with riot right now) there a little hack that allows compile with |
Taking up this issue and thinking a bit more... For simplicity the compiler needs only one character starting the expression, as in the The real question is how to tell to riot.tag2 (and to the mount function) an expression is raw text? preserving this first non-valid char? This is a problem that I found with the precompiled expressions, in order to support mixed modes. How to know when the expression was already compiled? expressions has no meta-data. Because this, we are using the ...maybe I'm wrong and this is not a problem. Thoughts? |
Thinking more... |
My preference would still be I like |
Double curly braces are used by default by very popular libraries like Angular, Handlebars (Ember), Mustache... Personnally I often put double curly braces by mistake inside my riot tags, because of the mustache background I come from. I am concerned that double curly braces could now work, but without the security provided by HTML escaping. Many users could use it by mistake and have tags that seem to work perfectly, but are vulnerable to XSS attacks. Inserting raw HTML content is an uncommon need, we do not need the syntax to be beautiful or handy. It must be explicit and warns the user about the risks. I vote for an explicit tag or for @GianlucaGuarini proposal with exclamation marks |
Why we need new brackets? the brackets is not the problem. The real problem is riot reads expressions from the DOM. This is the cause of many many issues.
the compiler generates code for call
Another things like We need not depend on the browser for reading the expressions source. I think this is inneficient anyway. In my opinion, we have 2 choises: With both solutions, we lose the ability to set/modify expressions directly into the DOM. EDIT: This also explains why I am denying the use of |
...mmm thinking more... |
@aMarCruz I understand the issue but I think we can just use a simpler solution without changing to much the riot source code: we can simply parse the expressions tagging the ones using a special template delimiter |
The compiler does not escape anything. Unless I'm missing something, conversion from |
...or you mean the compiler must create a special, precompiled function for hidding the desired string from the browser's html parser? |
e.g. assume
...now, riot.tag2 call to an inner
mkdom set div.innerHTML to Later, I'm ok? |
@aMarCruz I guess this are the steps to render unescaped strings in a tag element.
At moment I would not invest too much time working on this feature (we have too many things to do), we can plan it for riot 2.4.0 |
@aMarCruz can you have a look at this feature please? I would like to include it in the next riot release. Let me know if I should work on it as well |
@GianlucaGuarini , sure. |
Ok, This jsbin have 2 samples, one from riot#744, the other from the @GianlucaGuarini sample above, but using I'm using @GianlucaGuarini , The code in this block (only for test) inject the elements in the parent node 'cause we can't use innerHTML here (all the expressions live in #text nodes) and I don't know if it breaks parent-child relations or generates sync issues. So please write this part and test, I will push the PR for the other modules when you are ready. |
thanks @aMarCruz I cant wait to test it :) |
I thought about this feature and I would like that {! raw } Hello { user } I would like that // tmpl(string, data, mustEscape)
var str = '{! raw } Hello { user }',
data = { raw: '<b>Cool</b>', user: '<i>Foo<i>' }
tmpl(str, data, true) // escape only the output of expressions not prefixed with `!`
// => <b>Cool</b> Hello <i>Foo<i>
tmpl(str, data, false || undefined) // behave as the current riot-tmpl
// => <b>Cool</b> Hello <i>Foo<i> @aMarCruz let me know if you need help on this please. This will be part of a riot major release 3.0.0, so this means we can start tagging it |
This issue will be solved in the near future. |
@GianlucaGuarini @aMarCruz I'm using For now, I solved this with a custom method: riot.evalTmpl = function(expression, data, mustEscape) {
expression = String(expression || '').replace(/(\{\s*\!?)\s*([^}]*)\s*\}/g, function(m, prefix, expr) {
if (prefix.indexOf('!') > -1 || !mustEscape) {
return '{ ' + expr.trim() + ' }';
} else {
return '{ _.escape(' + expr.trim() + ') }';
}
});
return riot.util.tmpl(expression, data);
}; What do you think if my approach for the time being? It's similar to your proposal, but of course it's more of a syntactic-sugar preprocessor trick right now. |
@fabien we will completely rewrite |
In the docs it says we can define a raw tag to render unescaped HTML.
Well that works, but it's not very clean.
I would like Riot to support an alternative way of displaying unescaped HTML, why not like this:
If it's not that hard to solve and if it doesn't increase the code size too much...
The text was updated successfully, but these errors were encountered: