-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add raw html bind annotation #3362
Conversation
Doesn't this already work with |
Yes, except I consider that an absolutely horrendous hack which is not documented and is simply an artifact of how bindings to attributes work |
If it's a |
Say what you will, but it least it doesn't add new syntax. |
This says "safe," but I don't think it actually does anything to ensure the bound text doesn't do Bad Things any more than |
@arthurevans True, as I mentioned to @BLamy in slack I'm not actually expecting this to be accepted, more start a conversation since binding HTML isn't exactly a niche problem. And right now the only answer is happening upon some github issue somewhere where someone said "inner-h-t-m-l worked for me." Without making Polymer opinionated to Caja or something it would rely on the user sanitizing their HTML. Which they should be doing anyway but "You should have done it right" isn't the best design philosophy, I fully admit. And by "safe" it's meant to be "assumed safe"; naming isn't my forte |
Without knowing a whole ton about it, it seems like it would be prohibitively expensive (both in the library size and in runtime compute) to have a syntax that enforces safety, since it would involve processing and ensuring the sanitization of the HTML. However, there are legitimate uses for inserting HTML declaratively. For instance, in my own case, I need to insert a string dynamically based on a Polymer property, but I also need to wrap a portion of the string with HTML tags dynamically to style incremental search highlights. It is easy to style things with CSS when only the overall presentation is changing, but when the tags and the text content need to change orientation dynamically in response to logic, such as in this case, injecting HTML is the only way to accomplish it. And it would be nice to have a clean declarative way to do that with Polymer and not have to set innerHTML. It might be even nicer to have bindings within the injected HTML, akin to |
To update my previous example:
The current syntax and functionality allows us to do all that's being asked (aside from data bindings in the dynamic HTML). Just find some JS HTML sanitizer (I found this one from a quick Google search: https://github.com/punkave/sanitize-html) and incorporate it into your custom element as a function, then use the function to sanitize your dynamic HTML on-the-fly. |
A real annotation has a few advantages over
And you could do the same sanitization with annotation Also to just preempt any bikeshedding, I completely don't care at all what |
Hey, this PR is totally safe! As long as no one makes any mistakes! More srsly, this is totally a common request, but I think this is a use case where you want syntactic vinegar rather than a sugary sweet and easy to use footgun in the toolbox. Given that there's no simple way to safely support this, I suspect that implementing a If someone really and truly knows what they're doing, they could always write their own |
As @rictic noted, because of the security concerns here, we cannot add additional sugar around binding to raw html to the core library. We plan to allow users to hook into the binding system more directly such that we don't need to arbitrate these types of issues at the library level. |
@sorvell I can't really tell from the issue you linked - does that mean that it would be possible to write a custom element that would allow injecting dynamic data-bound HTML at some point? |
No, that will be covered when we address #1303. |
This adds a new
{# #}
annotation which will bind toinnerHTML
instead oftextContent
. By design it does not work on compound bindings for some reason I can't really justify. After the 5th or so request in the Polymer slack on advice on how to bind a value with html I threw this together. Please don't ban me for this insanity.Usage: