From 7234259fa025569d6a7682aa6c74fdcfb3ca7e17 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Fri, 21 Jun 2013 14:24:55 +0200 Subject: [PATCH] canvas api wip: getting previous node --- modules/documentCanvas/canvas.js | 15 +++++++++++++-- modules/documentCanvas/tests/canvas.test.js | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js index 92c02c6..eb0fd0b 100644 --- a/modules/documentCanvas/canvas.js +++ b/modules/documentCanvas/canvas.js @@ -36,6 +36,14 @@ Canvas.prototype.getNode = function(desc) { return toret; } +Canvas.prototype.getPreviousNode = function(options) { + var element = $(this.content.find('#' + options.node.id).get(0)); + var prev = element.prev() + if(prev.length === 0) + prev = element.parent(); + return new wlxmlNode.Node(prev); +} + Canvas.prototype._createNode = function(wlxmlTag, wlxmlClass) { var toBlock = ['div', 'document', 'section', 'header']; var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span'; @@ -49,8 +57,11 @@ Canvas.prototype._createNode = function(wlxmlTag, wlxmlClass) { Canvas.prototype.insertNode = function(options) { var element = $(this.content.find('#' + options.context.id).get(0)); - if(options.place == 'after') - element[options.place](this._createNode(options.tag, options.klass)); + if(options.place == 'after') { + var node = this._createNode(options.tag, options.klass); + element[options.place](node); + return node; + } else if(options.place == 'wrapText') { var elementContents = element.contents(); if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3) diff --git a/modules/documentCanvas/tests/canvas.test.js b/modules/documentCanvas/tests/canvas.test.js index 6ba3f42..57ad625 100644 --- a/modules/documentCanvas/tests/canvas.test.js +++ b/modules/documentCanvas/tests/canvas.test.js @@ -54,6 +54,20 @@ define([ assert.equal(header.tag, 'header'); assert.equal(header.klass, 'some-class'); }); + + test('getting previous node', function() { + var c = new canvas.Canvas('
Div 1
Div 2
'); + var secondDiv = c.getNode({tag: 'div'})[1]; + var firstDiv = c.getPreviousNode({node: secondDiv}); + assert.equal(firstDiv.klass, 'some-class'); + }) + + test('pervious node of node without "previous siblings" is its parent', function() { + var c = new canvas.Canvas('
Div 1
'); + var div = c.getNode({tag: 'div'})[0]; + var section = c.getPreviousNode({node: div}); + assert.equal(section.tag, 'section'); + }) test('inserting after', function() { var c = new canvas.Canvas('
Header 1
'); @@ -119,7 +133,7 @@ define([
a cat
\ \ '); - + }); }); -- 2.20.1