1 {% extends "base.html" %}
\r
3 {% block extrahead %}
\r
4 <link rel="stylesheet" href="{{STATIC_URL}}css/html.css" type="text/css" charset="utf-8">
\r
7 {% block maincontent %}
\r
10 <textarea id="source" cols="80" rows="10" ></textarea>
\r
13 <textarea id="dest" cols="80" rows="10" ></textarea>
\r
15 <button type="button" id="render">Render</button>
\r
16 <button type="button" id="unrender">Unrender</button>
\r
18 <div style="position: fixed; right: 0px; top: 0px; width: 640px; height: 300px; background: blue;">
\r
19 <div id="preview" class="htmlview">
\r
24 {% block extrabody %}
\r
25 <script type="text/javascript">
\r
27 function serializeAttribute(attr, acc) {
\r
28 return acc + ' ' + attr.nodeName + '="'+ attr.nodeValue + '"';
\r
31 function serializeElement(element, acc, level)
\r
33 var isBlock = false;
\r
35 if(element.nodeName != 'span' && element.nodeName != 'p')
\r
39 for(var i=0; i < level; i++)
\r
43 acc += "<" + element.nodeName;
\r
44 for(var i=0; i < element.attributes.length; i++)
\r
45 acc = serializeAttribute(element.attributes.item(i), acc);
\r
47 if(element.firstChild)
\r
51 if(element.firstChild)
\r
52 acc = serializeNode(element.firstChild, acc, level+1);
\r
56 for(var i=0; i < level; i++)
\r
59 acc += "</"+element.nodeName + ">";
\r
65 if(isBlock) acc += "\n";
\r
71 // step-by-step serializer
\r
72 function serializeNode(node, acc, level)
\r
76 if(node.nodeType == 11)
\r
77 return serializeNode(node.firstChild, acc, level);
\r
79 if(node.nodeType == 1)
\r
80 acc = serializeElement(node, acc, level);
\r
82 else if(node.nodeType == 3)
\r
83 acc += node.nodeValue;
\r
85 if(node.nextSibling)
\r
86 return serializeNode(node.nextSibling, acc, level);
\r
91 function serializeXML(element) {
\r
92 return serializeNode(element, '', 0);
\r
95 // w3c version first
\r
96 var xslt = new XSLTProcessor();
\r
99 url: "/static/xsl/wl2html_client.xsl",
\r
101 success: function(data) {
\r
102 xslt.importStylesheet(data);
\r
103 console.log('XSL loaded successfully');
\r
108 $('#render').click(function() {
\r
110 var src = '<?xml version="1.0"?><chunk>'+$('#source').val()+'</chunk>';
\r
111 src = src.replace(/\/\s+/g, '<br />');
\r
112 var doc = (new DOMParser()).parseFromString(src, "text/xml");
\r
113 // var string = (new XMLSerializer()).serializeToString(doc);
\r
114 // console.log('rendering', string);
\r
116 var result = xslt.transformToFragment(doc, document);
\r
117 var string = serializeXML(result);
\r
118 var dest = $('#dest');
\r
119 dest.val( string );
\r
120 $('#preview').html(string);
\r