How to include <meta charSet="utf-8" /> in react-testing-library? - 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.

Related

How to add a Bootstrap boilerplate/starter template as a snippet in vs code?

I'm trying to add this Bootstrap 5 starter template as a snippet on vs code but with no success.
I've tried to escape all " characters using \ but still does not work.
I'm not familiar with JSON files.
Do you know how to add this code as a snippet in vscode?
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css" integrity="sha384-r4NyP46KrjDleawBgD5tp8Y7UzmLA05oM1iAEQ17CSuDqnUK2+k9luXQOfXJCJ4I" crossorigin="anonymous">
<title>Document</title>
</head>
<body>
<!-- Optional JavaScript -->
<!-- Popper.js first, then Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/popper.js#1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.min.js" integrity="sha384-oesi62hOLfzrys4LxRF63OJCXdXDipiYWBnvTl9Y9/TRlw5xlKIEHpNyvvDShgf/" crossorigin="anonymous"></script>
</body>
</html>
There are a few talking points on your approach.
JSON strings are not string literals, meaning you can not simply add a new line to them, you have to use a \n character where applicable; if you wish to keep it all in one string:
"body" : "<!doctype html>\n<html lang="en">\n<head> .... etc"
You can not use a string delimiter in your snippet without escaping it, that's not snippet or even vscode specific, that's just how most 'language' rules are:
"body" : "<html lang="en">"
^ ^
When you do this it effectively means you want the body to be: <html lang= because you ended the string and then restarted it after en
Although you can keep it in a single string value, a better approach, for readability at least, is to use an array of values:
"body": [
"<!doctype html>",
"<html lang=\"en\">",
.. etc
]
Full snippet will look like the following:
"Bootstrap 5": {
"description": "Bootstrap 5 starter template",
"prefix": "bootstrap",
"body": [
"<!doctype html>",
"<html lang=\"en\">",
"\t<head>",
"\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">",
"\n\t\t<!-- Bootstrap CSS -->",
"\t\t<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com\" />",
"\n\n\t\t<title>Document</title>",
"\t</head>",
"\t<body>",
"\t\t$0",
"\n\n\n\t\t<!-- Optional JavaScript -->",
"\t\t<!-- Popper.js first, then Bootstrap JS -->",
"\t\t<script src=\"https://cdn.jsdelivr.net/npm/popper.js#1.16.0/dist/umd/popper.min.js\" integrity=\"sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo\" crossorigin=\"anonymous\"></script>",
"\t\t<script src=\"https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.min.js\" integrity=\"sha384-oesi62hOLfzrys4LxRF63OJCXdXDipiYWBnvTl9Y9/TRlw5xlKIEHpNyvvDShgf/\" crossorigin=\"anonymous\"></script>",
"\t</body>",
"</html>"
]
}
Syntax:
$0 means 'end here'
\t means 'add a tab here'
\n means 'add a new line here'
If you don't want to add new lines or new tabs you don't have to, if you have the setting to 'format on save' set to true it will correct itself, but it does look better to include them.
Resources
vscode has a thorough overview of how to create custom snippets and shorcuts and advanced snippets using variables; please review for further guidance here

find redirect META in DOMDocument with DOMXPath

I have the following HTML:
<html>
<head>
<meta http-equiv="refresh" content="0;URL=http://amazingjokes.com" />
</head>
</html>
I want to find the META with the redirect, so I wrote the following XPath query:
/html/head/meta[#http-equiv="refresh"]
However, the '-' in 'http-equiv' is causing an error:
Invalid regular expression: //html/head/meta[#http-equiv="refresh"]/:
Range out of order in character class
How can I properly rewrite the xpath query to be able to find the meta redirect?
I experimented with this, when I remove the '-' from the HTML code and the query things work as expected, but unfortunately the 'http-equiv' is a set standard, so I can not change that. This experiment showed me I am very close...
However, the '-' in 'http-equiv' is causing an error:
Invalid regular expression: //html/head/meta[#http-equiv="refresh"]/:
Range out of order in character class
Obviously, the XPath engine you are using is buggy.
The XPath expression used in the question is a valid XPath 1.0 expression and it selects the wanted <meta> element.
Here is an XSLT - based verification:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select="/html/head/meta[#http-equiv='refresh']"/>
</xsl:template>
</xsl:stylesheet>
When the transformation above is applied on the provided XML document:
<html>
<head>
<meta http-equiv="refresh" content="0;URL=http://amazingjokes.com" />
</head>
</html>
the XPath expression is evaluated and the selected in this evaluation node is output:
<meta http-equiv="refresh" content="0;URL=http://amazingjokes.com"/>

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

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.

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.

Changing <title> with Lift

Is it possible to dynamically switch the title of a page that is served by Lift without having to write an extra snippet for that particular case?
One option is of course <lift:mySnippet><title>Default Title</title></lift:mySnippet> but I thought there might be an option along the lines of <head_merge><title>New Title</title></head_merge> (which inserts a second title node).
I do not like the first approach since I do not want to stick all the title generation logic into a single snippet and ask what kind of page I am on etc.
Have you tried to use templates?
You can define template in templates-hidden/default.html like this:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:lift="http://liftweb.net/">
<head>
<title>
<lift:bind name="title" />
</title>
...
</head>
<body>
<lift:bind name="content" />
</body>
</html>
And use it in index.html for example:
<lift:surround with="default">
<lift:bind-at name="title">Home</lift:bind-at>
<lift:bind-at name="content">
my content
</lift:bind-at>
</lift:surround>
You can find more information about templates here:
http://www.assembla.com/spaces/liftweb/wiki?id=liftweb&wiki_id=Templates_and_Binding
One way is to use the Menu.title snippet.
In bootstrap/liftweb/Boot.scala you define the sitemap with page names:
class Boot {
def boot {
// ...
def sitemap = SiteMap(
Menu.i("Home") / "index",
Menu.i("About") / "about")
// ...
}
}
In templates-hidden/default.html you use the snippet:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:lift="http://liftweb.net/">
<head>
...
<title class="lift:Menu.title">AppName:</title>
...
Then page titles will be: "AppName: Home" and "AppName: About". This is nice if you use
<span class="lift:Menu.builder"></span>
to build the menu, because page titles will be the same used in the menu.
Another approach is to use head merge and define the title in the page's html. For this to work, you have to remove the <title> tag from templates-hidden/default.html and put an <head> or <head_merge> tag in your content block:
<!DOCTYPE html>
<html>
<body class="lift:content_id=main">
<div id="main" class="lift:surround?with=default;at=content">
<head_merge>
<title>TITLE OF THIS PAGE HERE</title>
</head_merge>
...