$ ('# @ ViewData.TemplateInfo.GetFullHtmlFieldName (string.Empty))
Yes, this is not a good approach overall. Razor will run HTML by default, but the context here is not just HTML, it is:
- identifier inside
- CSS selector inside
- JavaScript string literal, inside
- JavaScript instruction inside
- CDATA HTML element (
<script> )
so just HTML escaping is the wrong thing - any characters that are special in the context of JS selectors or string literals (such as dot, backslash, apostrophe, line delimiters ...) will violate this statement.
Perhaps this is not very important for this specific example, assuming that the result of GetFullHtmlFieldName will always be something safe, but this is not a good assumption for the general case.
C # Generated JavaScript: A bit awkward, and this somewhat strikes some of the benefits of using CSP
Agreed, avoid this. Obtaining caching rights and transferring information from ASP that generates page content to the page that generates the script is usually a pain.
Also, creating JavaScript is not necessarily that simple. ASP and Razor templates do not give you automatic JS-escaper. JSON encoding almost does this, except for the difference between JSON and JS (i.e. U + 2028 and U + 2029).
Hidden form fields
More generally, putting information in the DOM. Hidden form fields are one way to do this; The data- attributes are another commonly used attribute. In any case, I prefer the DOM approach.
When using hidden form fields, you should probably remove the name attribute, as if they were in a real <form> , as a rule, you don’t actually want to pass the data that you passed to JS.
This way you can enter content into the template using the default default HTML escaping. If you need an extra structure, you can always encode JSON data for data transfer. In this case, the jQuery data() JSON.parse will automatically JSON.parse for you.
<body data-mcefields="@Json.Encode(GetListOfHtmlFields())"> ... var mce_array = $('body').data('mcefields'); $.each(mce_array, function() { var element = document.getElementById(this); $(element).tinymce({ ... }); });
(Although ... for the specific case of selecting elements to use the HTML editor, perhaps a simpler way would be to simply use class="tinymce" and apply using $('.tinymce').tinymce(...) ?)