completed the xml tests - and lots of them run successfully

This commit is contained in:
DadaMonad
2015-02-26 18:28:35 +00:00
parent f9542b90db
commit f932f560bd
15 changed files with 10528 additions and 740 deletions

View File

@@ -487,6 +487,7 @@ module.exports = ()->
object: @parent.getCustomType() # TODO: You can combine getPosition + getParent in a more efficient manner! (only left Delimiter will hold @parent)
length: 1
changedBy: o.uid.creator
oldValue: @val()
]
#

View File

@@ -1,34 +1,7 @@
json_types_uninitialized = require "./JsonTypes"
# some dom implementations may call another dom.method that simulates the behavior of another.
# For example xml.insertChild(dom) , wich inserts an element at the end, and xml.insertAfter(dom,null) wich does the same
# But Y's proxy may be called only once!
proxy_token = false
dont_proxy = (f)->
proxy_token = true
try
f()
catch e
proxy_token = false
throw new Error e
proxy_token = false
_proxy = (f_name, f)->
old_f = @[f_name]
if old_f?
@[f_name] = ()->
if not proxy_token and not @_y?.isDeleted()
that = this
args = arguments
dont_proxy ()->
f.apply that, args
old_f.apply that, args
else
old_f.apply this, arguments
#else
# @[f_name] = f
Element?.prototype._proxy = _proxy
module.exports = (HB)->
@@ -46,202 +19,10 @@ module.exports = (HB)->
class XmlType extends types.Insert
constructor: (uid, @tagname, attributes, elements, @xml)->
### In case you make this instanceof Insert again
if prev? and (not next?) and prev.type?
# adjust what you actually mean. you want to insert after prev, then
# next is not defined. but we only insert after non-deleted elements.
# This is also handled in TextInsert.
while prev.isDeleted()
prev = prev.prev_cl
next = prev.next_cl
###
super(uid)
if @xml?._y?
d = new types.Delete undefined, @xml._y
HB.addOperation(d).execute()
@xml._y = null
if attributes? and elements?
@saveOperation 'attributes', attributes
@saveOperation 'elements', elements
else if (not attributes?) and (not elements?)
@attributes = new types.JsonType()
@attributes.setMutableDefault 'immutable'
HB.addOperation(@attributes).execute()
@elements = new types.WordType()
@elements.parent = @
HB.addOperation(@elements).execute()
else
throw new Error "Either define attribute and elements both, or none of them"
if @xml?
@tagname = @xml.tagName
for i in [0...@xml.attributes.length]
attr = xml.attributes[i]
@attributes.val(attr.name, attr.value)
for n in @xml.childNodes
if n.nodeType is n.TEXT_NODE
word = new TextNodeType(undefined, n)
HB.addOperation(word).execute()
@elements.push word
else if n.nodeType is n.ELEMENT_NODE
element = new XmlType undefined, undefined, undefined, undefined, n
HB.addOperation(element).execute()
@elements.push element
else
throw new Error "I don't know Node-type #{n.nodeType}!!"
@setXmlProxy()
undefined
#
# Identifies this class.
# Use it in order to check whether this is an xml-type or something else.
#
type: "XmlType"
applyDelete: (op)->
if @insert_parent? and not @insert_parent.isDeleted()
@insert_parent.applyDelete op
else
@attributes.applyDelete()
@elements.applyDelete()
super
cleanup: ()->
super()
setXmlProxy: ()->
@xml._y = @
that = @
@elements.on 'insert', (event, op)->
if op.creator isnt HB.getUserId() and this is that.elements
newNode = op.content.val()
right = op.next_cl
while right? and right.isDeleted()
right = right.next_cl
rightNode = null
if right.type isnt 'Delimiter'
rightNode = right.val().val()
dont_proxy ()->
that.xml.insertBefore newNode, rightNode
@elements.on 'delete', (event, op)->
del_op = op.deleted_by[0]
if del_op? and del_op.creator isnt HB.getUserId() and this is that.elements
deleted = op.content.val()
dont_proxy ()->
that.xml.removeChild deleted
@attributes.on ['add', 'update'], (event, property_name, op)->
if op.creator isnt HB.getUserId() and this is that.attributes
dont_proxy ()->
newval = op.val().val()
if newval?
that.xml.setAttribute(property_name, op.val().val())
else
that.xml.removeAttribute(property_name)
## Here are all methods that proxy the behavior of the xml
# you want to find a specific child element. Since they are carried by an Insert-Type, you want to find that Insert-Operation.
# @param child {DomElement} Dom element.
# @return {InsertType} This carries the XmlType that represents the DomElement (child). false if i couldn't find it.
#
findNode = (child)->
if not child?
throw new Error "you must specify a parameter!"
child = child._y
elem = that.elements.beginning.next_cl
while elem.type isnt 'Delimiter' and elem.content isnt child
elem = elem.next_cl
if elem.type is 'Delimiter'
false
else
elem
insertBefore = (insertedNode_s, adjacentNode)->
next = null
if adjacentNode?
next = findNode adjacentNode
prev = null
if next
prev = next.prev_cl
else
prev = @_y.elements.end.prev_cl
while prev.isDeleted()
prev = prev.prev_cl
inserted_nodes = null
if insertedNode_s.nodeType is insertedNode_s.DOCUMENT_FRAGMENT_NODE
child = insertedNode_s.lastChild
while child?
element = new XmlType undefined, undefined, undefined, undefined, child
HB.addOperation(element).execute()
that.elements.insertAfter prev, element
child = child.previousSibling
else
element = new XmlType undefined, undefined, undefined, undefined, insertedNode_s
HB.addOperation(element).execute()
that.elements.insertAfter prev, element
@xml._proxy 'insertBefore', insertBefore
@xml._proxy 'appendChild', insertBefore
@xml._proxy 'removeAttribute', (name)->
that.attributes.val(name, undefined)
@xml._proxy 'setAttribute', (name, value)->
that.attributes.val name, value
renewClassList = (newclass)->
dont_do_it = false
if newclass?
for elem in this
if newclass is elem
dont_do_it = true
value = Array.prototype.join.call this, " "
if newclass? and not dont_do_it
value += " "+newclass
that.attributes.val('class', value )
_proxy.call @xml.classList, 'add', renewClassList
_proxy.call @xml.classList, 'remove', renewClassList
@xml.__defineSetter__ 'className', (val)->
@setAttribute('class', val)
@xml.__defineGetter__ 'className', ()->
that.attributes.val('class')
@xml.__defineSetter__ 'textContent', (val)->
# remove all nodes
elem = that.xml.firstChild
while elem?
remove = elem
elem = elem.nextSibling
that.xml.removeChild remove
# insert word content
if val isnt ""
text_node = document.createTextNode val
that.xml.appendChild text_node
removeChild = (node)->
elem = findNode node
if not elem
throw new Error "You are only allowed to delete existing (direct) child elements!"
d = new types.Delete undefined, elem
HB.addOperation(d).execute()
node._y = null
@xml._proxy 'removeChild', removeChild
@xml._proxy 'replaceChild', (insertedNode, replacedNode)->
insertBefore.call this, insertedNode, replacedNode
removeChild.call this, replacedNode
val: (enforce = false)->
if document?
@@ -314,53 +95,3 @@ module.exports = (HB)->
} = json
new XmlType uid, tagname, attributes, elements, undefined
#
# @nodoc
# Defines an object that is cannot be changed. You can use this to set an immutable string, or a number.
#
class TextNodeType extends types.ImmutableObject
#
# @param {Object} uid A unique identifier. If uid is undefined, a new uid will be created.
# @param {Object} content
#
constructor: (uid, content)->
if content._y?
d = new types.Delete undefined, content._y
HB.addOperation(d).execute()
content._y = null
content._y = @
super uid, content
applyDelete: (op)->
if @insert_parent? and not @insert_parent.isDeleted()
@insert_parent.applyDelete op
else
super
type: "TextNodeType"
#
# Encode this operation in such a way that it can be parsed by remote peers.
#
_encode: ()->
json = {
'type': @type
'uid' : @getUid()
'content' : @content.textContent
}
json
parser['TextNodeType'] = (json)->
{
'uid' : uid
'content' : content
} = json
textnode = document.createTextNode content
new TextNodeType uid, textnode
types['XmlType'] = XmlType
json_types

