yay - every test on xml and dom succeeds

This commit is contained in:
DadaMonad 2015-02-26 21:28:06 +00:00
parent f932f560bd
commit 0a8118367d
5 changed files with 250 additions and 124 deletions

View File

@ -160,12 +160,22 @@ YXml = (function() {
}; };
YXml.prototype.attr = function(name, value) { YXml.prototype.attr = function(name, value) {
var attrs, classes; var attrs, c, classes, cs, _i, _len;
if (arguments.length > 1) { if (arguments.length > 1) {
if (value.constructor !== String) { if (value.constructor !== String) {
throw new Error("The attributes must be of type String!"); throw new Error("The attributes must be of type String!");
} }
if (name === "class") {
classes = value.split(" ");
cs = {};
for (_i = 0, _len = classes.length; _i < _len; _i++) {
c = classes[_i];
cs[c] = true;
}
this._model.val("classes", new this._model.custom_types.Object(cs));
} else {
this._model.val("attributes").val(name, value); this._model.val("attributes").val(name, value);
}
return this; return this;
} else if (arguments.length > 0) { } else if (arguments.length > 0) {
if (name === "class") { if (name === "class") {
@ -365,7 +375,7 @@ YXml = (function() {
}; };
YXml.prototype.getDom = function() { YXml.prototype.getDom = function() {
var attr_name, attr_value, child, dom, i, that, _i, _len, _ref, _ref1; var attr_name, attr_value, child, dom, i, setClasses, that, _i, _len, _ref, _ref1;
if (this._dom == null) { if (this._dom == null) {
this._dom = document.createElement(this._model.val("tagname")); this._dom = document.createElement(this._model.val("tagname"));
_ref = this.attr(); _ref = this.attr();
@ -394,14 +404,18 @@ YXml = (function() {
for (_j = 0, _len1 = events.length; _j < _len1; _j++) { for (_j = 0, _len1 = events.length; _j < _len1; _j++) {
event = events[_j]; event = events[_j];
if (event.type === "insert") { if (event.type === "insert") {
newNode = event.value.getDom(); if (event.value.constructor === String) {
children = that._dom.childNodes; newNode = document.createTextNode(event.value);
if (children.length > 0) {
rightNode = children[0];
} else { } else {
rightNode = null; newNode = event.value.getDom();
}
event.value._setParent(that); event.value._setParent(that);
}
children = that._dom.childNodes;
if (children.length === event.position) {
rightNode = null;
} else {
rightNode = children[event.position];
}
_results.push(dont_proxy(function() { _results.push(dont_proxy(function() {
return that._dom.insertBefore(newNode, rightNode); return that._dom.insertBefore(newNode, rightNode);
})); }));
@ -436,7 +450,8 @@ YXml = (function() {
} }
return _results; return _results;
}); });
this._model.val("classes").observe(function(events) { setClasses = function() {
return that._model.val("classes").observe(function(events) {
var event, _j, _len1, _results; var event, _j, _len1, _results;
_results = []; _results = [];
for (_j = 0, _len1 = events.length; _j < _len1; _j++) { for (_j = 0, _len1 = events.length; _j < _len1; _j++) {
@ -455,6 +470,30 @@ YXml = (function() {
} }
return _results; return _results;
}); });
};
setClasses();
this._model.observe(function(events) {
var event, _j, _len1, _results;
_results = [];
for (_j = 0, _len1 = events.length; _j < _len1; _j++) {
event = events[_j];
if (event.type === "add" || event.type === "update") {
dont_proxy(function() {
var classes;
classes = that.attr("class");
if ((classes == null) || classes === "") {
return that._dom.removeAttribute("class");
} else {
return that._dom.setAttribute("class", that.attr("class"));
}
});
_results.push(setClasses());
} else {
_results.push(void 0);
}
}
return _results;
});
} }
return this._dom; return this._dom;
}; };
@ -481,28 +520,32 @@ dont_proxy = function(f) {
}; };
initialize_proxies = function() { initialize_proxies = function() {
var insertBefore, removeChild, replaceChild, that, _proxy; var f_add, f_remove, insertBefore, removeChild, replaceChild, that, _proxy;
_proxy = function(f_name, f, source) { _proxy = function(f_name, f, source, y) {
var old_f; var old_f;
if (source == null) { if (source == null) {
source = Element.prototype; source = Element.prototype;
} }
old_f = source[f_name]; old_f = source[f_name];
return source[f_name] = function() { return source[f_name] = function() {
if ((this._y_xml == null) || proxy_token) { if ((!((y != null) || (this._y_xml != null))) || proxy_token) {
return old_f.apply(this, arguments); return old_f.apply(this, arguments);
} else { } else if (this._y_xml != null) {
return f.apply(this._y_xml, arguments); return f.apply(this._y_xml, arguments);
} else {
return f.apply(y, arguments);
} }
}; };
}; };
that = this; that = this;
this._dom.classList.add = function(c) { f_add = function(c) {
return that.addClass(c); return that.addClass(c);
}; };
this._dom.classList.remove = function(c) { _proxy("add", f_add, this._dom.classList, this);
f_remove = function(c) {
return that.removeClass(c); return that.removeClass(c);
}; };
_proxy("remove", f_remove, this._dom.classList, this);
this._dom.__defineSetter__('className', function(val) { this._dom.__defineSetter__('className', function(val) {
return that.attr('class', val); return that.attr('class', val);
}); });
@ -558,12 +601,12 @@ initialize_proxies = function() {
removeChild = function(node) { removeChild = function(node) {
return node._y_xml.remove(); return node._y_xml.remove();
}; };
_proxy('removeChild', removeChild, this._dom); _proxy('removeChild', removeChild);
replaceChild = function(insertedNode, replacedNode) { replaceChild = function(insertedNode, replacedNode) {
insertBefore.call(this, insertedNode, replacedNode); insertBefore.call(this, insertedNode, replacedNode);
return removeChild.call(this, replacedNode); return removeChild.call(this, replacedNode);
}; };
return _proxy('replaceChild', replaceChild, this._dom); return _proxy('replaceChild', replaceChild);
}; };
if (typeof window !== "undefined" && window !== null) { if (typeof window !== "undefined" && window !== null) {

File diff suppressed because one or more lines are too long

View File

@ -1,9 +1,6 @@
json_types_uninitialized = require "./JsonTypes" json_types_uninitialized = require "./JsonTypes"
module.exports = (HB)-> module.exports = (HB)->
json_types = json_types_uninitialized HB json_types = json_types_uninitialized HB
types = json_types.types types = json_types.types

View File

@ -109,6 +109,14 @@ class YXml
if arguments.length > 1 if arguments.length > 1
if value.constructor isnt String if value.constructor isnt String
throw new Error "The attributes must be of type String!" throw new Error "The attributes must be of type String!"
if name is "class"
classes = value.split(" ")
cs = {}
for c in classes
cs[c] = true
@_model.val("classes", new @_model.custom_types.Object(cs))
else
@_model.val("attributes").val(name, value) @_model.val("attributes").val(name, value)
@ @
else if arguments.length > 0 else if arguments.length > 0
@ -323,13 +331,17 @@ class YXml
@_model.val("children").observe (events)-> @_model.val("children").observe (events)->
for event in events for event in events
if event.type is "insert" if event.type is "insert"
newNode = event.value.getDom() if event.value.constructor is String
children = that._dom.childNodes newNode = document.createTextNode(event.value)
if children.length > 0
rightNode = children[0]
else else
rightNode = null newNode = event.value.getDom()
event.value._setParent that event.value._setParent that
children = that._dom.childNodes
if children.length is event.position
rightNode = null
else
rightNode = children[event.position]
dont_proxy ()-> dont_proxy ()->
that._dom.insertBefore newNode, rightNode that._dom.insertBefore newNode, rightNode
else if event.type is "delete" else if event.type is "delete"
@ -345,7 +357,8 @@ class YXml
else if event.type is "delete" else if event.type is "delete"
dont_proxy ()-> dont_proxy ()->
that._dom.removeAttribute event.name that._dom.removeAttribute event.name
@_model.val("classes").observe (events)-> setClasses = ()->
that._model.val("classes").observe (events)->
for event in events for event in events
if event.type is "add" or event.type is "update" if event.type is "add" or event.type is "update"
dont_proxy ()-> dont_proxy ()->
@ -353,6 +366,18 @@ class YXml
else if event.type is "delete" else if event.type is "delete"
dont_proxy ()-> dont_proxy ()->
that._dom.classList.remove event.name that._dom.classList.remove event.name
setClasses()
@_model.observe (events)->
for event in events
if event.type is "add" or event.type is "update"
dont_proxy ()->
classes = that.attr("class")
if (not classes?) or classes is ""
that._dom.removeAttribute "class"
else
that._dom.setAttribute "class", that.attr("class")
setClasses()
@_dom @_dom
proxies_are_initialized = false proxies_are_initialized = false
@ -371,20 +396,26 @@ dont_proxy = (f)->
initialize_proxies = ()-> initialize_proxies = ()->
_proxy = (f_name, f, source = Element.prototype)-> _proxy = (f_name, f, source = Element.prototype, y)->
old_f = source[f_name] old_f = source[f_name]
source[f_name] = ()-> source[f_name] = ()->
if (not @_y_xml?) or proxy_token if (not (y? or @_y_xml?)) or proxy_token
old_f.apply this, arguments old_f.apply this, arguments
else else if @_y_xml?
f.apply @_y_xml, arguments f.apply @_y_xml, arguments
else
f.apply y, arguments
that = this that = this
@_dom.classList.add = (c)-> f_add = (c)->
that.addClass c that.addClass c
@_dom.classList.remove = (c)-> _proxy "add", f_add, @_dom.classList, @
f_remove = (c)->
that.removeClass c that.removeClass c
_proxy "remove", f_remove, @_dom.classList, @
@_dom.__defineSetter__ 'className', (val)-> @_dom.__defineSetter__ 'className', (val)->
that.attr('class', val) that.attr('class', val)
@_dom.__defineGetter__ 'className', ()-> @_dom.__defineGetter__ 'className', ()->
@ -436,11 +467,11 @@ initialize_proxies = ()->
removeChild = (node)-> removeChild = (node)->
node._y_xml.remove() node._y_xml.remove()
_proxy 'removeChild', removeChild, @_dom _proxy 'removeChild', removeChild
replaceChild = (insertedNode, replacedNode)-> replaceChild = (insertedNode, replacedNode)-> # TODO: handle replace with replace behavior...
insertBefore.call this, insertedNode, replacedNode insertBefore.call this, insertedNode, replacedNode
removeChild.call this, replacedNode removeChild.call this, replacedNode
_proxy 'replaceChild', replaceChild, @_dom _proxy 'replaceChild', replaceChild
if window? if window?
if window.Y? if window.Y?

View File

@ -296,7 +296,8 @@ describe "Y-Xml", ->
expect(@dom.getAttribute("test_attribute")).to.equal("newVal") expect(@dom.getAttribute("test_attribute")).to.equal("newVal")
@dom.removeAttribute("test_attribute") @dom.removeAttribute("test_attribute")
expect(@u1.attr("test_attribute")).to.be.undefined expect(@u1.attr("test_attribute")).to.be.undefined
expect(@dom.getAttribute("test_attribute")).to.be.undefined attr = @dom.getAttribute("test_attribute")
expect(attr?).to.be.false
it "supports dom.removeChild", -> it "supports dom.removeChild", ->
newdom = $("<p>dtrn</p>")[0] newdom = $("<p>dtrn</p>")[0]
@ -310,12 +311,12 @@ describe "Y-Xml", ->
it "supports dom.replaceChild", -> it "supports dom.replaceChild", ->
dom = $("<p>dtrn</p>")[0] dom = $("<p>dtrn</p>")[0]
@dom.appendChild(newdom) @dom.appendChild(dom)
expect(@u1+"").to.equal("<div><p>dtrn</p></div>") expect(@u1+"").to.equal("<div><p>dtrn</p></div>")
expect(@dom.outerHTML).to.equal("<div><p>dtrn</p></div>") expect(@dom.outerHTML).to.equal("<div><p>dtrn</p></div>")
newdom = $("<p>replaced</p>")[0] newdom = $("<p>replaced</p>")[0]
@dom.replaceChild(dom,newdom) @dom.replaceChild(newdom, dom)
expect(@dom.outerHTML).to.equal("<div><p>replaced</p></div>") expect(@dom.outerHTML).to.equal("<div><p>replaced</p></div>")
expect(@u1+"").to.equal("<div><p>replaced</p></div>") expect(@u1+"").to.equal("<div><p>replaced</p></div>")
@ -328,7 +329,7 @@ describe "Y-Xml", ->
it "supports dom.textContent", -> it "supports dom.textContent", ->
dom = $("<p>dtrn</p>")[0] dom = $("<p>dtrn</p>")[0]
@dom.appendChild(newdom) @dom.appendChild(dom)
expect(@u1+"").to.equal("<div><p>dtrn</p></div>") expect(@u1+"").to.equal("<div><p>dtrn</p></div>")
expect(@dom.outerHTML).to.equal("<div><p>dtrn</p></div>") expect(@dom.outerHTML).to.equal("<div><p>dtrn</p></div>")
@ -338,7 +339,7 @@ describe "Y-Xml", ->
it "suppports dom.textContent (non empty string)", -> it "suppports dom.textContent (non empty string)", ->
dom = $("<p>dtrn</p>")[0] dom = $("<p>dtrn</p>")[0]
@dom.appendChild(newdom) @dom.appendChild(dom)
expect(@u1+"").to.equal("<div><p>dtrn</p></div>") expect(@u1+"").to.equal("<div><p>dtrn</p></div>")
expect(@dom.outerHTML).to.equal("<div><p>dtrn</p></div>") expect(@dom.outerHTML).to.equal("<div><p>dtrn</p></div>")
@ -347,9 +348,8 @@ describe "Y-Xml", ->
expect(@dom.outerHTML).to.equal("<div>stuff</div>") expect(@dom.outerHTML).to.equal("<div>stuff</div>")
it "supports jquery.addClass", -> it "supports jquery.addClass", ->
@j.addClass("testy")
@j.addClass("testedy tested") @j.addClass("testedy tested")
expect(@dom.getAttribute("class")).to.equal("testy testedy tested") expect(@dom.getAttribute("class")).to.equal("testedy tested")
it "supports jquery.after", -> it "supports jquery.after", ->
d = $("<span></span>") d = $("<span></span>")
@ -362,27 +362,27 @@ describe "Y-Xml", ->
d = $("<span></span>")[0] d = $("<span></span>")[0]
@j.append(d) @j.append(d)
d = $("<div></div>")[0] d = $("<div></div>")[0]
@dom.append(d) @j.append(d)
expect(@dom.outerHTML).to.equal("<div><span></span><div></div></div>") expect(@dom.outerHTML).to.equal("<div><span></span><div></div></div>")
expect(@u1+"").to.equal("<div><span></span><div></div></div>") expect(@u1+"").to.equal("<div><span></span><div></div></div>")
it "supports jquery.appendTo", -> it "supports jquery.appendTo", ->
$("<b>appendedTo</b>").appendTo(@dom) $("<b>appendedTo</b>").appendTo(@dom)
$("p").appendTo(@dom) $("<p>").appendTo(@dom)
expect(@dom.outerHTML).to.equal("<div><b>appendedTo</b><p></p></div>") expect(@dom.outerHTML).to.equal("<div><b>appendedTo</b><p></p></div>")
expect(@u1+"").to.equal("<div><b>appendedTo</b><p></p></div>") expect(@u1+"").to.equal("<div><b>appendedTo</b><p></p></div>")
it "supports jquery.before", -> it "supports jquery.before", ->
newdom = $("p") newdom = $("<p>")
$(@dom).append(newdom) $(@dom).append(newdom)
newdom.before("<div>before</div>") newdom.before("<div>before</div>")
expect(@dom.outerHTML).to.equal("<div><div>before</div><p></p></div>") expect(@dom.outerHTML).to.equal("<div><div>before</div><p></p></div>")
expect(@u1+"").to.equal("<div><div>before</div><p></p></div>") expect(@u1+"").to.equal("<div><div>before</div><p></p></div>")
it "supports jquery.detach", -> it "supports jquery.detach", ->
d = $("p") d = $("<p>")
$j.append(d) @j.append(d)
$j.detach("p") d.detach()
expect(@dom.outerHTML).to.equal("<div></div>") expect(@dom.outerHTML).to.equal("<div></div>")
expect(@u1+"").to.equal("<div></div>") expect(@u1+"").to.equal("<div></div>")
@ -396,16 +396,16 @@ describe "Y-Xml", ->
expect(@u1+"").to.equal("<div></div>") expect(@u1+"").to.equal("<div></div>")
it "supports jquery.insertAfter", -> it "supports jquery.insertAfter", ->
d = $("span") d = $("<span>")
d.appendTo(@dom) d.appendTo(@dom)
$("<p>after</p>").insertAfter(d) $("<p>after</p>").insertAfter(d)
expect(@dom.outerHTML).to.equal("<div><span></span><p>after</p></div>") expect(@dom.outerHTML).to.equal("<div><span></span><p>after</p></div>")
expect(@u1+"").to.equal("<div><span></span><p>after</p></div>") expect(@u1+"").to.equal("<div><span></span><p>after</p></div>")
it "supports jquery.insertBefore", -> it "supports jquery.insertBefore", ->
d = $("span") d = $("<span>")
d.appendTo(@j) d.appendTo(@j)
$("<p>after</p>").insertAfter(d) $("<p>before</p>").insertBefore(d)
expect(@dom.outerHTML).to.equal("<div><p>before</p><span></span></div>") expect(@dom.outerHTML).to.equal("<div><p>before</p><span></span></div>")
expect(@u1+"").to.equal("<div><p>before</p><span></span></div>") expect(@u1+"").to.equal("<div><p>before</p><span></span></div>")
@ -438,30 +438,35 @@ describe "Y-Xml", ->
expect(@j.attr("test_attribute")).to.be.undefined expect(@j.attr("test_attribute")).to.be.undefined
it "supports jquery.removeClass", -> it "supports jquery.removeClass", ->
@j.addClass("testy")
@j.addClass("testedy tested") @j.addClass("testedy tested")
expect(@dom.getAttribute("class")).to.equal("testy testedy tested") expect(@dom.getAttribute("class")).to.equal("testedy tested")
@j.removeClass("testedy") @j.removeClass("testedy")
expect(@dom.getAttribute("class")).to.equal("testy tested") expect(@dom.getAttribute("class")).to.equal("tested")
expect(@u1.hasClass("testedy")).to.be.false expect(@u1.hasClass("testedy")).to.be.false
it "supports jquery.attr", -> it "supports jquery.attr", ->
@j.attr("atone", "yeah") @j.attr("atone", "yeah")
expect(@u1.attr("atone")).to.equal("yeah") expect(@u1.attr("atone")).to.equal("yeah")
expect(@j.attr("atone")).to.equal("yeah")
it "supports jquery.replaceAll", -> it "supports jquery.replaceAll", ->
$("<span>New span content </span>").replaceAll("#test_dom div") d = $("<p />")
@check() d.appendTo(@dom)
d = $("<p />")
d.appendTo(@dom)
$("<span>").replaceAll($(@dom).find("p"))
expect(@dom.outerHTML).to.equal("<div><span></span><span></span></div>")
expect(@u1+"").to.equal("<div><span></span><span></span></div>")
it "supports jquery.replaceWith", -> it "supports jquery.replaceWith", ->
d = $("span") d = $("<span>")
@j.prepend(d) @j.prepend(d)
d = $("span") d = $("<span>")
@j.prepend(d) @j.prepend(d)
d = $("span") d = $("<span>")
@j.prepend(d) @j.prepend(d)
d = @j.getElementsByTagName("span") d = @j.find("span")
d.replaceWith("<div></div>") d.replaceWith("<div></div>")
expect(@dom.outerHTML).to.equal("<div><div></div><div></div><div></div></div>") expect(@dom.outerHTML).to.equal("<div><div></div><div></div><div></div></div>")