From e44a7e37b6c7b5961adaffc62b9042b8d442938e Mon Sep 17 00:00:00 2001 From: mensonge Date: Thu, 13 Nov 2008 09:49:11 +0000 Subject: New feature: basic Ajax suggestion for tags and implementation of Dojo toolkit git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f --- includes/js/dojox/dtl/tests/html/buffer.js | 35 +++++ includes/js/dojox/dtl/tests/html/tag.js | 233 +++++++++++++++++++++++++++++ includes/js/dojox/dtl/tests/html/util.js | 160 ++++++++++++++++++++ 3 files changed, 428 insertions(+) create mode 100644 includes/js/dojox/dtl/tests/html/buffer.js create mode 100644 includes/js/dojox/dtl/tests/html/tag.js create mode 100644 includes/js/dojox/dtl/tests/html/util.js (limited to 'includes/js/dojox/dtl/tests/html') diff --git a/includes/js/dojox/dtl/tests/html/buffer.js b/includes/js/dojox/dtl/tests/html/buffer.js new file mode 100644 index 0000000..3077d8c --- /dev/null +++ b/includes/js/dojox/dtl/tests/html/buffer.js @@ -0,0 +1,35 @@ +if(!dojo._hasResource["dojox.dtl.tests.html.buffer"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.dtl.tests.html.buffer"] = true; +dojo.provide("dojox.dtl.tests.html.buffer"); + +dojo.require("dojox.dtl.html"); +dojo.require("dojox.dtl.Context"); +dojo.require("dojox.dtl.tests.html.util"); + +doh.register("dojox.dtl.html.buffer", + [ + function test_insertion_order_text(t){ + var dd = dojox.dtl; + + var context = new dd.Context({ + first: false, + last: false + }); + + var template = new dd.HtmlTemplate("
{% if first %}first{% endif %}middle{% if last %}last{% endif %}
"); + t.is("
middle
", dd.tests.html.util.render(template, context)); + + context.first = true; + t.is("
firstmiddle
", dd.tests.html.util.render(template, context)); + + context.first = false; + context.last = true; + t.is("
middlelast
", dd.tests.html.util.render(template, context)); + + context.first = true; + t.is("
firstmiddlelast
", dd.tests.html.util.render(template, context)); + } + ] +); + +} diff --git a/includes/js/dojox/dtl/tests/html/tag.js b/includes/js/dojox/dtl/tests/html/tag.js new file mode 100644 index 0000000..c964913 --- /dev/null +++ b/includes/js/dojox/dtl/tests/html/tag.js @@ -0,0 +1,233 @@ +if(!dojo._hasResource["dojox.dtl.tests.html.tag"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.dtl.tests.html.tag"] = true; +dojo.provide("dojox.dtl.tests.html.tag"); + +dojo.require("dojox.dtl.html"); +dojo.require("dojox.dtl.Context"); +dojo.require("dojox.dtl.tests.html.util"); + +doh.register("dojox.dtl.html.tag", + [ + function test_errors(t){ + var dd = dojox.dtl; + var template; + + // No root node after rendering + var found = false; + try { + template = new dd.HtmlTemplate('No div'); + dd.tests.html.util.render(template); + }catch(e){ + t.is("Text should not exist outside of the root node in template", e.message); + found = true; + } + t.t(found); + + var context = new dojox.dtl.Context({test: "Pocket"}); + found = false; + try { + template = new dd.HtmlTemplate('{{ test }}'); + dd.tests.html.util.render(template, context); + }catch(e){ + t.is("Text should not exist outside of the root node in template", e.message); + found = true; + } + t.t(found); + + template = new dd.HtmlTemplate('
extra content'); + found = false; + try { + dd.tests.html.util.render(template); + }catch(e){ + t.is("Content should not exist outside of the root node in template", e.message); + found = true; + } + t.t(found); + + // More than one top-level node (except for blocks) + template = new dd.HtmlTemplate('
'); + found = false; + try { + dd.tests.html.util.render(template); + }catch(e){ + t.is("Content should not exist outside of the root node in template", e.message); + found = true; + } + t.t(found); + + // Logic block rules out any root node + template = new dd.HtmlTemplate('{% if missing %}
{% endif %}'); + found = false; + try { + dd.tests.html.util.render(template); + }catch(e){ + t.is("Rendered template does not have a root node", e.message); + found = true; + } + t.t(found); + }, + function test_structures(t){ + var dd = dojox.dtl; + + var context = new dd.Context({ + actions: ["ate", "picked"], + items: [ + { + name: "apple" + }, + { + name: "banana", + date: new Date(2007, 2, 16, 14, 30, 10) + }, + { + name: "orange", + date: new Date(2008, 0, 1, 12, 0, 0) + } + ] + }); + + var template = new dd.HtmlTemplate('
'); + t.is('
', dd.tests.html.util.render(template, context)); + }, + function test_tag_extend(t){ + // Problems to look for: + // * Content outside of blocks + }, + function test_tag_for(t){ + var dd = dojox.dtl; + + var context = new dd.Context({ + items: ["apple", "banana", "lemon"] + }); + var template = new dd.HtmlTemplate('
'); + + t.is('
', dd.tests.html.util.render(template, context)); + + // The line break is there to make sure our regex works + template = new dd.HtmlTemplate('
'); + + t.is('
', dd.tests.html.util.render(template, context)); + }, + function test_tag_if(t){ + var dd = dojox.dtl; + + var context = new dd.Context({key: true}); + var template = new dd.HtmlTemplate('{% if key %}
has key
{% else %}
no key
{% endif %}'); + t.is("
has key
", dd.tests.html.util.render(template, context)); + context.key = false; + t.is("
no key
", dd.tests.html.util.render(template, context)); + }, + function test_tag_ifchanged(t){ + var dd = dojox.dtl; + + var context = new dd.Context({ + year: 2008, + days: [ + new Date(2008, 0, 12), + new Date(2008, 0, 28), + new Date(2008, 1, 1), + new Date(2008, 1, 1), + new Date(2008, 1, 1) + ] + }); + + var template = new dd.HtmlTemplate("

Archive for {{ year }}

"+ +"{% for date in days %}"+ +'{% ifchanged %}

Month:

{{ date|date:"F" }}

{% endifchanged %}'+ +'{{ date|date:\'j\' }}'+ +"{% endfor %}
"); + + t.is('

Archive for 2008

'+ +'

Month:

January

'+ +'12'+ +'28'+ +'

Month:

February

'+ +'1'+ +'1'+ +'1
', dd.tests.html.util.render(template, context)); + + template = new dd.HtmlTemplate('
{% for date in days %}'+ +'{% ifchanged date.date %} {{ date.date }} {% endifchanged %}'+ +'{% ifchanged date.hour date.date %}'+ +'{{ date.hour }}'+ +'{% endifchanged %}'+ +'{% endfor %}
'); + t.is('
2008-01-12 0 2008-01-28 0 2008-02-01 0
', dd.tests.html.util.render(template, context)); + }, + function test_tag_ifequal(t){ + var dd = dojox.dtl; + + var context = new dd.Context({ + items: [ + { name: "apple", color: "red" }, + { name: "banana", color: "yellow" }, + { name: "pear", color: "green" }, + { name: "kiwi", color: "brown" } + ], + edit_item: "banana" + }); + + var template = new dd.HtmlTemplate("
"); + t.is('
', dd.tests.html.util.render(template, context)); + + template = new dd.HtmlTemplate("
"); + t.is('
', dd.tests.html.util.render(template, context)); + + template = new dd.HtmlTemplate("
"); + t.is('
', dd.tests.html.util.render(template, context)); + + template = new dd.HtmlTemplate("
"); + t.is('
', dd.tests.html.util.render(template, context)); + + template = new dd.HtmlTemplate("
"); + t.is('
', dd.tests.html.util.render(template, context)); + }, + function test_tag_include(t){ + var dd = dojox.dtl; + + var context = new dd.Context({ + hello: dojo.moduleUrl("dojox.dtl.tests.templates", "hello.html"), + person: "Bob", + people: ["Charles", "Ralph", "Julia"] + }); + + var template = new dd.HtmlTemplate("
{% include hello %}
"); + t.is("
Hello, Bob
", dd.tests.html.util.render(template, context)); + + template = new dd.HtmlTemplate('
{% include "../../dojox/dtl/tests/templates/hello.html" %}
'); + t.is("
Hello, Bob
", dd.tests.html.util.render(template, context)); + + template = new dd.HtmlTemplate('
{% for person in people %}
{% include hello %}
{% endfor %}
'); + t.is('
Hello, Charles
Hello, Ralph
Hello, Julia
', dd.tests.html.util.render(template, context)); + }, + function test_tag_spaceless(t){ + var dd = dojox.dtl; + + var template = new dd.HtmlTemplate("{% spaceless %}{% endspaceless %}"); + t.is("", dd.tests.html.util.render(template)); + }, + function test_tag_ssi(t){ + var dd = dojox.dtl; + + var context = new dd.Context({ + hello: dojo.moduleUrl("dojox.dtl.tests.templates", "hello.html"), + person: "Bob", + people: ["Charles", "Ralph", "Julia"] + }); + + var template = new dd.HtmlTemplate("
{% ssi hello parsed %}
"); + t.is("
Hello, Bob
", dd.tests.html.util.render(template, context)); + + template = new dd.HtmlTemplate("
{% ssi hello %}
"); + t.is("
Hello, {{ person }}
", dd.tests.html.util.render(template, context)); + + template = new dd.HtmlTemplate('
{% ssi "../../dojox/dtl/tests/templates/hello.html" parsed %}
'); + t.is("
Hello, Bob
", dd.tests.html.util.render(template, context)); + + template = new dd.HtmlTemplate('
{% for person in people %}{% ssi hello parsed %} {% endfor %}
'); + t.is("
Hello, Charles Hello, Ralph Hello, Julia
", dd.tests.html.util.render(template, context)); + } + ] +); + +} diff --git a/includes/js/dojox/dtl/tests/html/util.js b/includes/js/dojox/dtl/tests/html/util.js new file mode 100644 index 0000000..b4efe5a --- /dev/null +++ b/includes/js/dojox/dtl/tests/html/util.js @@ -0,0 +1,160 @@ +if(!dojo._hasResource["dojox.dtl.tests.html.util"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.dtl.tests.html.util"] = true; +dojo.provide("dojox.dtl.tests.html.util"); + +dojo.require("dojox.dtl.html"); +dojo.require("dojox.dtl.render.html"); +dojo.require("dojox.string.Builder"); + +dojox.dtl.HtmlBuffer.prototype.onClone = function(from, to){ + var clones = this._clones = this._clones || []; + + for(var i = 0, group; group = clones[i]; i++){ + for(var j = 0, item; item = group[j]; j++){ + if(item === from){ + group.push(to); + return + }else if(item === to){ + group.push(from); + return; + } + } + } + + clones.push([from, to]); +} +dojox.dtl.HtmlBuffer.prototype.onAddEvent = function(node, type, description){ + var events = this._events = this._events || []; + + var found = false; + for(var i = 0, evt; evt = events[i]; i++){ + if(evt[0] === node){ + found = true; + evt[1] = type; + evt[2] = description; + } + } + + if(!found){ + events.push([node, type, description]); + } +} + +dojox.dtl.tests.html.util.render = function(/*HtmlTemplate*/ template, /*Context*/ context) { + try { + var div = document.createElement("div"); + dojo.style(div, "visibility", "hidden"); + var attach = document.createElement("div"); + div.appendChild(attach); + dojo.body().appendChild(div); + + var buffer = template.getBuffer(); + var canvas = new dojox.dtl.render.html.Render(attach, template); + canvas.render(context, template, buffer); + var clones = buffer._clones; + var events = buffer._events; + + var first = dojox.dtl.tests.html.util.serialize(canvas.domNode, template.tokens, clones, events).toString(); + + buffer = template.getBuffer(); + buffer._clones = clones; + buffer._events = events; + canvas.render(context, template, buffer); + + var second = dojox.dtl.tests.html.util.serialize(canvas.domNode, template.tokens, clones, events).toString(); + + doh.is("Compare re-render: " + first, "Compare re-render: " + second); + return first; + } + catch(e){ + throw e; + }finally{ + div.parentNode.removeChild(div); + } +} + +dojox.dtl.tests.html.util.serialize = function(node, tokens, clones, events, output) { + var types = dojox.dtl.html.types; + clones = clones || []; + events = events || []; + + if (node.nodeType == 3) { + output.append(node.nodeValue); + }else{ + var name = node.nodeName.toLowerCase(); + + if (!output) { + output = new dojox.string.Builder(); + } + output.append("<").append(name); + + var attributes = dojo.filter(tokens, function(token){ + if(token[0] == types.attr){ + for(var i = 0, group; group = clones[i]; i++){ + // group is any set of nodes that were originally the sam + var count = 0; + for(var j = 0, item; item = group[j]; j++){ + if(item === token[1] || item === node){ + if(count++){ + // This is entered when we have 2 hits within a clone group. + // The first would be the original node + // The second would be if our current node is a clone + // of the original + return true; + } + } + } + } + } + }); + + for(var i = 0, attribute; attribute = attributes[i]; i++){ + var value = ""; + if(attribute[2] == "class"){ + value = node.className || value; + }else if(attribute[2] == "for"){ + value = node.htmlFor || value; + }else if(node.getAttribute){ + value = node.getAttribute(attribute[2], 2) || value; + if(dojo.isIE && (attribute[2] == "href" || attribute[2] == "src")){ + if(dojo.isIE){ + var hash = location.href.lastIndexOf(location.hash); + var href = location.href.substring(0, hash).split("/"); + href.pop(); + href = href.join("/") + "/"; + if(value.indexOf(href) == 0){ + value = value.replace(href, ""); + } + value = decodeURIComponent(value); + } + } + } + if(value){ + output.append(" ").append(attribute[2]).append('="').append(value.replace(/"/g, '\\"')).append('"'); + } + } + + // Deal with events + if(events){ + for(var i = 0, evt; evt = events[i]; i++){ + if(evt[0] === node){ + output.append(" ").append(evt[1]).append('="').append(evt[2]).append('"'); + } + } + } + + if(!node.childNodes.length){ + output.append("/>"); + }else{ + output.append(">"); + dojo.forEach(node.childNodes, function(node){ + dojox.dtl.tests.html.util.serialize(node, tokens, clones, events, output); + }); + output.append(""); + } + + return output; + } +} + +} -- cgit v1.2.3