Y-Xml tests pass

This commit is contained in:
DadaMonad 2015-02-25 22:50:26 +00:00
parent 9a8f8fba05
commit 9059618d1f
14 changed files with 218 additions and 133 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -256,21 +256,21 @@ module.exports = function() {
};
ListManager.prototype.push = function(content) {
return this.insertAfter(this.end.prev_cl, content);
return this.insertAfter(this.end.prev_cl, [content]);
};
ListManager.prototype.insertAfter = function(left, content) {
ListManager.prototype.insertAfter = function(left, contents) {
var c, right, tmp, _i, _len;
right = left.next_cl;
while (right.isDeleted()) {
right = right.next_cl;
}
left = right.prev_cl;
if (content instanceof ops.Operation) {
if (contents instanceof ops.Operation) {
(new ops.Insert(null, content, void 0, left, right)).execute();
} else {
for (_i = 0, _len = content.length; _i < _len; _i++) {
c = content[_i];
for (_i = 0, _len = contents.length; _i < _len; _i++) {
c = contents[_i];
if ((c != null) && (c._name != null) && (c._getModel != null)) {
c = c._getModel(this.custom_types, this.operations);
}
@ -281,14 +281,17 @@ module.exports = function() {
return this;
};
ListManager.prototype.insert = function(position, content) {
ListManager.prototype.insert = function(position, contents) {
var ith;
ith = this.getOperationByPosition(position);
return this.insertAfter(ith, [content]);
return this.insertAfter(ith, contents);
};
ListManager.prototype["delete"] = function(position, length) {
var d, delete_ops, i, o, _i;
if (length == null) {
length = 1;
}
o = this.getOperationByPosition(position + 1);
delete_ops = [];
for (i = _i = 0; 0 <= length ? _i < length : _i > length; i = 0 <= length ? ++_i : --_i) {

View File

@ -16,7 +16,7 @@ YList = (function() {
YList.prototype._getModel = function(types, ops) {
if (this._model == null) {
this._model = new ops.ListManager(this).execute();
this.insert(0, this._list);
this._model.insert(0, this._list);
}
delete this._list;
return this._model;
@ -45,7 +45,15 @@ YList = (function() {
if (typeof position !== "number") {
throw new Error("Y.List.insert expects a Number as the first parameter!");
}
this._model.insert(position, content);
this._model.insert(position, [content]);
return this;
};
YList.prototype.insertContents = function(position, contents) {
if (typeof position !== "number") {
throw new Error("Y.List.insert expects a Number as the first parameter!");
}
this._model.insert(position, contents);
return this;
};

View File

@ -42,6 +42,9 @@ YXml = (function() {
this._model.val("classes", new Y.Object(this._xml.classes));
this._model.val("tagname", this._xml.tagname);
this._model.val("children", new Y.List());
if (this._xml.parent != null) {
this._model.val("parent", this._xml.parent);
}
}
delete this._xml;
return this._model;
@ -54,8 +57,12 @@ YXml = (function() {
YXml.prototype._setParent = function(parent) {
if (parent instanceof YXml) {
this.remove();
return this._model.val("parent", parent);
if (this._model != null) {
this.remove();
return this._model.val("parent", parent);
} else {
return this._xml.parent = parent;
}
} else {
throw new Error("parent must be of type Y.Xml!");
}
@ -119,7 +126,7 @@ YXml = (function() {
if (parent == null) {
throw new Error("This Xml Element must not have siblings! (for it does not have a parent)");
}
_ref = parent.val("children").val();
_ref = parent.getChildren();
for (position = _i = 0, _len = _ref.length; _i < _len; position = ++_i) {
c = _ref[position];
if (c === this) {
@ -129,25 +136,28 @@ YXml = (function() {
contents = [];
for (_j = 0, _len1 = arguments.length; _j < _len1; _j++) {
content = arguments[_j];
if (!(content instanceof YXml || content.constructor === String)) {
if (content instanceof YXml) {
content._setParent(this._model.val("parent"));
} else if (content.constructor !== String) {
throw new Error("Y.Xml.after expects instances of YXml or String as a parameter");
}
contents.push(content);
}
return parent._model.val("children").insert(position + 1, contents);
return parent._model.val("children").insertContents(position + 1, contents);
};
YXml.prototype.append = function() {
var content, contents, _i, _len;
contents = [];
var content, _i, _len;
for (_i = 0, _len = arguments.length; _i < _len; _i++) {
content = arguments[_i];
if (!(content instanceof YXml || content.constructor === String)) {
if (content instanceof YXml) {
content._setParent(this);
} else if (content.constructor !== String) {
throw new Error("Y.Xml.after expects instances of YXml or String as a parameter");
}
contents.push(content);
this._model.val("children").push(content);
}
return this._model.val("children").push(contents);
return this;
};
YXml.prototype.before = function() {
@ -156,7 +166,7 @@ YXml = (function() {
if (parent == null) {
throw new Error("This Xml Element must not have siblings! (for it does not have a parent)");
}
_ref = parent.val("children").val();
_ref = parent.getChildren();
for (position = _i = 0, _len = _ref.length; _i < _len; position = ++_i) {
c = _ref[position];
if (c === this) {
@ -166,12 +176,14 @@ YXml = (function() {
contents = [];
for (_j = 0, _len1 = arguments.length; _j < _len1; _j++) {
content = arguments[_j];
if (!(content instanceof YXml || content.constructor === String)) {
if (content instanceof YXml) {
content._setParent(this._model.val("parent"));
} else if (content.constructor !== String) {
throw new Error("Y.Xml.after expects instances of YXml or String as a parameter");
}
contents.push(content);
}
return parent._model.val("children").insert(position, contents);
return parent._model.val("children").insertContents(position, contents);
};
YXml.prototype.empty = function() {
@ -194,16 +206,17 @@ YXml = (function() {
};
YXml.prototype.prepend = function() {
var content, contents, _i, _len;
contents = [];
var content, _i, _len;
for (_i = 0, _len = arguments.length; _i < _len; _i++) {
content = arguments[_i];
if (!(content instanceof YXml || content.constructor === String)) {
if (content instanceof YXml) {
content._setParent(this);
} else if (content.constructor !== String) {
throw new Error("Y.Xml.after expects instances of YXml or String as a parameter");
}
contents.push(content);
this._model.val("children").insert(0, content);
}
return this._model.val("children").insert(0, contents);
return this;
};
YXml.prototype.remove = function() {
@ -214,7 +227,7 @@ YXml = (function() {
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
c = _ref[i];
if (c === this) {
parent._model["delete"](i);
parent._model.val("children")["delete"](i);
break;
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -216,19 +216,19 @@ module.exports = ()->
o
push: (content)->
@insertAfter @end.prev_cl, content
@insertAfter @end.prev_cl, [content]
insertAfter: (left, content)->
insertAfter: (left, contents)->
right = left.next_cl
while right.isDeleted()
right = right.next_cl # find the first character to the right, that is not deleted. In the case that position is 0, its the Delimiter.
left = right.prev_cl
# TODO: always expect an array as content. Then you can combine this with the other option (else)
if content instanceof ops.Operation
if contents instanceof ops.Operation
(new ops.Insert null, content, undefined, left, right).execute()
else
for c in content
for c in contents
if c? and c._name? and c._getModel?
c = c._getModel(@custom_types, @operations)
tmp = (new ops.Insert null, c, undefined, left, right).execute()
@ -236,22 +236,23 @@ module.exports = ()->
@
#
# Inserts a string into the word.
# Inserts an array of content into this list.
# @Note: This expects an array as content!
#
# @return {ListManager Type} This String object.
#
insert: (position, content)->
insert: (position, contents)->
ith = @getOperationByPosition position
# the (i-1)th character. e.g. "abc" the 1th character is "a"
# the 0th character is the left Delimiter
@insertAfter ith, [content]
@insertAfter ith, contents
#
# Deletes a part of the word.
#
# @return {ListManager Type} This String object
#
delete: (position, length)->
delete: (position, length = 1)->
o = @getOperationByPosition(position+1) # position 0 in this case is the deletion of the first character
delete_ops = []

View File

@ -17,7 +17,7 @@ class YList
_getModel: (types, ops)->
if not @_model?
@_model = new ops.ListManager(@).execute()
@insert 0, @_list
@_model.insert 0, @_list
delete @_list
@_model
@ -36,14 +36,20 @@ class YList
@
#
# Inserts a Array into the list.
# Inserts an Object into the list.
#
# @return {ListManager Type} This String object.
#
insert: (position, content)->
if typeof position isnt "number"
throw new Error "Y.List.insert expects a Number as the first parameter!"
@_model.insert position, content
@_model.insert position, [content]
@
insertContents: (position, contents)->
if typeof position isnt "number"
throw new Error "Y.List.insert expects a Number as the first parameter!"
@_model.insert position, contents
@
delete: (position, length)->

View File

@ -30,6 +30,8 @@ class YXml
@_model.val("classes", new Y.Object(@_xml.classes))
@_model.val("tagname", @_xml.tagname)
@_model.val("children", new Y.List())
if @_xml.parent?
@_model.val("parent", @_xml.parent)
delete @_xml
@_model
@ -38,8 +40,11 @@ class YXml
_setParent: (parent)->
if parent instanceof YXml
@remove()
@_model.val("parent", parent)
if @_model?
@remove()
@_model.val("parent", parent)
else
@_xml.parent = parent
else
throw new Error "parent must be of type Y.Xml!"
@ -95,30 +100,32 @@ class YXml
throw new Error "This Xml Element must not have siblings! (for it does not have a parent)"
# find the position of this element
for c,position in parent.val("children").val()
for c,position in parent.getChildren()
if c is @
break
contents = []
for content in arguments
if not (content instanceof YXml or content.constructor is String)
if content instanceof YXml
content._setParent(@_model.val("parent"))
else if content.constructor isnt String
throw new Error "Y.Xml.after expects instances of YXml or String as a parameter"
contents.push content
parent._model.val("children").insert(position+1, contents)
parent._model.val("children").insertContents(position+1, contents)
#
# Insert content, specified by the parameter, to the end of this element
# .append(content [, content])
#
append: ()->
contents = []
for content in arguments
if not (content instanceof YXml or content.constructor is String)
if content instanceof YXml
content._setParent(@)
else if content.constructor isnt String
throw new Error "Y.Xml.after expects instances of YXml or String as a parameter"
contents.push content
@_model.val("children").push(contents)
@_model.val("children").push(content)
@
#
# Insert content, specified by the parameter, after this element
@ -130,23 +137,26 @@ class YXml
throw new Error "This Xml Element must not have siblings! (for it does not have a parent)"
# find the position of this element
for c,position in parent.val("children").val()
for c,position in parent.getChildren()
if c is @
break
contents = []
for content in arguments
if not (content instanceof YXml or content.constructor is String)
if content instanceof YXml
content._setParent(@_model.val("parent"))
else if content.constructor isnt String
throw new Error "Y.Xml.after expects instances of YXml or String as a parameter"
contents.push content
parent._model.val("children").insert(position, contents)
parent._model.val("children").insertContents(position, contents)
#
# Remove all child nodes of the set of matched elements from the DOM.
# .empty()
#
empty: ()->
# TODO: do it like this : @_model.val("children", new Y.List())
for child in @_model.val("children").val()
child.remove()
@ -165,13 +175,13 @@ class YXml
# .prepend(content [, content])
#
prepend: ()->
contents = []
for content in arguments
if not (content instanceof YXml or content.constructor is String)
if content instanceof YXml
content._setParent(@)
else if content.constructor isnt String
throw new Error "Y.Xml.after expects instances of YXml or String as a parameter"
contents.push content
@_model.val("children").insert(0, contents)
@_model.val("children").insert(0, content)
@
#
# Remove this element from the DOM
@ -182,7 +192,7 @@ class YXml
if parent instanceof YXml
for c,i in parent.getChildren()
if c is @
parent._model.delete i
parent._model.val("children").delete i
break
undefined

View File

@ -29,9 +29,9 @@ class XmlTest extends Test
type: "XmlTest"
compare: (o1, o2)->
compare: (o1, o2, depth)->
if o1.constructor is Y.Xml
@compare o1._model, o2._model
@compare o1._model, o2._model, depth
else
super
@ -156,7 +156,7 @@ describe "Y-Xml", ->
@yTest.compareAll()
@u2.removeAttr("dtrn")
@yTest.compareAll()
@expect(@u3.attr("dtrn")).to.be.undefined
expect(@u3.attr("dtrn")).to.be.undefined
it "removeClass", ->
@u1.addClass("dtrn")
@ -164,7 +164,7 @@ describe "Y-Xml", ->
@yTest.compareAll()
@u2.removeClass("dtrn")
@yTest.compareAll()
@expect(@u3.attr("class")).to.equal("iExist")
expect(@u3.attr("class")).to.equal("iExist")
it "toggleClass", ->
@u1.addClass("dtrn")
@ -172,7 +172,7 @@ describe "Y-Xml", ->
@yTest.compareAll()
@u2.removeClass("dtrn")
@yTest.compareAll()
@expect(@u3.attr("class")).to.equal("iExist")
expect(@u3.attr("class")).to.equal("iExist")
@u3.toggleClass("iExist")
@u3.toggleClass("wraa")
@yTest.compareAll()

File diff suppressed because one or more lines are too long

2
y.js

File diff suppressed because one or more lines are too long