Forum How do I...?

Namespace and Javascript

nico
Hello,

I have a problem when creating elements with javascript in documents that have a namespace. It works only if Prince is called with "--input=HTML", but not with "--input=XML". Note that the elements are generated in both cases. But in XML mode, they are not recognized correctly. For example a link is rendered as plain text only.

I tried to add an "xmlns" attribute to the elements created with javascript, but it didn't help.

There is an example below with the bash commands I used for the pdf generation.

In this example, if you remove the doctype and xmlns attribute of the html element, it will work with "--input=HTML" and "--input=XML".

Note that it would be very usefull to have access to a string representation of the DOM tree generated by Prince after javascript has been executed as in the developper tools in Safari or in Firebug.

Thank you for your help,

Nicolas





namespace_problem.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
		<title>Namespace problem</title>
		<script type="text/javascript">

			window.addEventListener( "load", main, false );

			function main()
			{
				var InA  = document.createElement( 'a' );
				InA.setAttribute( 'xmlns', 'http://www.w3.org/1999/xhtml' );
				InA.setAttribute( 'href', '#' );
				InA.innerHTML = "Hello";
				var target = document.getElementById( 'target' );
				target.appendChild( InA );
			}
		</script>
	</head>
	<body>
		<h1>Namespace problem</h1>
		<div id="target"></div>
	</body>
</html>


#!/bin/bash
# Works on OSX 10.8

FILE_IN="namespace_problem.html"
FILE_OUT_HTML="namespace_problem_html.pdf"
FILE_OUT_XML="namespace_problem_xml.pdf"

time( prince --input=HTML --javascript --verbose $FILE_IN -o $FILE_OUT_HTML )
time( prince --input=XML  --javascript --verbose $FILE_IN -o $FILE_OUT_XML )

open $FILE_OUT_HTML
open $FILE_OUT_XML
mikeday
You can use createElementNS() to create an element in a particular namespace, and use the innerHTML property to get a string represention of a DOM tree.