JavaScript try/catch doesn't work when <meta charset="utf-8" /> is defined - throw

I have a very simple JavaScript function that uses try/catch, though when the meta tag <meta charset="utf-8" /> is defined on the page, it doesn't work:
<!doctype html>
<meta charset="utf-8" />
<script type="text/javascript">
function demo(){
try{
undefinedfunction()
alert("I guess you do exist")
} catch(e){
alert("An error has occured: "+e.message)
}
}
</script>
<form method="post">
<b>Demo:</b> <input type="button" value="Click Me" onClick="demo()" />
</form>
In the above case instead of catching the undefinedfunction(), the console logs "Syntax Error: illegal character". If I remove the meta tag, the function behaves as expected, alerting a message.
Does anyone know why this is the case?

Ah nevermind, it seems the problem was with the spaces inside the function, instead of tabs. I guess "utf-8" is picky about those kinds of things.

Related

How to include <meta charSet="utf-8" /> in react-testing-library?

I'm using jest-image-snapshot with react-testing-library so that I can run some visual regression tests. The issue I'm having is that I need to define <meta charSet="UTF-8" /> (otherwise my image-snapshots don't render correctly), however react-testing-library strips out all <html>, <head> and <body> tags.
If I wrap my component like so:
<html>
<head>
<meta charSet="utf-8" />
</head>
<body>
<ComponentToTest />
</body>
</html>
Then it ends up rendering as:
<DocumentFragment>
<meta charset="UTF-8" />
<ComponentToTest />
</DocumentFragment>
Which of course doesn't set the encoding to UTF-8 as it's not within <head>.
The result of this is that any components that include a unicode character will render with a weird character instead.
For example, using ​ (zero width space) will render as â€<. This results in my image-snapshots looking like this: https://i.imgur.com/fSK5gJ0.png
Is there any workaround here to define the character encoding?
EDIT: It seems I can change the container element (https://testing-library.com/docs/react-testing-library/api#container), but I can't define the container as { container: document.documentElement } as apparently it needs an .appendChild() to work. Is it possible to define the container element as the <html> itself? That would solve my issue if it's possible.

Displaying XML using google-code-prettify

I can't seem to get google prettify to work with basic XML: anyone got this to work, or can see what I am doing wrong: here is my code:
<html>
<head>
<meta charset="utf-8" />
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js?autoload=true&skin=sunburst&lang=xml"></script>
</head>
<body>
<h1>XML Output</h1>
<pre class="prettyprint" id="quine" style="border:4px solid #88c">
<Rest_appt_pull licenseKey="123" passWord="456" start="30-oct-2014 00:00:00" finish="31-oct-2014 23:59:59" p_method="event">
<timings>
<entry label="Read"
time=".03"
segment=".03" />
<entry label="Processing XML"
time=".04"
segment=".01" />
</timings>
</Rest_appt_pull>
</pre>
</body>
</html>
Any help would be gratefully appreciated
The root cause is html tag symbol.
Change < to <
Change > to >
You need to html encode your xml code example first. You can use one of available online tools to do that. Then just wrap it in pre/code
<pre class="prettyprint lang-xml"> ... your html encoded xml code ... </pre>
and attach js code on the site.

GWT, RootPanel.get() is null

I'm following a beginner's tutorial at http://www.tutorialspoint.com/gwt/gwt_style_with_css.htm.
The code for the HelloWorld.html file is:
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript" src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
<div id="mytext"><h1>Hello, World!</h1></div>
<div id="gwtGreenButton"></div>
<div id="gwtRedButton"></div>
</body>
</html>
In the HelloWorld.java (extending EntryPoint), I have:
RootPanel.get("gwtGreenButton").add(Btn1);
RootPanel.get("gwtRedButton").add(Btn2);
The 2 lines: RootPanel.get() always result in null. I don't know what happen, what to check? (too bad the site doesn't have a comment/discussion section)
Thanks.
Docs says
RootPanel.get(java.lang.String id)
Gets the root panel associated with a given browser element.
DOM.getElementById(java.lang.String)
Gets the element associated with the given unique id within the entire document.
Try with
com.google.gwt.user.client.DOM.getElementById("gwtGreenButton");
com.google.gwt.user.client.DOM.getElementById("gwtRedButton");

HTML5 Valid Form Input/Fields

I'm a little confused as to what is considered valid markup for HTML5 input fields.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Site Name</title>
</head>
<body>
<form name="contact" method="post" action="/">
<p><input type="input" name="first_name" maxlength="255" /></p>
</form>
</body>
</html>
When I run this through the validator at w3.org I get the error Bad value input for attribute type on element input. with the /> highlighted in red. I look at the HTML-Tidy version it creates and it tells me to write it like this instead:
<p><input type="input" name="first_name" maxlength="255"></p>
But then when I validate that I then get the same error but with just the > highlighted in red. Then looking at the HTML-Tidy to see what it has corrected it to and it leaves is the same as if it's okay, but the error is still there. Is this considered valid HTML5 markup? Or is there a specific way of doing this?
type="input" is an invalid value of the attribute type of the input element.
This guide on the input element shows the allowed type attributes. Or check the HTML5 additional input types if you require one of those.
For a regular textfield you need to set the type attribute to type="text".
Input type "input" is not correct, you probably intented to use type="text".

embed multiple kid template files into a main one

In the main kid template file, I want it to have only div tags, each of which do only call a rendered kid file and paste content inside it. (like "include" function in php) but I don't know how to do this. Does someone have any ideas about it?
If you swap to genshi instead of the default kid you can do this with an include tag:
<xi:include href="menu.html" />
Swapping to genshi is fairly easy, I think its a matter of confuration only. The templates tags works otherwise the same. You should rename the extensions from .kid to .html though.
You can first define a "base_layout.kid" template:
<html xmlns:py="http://purl.org/kid/ns#">
<head>
<title>App Name - ${page_title}</title>
<link href="layout.css" type="text/css" rel="stylesheet" />
${page_specific_css()}
</head>
<body>
<h1>Now viewing: ${page_title} of App Name</h1>
<content>Default content</content>
<div class="footer">Page Footer Text</div>
</body>
</html>
Then replace the "content" tag in "page.kid" with whatever data you want:
<html py:layout="'base_layout.kid'"
xmlns:py="http://purl.org/kid/ns#">
<link py:def="page_specific_css()"
href="layout.css" type="text/css" rel="stylesheet" />
<div py:match="item.tag == 'content'">
<ul>
<li>Content Item 1</li>
<li>Content Item 2</li>
<li>Content Item 3</li>
</ul>
</div>
</html>
You can check whether you get the correct html in python shell (after removing all the identifiers used):
>>> import kid
>>> t = kid.Template("page.kid")
>>> print t.serialize()