Forward slashes in input object do not render in Handlebars #each partial - character-encoding

When trying to render a Handlebars partial, all the forward slashes in my object render as spaces instead.
My input object is like this:
let mySkills = {
"skills": [
{
"name": "HTML5",
"icon": "/img/HTML5.png"
},
{
"name": "CSS3",
"icon": "/img/CSS3.png"
},
{
"name": "JavaScript",
"icon": "/img/JS.png"
}
]
};
The Handlebars partial is like this (I simplified it for this post. I know it won't look right, but the point is that the output code is incorrect):
Handlebars.registerPartial(
'skillsBullet',
'<ul>{{#each skills}}<li style="background-image: url("{{icon}}");">{{name}}</li>{{/each}}</ul>'
);
$('#skills-container').append(Handlebars.compile($('#skills-template').html())(mySkills));
But the output snippet is like this:
Broken background image url
Here is a Codepen. You can inspect the Elements panel to see the bug in the above screenshot: https://codepen.io/car1sle/pen/NWYJeee
I already tried:
Putting backslashes before the forward slashes in the object like this "icon": "\/img\/HTML5.png"
Doing triple curly braces in the Partial like this {{{icon}}}
Doing regular brackets inside the braces in the Partial like this {{[icon]}}

Your issue is due to the fact that your resultant HTML is malformed.
Let's examine this piece of your partial:
<li style="background-image: url("{{icon}}");">
You are using double-quotes (") to wrap both the style= value and the url() value. After Handlebars processing, the resultant HTML becomes:
<li style="background-image: url("/img/CSS3.png");">
This HTML instructs the browser that the style value - between the two double-quotes - is background-image: url(. The browser then tries to determine what to do with the /img/CSS3.png");" and it, because browsers are forgiving of malformed HTML, it ignores it and carries on.
In order to get your code to work properly, you will need to use different quotes for the background-image URL than you use for the style attribute.
<li style="background-image: url(\'{{icon}}\');">
I have forked for your Codepen to provide an example.

Related

Escaping JSON string in Sightly

I have the following Sightly template:
<body>
<script type="application/json" id="test">
[
{
"url": "${test}"
}
]
</script>
</body>
I want to escape the value of 'test' using JSON style escaping. Unfortunately, using context='scriptString' escapes a dash as '\-', which is invalid JSON.
How can I use JSON style escaping on this value?
You need to use context='unsafe' to write JSON like this:
${test # context='unsafe'}
Sightly uses OWASP Encode#forJavascript() for the scriptString context. It seems to be a bug or a missing documentation about escaping dashes.
For URLs you can use context='uri'. For plain text you can use context='text'.

Mathjax not rendering latex formula

Within my rails application I use mathjax (TEX-AMS_HTML) to render latex formulas.
When entering formulas in a comment I use the codecogs Equation Editor with TinyMCE.
My current mathjax config ontop of (TEX-AMS_HTML) is:
"HTML-CSS": {linebreaks: { automatic: true, width: "container" }},
displayAlign: "left",
extensions: ["tex2jax.js","MathMenu.js","MathZoom.js"],
jax: ["input/TeX","output/HTML-CSS"],
TeX: { extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"] },
tex2jax: { ignoreClass: "w1", processClass: "active|comments-content" }
My problem is that some formulas are not rendering out, consider the following:
\[ foo\textup{bar} \]
This renders foo \textup bar. I have also noticed some symbols rendering textually such as:
\[ \AE \SS \]
This would render \AE \SS instead of the appropriate symbols.
I have tried switching to SVG, adjusting inline/display settings and cannot solve this.
Anyone have some idea as to why this is happening?
EDIT: Here is a contained example
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Mathjax</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
"HTML-CSS": {linebreaks: { automatic: true, width: "container" }},
displayAlign: "left",
extensions: ["tex2jax.js","MathMenu.js","MathZoom.js"],
jax: ["input/TeX","output/HTML-CSS"],
TeX: { extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"] },
tex2jax: { ignoreClass: "w1", processClass: "active|comments-content" }
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>
<div id="wrapper">
<div id="main">
<div class="w1">
<div class="comments-content">
<p>\[foo\textup{bar}\AE\SS\]</p>
<p>\[K_{E}=\frac{1}{2}m(\frac{80}{3.6})^{2}\]</p>
</div>
</div>
</div>
</div>
</body>
</html>
Which renders as: http://i.imgur.com/bUJXZPX.png
Notice that whilst mathjax is working and correctly renders the 2nd line, the first line of latex is not escaped.
EDIT2 (22/06/2017):
"Note from the future: cdn.mathjax.org is nearing its end-of-life, check mathjax.org/cdn-shutting-down for migration tips (and perhaps update your answer for future readers)" - Thanks to user Peter Krautzberger
Basically this means the cdn used in my code example will need to be replaced with a localised version of mathjax.
The macros \textup, \AE, and \SS are not part of the default MathJax TeX macros and are not defined in your inline configuration, which is why the rendering in the image has them marked in red.
You'll see a more specific error if you remove the noundefined and noerrors extensions from your configuration -- which are also in the combined configuration TeX-AMS_HTML, so you'll need to drop that as well; as you can see from the link, your inline configuration entails all of TeX-AMS_HTML. (In production, it's preferable to use a combined configuration file since they load as a single large file.)
For a list of all default MathJax macros, see http://docs.mathjax.org/en/latest/tex.html#supported-latex-commands.
For how to define macros in MathJax, see https://tex.stackexchange.com/questions/139699/equivalent-of-mathup-for-upright-text-in-math-mode. From there an example:
MathJax.Hub.Config({
TeX: {
Macros: {
RR: "{\\bf R}",
bold: ["{\\bf #1}",1]
}
}
});
i.e., add a Macros block to the TeX block in your configuration.
(A particular case is\textup, which is a LaTeX text mode macro and MathJax focuses on math mode. Depending on the use case, the math equivalent might be \text{}, \mathrm{}, or something else, see this TeX.SE questions. Of course you can define \textup to be whatever you like but you might run into trouble backporting content to real TeX).

Passing complex information in Angular directives

I am using JQuery UI in an Angular application. I noticed that I was repeating the same pattern for each directive: Call the JQuery UI function with a single complex object for initialization. Rather than write separate directives for each component, I have found it easier to use a stub directive. This may not be a long term solution but it works for now.
Is there a better way to inject the attributes to make the markup more readable while still keeping the generic nature of the directive? Specifically, can I avoid using a JSON string so that it reads more like a normal angular directive.
The existing line:
<button jquiw-control='{"control":"button","options":{"label":"Hello","icons": {"primary": "ui-icon-gear","secondary": "ui-icon-triangle-1-s"}}}' ng-click="jump()"></button>
<button jquiw-control="button" jquiw-button-label="Hello" jquiw-button-icons-primary= "ui-icon-gear" jquiw-button-icons-secondary="ui-icon-triangle-1-s" ng-click="jump()"></button>
Here is a plunk of a working example of my Generic ui directive. http://plnkr.co/edit/eRoOeq
At least you can put the hardcoded JSON in the controller like this
$scope.config = {
"control": "button",
"options": {
"label": "Hello",
"icons": {
"primary": "ui-icon-gear",
"secondary": "ui-icon-triangle-1-s"
}
}
};
and then change the template to
<button jquiw-control='{{config}}' ng-click="jump()"></button>
Plunker: http://plnkr.co/edit/yY1Lc2?p=preview

HTML markup breaking razor using

I have this markup:
#using (Html.BeginRouteForm("Default", new { controller = "Home", action = "Form" }, FormMethod.Post, new { #ID="FormId", #Name="FormId"}))
{
<span>...</span>
<div class="clearFloats"><!-- ---></div>
<span>...</span>
}
And since moving to Razor 2, MVC 4 I now get the error:
The using block is missing a closing "}" character. Make sure you
have a matching "}" character for all the "{" characters within this
block, and that none of the "}" characters are being interpreted as
markup.
If I remove the <div class="clearFloats"><!-- ---></div> code, everything is OK. I know the solution is to change these float clearing divs for a different construct of somesort, but why is it now breaking?
A temporary solution would be to wrap the <!-- --> with #Html.Raw("<!-- -->") which seems to eliminate the error.
Edit: Actually the problem in your example is is the trailing three dashes ---> on the close comment tag. Remove one of those and it works correctly.

Please help me understand output from epubcheck

I am modifying an ebook I purchased. I'm getting stuck on an error that epubcheck finds. The error is:
/path/to/ebook/file.html(43,53): element "blockquote" not allowed here; expected the element end-tag, text or element "a", "abbr", "acronym", "applet", "b", "bdo", "big", "br", "cite", "code", "del", "dfn", "em", "i", "iframe", "img", "ins", "kbd", "map", "noscript", "ns:svg", "object", "q", "samp", "script", "small", "span", "strong", "sub", "sup", "tt" or "var" (with xmlns:ns="http://www.w3.org/2000/svg")
Line 43 of the file in question looks like:
<h4 class="calibre41"><blockquote class="calibre42"><span class="calibre2">SUGGESTIONS FOR GETTING THE MOST FROM YOUR STUDY OF THE SCRIPTURES AND OF THIS COMPANION VOLUME</span></blockquote></h4><div class="calibre43"> </div>
I'm getting many similar messages and I don't know what they mean. Can someone help me understand what is wrong with this line so I can fix it (and the many others)?
Thanks!
A <blockquote> tag is not allowed inside an <h4> tag. This rule applies to HTML in general, not just epub. Change the markup so that it's valid, either by replacing <blockquote> with one of the allowed tags, or removing it. Note: you might lose some CSS styling if you don't preserve the class attribute. You might lose it anyway if the <blockquote> tag is styled explicitly.

Resources