From: Aleksander Ɓukasz Date: Thu, 1 Aug 2013 08:26:21 +0000 (+0200) Subject: Cleanup: removing old unused code X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/80c653023eaa918f6b736e84d12e17d3c9c24d88 Cleanup: removing old unused code --- diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js deleted file mode 100644 index ab8a578..0000000 --- a/modules/documentCanvas/canvas.js +++ /dev/null @@ -1,314 +0,0 @@ -define([ -'libs/jquery-1.9.1.min', -'libs/underscore-min', -'modules/documentCanvas/transformations', -'modules/documentCanvas/canvasNode', -'libs/text!./template.html' -], function($, _, transformations, canvasNode, template) { - -'use strict'; - -var Canvas = function(html) { - this.dom = $(template); - this.content = this.dom.find('#rng-module-documentCanvas-content'); - this.setHTML(html); -}; - -Canvas.prototype.setHTML = function(html) { - if(html) { - this.content.html(html); - } -}; - -Canvas.prototype.getContent = function() { - return this.content.contents(); -}; - -Canvas.prototype.findNodes = function(desc) { - var selector = ''; - if(typeof desc === 'string') { - selector = desc; - } - else { - if(desc.klass) - selector += '[wlxml-class=' + desc.klass + ']'; - if(desc.tag) - selector += '[wlxml-tag=' + desc.tag + ']'; - } - var toret = []; - this.content.find(selector).each(function() { - toret.push(canvasNode.create($(this))); - }); - return toret; -}; - -Canvas.prototype.getNodeById = function(id) { - return canvasNode.create($(this.content.find('#' +id))); -}; - -Canvas.prototype.nodeAppend = function(options) { - var element; // = $(this.content.find('#' + options.context.id).get(0)); - if(options.to === 'root') { - element = this.content; - } else { - element = $(this.content.find('#' + options.to.getId()).get(0)); - } - element.append(options.node.dom); -}; - -Canvas.prototype.nodeInsertAfter = function(options) { - var element = $(this.content.find('#' + options.after.getId()).get(0)); - element.after(options.node.dom); -}; - -Canvas.prototype.nodeWrap = function(options) { - options = _.extend({textNodeIdx: 0}, options); - if(typeof options.textNodeIdx === 'number') - options.textNodeIdx = [options.textNodeIdx]; - - var container = $(this.content.find('#' + options.inside.getId()).get(0)), - containerContent = container.contents(), - idx1 = Math.min.apply(Math, options.textNodeIdx), - idx2 = Math.max.apply(Math, options.textNodeIdx), - textNode1 = $(containerContent.get(idx1)), - textNode2 = $(containerContent.get(idx2)), - sameNode = textNode1.get(0) === textNode2.get(0), - prefixOutside = textNode1.text().substr(0, options.offsetStart), - prefixInside = textNode1.text().substr(options.offsetStart), - suffixInside = textNode2.text().substr(0, options.offsetEnd), - suffixOutside = textNode2.text().substr(options.offsetEnd) - ; - - textNode1.after(options._with.dom); - textNode1.detach(); - - options._with.dom.before(prefixOutside); - if(sameNode) { - var core = textNode1.text().substr(options.offsetStart, options.offsetEnd - options.offsetStart); - options._with.setContent(core); - } else { - textNode2.detach(); - options._with.dom.append(prefixInside); - for(var i = idx1 + 1; i < idx2; i++) { - options._with.dom.append(containerContent[i]); - } - options._with.dom.append(suffixInside); - } - options._with.dom.after(suffixOutside); -}; - -Canvas.prototype.nodeUnwrap = function(options) { - - var removeWithJoin = function(node) { - var contents = node.parent().contents(), - idx = contents.index(node), - prev = idx > 0 ? contents[idx-1] : null, - next = idx + 1 < contents.length ? contents[idx+1] : null; - - if(prev && prev.nodeType === 3 && next && next.nodeType === 3) { - prev.data = prev.data + next.data; - $(next).remove(); - } - node.remove(); - }; - - var toUnwrap = $(this.content.find('#' + options.node.getId()).get(0)); - - - var parent = toUnwrap.parent(); - var parentContents = parent.contents(); - - if(toUnwrap.contents().length !== 1 || toUnwrap.contents()[0].nodeType !== 3) - return false; - - var idx = parentContents.index(toUnwrap); - - var combineWith, - action; - - if(idx > 0 && parentContents[idx-1].nodeType === 3) { - combineWith = parentContents[idx-1]; - action = 'append'; - } else if(idx + 1 < parentContents.length && parentContents[idx+1].nodeType === 3) { - combineWith = parentContents[idx+1]; - action = 'prepend'; - } - - if(combineWith) { - var text = - (action === 'prepend' ? toUnwrap.text() : '') + - combineWith.data + - (action === 'append' ? toUnwrap.text() : '') - ; - combineWith.data = text; - removeWithJoin(toUnwrap); - } else { - if(parentContents.length === 1 || idx === 0) { - parent.append(toUnwrap.text()); - } else { - toUnwrap.prev().after(toUnwrap.text()); - } - toUnwrap.remove(); - } -}; - -Canvas.prototype.nodeSplit = function(options) { - options = _.extend({textNodeIdx: 0}, options); - - var nodeToSplit = $(this.content.find('#' + options.node.getId()).get(0)); - - var nodeContents = nodeToSplit.contents(); - if(nodeContents.length === 0 || - nodeContents.length - 1 < options.textNodeIdx || - nodeContents.get(options.textNodeIdx).nodeType != 3) - return false; - - var textNode = $(nodeContents.get(options.textNodeIdx)); - - var succeedingNodes = []; - var passed = false; - nodeContents.each(function() { - var node = this; - if(passed) - succeedingNodes.push(node); - if(node === textNode.get(0)) - passed = true; - }); - - var prefix = $.trim(textNode.text().substr(0, options.offset)); - var suffix = $.trim(textNode.text().substr(options.offset)); - - textNode.before(prefix); - textNode.remove(); - - var newNode = canvasNode.create({tag: nodeToSplit.attr('wlxml-tag'), klass: nodeToSplit.attr('wlxml-class')}); - newNode.dom.append(suffix); - succeedingNodes.forEach(function(node) { - newNode.dom.append(node); - }); - nodeToSplit.after(newNode.dom); - return newNode; -}; - -Canvas.prototype.nodeRemove = function(options) { - var toRemove = $(this.content.find('#' + options.node.getId()).get(0)); - toRemove.remove(); -}; - -Canvas.prototype.listCreate = function(options) { - var element1 = $(this.content.find('#' + options.start.getId()).get(0)); - var element2 = $(this.content.find('#' + options.end.getId()).get(0)); - if(element1.parent().get(0) !== element2.parent().get(0)) - return false; - - var parent = element1.parent(); - - if(parent.contents().index(element1) > parent.contents().index(element2)) { - var tmp = element1; - element1 = element2; - element2 = tmp; - } - - var nodesToWrap = []; - - var place = 'before'; - var canvas = this; - parent.contents().each(function() { - var node = this; - if(node === element1.get(0)) - place = 'inside'; - if(place === 'inside') { - var $node; - if(node.nodeType === 3) { - $node = canvasNode.create({tag: 'div', content: $.trim(node.data)}).dom; //canvas._createNode('div').text(node.data); - $(node).remove(); - } - else { - $node = $(node); - } - $node.attr('wlxml-class', 'item'); - nodesToWrap.push($node); - } - if(node === element2.get(0)) - return false; - }); - - var list = canvasNode.create({tag: 'div', klass: 'list-items' + (options.type === 'enum' ? '-enum' : '')}).dom; //this._createNode('div', 'list-items'); - - var parentNode = options.start.parent(); - - var toret; - if(parentNode && parentNode.isOfClass('list-items')) { - list.wrap('
'); - toret = list.parent(); - } else { - toret = list; - } - - - element1.before(toret); - - nodesToWrap.forEach(function(node) { - node.remove(); - list.append(node); - }); -}; - -Canvas.prototype.listRemove = function(options) { - var pointerElement = $(this.content.find('#' + options.pointer.getId())); - var listElement = options.pointer.isOfClass('list-items') ? pointerElement : - pointerElement.parents('[wlxml-class|="list-items"][wlxml-tag]'); - - var nested = false, - nestedLists; - if(listElement.length > 1) { - listElement = $(listElement[0]); - nested = true; - } - - if(nested) { - // We are only moving one level up - listElement.unwrap(); - } else { - // We are removing the whole list - nestedLists = listElement.find('[wlxml-class=item] > [wlxml-class|=list-items]'); - nestedLists.unwrap(); - listElement.find('[wlxml-class=item]').each(function() { - $(this).removeAttr('wlxml-class'); - }); - } - - listElement.children().unwrap(); - - var c = this; - if(nestedLists) { - nestedLists.each(function() { - c.listRemove({pointer: canvasNode.create($(this))}); - }); - } -}; - -Canvas.prototype.getPrecedingNode = function(options) { - var element = $(this.content.find('#' + options.node.getId()).get(0)); - var prev = element.prev(); - if(prev.length === 0) - prev = element.parent(); - return canvasNode.create(prev); -}; - -Canvas.prototype.nodeInsideList = function(options) { - if(options.node) { - if(options.node.isOfClass('list-items') || options.node.isOfClass('item')) - return true; - var pointerElement = $(this.content.find('#' + options.node.getId())); - return pointerElement.parents('[wlxml-class=list-items], [wlxml-class=item]').length > 0; - } - return false; -}; - - -return { - create: function(desc) { return new Canvas(desc); } -}; - -}); \ No newline at end of file diff --git a/modules/documentCanvas/canvasManager.js b/modules/documentCanvas/canvasManager.js deleted file mode 100644 index 69cfd07..0000000 --- a/modules/documentCanvas/canvasManager.js +++ /dev/null @@ -1,187 +0,0 @@ -define([ -'libs/jquery-1.9.1.min', -'./canvasNode' -], function($, canvasNode) { - -'use strict'; - -var getCursorPosition = function() { - var selection = window.getSelection(); - var anchorNode = $(selection.anchorNode); - var parent = anchorNode.parent(); - return { - textNode: anchorNode, - textNodeOffset: selection.anchorOffset, - textNodeIndex: parent.contents().index(anchorNode), - parentNode: parent, - focusNode: $(selection.focusNode).parent(), - isAtEnd: selection.anchorOffset === anchorNode.text().length - }; -}; - -var Manager = function(canvas, sandbox) { - this.canvas = canvas; - this.sandbox = sandbox; - this.shownAlready = false; - this.scrollbarPosition = 0; - this.currentNode = null; - var manager = this; - - // canvas.doc().dom().find('#rng-module-documentCanvas-content').on('keyup', function() { - // manager.sandbox.publish('contentChanged'); - // }); - - // canvas.doc().dom().on('mouseover', '[wlxml-tag]', function(e) { - // e.stopPropagation(); - // manager.sandbox.publish('nodeHovered', canvasNode.create($(e.target))); - // }); - // canvas.doc().dom().on('mouseout', '[wlxml-tag]', function(e) { - // e.stopPropagation(); - // manager.sandbox.publish('nodeBlured', canvasNode.create($(e.target))); - // }); - // canvas.doc().dom().on('click', '[wlxml-tag]', function(e) { - // e.stopPropagation(); - // console.log('clicked node type: '+e.target.nodeType); - // manager.selectNode(canvasNode.create($(e.target))); - // }); - - // canvas.doc().dom().on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) { - // var anchor = $(window.getSelection().anchorNode); - - // if(anchor[0].nodeType === Node.TEXT_NODE) - // anchor = anchor.parent(); - // if(!anchor.is('[wlxml-tag]')) - // return; - // manager.selectNode(canvasNode.create(anchor)); - // }); - - // canvas.doc().dom().on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) { - // if(e.which === 13) { - // manager.onEnterKey(e); - // } - // if(e.which === 8) { - // manager.onBackspaceKey(e); - // } - // }); - - // canvas.doc().dom().onShow = function() { - // if(!manager.shownAlready) { - // manager.shownAlready = true; - // manager.selectFirstNode(); - // } else if(manager.currentNode) { - // manager.movecaretToNode(manager.getNodeElement(manager.currentNode)); - // canvas.doc().dom().find('#rng-module-documentCanvas-contentWrapper').scrollTop(manager.scrollbarPosition); - // } - // }; - // canvas.doc().dom().onHide = function() { - // manager.scrollbarPosition = canvas.doc().dom().find('#rng-module-documentCanvas-contentWrapper').scrollTop(); - // }; -}; - -Manager.prototype.selectNode = function(cnode, options) { - options = options || {}; - var nodeElement = this.getNodeElement(cnode); - - this.dimNode(cnode); - - this.canvas.doc().dom().find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode'); - nodeElement.addClass('rng-module-documentCanvas-currentNode'); - - if(options.movecaret) { - this.movecaretToNode(nodeElement, options.movecaret); - } - - this.currentNode = cnode; - this.sandbox.publish('nodeSelected', cnode); -}; - -Manager.prototype.getNodeElement = function(cnode) { - return this.canvas.doc().dom().find('#'+cnode.getId()); -}; - -Manager.prototype.highlightNode = function(cnode) { - var nodeElement = this.getNodeElement(cnode); - if(!this.gridToggled) { - nodeElement.addClass('rng-common-hoveredNode'); - var label = nodeElement.attr('wlxml-tag'); - if(nodeElement.attr('wlxml-class')) - label += ' / ' + nodeElement.attr('wlxml-class'); - var tag = $('
').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label); - nodeElement.append(tag); - } -}; - -Manager.prototype.dimNode = function(cnode) { - var nodeElement = this.getNodeElement(cnode); - if(!this.gridToggled) { - nodeElement.removeClass('rng-common-hoveredNode'); - nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove(); - } -}; - -Manager.prototype.selectFirstNode = function() { - var firstNodeWithText = this.canvas.doc().dom().find('[wlxml-tag]').filter(function() { - return $(this).clone().children().remove().end().text().trim() !== ''; - }).first(); - var node; - if(firstNodeWithText.length) - node = $(firstNodeWithText[0]); - else { - node = this.canvas.doc().dom().find('[wlxml-class|="p"]'); - } - this.selectNode(canvasNode.create(node), {movecaret: true}); -}; - -Manager.prototype.movecaretToNode = function(nodeElement, where) { - if(!nodeElement.length) - return; - var range = document.createRange(); - range.selectNodeContents(nodeElement[0]); - - var collapseArg = true; - if(where === 'end') - collapseArg = false; - range.collapse(collapseArg); - var selection = document.getSelection(); - selection.removeAllRanges(); - selection.addRange(range); -}; - -Manager.prototype.onEnterKey = function(e) { - e.preventDefault(); - var pos = getCursorPosition(); - var contextNode = this.canvas.getNodeById(pos.parentNode.attr('id')); - var newNode; - - if(pos.isAtEnd) { - newNode = canvasNode.create({tag: pos.parentNode.attr('wlxml-tag'), klass: pos.parentNode.attr('wlxml-class')}); - this.canvas.nodeInsertAfter({node: newNode, after: this.canvas.getNodeById(pos.parentNode.attr('id'))}); - } else { - newNode = this.canvas.nodeSplit({node: contextNode, textNodeIdx: pos.textNodeIndex, offset: pos.textNodeOffset}); - } - if(newNode) - this.selectNode(newNode, {movecaret: true}); - this.sandbox.publish('contentChanged'); -}; - -Manager.prototype.onBackspaceKey = function(e) { - var pos = getCursorPosition(); - var len = pos.textNode.text().length; - if(len === 1) { - // Prevent deleting node by browser after last character removed; - e.preventDefault(); - pos.parentNode.text(''); - } - if(len === 0) { - e.preventDefault(); - var toRemove = canvasNode.create(pos.textNode); - var prevNode = this.canvas.getPrecedingNode({node:toRemove}); - this.canvas.nodeRemove({node: toRemove}); // jesli nie ma tekstu, to anchor nie jest tex nodem - this.selectNode(prevNode, {movecaret: 'end'}); - } -}; - - -return Manager; - -}); \ No newline at end of file diff --git a/modules/documentCanvas/canvasNode.js b/modules/documentCanvas/canvasNode.js deleted file mode 100644 index 72343a4..0000000 --- a/modules/documentCanvas/canvasNode.js +++ /dev/null @@ -1,135 +0,0 @@ -define([ -'libs/jquery-1.9.1.min', -'libs/underscore-min', -'modules/documentCanvas/classAttributes' -], function($, _, classAttributes) { - -'use strict'; - - - - -var tagSelector = '[wlxml-tag]'; - -var CanvasNode = function(desc) { - if(desc instanceof $) { - this.dom = desc; - if(!this.dom.attr('id')) { - this.dom.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);})); - } - } else { - var toBlock = ['div', 'document', 'section', 'header']; - var htmlTag = _.contains(toBlock, desc.tag) ? 'div' : 'span'; - this.dom = $('<' + htmlTag + '>'); - this.dom.attr('wlxml-tag', desc.tag); - if(desc.klass) - this.dom.attr('wlxml-class', desc.klass); - if(desc.content) - this.dom.text(desc.content); - if(desc.meta) { - var c = this; - _.keys(desc.meta) - .filter(function(key) {return classAttributes.hasMetaAttr(c.getClass(), key);}) - .forEach(function(key) { - c.dom.attr('wlxml-meta-'+key, desc.meta[key]); - }); - } - this.dom.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);})); - } -}; - -CanvasNode.prototype.getTag = function() { - return this.dom.attr('wlxml-tag'); -}; - -CanvasNode.prototype.getClass = function() { - return this.dom.attr('wlxml-class'); -}; - -CanvasNode.prototype.setClass = function(klass) { - if(klass != this.getClass()) { - var c = this; - this.getMetaAttrs().forEach(function(attr) { - if(!classAttributes.hasMetaAttr(klass, attr.name)) - c.dom.removeAttr('wlxml-meta-' + attr.name); - }); - this.dom.attr('wlxml-class', klass); - } -}; - -CanvasNode.prototype.getId = function() { - return this.dom.attr('id'); -}; - -CanvasNode.prototype.getContent = function() { - return this.dom.text(); -}; - -CanvasNode.prototype.setContent = function(content) { - this.dom.text(content); -}; - -CanvasNode.prototype.isSame = function(other) { - return (other instanceof CanvasNode) && this.dom.get(0) === other.dom.get(0); -}; - -CanvasNode.prototype.children = function() { - var list = []; - this.dom.children(tagSelector).each(function() { - list.push(new CanvasNode($(this))); - }); - return $(list); -}; - - -CanvasNode.prototype.parent = function() { - var node = this.dom.parent(tagSelector); - if(node.length) - return new CanvasNode(node); - return null; -}; - -CanvasNode.prototype.parents = function() { - var list = []; - this.dom.parents(tagSelector).each(function() { - list.push(new CanvasNode($(this))); - }); - return $(list); -}; - - -CanvasNode.prototype.isOfClass = function(klass) { - return this.getClass() && this.getClass().substr(0, klass.length) === klass; -}; - -CanvasNode.prototype.getMetaAttr = function(attr) { - return this.dom.attr('wlxml-meta-'+attr); -}; - -CanvasNode.prototype.getMetaAttrs = function() { - var toret = []; - var metaAttrPrefix = 'wlxml-meta-'; - - var attrList = classAttributes.getMetaAttrsList(this.getClass()); - var c = this; - attrList.all.forEach(function(attr) { - toret.push({name: attr.name, value: c.getMetaAttr(attr.name) || ''}); - }); - - - return toret; -}; - -CanvasNode.prototype.setMetaAttr = function(attr, value) { - this.dom.attr('wlxml-meta-'+attr, value); -}; - -return { - create: function(desc) { - return new CanvasNode(desc); - } - -}; - - -}); \ No newline at end of file diff --git a/modules/documentCanvas/documentCanvas.js b/modules/documentCanvas/documentCanvas.js index 5689a8b..decda83 100644 --- a/modules/documentCanvas/documentCanvas.js +++ b/modules/documentCanvas/documentCanvas.js @@ -2,19 +2,15 @@ define([ 'libs/underscore-min', -'./transformations', -'./canvas', -'./canvasManager', './canvas/canvas', './commands', -'libs/text!./template.html'], function(_, transformations, Canvas, CanvasManager, canvas3, commands, template) { +'libs/text!./template.html'], function(_, canvas3, commands, template) { 'use strict'; return function(sandbox) { - var canvas = canvas3.fromXML('', sandbox.publish); //canvasCanvas.create(); - var manager; + var canvas = canvas3.fromXML('', sandbox.publish); var canvasWrapper = $(template); var shownAlready = false; var scrollbarPosition = 0, @@ -42,9 +38,8 @@ return function(sandbox) { return canvasWrapper; }, setDocument: function(xml) { - canvas.loadWlxml(xml); //canvas.setHTML(transformations.fromXML.getHTMLTree(xml)); + canvas.loadWlxml(xml); canvasWrapper.find('#rng-module-documentCanvas-content').empty().append(canvas.view()); - manager = new CanvasManager(canvas, sandbox); sandbox.publish('documentSet'); }, getDocument: function() { diff --git a/modules/documentCanvas/tests/canvas.test.js b/modules/documentCanvas/tests/canvas.test.js deleted file mode 100644 index 5dc017b..0000000 --- a/modules/documentCanvas/tests/canvas.test.js +++ /dev/null @@ -1,326 +0,0 @@ -define([ -'libs/jquery-1.9.1.min', -'libs/chai', -'./utils.js', -'modules/documentCanvas/canvas', -'modules/documentCanvas/canvasNode' -], function($, chai, utils, canvas, canvasNode) { - - 'use strict'; - - var assert = chai.assert; - var assertDomEqual = utils.assertDomEqual; - - - suite('Quering nodes', function() { - test('getting preceding node', function() { - var c = canvas.create('
a
b
'); - var secondP = c.findNodes({tag: 'p'})[1]; - var firstP = c.getPrecedingNode({node: secondP}); - assert.equal(firstP.getContent(), 'a'); - }); - - test('pervious node of node without "previous siblings" is its parent', function() { - var c = canvas.create('
a
'); - var paragraph = c.findNodes({tag: 'p'})[0]; - assert.equal(c.getPrecedingNode({node: paragraph}).getTag(), 'section'); - }); - - }); - - - suite('Inserting nodes', function() { - test('append node to root', function() { - var c = canvas.create(); - var node = canvasNode.create({tag: 'header', klass: 'some-class'}); - c.nodeAppend({node: node, to: 'root'}); - assertDomEqual(c.getContent(), '
'); - }); - - test('append node to another node', function() { - var c = canvas.create('
'); - var node = canvasNode.create({tag: 'header', klass: 'some-class'}); - var to = c.findNodes('div')[0]; - c.nodeAppend({node: node, to: to}); - assertDomEqual(c.getContent(), '
'); - }); - - test('insert node after another node', function() { - var c = canvas.create('
'); - var node = canvasNode.create({tag: 'header', klass: 'some-class'}); - var after = c.findNodes('div')[0]; - c.nodeInsertAfter({node: node, after: after}); - assertDomEqual(c.getContent(), '
'); - }); - - test('wrap text in node', function() { - var c = canvas.create('
Header 1
'); - var header = c.findNodes({tag: 'header'})[0]; - var wrapper = canvasNode.create({tag: 'aside'}); - c.nodeWrap({inside: header, _with: wrapper, offsetStart: 1, offsetEnd: 6}); - assertDomEqual(c.getContent(), '
Header 1
'); - }); - - test('wrap text in node - text not a first node', function() { - var c = canvas.create('
Alice has a cat
'); - var header = c.findNodes({tag: 'header'})[0]; - var wrapper = canvasNode.create({tag: 'aside'}); - c.nodeWrap({inside: header, _with: wrapper, offsetStart: 1, offsetEnd: 4, textNodeIdx: 2}); - assertDomEqual(c.getContent(), '
Alice has a cat
'); - }); - - test('wrap text with nodes inside', function() { - var c = canvas.create('
Alice has a small cat
'); - var header = c.findNodes({tag: 'header'})[0]; - var wrapper = canvasNode.create({tag: 'aside'}); - c.nodeWrap({inside: header, _with: wrapper, offsetStart: 6, offsetEnd: 4, textNodeIdx: [0,2]}); - assertDomEqual(c.getContent(), '
Alice has a small cat
'); - }); - - test('unwrap text', function() { - var c = canvas.create('
Alice has a cat
'); - var span = c.findNodes({tag:'span'})[0]; - c.nodeUnwrap({node: span}); - assertDomEqual(c.getContent(), '
Alice has a cat
'); - }); - - test('unwrap text - first text node', function() { - var c = canvas.create('
Alice has a cat
'); - var span = c.findNodes({tag:'span'})[0]; - c.nodeUnwrap({node: span}); - assertDomEqual(c.getContent(), '
Alice has a cat
'); - }); - - test('unwrap text - only text node', function() { - var c = canvas.create('
Alice
'); - var span = c.findNodes({tag:'span'})[0]; - c.nodeUnwrap({node: span}); - assertDomEqual(c.getContent(), '
Alice
'); - }); - - - test('unwrap text - non text neighbours', function() { - var c = canvas.create('
a
Alice
b
'); - var span = c.findNodes({tag:'span'})[0]; - c.nodeUnwrap({node: span}); - assertDomEqual(c.getContent(), '
a
Alice
b
'); - }); - - test('split node', function() { - var c = canvas.create('
Header 1
'); - var header = c.findNodes({tag: 'header'})[0]; - var newNode = c.nodeSplit({node: header, offset: 4}); - assertDomEqual(c.getContent(), utils.cleanUp('\ -
\ -
Head
\ -
er 1
\ -
')); - assert.ok(newNode.isSame(c.findNodes({tag: 'header'})[1])); - }); - - test('split root node', function() { - var c = canvas.create('
cat
'); - var header = c.findNodes({tag: 'header'})[0]; - var newNode = c.nodeSplit({node: header, offset: 1}); - assertDomEqual(c.getContent(), utils.cleanUp('\ -
c
\ -
at
')); - assert.ok(newNode.isSame(c.findNodes({tag: 'header'})[1])); - }); - - test('split node with subnodes', function() { - var c = canvas.create(utils.cleanUp('\ -
\ -
Fancy and niceheader 1
\ -
')); - var header = c.findNodes({tag: 'header'})[0]; - var newNode = c.nodeSplit({node: header, offset: 5}); - assertDomEqual(c.getContent(), utils.cleanUp('\ -
\ -
Fancy
\ -
and niceheader 1
\ -
')); - }); - - test('remove node', function() { - var c = canvas.create('
some text
'); - var span = c.findNodes({tag: 'span'})[0]; - c.nodeRemove({node: span}); - assertDomEqual(c.getContent(), '
'); - }); - }); - - - suite('Lists', function() { - test('create from existing nodes', function() { - var c = canvas.create(utils.cleanUp('\ -
\ -
alice
\ - has\ -
a
\ -
cat
\ -
or not
\ -
' - )); - - var div_alice = c.findNodes({tag: 'div'})[0]; - var div_cat = c.findNodes({tag:'div'})[2]; - - c.listCreate({start: div_alice, end: div_cat}); - - assertDomEqual(c.getContent(), utils.cleanUp('\ -
\ -
\ -
alice
\ -
has
\ -
a
\ -
cat
\ -
\ -
or not
\ -
')); - }); - - test('create from existing nodes - start/end order doesn\'t matter', function() { - var html = utils.cleanUp('\ -
alice
\ -
cat
'); - var expected = utils.cleanUp('\ -
\ -
alice
\ -
cat
\ -
'); - - var c = canvas.create(html); - var div_alice = c.findNodes({tag: 'div'})[0]; - var div_cat = c.findNodes({tag:'div'})[1]; - c.listCreate({start: div_cat, end: div_alice}); - assertDomEqual(c.getContent(), expected); - - c = canvas.create(html); - div_alice = c.findNodes({tag: 'div'})[0]; - div_cat = c.findNodes({tag:'div'})[1]; - c.listCreate({start: div_alice, end: div_cat}); - assertDomEqual(c.getContent(), expected); - }); - - test('remove', function() { - var c = canvas.create(utils.cleanUp('\ -
\ -
\ -
alice
\ -
cat
\ -
\ -
')); - var item = c.findNodes({klass: 'item'})[1]; - c.listRemove({pointer: item}); - assertDomEqual(c.getContent(), utils.cleanUp('\ -
\ -
alice
\ -
cat
\ -
')); - }); - - test('checking if node is inside a list', function() { - var c = canvas.create(utils.cleanUp('\ -
\ -
\ -
alice \ -
cat
\ -
\ -
')); - assert.ok(c.nodeInsideList({node: c.findNodes({klass: 'item'})[1]}), 'item is inside a list'); - assert.ok(c.nodeInsideList({node: c.findNodes({tag: 'span'})[0]}), 'things nested in item are inside a list'); - }); - - test('moving items to nested list', function() { - var listHTML = utils.cleanUp('\ -
\ -
alice
\ -
cat
\ -
dog
\ -
bee
\ -
'); - var c = canvas.create(listHTML); - var items = c.findNodes({klass: 'item'}); - var cat_item = items[1]; - var dog_item = items[2]; - - c.listCreate({start: cat_item, end: dog_item}); - - assertDomEqual(c.getContent(), utils.cleanUp('\ -
\ -
alice
\ -
\ -
\ -
cat
\ -
dog
\ -
\ -
\ -
bee
\ -
' - )); - }); - - test('removing nested list', function() { - var nestedList = utils.cleanUp('\ -
\ -
alice
\ -
\ -
\ -
cat
\ -
dog
\ -
\ -
\ -
bee
\ -
'); - - var c = canvas.create(nestedList); - var dog_item = c.findNodes('[wlxml-class=list-items] [wlxml-class=list-items] > div')[1]; - assert.equal(dog_item.getContent(), 'dog'); - - c.listRemove({pointer: dog_item}); - - assertDomEqual(c.getContent(), utils.cleanUp('\ -
\ -
alice
\ -
cat
\ -
dog
\ -
bee
\ -
')); - - - }); - - test('removing list containing nested list', function() { - var nestedList = utils.cleanUp('\ -
\ -
\ -
alice
\ -
\ -
\ -
cat
\ -
dog
\ -
\ -
\ -
bee
\ -
\ -
'); - - var c = canvas.create(nestedList); - var alice_item = c.findNodes('[wlxml-class=list-items] > div')[0]; - assert.equal(alice_item.getContent(), 'alice'); - - c.listRemove({pointer: alice_item}); - - assertDomEqual(c.getContent(), utils.cleanUp('\ -
\ -
alice
\ -
cat
\ -
dog
\ -
bee
\ -
')); - - - }); - }); -}); \ No newline at end of file diff --git a/modules/documentCanvas/tests/canvasNode.test.js b/modules/documentCanvas/tests/canvasNode.test.js deleted file mode 100644 index 2452fff..0000000 --- a/modules/documentCanvas/tests/canvasNode.test.js +++ /dev/null @@ -1,108 +0,0 @@ -define([ -'libs/jquery-1.9.1.min', -'libs/chai', -'./utils.js', -'modules/documentCanvas/canvasNode' -], function($, chai, utils, canvasNode) { - -'use strict'; - -var assert = chai.assert; - -var assertDomEqual = function(lhs, rhs) { - lhs.attr('id', ''); - rhs.attr('id', ''); - return assert.ok(lhs[0].isEqualNode(rhs[0]), 'nodes are equal'); - -}; - -suite('Create canvas node', function() { - test('from description', function() { - var node = canvasNode.create({ - tag: 'header', - klass: 'uri', - content: 'some text content', - meta: {uri: 'some uri'} - }); - assert.equal(node.getTag(), 'header'); - assert.equal(node.getClass(), 'uri'); - assert.equal(node.getContent(), 'some text content'); - assert.equal(node.getMetaAttr('uri'), 'some uri'); - assertDomEqual($('
some text content
'), node.dom); - }); - - test('from dom object', function() { - var node = canvasNode.create($('
')); - assert.equal(node.getTag(), 'header'); - assert.equal(node.getClass(), 'some-class'); - assert.equal(node.getMetaAttr('uri'), 'some uri'); - //assertDomEqual($('
'), node.dom); - }); -}); - -suite('class information', function() { - test('class of', function() { - var node = canvasNode.create({tag: 'header', klass: 'a-b-c'}); - assert.ok(node.isOfClass('a'), 'first level'); - assert.ok(node.isOfClass('a-b'), 'second level'); - assert.ok(node.isOfClass('a-b-c'), 'third level'); - assert.notOk(node.isOfClass('b-c')); - - var node2 = canvasNode.create({tag: 'header'}); - assert.notOk(node2.isOfClass('b')); - - }); - -}); - -suite('comparing nodes', function() { - test('isSame', function() { - var html = '
'; - var dom1 = $(html); - var dom2 = $(html); - assert.ok(canvasNode.create(dom1).isSame(canvasNode.create(dom1))); - assert.notOk(canvasNode.create(dom1).isSame(canvasNode.create(dom2))); - }); -}); - -suite('meta attributes', function() { - test('get list of node\'s meta attributes', function() { - var node = canvasNode.create({tag: 'span', klass: 'uri', meta: {uri:'http://some.uri.com'}}); - var attrs = node.getMetaAttrs(); - var expected = [{name: 'uri', value: 'http://some.uri.com'}]; - - assert.deepEqual(attrs.sort(), expected.sort()); - }); - - test('get list of node\'s meta attributes when attributes not set', function() { - var node = canvasNode.create({tag: 'span', klass: 'uri'}); - var attrs = node.getMetaAttrs(); - var expected = [{name: 'uri', value: ''}]; - assert.deepEqual(attrs.sort(), expected.sort()); - }); - - test('set meta attribute', function() { - var node = canvasNode.create({tag: 'tag', klass: 'uri', meta: {'uri': 'some uri'}}); - node.setMetaAttr('uri', 'some uri 2'); - assert.equal(node.dom.attr('wlxml-meta-uri'), 'some uri 2'); - }); - - test('changing class changes meta attributes', function() { - var node = canvasNode.create({tag: 'span', klass: 'uri', meta: {uri: 'http://some.uri.com'}}); - - assert.equal(node.getMetaAttr('uri'), 'http://some.uri.com'); - - node.setClass('author'); - - assert.equal(node.getMetaAttr('uri'), undefined); - }); - - test('changing class to another with the same attribute keeps the value', function() { - var node = canvasNode.create({tag: 'span', klass: 'uri', meta: {uri: 'http://some.uri.com'}}); - assert.equal(node.getMetaAttr('uri'), 'http://some.uri.com'); - node.setClass('uri-subclass'); - assert.equal(node.getMetaAttr('uri'), 'http://some.uri.com'); - }); -}); - -}); \ No newline at end of file diff --git a/modules/documentCanvas/tests/utils.js b/modules/documentCanvas/tests/utils.js deleted file mode 100644 index fcb9853..0000000 --- a/modules/documentCanvas/tests/utils.js +++ /dev/null @@ -1,49 +0,0 @@ -define(['libs/jquery-1.9.1.min', 'libs/chai'], function($, chai) { - return { - cleanUp: function(xml) { - var rmws = function(node) { - if(node.nodeType === 3) { - node.data = $.trim(node.data); - } - else { - $(node).contents().each(function() { - rmws(this); - }); - } - }; - - xml = $($.trim(xml)); - xml.each(function() { - rmws(this); - }); - - /*var toret = xml - .replace(/(<.*>)\s*(<.*>)/gm, '$1$2') - .replace(/(<\/.*>)\s*(<\/.*>)/gm, '$1$2') - .replace(/(<\/.*>)\s*(<.*>)/gm, '$1$2'); - return $.trim(toret);*/ - return $('
').append(xml).html(); - }, - - assertDomEqual: function(lhs, rhs) { - lhs = lhs.clone(); - var rhsArr = $.parseHTML(rhs); - if(rhsArr.length === 1) { - rhs = $(rhsArr[0]); - } else { - rhs = $('
'); - $.each(rhsArr, function(i, el) { - rhs.append(el); - }); - } - if(lhs.length > 1) { - lhs = $('
').append(lhs); - } - lhs.attr('id', ''); - rhs.attr('id', ''); - lhs.find('*').each(function() {$(this).attr('id', '');}); - rhs.find('*').each(function() {$(this).attr('id', '');}); - return chai.assert.ok(lhs[0].isEqualNode(rhs[0]), 'nodes are equal'); - } - }; -}); \ No newline at end of file diff --git a/modules/documentCanvas/tests/utils.test.js b/modules/documentCanvas/tests/utils.test.js deleted file mode 100644 index c82e6fb..0000000 --- a/modules/documentCanvas/tests/utils.test.js +++ /dev/null @@ -1,29 +0,0 @@ -define(['libs/chai', './utils.js'], function(chai, utils) { - -'use strict'; -var assert = chai.assert; - -test('open+open', function() { - assert.equal(utils.cleanUp('
\n
'), '
'); -}); - -test('close+close', function() { - assert.equal(utils.cleanUp('
\n
'), '
'); -}); - -test('close+open', function() { - assert.equal(utils.cleanUp('
\n
'), '
'); -}); - -test('bug', function() { - var txt = '\ -
\ -
Head
\ -
er 1
\ -
'; - var txt2 = '
Head
er 1
'; - assert.equal(utils.cleanUp(txt), txt2); -}); - - -}); \ No newline at end of file diff --git a/modules/documentCanvas/transformations.js b/modules/documentCanvas/transformations.js deleted file mode 100644 index effe253..0000000 --- a/modules/documentCanvas/transformations.js +++ /dev/null @@ -1,107 +0,0 @@ -define(['libs/jquery-1.9.1.min'], function($) { - - 'use strict'; - - var transformations = {}; - - transformations.fromXML = { - getHTMLTree: function(xml) { - var inner = $(xml).clone(); - var toret = $('
'); - toret.append(inner); - - var toBlock = ['div', 'section', 'header']; - var toInline = ['aside', 'span']; - - var transform = function(tags, replacingTagName) { - tags.forEach(function(tagName) { - tagName = tagName.toLowerCase(); - console.log('running ' + tagName); - toret.find(tagName).replaceWith(function() { - var currentTag = $(this); - if(currentTag.attr('wlxml-tag')) - return; - var toret = $('<' + replacingTagName + '>').attr('wlxml-tag', tagName); - toret.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);})); - for(var i = 0; i < this.attributes.length; i++) { - var attr = this.attributes.item(i); - var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value; - toret.attr('wlxml-' + attr.name, value); - } - toret.append(currentTag.contents()); - return toret; - }); - }); - }; - - transform(toBlock, 'div'); - transform(toInline, 'span'); - - toret.find(":not(iframe)").addBack().contents().filter(function() { - return this.nodeType == 3;} ).each(function() { - var n = $(this); - var hasText = /\S/g.test(n.text()); - if(!hasText) { - n.remove(); - return; - } - var startSpace = /\s/g.test(n.text().substr(0,1)); - var endSpace = /\s/g.test(n.text().substr(-1)) && n.text().length > 1; - var trimmed = $.trim(n.text()); - n.get(0).data = (startSpace ? ' ' : '') + trimmed + (endSpace ? ' ' : ''); - }); - - return toret.children(); - }, - getMetaData: function(xml) { - var toret = {}; - $(xml).find('metadata').children().each(function() { - var node = $(this); - toret[this.nodeName.split(':')[1].toLowerCase()] = node.text(); - }); - return toret; - }, - getDocumentDescription: function(xml) { - return { - HTMLTree: this.getHTMLTree(xml), - metadata: this.getMetaData(xml) - }; - } - }; - - transformations.toXML = { - getXML: function(body) { - - var inner = body.clone(); - var toret = $('
'); - toret.append(inner); - - toret.find('div, span').replaceWith(function() { - var div = $(this); - var tagName = div.attr('wlxml-tag'); - var toret = $('<'+tagName+'>'); - - for(var i = 0; i < this.attributes.length; i++) { - var attr = this.attributes.item(i); - var split = attr.name.split('-'); - console.log(split); - if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag')) - continue; - var wlxmlName = split.splice(1).join('-'); - var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value; - console.log(name + ': ' + value); - if(value.length && value.length > 0) - toret.attr(wlxmlName, value); - } - - toret.append(div.contents()); - return toret; - }); - - return vkbeautify.xml(toret.html()); - } - }; - - return transformations; - -}); \ No newline at end of file