14

Per the title, is it possible to create a (non-trivial) quine in HTML?

My definition of an HTML quine:

A non-trivial HTML quine is one that is not null and uses at least one HTML tag, under the assumption that some string in an HTML file is rendered by a browser as plain text. An HTML quine is defined such that the output of the q.html as rendered by a standard browser is the contents of q.html itself.

(I'm open for any comments on this definition, I kind of hacked it up right now)

HTML is not turing-complete, therefore the fixed-point theorem cannot be applied to prove that it is indeed possible.

However, this does not necessarily mean an HTML quine is impossible. Or can it in fact be proven that an HTML quine is impossible?

Yuval Adam
  • 161,610
  • 92
  • 305
  • 395

4 Answers4

20

It's certainly not possible with "plain" HTML. Obviously it would be possible to do with JavaScript, but it's also possible with CSS:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>This is the title</title>
<style type="text/css"><![CDATA[ 
* {
  display:inline;
  white-space:pre;
}
html:last-child {
  white-space:normal;
}
html:before  {content:'<html xmlns="http://www.w3.org/1999/xhtml">';}
html:after   {content:'</html>';}
head:before  {content:'<head>';}
head:after   {content:'</head>';}
title:before {content:'<title>';}
title:after  {content:'</title>';}
style:before {content:'<style type="text/css"><![CDATA[';}
style:after  {content:']]\00003e</style>';}
body:before  {content:'<body/>';position:absolute;left:0;}
]]></style>
</head>
<body/>
</html>
Thomas W
  • 14,757
  • 6
  • 48
  • 67
  • Nice idea. Firefox is not processing that nicely though. What am I missing? – Yuval Adam Oct 09 '12 at 17:59
  • 1
    Firefox obviously doesn't always realize that it's XHTML and parses it as "normal" HTML, failing to properly parse the CDATA escaped style content. Saving the code as local quine.xhtml makes Firefox handle it proplery for me, though. – Thomas W Oct 10 '12 at 10:59
0

This is not possible. Since every unescaped element will be interpreted as markup by the browser, the browser will attempt to render or handle the contents of those elements or as a control. You cannot display an element that is not escaped in some way, such as with the <pre> tag. But then such tags will also not be displayed.

Of course, you could serve it as text/plain with appropriate HTTP headers but, then, that's not HTML. Just using the .html extension will make browsers render it as html anyway in most (all?) cases.

Rob
  • 14,746
  • 28
  • 47
  • 65
-1

You can't.

<!DOCTYPE html>
<html>
<head>
<title></title>
<head>
<body>
<div id="quine" ></div>
<script>
document.getElementById("quine").innerHTML = document.documentElement.innerHTML.replace(/\&/g, "&amp;" ).replace(/\</g, "&lt;" ).replace(/\>/g, "&gt;");
</script>
</body>
</html>

You can cheat though and do

<body onload="document.getElementById("quine").innerHTML = document.documentElement.innerHTML.replace(/\&/g, "&amp;" ).replace(/\</g, "&lt;" ).replace(/\>/g, "&gt;");" >
<div id="quine" ></div>
</body>
-6

Sure, just like with all HTML "what ifs", the answer is just use JQuery!

devshorts
  • 8,572
  • 4
  • 50
  • 73
  • While I realize my statement was kind of in jest, you really could do this with javascript. An HTML quine would only mean that if hte markup is "
    " that when it renders it also writes "
    " but you could modify the actual markup and do whatever you want with javascript. Nobody said that modifying the DOM doesn't count.
    – devshorts Apr 05 '12 at 21:08