View File

@@ -1,42 +1,72 @@
class YXml
constructor: (tagname, attributes = {})->
@_xml = {}
#TODO: How to force the user to specify parameters?
#if not tagname?
# throw new Error "You must specify a tagname"
@_xml.tagname = tagname
if attributes.constructor isnt Object
throw new Error "The attributes must be specified as a Object"
for a_name, a of attributes
if a.constructor isnt String
throw new Error "The attributes must be of type String!"
@_xml.attributes = attributes
@_xml.classes = {}
_classes = @_xml.attributes.class
delete @_xml.attributes.class
if _classes?
for c_name, c in _classes.split(" ")
if c.length > 0
@_xml.classes[c_name] = c
undefined
constructor: (tag_or_dom, attributes = {})->
if not tag_or_dom?
# nop
else if tag_or_dom.constructor is String
tagname = tag_or_dom
@_xml = {}
@_xml.children = []
#TODO: How to force the user to specify parameters?
#if not tagname?
# throw new Error "You must specify a tagname"
@_xml.tagname = tagname
if attributes.constructor isnt Object
throw new Error "The attributes must be specified as a Object"
for a_name, a of attributes
if a.constructor isnt String
throw new Error "The attributes must be of type String!"
@_xml.attributes = attributes
@_xml.classes = {}
_classes = @_xml.attributes.class
delete @_xml.attributes.class
if _classes?
for c_name, c in _classes.split(" ")
if c.length > 0
@_xml.classes[c_name] = c
undefined
else if tag_or_dom instanceof Element
@_dom = tag_or_dom
@_xml = {}
_name: "Xml"
_getModel: (Y, ops)->
if not @_model?
if @_dom?
@_xml.tagname = @_dom.tagName.toLowerCase()
@_xml.attributes = {}
@_xml.classes = {}
for attribute in @_dom.attributes
if attribute.name is "class"
for c in attribute.value.split(" ")
@_xml.classes[c] = true
else
@_xml.attributes[attribute.name] = attribute.value
@_xml.children = []
for child in @_dom.childNodes
if child.nodeType is child.TEXT_NODE
@_xml.children.push child.textContent
else
@_xml.children.push(new YXml(child))
@_model = new ops.MapManager(@).execute()
@_model.val("attributes", new Y.Object(@_xml.attributes))
@_model.val("classes", new Y.Object(@_xml.classes))
@_model.val("tagname", @_xml.tagname)
@_model.val("children", new Y.List())
@_model.val("children", new Y.List(@_xml.children))
if @_xml.parent?
@_model.val("parent", @_xml.parent)
@_setModel @_model
if @_dom?
@getDom() # two way bind dom to this xml type
@_setModel @_model
@_model
_setModel: (@_model)->
delete @_xml
@_model.observe (events)->
for event in events
if event.name is "parent" and event.type isnt "add"
@@ -47,8 +77,7 @@ class YXml
if c is @
parent._model.val("children").delete i
break
undefined
delete @_xml
_setParent: (parent)->
if parent instanceof YXml
@@ -260,6 +289,158 @@ class YXml
getChildren: ()->
@_model.val("children").val()
getPosition: ()->
parent = @_model.val("parent")
if parent?
for c,i in parent._model.val("children").val()
if c is @
return i
throw new Error "This is not a child of its parent (should not happen in Y.Xml!)"
else
null
getDom: ()->
if not @_dom?
@_dom = document.createElement(@_model.val("tagname"))
# set the attributes _and_ the classes (@see .attr())
for attr_name, attr_value of @attr()
@_dom.setAttribute attr_name, attr_value
for child,i in @getChildren()
if child.constructor is String
dom = document.createTextNode child
else
dom = child.getDom()
@_dom.insertBefore dom
that = @
if (not @_dom._y_xml?)
@_dom._y_xml = @
initialize_proxies.call @
@_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]
else
rightNode = null
event.value._setParent that
dont_proxy ()->
that._dom.insertBefore newNode, rightNode
else if event.type is "delete"
deleted = event.oldValue.getDom()
dont_proxy ()->
that._dom.removeChild deleted
@_model.val("attributes").observe (events)->
for event in events
if event.type is "add" or event.type is "update"
newval = event.object.val(event.name)
dont_proxy ()->
that._dom.setAttribute event.name, newval
else if event.type is "delete"
dont_proxy ()->
that._dom.removeAttribute event.name
@_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
@_dom
proxies_are_initialized = false
# some dom implementations may call another dom.method that simulates the behavior of another.
# For example xml.insertChild(dom) , wich inserts an element at the end, and xml.insertAfter(dom,null) wich does the same
# But Y's proxy may be called only once!
proxy_token = false
dont_proxy = (f)->
proxy_token = true
try
f()
catch e
proxy_token = false
throw new Error e
proxy_token = false
initialize_proxies = ()->
_proxy = (f_name, f, source = Element.prototype)->
old_f = source[f_name]
source[f_name] = ()->
if (not @_y_xml?) or proxy_token
old_f.apply this, arguments
else
f.apply @_y_xml, arguments
that = this
@_dom.classList.add = (c)->
that.addClass c
@_dom.classList.remove = (c)->
that.removeClass c
@_dom.__defineSetter__ 'className', (val)->
that.attr('class', val)
@_dom.__defineGetter__ 'className', ()->
that.attr('class')
@_dom.__defineSetter__ 'textContent', (val)->
# remove all nodes
that.empty()
# insert word content
if val isnt ""
that.append val
if proxies_are_initialized
return
proxies_are_initialized = true
# the following methods are initialized on prototypes and therefore they need to be written only once!
insertBefore = (insertedNode_s, adjacentNode)->
if adjacentNode?
pos = adjacentNode._y_xml.getPosition()
else
pos = @getChildren().length
new_childs = []
if insertedNode_s.nodeType is insertedNode_s.DOCUMENT_FRAGMENT_NODE
child = insertedNode_s.firstChild
while child?
new_childs.push child
child = child.nextSibling
else
new_childs.push insertedNode_s
new_childs = new_childs.map (child)->
if child._y_xml?
child._y_xml
else if child.nodeType == child.TEXT_NODE
child.textContent
else
new YXml(child)
@_model.val("children").insertContents pos, new_childs
_proxy 'insertBefore', insertBefore
_proxy 'appendChild', insertBefore
_proxy 'removeAttribute', (name)->
@removeAttr name
_proxy 'setAttribute', (name, value)->
@attr name, value
removeChild = (node)->
node._y_xml.remove()
_proxy 'removeChild', removeChild, @_dom
replaceChild = (insertedNode, replacedNode)->
insertBefore.call this, insertedNode, replacedNode
removeChild.call this, replacedNode
_proxy 'replaceChild', replaceChild, @_dom
if window?
if window.Y?