+++ /dev/null
-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('<div wlxml-tag="div" wlxml-class="item" class="canvas-silent-item">');
- 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
+++ /dev/null
-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 = $('<div>').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
+++ /dev/null
-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
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,
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() {
+++ /dev/null
-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('<div wlxml-tag="section"><div wlxml-tag="p">a</div><div wlxml-tag="p">b</div></div>');
- 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('<div wlxml-tag="section"><div wlxml-tag="p">a</div></div>');
- 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(), '<div wlxml-tag="header" wlxml-class="some-class">');
- });
-
- test('append node to another node', function() {
- var c = canvas.create('<div wlxml-tag="section"></div>');
- var node = canvasNode.create({tag: 'header', klass: 'some-class'});
- var to = c.findNodes('div')[0];
- c.nodeAppend({node: node, to: to});
- assertDomEqual(c.getContent(), '<div wlxml-tag="section"><div wlxml-tag="header" wlxml-class="some-class"></div></div>');
- });
-
- test('insert node after another node', function() {
- var c = canvas.create('<div wlxml-tag="section"></div>');
- var node = canvasNode.create({tag: 'header', klass: 'some-class'});
- var after = c.findNodes('div')[0];
- c.nodeInsertAfter({node: node, after: after});
- assertDomEqual(c.getContent(), '<div wlxml-tag="section"></div><div wlxml-tag="header" wlxml-class="some-class"></div>');
- });
-
- test('wrap text in node', function() {
- var c = canvas.create('<div wlxml-tag="section"><div wlxml-tag="header">Header 1</div></div>');
- 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(), '<div wlxml-tag="section"><div wlxml-tag="header">H<span wlxml-tag="aside">eader</span> 1</div></div>');
- });
-
- test('wrap text in node - text not a first node', function() {
- var c = canvas.create('<div wlxml-tag="header">Alice <span wlxml-tag="span">has a</span> cat</div>');
- 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(), '<div wlxml-tag="header">Alice <span wlxml-tag="span">has a</span> <span wlxml-tag="aside">cat</span></div>');
- });
-
- test('wrap text with nodes inside', function() {
- var c = canvas.create('<div wlxml-tag="header">Alice has a <span wlxml-tag="span">small</span> cat</div>');
- 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(), '<div wlxml-tag="header">Alice <span wlxml-tag="aside">has a <span wlxml-tag="span">small</span> cat</span></div>');
- });
-
- test('unwrap text', function() {
- var c = canvas.create('<div wlxml-tag="div">Alice <span wlxml-tag="span">has</span> a cat</div>');
- var span = c.findNodes({tag:'span'})[0];
- c.nodeUnwrap({node: span});
- assertDomEqual(c.getContent(), '<div wlxml-tag="div">Alice has a cat</div>');
- });
-
- test('unwrap text - first text node', function() {
- var c = canvas.create('<div wlxml-tag="div"><span wlxml-tag="span">Alice</span> has a cat</div>');
- var span = c.findNodes({tag:'span'})[0];
- c.nodeUnwrap({node: span});
- assertDomEqual(c.getContent(), '<div wlxml-tag="div">Alice has a cat</div>');
- });
-
- test('unwrap text - only text node', function() {
- var c = canvas.create('<div wlxml-tag="div"><span wlxml-tag="span">Alice</span></div>');
- var span = c.findNodes({tag:'span'})[0];
- c.nodeUnwrap({node: span});
- assertDomEqual(c.getContent(), '<div wlxml-tag="div">Alice</div>');
- });
-
-
- test('unwrap text - non text neighbours', function() {
- var c = canvas.create('<div wlxml-tag="div"><div wlxml-tag"div">a</div><span wlxml-tag="span">Alice</span><div wlxml-tag"div">b</div></div>');
- var span = c.findNodes({tag:'span'})[0];
- c.nodeUnwrap({node: span});
- assertDomEqual(c.getContent(), '<div wlxml-tag="div"><div wlxml-tag"div">a</div>Alice<div wlxml-tag"div">b</div></div>');
- });
-
- test('split node', function() {
- var c = canvas.create('<div wlxml-tag="section"><div wlxml-tag="header">Header 1</div></div>');
- var header = c.findNodes({tag: 'header'})[0];
- var newNode = c.nodeSplit({node: header, offset: 4});
- assertDomEqual(c.getContent(), utils.cleanUp('\
- <div wlxml-tag="section">\
- <div wlxml-tag="header">Head</div>\
- <div wlxml-tag="header">er 1</div>\
- </div>'));
- assert.ok(newNode.isSame(c.findNodes({tag: 'header'})[1]));
- });
-
- test('split root node', function() {
- var c = canvas.create('<div wlxml-tag="header">cat</div>');
- var header = c.findNodes({tag: 'header'})[0];
- var newNode = c.nodeSplit({node: header, offset: 1});
- assertDomEqual(c.getContent(), utils.cleanUp('\
- <div wlxml-tag="header">c</div>\
- <div wlxml-tag="header">at</div>'));
- assert.ok(newNode.isSame(c.findNodes({tag: 'header'})[1]));
- });
-
- test('split node with subnodes', function() {
- var c = canvas.create(utils.cleanUp('\
- <div wlxml-tag="section">\
- <div wlxml-tag="header">Fancy and nice<span wlxml-tag="aside">header</span> 1</div>\
- </div>'));
- var header = c.findNodes({tag: 'header'})[0];
- var newNode = c.nodeSplit({node: header, offset: 5});
- assertDomEqual(c.getContent(), utils.cleanUp('\
- <div wlxml-tag="section">\
- <div wlxml-tag="header">Fancy</div>\
- <div wlxml-tag="header">and nice<span wlxml-tag="aside">header</span> 1</div>\
- </div>'));
- });
-
- test('remove node', function() {
- var c = canvas.create('<div wlxml-tag="section"><span wlxml-tag="span">some text</span></div>');
- var span = c.findNodes({tag: 'span'})[0];
- c.nodeRemove({node: span});
- assertDomEqual(c.getContent(), '<div wlxml-tag="section"></div>');
- });
- });
-
-
- suite('Lists', function() {
- test('create from existing nodes', function() {
- var c = canvas.create(utils.cleanUp('\
- <div wlxml-tag="section">\
- <div wlxml-tag="div">alice</div>\
- has\
- <div wlxml-tag="div">a</div>\
- <div wlxml-tag="div">cat</div>\
- <div wlxml-tag="div">or not</div>\
- </div>'
- ));
-
- 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('\
- <div wlxml-tag="section">\
- <div wlxml-tag="div" wlxml-class="list-items">\
- <div wlxml-tag="div" wlxml-class="item">alice</div>\
- <div wlxml-tag="div" wlxml-class="item">has</div>\
- <div wlxml-tag="div" wlxml-class="item">a</div>\
- <div wlxml-tag="div" wlxml-class="item">cat</div>\
- </div>\
- <div wlxml-tag="div">or not</div>\
- </div>'));
- });
-
- test('create from existing nodes - start/end order doesn\'t matter', function() {
- var html = utils.cleanUp('\
- <div wlxml-tag="div">alice</div>\
- <div wlxml-tag="div">cat</div>');
- var expected = utils.cleanUp('\
- <div wlxml-tag="div" wlxml-class="list-items">\
- <div wlxml-tag="div" wlxml-class="item">alice</div>\
- <div wlxml-tag="div" wlxml-class="item">cat</div>\
- </div>');
-
- 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('\
- <div wlxml-tag="section">\
- <div wlxml-tag="div" wlxml-class="list-items">\
- <div wlxml-tag="div" wlxml-class="item">alice</div>\
- <div wlxml-tag="div" wlxml-class="item">cat</div>\
- </div>\
- </div>'));
- var item = c.findNodes({klass: 'item'})[1];
- c.listRemove({pointer: item});
- assertDomEqual(c.getContent(), utils.cleanUp('\
- <div wlxml-tag="section">\
- <div wlxml-tag="div">alice</div>\
- <div wlxml-tag="div">cat</div>\
- </div>'));
- });
-
- test('checking if node is inside a list', function() {
- var c = canvas.create(utils.cleanUp('\
- <div wlxml-tag="section">\
- <div wlxml-tag="div" wlxml-class="list-items-enum">\
- <div wlxml-tag="div" wlxml-class="item">alice <span wlxml-tag="span"></span</div>\
- <div wlxml-tag="div" wlxml-class="item">cat</div>\
- </div>\
- </div>'));
- 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('\
- <div wlxml-tag="div" wlxml-class="list-items">\
- <div wlxml-tag="div" wlxml-class="item">alice</div>\
- <div wlxml-tag="div" wlxml-class="item">cat</div>\
- <div wlxml-tag="div" wlxml-class="item">dog</div>\
- <div wlxml-tag="div" wlxml-class="item">bee</div>\
- </div>');
- 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('\
- <div wlxml-tag="div" wlxml-class="list-items">\
- <div wlxml-tag="div" wlxml-class="item">alice</div>\
- <div wlxml-tag="div" wlxml-class="item" class="canvas-silent-item">\
- <div wlxml-tag="div" wlxml-class="list-items">\
- <div wlxml-tag="div" wlxml-class="item">cat</div>\
- <div wlxml-tag="div" wlxml-class="item">dog</div>\
- </div>\
- </div>\
- <div wlxml-tag="div" wlxml-class="item">bee</div>\
- </div>'
- ));
- });
-
- test('removing nested list', function() {
- var nestedList = utils.cleanUp('\
- <div wlxml-tag="div" wlxml-class="list-items">\
- <div wlxml-tag="div" wlxml-class="item">alice</div>\
- <div wlxml-tag="div" wlxml-class="item">\
- <div wlxml-tag="div" wlxml-class="list-items">\
- <div wlxml-tag="div" wlxml-class="item">cat</div>\
- <div wlxml-tag="div" wlxml-class="item">dog</div>\
- </div>\
- </div>\
- <div wlxml-tag="div" wlxml-class="item">bee</div>\
- </div>');
-
- 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('\
- <div wlxml-tag="div" wlxml-class="list-items">\
- <div wlxml-tag="div" wlxml-class="item">alice</div>\
- <div wlxml-tag="div" wlxml-class="item">cat</div>\
- <div wlxml-tag="div" wlxml-class="item">dog</div>\
- <div wlxml-tag="div" wlxml-class="item">bee</div>\
- </div>'));
-
-
- });
-
- test('removing list containing nested list', function() {
- var nestedList = utils.cleanUp('\
- <div wlxml-tag="section">\
- <div wlxml-tag="div" wlxml-class="list-items">\
- <div wlxml-tag="div" wlxml-class="item">alice</div>\
- <div wlxml-tag="div" wlxml-class="item">\
- <div wlxml-tag="div" wlxml-class="list-items">\
- <div wlxml-tag="div" wlxml-class="item">cat</div>\
- <div wlxml-tag="div" wlxml-class="item">dog</div>\
- </div>\
- </div>\
- <div wlxml-tag="div" wlxml-class="item">bee</div>\
- </div>\
- </div>');
-
- 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('\
- <div wlxml-tag="section">\
- <div wlxml-tag="div">alice</div>\
- <div wlxml-tag="div">cat</div>\
- <div wlxml-tag="div">dog</div>\
- <div wlxml-tag="div">bee</div>\
- </div>'));
-
-
- });
- });
-});
\ No newline at end of file
+++ /dev/null
-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($('<div wlxml-tag="header" wlxml-class="uri" wlxml-meta-uri="some uri">some text content</div>'), node.dom);
- });
-
- test('from dom object', function() {
- var node = canvasNode.create($('<div wlxml-tag="header" wlxml-class="some-class" id="1" wlxml-meta-uri="some uri">'));
- assert.equal(node.getTag(), 'header');
- assert.equal(node.getClass(), 'some-class');
- assert.equal(node.getMetaAttr('uri'), 'some uri');
- //assertDomEqual($('<div wlxml-tag="header" wlxml-class="some-class">'), 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 = '<div wlxml-class="some-class" wlxml-tag="some-tag">';
- 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
+++ /dev/null
-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 $('<div>').append(xml).html();
- },
-
- assertDomEqual: function(lhs, rhs) {
- lhs = lhs.clone();
- var rhsArr = $.parseHTML(rhs);
- if(rhsArr.length === 1) {
- rhs = $(rhsArr[0]);
- } else {
- rhs = $('<div>');
- $.each(rhsArr, function(i, el) {
- rhs.append(el);
- });
- }
- if(lhs.length > 1) {
- lhs = $('<div>').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
+++ /dev/null
-define(['libs/chai', './utils.js'], function(chai, utils) {
-
-'use strict';
-var assert = chai.assert;
-
-test('open+open', function() {
- assert.equal(utils.cleanUp('<div class="class"> \n <div class="class"></div></div>'), '<div class="class"><div class="class"></div></div>');
-});
-
-test('close+close', function() {
- assert.equal(utils.cleanUp('<div><div></div>\n </div>'), '<div><div></div></div>');
-});
-
-test('close+open', function() {
- assert.equal(utils.cleanUp('<div></div>\n <div class="class"></div>'), '<div></div><div class="class"></div>');
-});
-
-test('bug', function() {
- var txt = '\
- <section> \
- <header class="some.class">Head</header>\
- <header class="some.class">er 1</header>\
- </section>';
- var txt2 = '<section><header class="some.class">Head</header><header class="some.class">er 1</header></section>';
- assert.equal(utils.cleanUp(txt), txt2);
-});
-
-
-});
\ No newline at end of file
+++ /dev/null
-define(['libs/jquery-1.9.1.min'], function($) {
-
- 'use strict';
-
- var transformations = {};
-
- transformations.fromXML = {
- getHTMLTree: function(xml) {
- var inner = $(xml).clone();
- var toret = $('<div></div>');
- 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 = $('<div></div>');
- 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