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) {
var attrs, classes;
var attrs, c, classes, cs, _i, _len;
if (arguments.length > 1) {
if (value.constructor !== String) {
throw new Error("The attributes must be of type String!");
}
this._model.val("attributes").val(name, value);
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);
}
return this;
} else if (arguments.length > 0) {
if (name === "class") {
@ -365,7 +375,7 @@ YXml = (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) {
this._dom = document.createElement(this._model.val("tagname"));
_ref = this.attr();
@ -394,14 +404,18 @@ YXml = (function() {
for (_j = 0, _len1 = events.length; _j < _len1; _j++) {
event = events[_j];
if (event.type === "insert") {
newNode = event.value.getDom();
children = that._dom.childNodes;
if (children.length > 0) {
rightNode = children[0];
if (event.value.constructor === String) {
newNode = document.createTextNode(event.value);
} else {
rightNode = null;
newNode = event.value.getDom();
event.value._setParent(that);
}
children = that._dom.childNodes;
if (children.length === event.position) {
rightNode = null;
} else {
rightNode = children[event.position];
}
event.value._setParent(that);
_results.push(dont_proxy(function() {
return that._dom.insertBefore(newNode, rightNode);
}));
@ -436,19 +450,44 @@ YXml = (function() {
}
return _results;
});
this._model.val("classes").observe(function(events) {
setClasses = function() {
return that._model.val("classes").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") {
_results.push(dont_proxy(function() {
return that._dom.classList.add(event.name);
}));
} else if (event.type === "delete") {
_results.push(dont_proxy(function() {
return that._dom.classList.remove(event.name);
}));
} else {
_results.push(void 0);
}
}
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") {
_results.push(dont_proxy(function() {
return that._dom.classList.add(event.name);
}));
} else if (event.type === "delete") {
_results.push(dont_proxy(function() {
return that._dom.classList.remove(event.name);
}));
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);
}
@ -481,28 +520,32 @@ dont_proxy = function(f) {
};
initialize_proxies = function() {
var insertBefore, removeChild, replaceChild, that, _proxy;
_proxy = function(f_name, f, source) {
var f_add, f_remove, insertBefore, removeChild, replaceChild, that, _proxy;
_proxy = function(f_name, f, source, y) {
var old_f;
if (source == null) {
source = Element.prototype;
}
old_f = source[f_name];
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);
} else {
} else if (this._y_xml != null) {
return f.apply(this._y_xml, arguments);
} else {
return f.apply(y, arguments);
}
};
};
that = this;
this._dom.classList.add = function(c) {
f_add = function(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);
};
_proxy("remove", f_remove, this._dom.classList, this);
this._dom.__defineSetter__('className', function(val) {
return that.attr('class', val);
});
@ -558,12 +601,12 @@ initialize_proxies = function() {
removeChild = function(node) {
return node._y_xml.remove();
};
_proxy('removeChild', removeChild, this._dom);
_proxy('removeChild', removeChild);
replaceChild = function(insertedNode, replacedNode) {
insertBefore.call(this, insertedNode, replacedNode);
return removeChild.call(this, replacedNode);
};
return _proxy('replaceChild', replaceChild, this._dom);
return _proxy('replaceChild', replaceChild);
};
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"
module.exports = (HB)->
json_types = json_types_uninitialized HB
types = json_types.types

View File

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

View File

@ -296,7 +296,8 @@ describe "Y-Xml", ->
expect(@dom.getAttribute("test_attribute")).to.equal("newVal")
@dom.removeAttribute("test_attribute")
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", ->
newdom = $("<p>dtrn</p>")[0]
@ -310,12 +311,12 @@ describe "Y-Xml", ->
it "supports dom.replaceChild", ->
dom = $("<p>dtrn</p>")[0]
@dom.appendChild(newdom)
@dom.appendChild(dom)
expect(@u1+"").to.equal("<div><p>dtrn</p></div>")
expect(@dom.outerHTML).to.equal("<div><p>dtrn</p></div>")
newdom = $("<p>replaced</p>")[0]
@dom.replaceChild(dom,newdom)
@dom.replaceChild(newdom, dom)
expect(@dom.outerHTML).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", ->
dom = $("<p>dtrn</p>")[0]
@dom.appendChild(newdom)
@dom.appendChild(dom)
expect(@u1+"").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)", ->
dom = $("<p>dtrn</p>")[0]
@dom.appendChild(newdom)
@dom.appendChild(dom)
expect(@u1+"").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>")
it "supports jquery.addClass", ->
@j.addClass("testy")
@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", ->
d = $("<span></span>")
@ -362,27 +362,27 @@ describe "Y-Xml", ->
d = $("<span></span>")[0]
@j.append(d)
d = $("<div></div>")[0]
@dom.append(d)
@j.append(d)
expect(@dom.outerHTML).to.equal("<div><span></span><div></div></div>")
expect(@u1+"").to.equal("<div><span></span><div></div></div>")
it "supports jquery.appendTo", ->
$("<b>appendedTo</b>").appendTo(@dom)
$("p").appendTo(@dom)
$("<p>").appendTo(@dom)
expect(@dom.outerHTML).to.equal("<div><b>appendedTo</b><p></p></div>")
expect(@u1+"").to.equal("<div><b>appendedTo</b><p></p></div>")
it "supports jquery.before", ->
newdom = $("p")
newdom = $("<p>")
$(@dom).append(newdom)
newdom.before("<div>before</div>")
expect(@dom.outerHTML).to.equal("<div><div>before</div><p></p></div>")
expect(@u1+"").to.equal("<div><div>before</div><p></p></div>")
it "supports jquery.detach", ->
d = $("p")
$j.append(d)
$j.detach("p")
d = $("<p>")
@j.append(d)
d.detach()
expect(@dom.outerHTML).to.equal("<div></div>")
expect(@u1+"").to.equal("<div></div>")
@ -396,16 +396,16 @@ describe "Y-Xml", ->
expect(@u1+"").to.equal("<div></div>")
it "supports jquery.insertAfter", ->
d = $("span")
d = $("<span>")
d.appendTo(@dom)
$("<p>after</p>").insertAfter(d)
expect(@dom.outerHTML).to.equal("<div><span></span><p>after</p></div>")
expect(@u1+"").to.equal("<div><span></span><p>after</p></div>")
it "supports jquery.insertBefore", ->
d = $("span")
d = $("<span>")
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(@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
it "supports jquery.removeClass", ->
@j.addClass("testy")
@j.addClass("testedy tested")
expect(@dom.getAttribute("class")).to.equal("testy testedy tested")
expect(@dom.getAttribute("class")).to.equal("testedy tested")
@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
it "supports jquery.attr", ->
@j.attr("atone", "yeah")
expect(@u1.attr("atone")).to.equal("yeah")
expect(@j.attr("atone")).to.equal("yeah")
it "supports jquery.replaceAll", ->
$("<span>New span content </span>").replaceAll("#test_dom div")
@check()
d = $("<p />")
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", ->
d = $("span")
d = $("<span>")
@j.prepend(d)
d = $("span")
d = $("<span>")
@j.prepend(d)
d = $("span")
d = $("<span>")
@j.prepend(d)
d = @j.getElementsByTagName("span")
d = @j.find("span")
d.replaceWith("<div></div>")
expect(@dom.outerHTML).to.equal("<div><div></div><div></div><div></div></div>")