From: Aleksander Łukasz Date: Sun, 8 Dec 2013 00:01:45 +0000 (+0100) Subject: linting, cleanup, removing unused code X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/21cd91531dd9c6e88d00da03f6b2f3ad88fb0da0 linting, cleanup, removing unused code --- diff --git a/src/editor/modules/data/data.js b/src/editor/modules/data/data.js index f3494a0..e90a1b6 100644 --- a/src/editor/modules/data/data.js +++ b/src/editor/modules/data/data.js @@ -21,7 +21,7 @@ return function(sandbox) { function readCookie(name) { - /* global escape, unescape */ + /* global escape, unescape, document */ var nameEQ = escape(name) + '='; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { diff --git a/src/editor/modules/documentCanvas/canvas/canvas.test.js b/src/editor/modules/documentCanvas/canvas/canvas.test.js index 2ee505d..3b33751 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.test.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.test.js @@ -19,9 +19,10 @@ var getCanvasFromXML = function(xml) { var getDocumentFromXML = function(xml) { return wlxml.WLXMLDocumentFromXML(xml); -} +}; var wait = function(callback, timeout) { + /* globals window */ return window.setTimeout(callback, timeout || 0.5); }; @@ -56,7 +57,7 @@ describe('Handling changes to the document', function() { c = canvas.fromXMLDocument(doc); var header = doc.root.replaceWith({tagName: 'header'}); - expect(c.doc().data('wlxmlNode').sameNode(header)).to.be.true; + expect(c.doc().data('wlxmlNode').sameNode(header)).to.equal(true); }); }); @@ -90,7 +91,7 @@ describe('Listening to document changes', function() { }); describe('Cursor', function() { - + /* globals Node */ var getSelection; var findTextNode = function(inside, text) { @@ -104,6 +105,7 @@ describe('Cursor', function() { }; beforeEach(function() { + /* globals window */ getSelection = sinon.stub(window, 'getSelection'); }); diff --git a/src/editor/modules/documentCanvas/canvas/utils.js b/src/editor/modules/documentCanvas/canvas/utils.js index e1f0a55..ae20a09 100644 --- a/src/editor/modules/documentCanvas/canvas/utils.js +++ b/src/editor/modules/documentCanvas/canvas/utils.js @@ -3,7 +3,7 @@ define([ ], function($) { 'use strict'; - +/* globals Node */ var nearestInDocumentOrder = function(selector, direction, element) { var parents = $(element).parents(), diff --git a/src/editor/modules/documentCanvas/canvas/wlxmlManagers.js b/src/editor/modules/documentCanvas/canvas/wlxmlManagers.js index d566686..c643a88 100644 --- a/src/editor/modules/documentCanvas/canvas/wlxmlManagers.js +++ b/src/editor/modules/documentCanvas/canvas/wlxmlManagers.js @@ -16,7 +16,7 @@ var DocumentElementWrapper = function(documentElement) { this.clearWidgets = function() { documentElement.dom().children('.canvas-widgets').empty(); - } + }; this.setDisplayStyle = function(displayStyle) { documentElement.dom().css('display', displayStyle || ''); @@ -33,25 +33,28 @@ var DocumentElementWrapper = function(documentElement) { this.toggle = function(toggle) { documentElement._container().toggle(toggle); - } + }; var eventBus = documentElement.canvas ? documentElement.canvas.eventBus : {trigger: function() {}}; this.trigger = function() { eventBus.trigger.apply(eventBus, arguments); - } + }; -} +}; var getDisplayStyle = function(tag, klass) { - if(tag === 'metadata') + if(tag === 'metadata') { return 'none'; - if(tag === 'span') + } + if(tag === 'span') { return 'inline'; - if(klass === 'item') + } + if(klass === 'item') { return null; + } return 'block'; -} +}; var GenericManager = function(wlxmlElement) { this.el = wlxmlElement; @@ -69,21 +72,23 @@ $.extend(GenericManager.prototype, { this.el.toggle(toggle); } -}) +}); var managers = { _m: {}, set: function(tag, klass, manager) { - if(!this._m[tag]) + if(!this._m[tag]) { this._m[tag] = {}; + } this._m[tag][klass] = manager; }, get: function(tag,klass) { - if(this._m[tag] && this._m[tag][klass]) + if(this._m[tag] && this._m[tag][klass]) { return this._m[tag][klass]; + } return GenericManager; } -} +}; var FootnoteManager = function(wlxmlElement) { this.el = wlxmlElement; @@ -114,10 +119,11 @@ $.extend(FootnoteManager.prototype, { this.el.setDisplayStyle(toggle ? 'block' : 'inline'); this.el.toggle(toggle); - if(!options.silent) + if(!options.silent) { this.el.trigger('elementToggled', toggle, this.el.documentElement); + } } -}) +}); managers.set('aside', 'footnote', FootnoteManager); diff --git a/src/editor/modules/documentToolbar/documentToolbar.js b/src/editor/modules/documentToolbar/documentToolbar.js index 75fcc72..d903483 100644 --- a/src/editor/modules/documentToolbar/documentToolbar.js +++ b/src/editor/modules/documentToolbar/documentToolbar.js @@ -39,7 +39,7 @@ return function(sandbox) { if(command === 'undo' || command === 'redo') { params.callback = function(disable) { btn.attr('disabled', !disable); - } + }; } sandbox.publish('command', command, params); diff --git a/src/editor/modules/metadataEditor/metadataEditor.js b/src/editor/modules/metadataEditor/metadataEditor.js index 0c8a816..623dee9 100644 --- a/src/editor/modules/metadataEditor/metadataEditor.js +++ b/src/editor/modules/metadataEditor/metadataEditor.js @@ -29,6 +29,7 @@ return function(sandbox) { }); this.metaTable.on('keydown', '[contenteditable]', function(e) { + /* globals document */ if(e.which === 13) { if($(document.activeElement).hasClass('rng-module-metadataEditor-metaItemKey')) { metaTable.find('.rng-module-metadataEditor-metaItemValue').focus(); diff --git a/src/editor/modules/nodePane/metaWidget/metaWidget.js b/src/editor/modules/nodePane/metaWidget/metaWidget.js index 7561acd..6f9b34c 100644 --- a/src/editor/modules/nodePane/metaWidget/metaWidget.js +++ b/src/editor/modules/nodePane/metaWidget/metaWidget.js @@ -12,14 +12,14 @@ var templates = { }; var getAttrElement = function(attrName, attr) { - var toret = $('
'); + var toret = $('
'); toret.append(templates.string({name: attrName, value: attr.value})); return toret; }; var MetaWidget = Backbone.View.extend({ events: { - 'change [metaField-name]': 'onMetaFieldChange' + 'change [metaField-name]': 'onMetaFieldChange' }, initialize: function() { var view = this; diff --git a/src/editor/modules/nodePane/metaWidget/metaWidget.test.js b/src/editor/modules/nodePane/metaWidget/metaWidget.test.js index f546248..bc1a579 100644 --- a/src/editor/modules/nodePane/metaWidget/metaWidget.test.js +++ b/src/editor/modules/nodePane/metaWidget/metaWidget.test.js @@ -1,10 +1,12 @@ define([ +'libs/jquery', 'libs/chai', 'libs/sinon', 'modules/nodePane/metaWidget/metaWidget' -], function(chai, sinon, metaWidget) { +], function($, chai, sinon, metaWidget) { 'use strict'; +/* globals describe, it */ var assert = chai.assert; diff --git a/src/editor/modules/rng/rng.js b/src/editor/modules/rng/rng.js index b6b103f..bc4ea7f 100644 --- a/src/editor/modules/rng/rng.js +++ b/src/editor/modules/rng/rng.js @@ -226,6 +226,7 @@ return function(sandbox) { sandbox.getModule('data').restoreVersion(event); }, displayVersion: function(event) { + /* globals window */ window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId()); } }; diff --git a/src/editor/plugins/core.js b/src/editor/plugins/core.js deleted file mode 100644 index c4991f8..0000000 --- a/src/editor/plugins/core.js +++ /dev/null @@ -1,86 +0,0 @@ -define([ - -], function() { - -'use strict'; - -var breakContentTransformation = { - impl: function(args) { - var node = this.context, - newNodes, emptyNode, emptyText; - newNodes = node.transform('smartxml.split', {offset: args.offset}); - [newNodes.first, newNodes.second].some(function(newNode) { - if(!(newNode.contents().length)) { - newNode.transform('smartxml.append', {text: ''}); - return true; // break - } - }); - return _.extend(newNodes, {emptyText: emptyText}); - }, - isAllowed: function() { - - } -}; - - -var breakContentAction = function(document, context) { //@ editor.getDocument(); editor.getContext('...') - var textNode = context.cursor.currentNode; - if(textNode) { - var result, goto; - - result = textNode.transform('core.break-content', {offset: context.cursor.offset}); - - if(result.emptyText) { - goto = result.createdEmpty; - gotoOptions = {}; - } else { - goto = result.second; - gotoOptions = {caretTo: 'start'}; - } - - context.setCurrentElement(goto, gotoOptions); - } -} -breakContentAction.isAllowed = function(document, context) { - /* globals Node */ - var node = context.cursor.currentNode; - return node.nodeType === Node.TEXT_NODE; -} - -return { - keyHandlers: [ - {key: 'ENTER', target: 'main-document-area', handler: function(editor) { - var action = editor.getAction('core.break-document-content'); - if(action.isAllowed()) { - action.execute(); - } - }}, - {key: 'ENTER', target: 'main-document-area', actionHandler: 'core.break-document-content'} - ], - - documentActions: [ - { - name: 'core.break-document-content', - context: 'main-document-area', - label: 'break here' - icon: 'core:some-name', - action: breakContentAction - } - ], - - // zapisywanie dokumentu: - - documentTransformations: [ - {name: 'core.break-content', textNode: true, t: breakContentTransformation}, - - // transformacja z poziomu smartxml - {name: 'core.wrap-with', textNode: true, t: wrapWith} - - // list plugin: - {name: 'list.remove-list', elementNode: 'list', t: null} - // hipotetyczna akcja na itemie listy - {name: 'list.extract', elementNode: 'item', requiresParent: 'list', requiresInParents: '?'} - ] -}; - -}); \ No newline at end of file diff --git a/src/smartxml/core.js b/src/smartxml/core.js index dd472ce..40fd9b8 100644 --- a/src/smartxml/core.js +++ b/src/smartxml/core.js @@ -1,10 +1,11 @@ -define([ - -], function() { +define(function(require) { 'use strict'; /* globals Node */ -var TEXT_NODE = Node.TEXT_NODE; + +var _ = require('libs/underscore'), + TEXT_NODE = Node.TEXT_NODE; + var INSERTION = function(implementation) { var toret = function(node) { @@ -186,7 +187,6 @@ var elementNodeTransformations = { var textNodeTransformations = { setText: function(text) { - //console.log('smartxml: ' + text); this.nativeNode.data = text; this.triggerTextChangeEvent(); }, @@ -347,7 +347,7 @@ var documentTransformations = { insertion.ofNode.triggerChangeEvent('nodeAdded'); return insertion.ofNode; } -} +}; return { document: { diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index bf64c27..9c233bd 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -34,13 +34,6 @@ $.extend(DocumentNode.prototype, { clone: function() { var clone = this._$.clone(true, true); - // clone.find('*').addBack().each(function() { - // var n = $(this); - // if(n.data('canvasElement')) { - // n.data('canvasElement', $.extend(true, {}, n.data('canvasElement'))); - // n.data('canvasElement').$element = n.data('canvasElement').$element.clone(true, true); - // } - // }); return this.document.createDocumentNode(clone[0]); }, @@ -234,13 +227,11 @@ var parseXML = function(xml) { var registerTransformation = function(desc, name, target) { var Transformation = transformations.createContextTransformation(desc, name); - //+ to sie powinna nazywac registerTransformationFromDesc or sth - //+ ew. spr czy nie override (tylko jesli powyzej sa prototypy to trudno do nich dojsc) target[name] = function() { var instance = this, args = Array.prototype.slice.call(arguments, 0); return instance.transform(Transformation, args); - } + }; }; var registerMethod = function(methodName, method, target) { @@ -350,7 +341,6 @@ $.extend(Document.prototype, Backbone.Events, { }, trigger: function() { - //console.log('trigger: ' + arguments[0] + (arguments[1] ? ', ' + arguments[1].type : '')); Backbone.Events.trigger.apply(this, arguments); }, @@ -389,9 +379,7 @@ $.extend(Document.prototype, Backbone.Events, { }, registerExtension: function(extension) { - //debugger; - var doc = this, - existingPropertyNames = _.values(this); + var doc = this; ['document', 'documentNode', 'elementNode', 'textNode'].forEach(function(dstName) { var dstExtension = extension[dstName]; @@ -418,11 +406,7 @@ $.extend(Document.prototype, Backbone.Events, { }, transform: function(Transformation, args) { - //console.log('transform'); var toret, transformation; - //debugger; - - // ref: odrebnie przygotowanie transformacji, odrebnie jej wykonanie (to pierwsze to analog transform z node) if(typeof Transformation === 'function') { transformation = new Transformation(this, this, args); @@ -436,7 +420,6 @@ $.extend(Document.prototype, Backbone.Events, { this.undoStack.push(transformation); } this._transformationLevel--; - //console.log('clearing redo stack'); if(!this._undoInProgress) { this.redoStack = []; } diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 0bf5dd5..4049dff 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -1,8 +1,9 @@ define([ 'libs/chai', 'libs/sinon', + 'libs/underscore', './smartxml.js' -], function(chai, sinon, smartxml) { +], function(chai, sinon, _, smartxml) { 'use strict'; /*jshint expr:true */ @@ -939,7 +940,7 @@ describe('smartxml', function() { expect(elementNode.textTestTransformation).to.be.undefined; expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true; - expect(textNode.elementTestTransfomation).to.be.undefined; + expect(textNode.elementTestTransfomation).to.be.undefined; }); it('allows text/element node methods and transformations to access node and transormations on document node', function() { @@ -998,8 +999,7 @@ describe('smartxml', function() { it('smoke tests', function() { var doc = getDocumentFromXML('
Alice
'), - textNode = doc.root.contents()[0], - result; + textNode = doc.root.contents()[0]; expect(doc.undoStack).to.have.length(0); @@ -1011,7 +1011,6 @@ describe('smartxml', function() { expect(doc.undoStack).to.have.length(0, '2'); expect(doc.toXML()).to.equal('
Alice
'); - debugger; doc.redo(); expect(doc.undoStack).to.have.length(1, '3'); expect(doc.toXML()).to.equal('
Alice
'); @@ -1028,10 +1027,8 @@ describe('smartxml', function() { it('smoke tests 2', function() { var doc = getDocumentFromXML('
Alice
'), textNode = doc.root.contents()[0], - path = textNode.getPath(), - result; + path = textNode.getPath(); - debugger; textNode.setText('Alice '); textNode.setText('Alice h'); textNode.setText('Alice ha'); @@ -1147,7 +1144,7 @@ describe('smartxml', function() { expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'empty redoStack so redo was noop'); doc.undo(); expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undoing additional transformation'); - doc.redo() + doc.redo(); expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'redoing additional transformation'); }); }); diff --git a/src/smartxml/transformation_api_exp.js b/src/smartxml/transformation_api_exp.js deleted file mode 100644 index 7a03763..0000000 --- a/src/smartxml/transformation_api_exp.js +++ /dev/null @@ -1,288 +0,0 @@ -//use case: edytor robi split, jesli split był na koncu (czyli druga czesc jest pusta) -// chce tam dodac wezel tekstowy - -// flow: plugin_key_handler(enter, main-canvas-area) -> plugin_document_action('break-content') -// --w srodku--> refactoring tego co teraz w keyboard: - -//1. jedna transformacja z warunkiem (Zarejestrowana przez plugin) - - - -var breakContentTransformation = { - impl: function(args) { - var node = this.context; - emptyText = false, - newNodes, - emptyNode; - - newNodes = node.transform('core.split', {offset: args.offset}); - - if(args.offset === 0) - emptyNode = newNodes.first; - else if(args.offset === node.getText().length); //@ nie ma atEnd :( - emptyNode = newNodes.second; - - if(emptyNode) { - emptyText = emptyNode.transform('core.append', {text: ''}); - } - - return _.extend(newNodes, {emptyText: emptyText}); - } -}; - - -var breakContentAction = function(document, context) { - var textNode = context.currentTextNode; - if(textNode) { - var result, goto; - - result = textNode.transform('core.break-content', {offset: context.offset}); - - if(result.emptyText) { - goto = result.createdEmpty; - gotoOptions = {}; - } else { - goto = result.second; - gotoOptions = {caretTo: 'start'}; - } - - context.setCurrentElement(goto, gotoOptions); - } -} - -var toret = { - keyHandlers: [ - {key: 'ENTER', target: 'main-document-area', handler: function(editor) { - editor.getAction('core.break-document-content').execute(); - }}, - ], - - actions: [ - {name: 'core.break-document-content', context: 'main-document-area', action: breakContentAction} - ], - - // zapisywanie dokumentu: - - contextTransformations: [ - {name: 'core.break-content', textNode: true, t: breakContentTransformation}, - - // list plugin: - {name: 'list.remove-list', elementNode: 'list', t: null} - // hipotetyczna akcja na itemie listy - {name: 'list.extract', elementNode: 'item', requiresParent: 'list', requiresInParents: '?'} - ], - -} - - -/// STARE - -// 1. detach via totalny fallback -var DetachNodeTransformation = function(args) { - this.node = args.node; - this.document = this.node.document; -}; -$.extend(DetachNodeTransformation.prototype, { - run: function() { - this.oldRoot = this.node.document.root.clone(); - this.path = this.node.getPath(); - this.node.detach(); // @TS - - }, - undo: function() { - this.document.root.replaceWith(this.oldRoot); // this.getDocument? - this.node = this.document.getNodeByPath(this.path); - } -}); -transformations['detach'] = DetachNodeTransformation; - -//2. detach via wskazanie changeroot - -var Detach2NodeTransformation = function(args) { - this.nodePath = args.node.getPath(); - this.document = args.node.document; -}; -$.extend(Detach2NodeTransformation.prototype, { - run: function() { - var node = this.document.getNodeByPath(this.nodePath), - root = node.parent() ? node.parent() : this.document.root; - - this.rootPath = root.getPath(); - this.oldRoot = (root).clone(); - node.detach(); - }, - undo: function() { - this.document.getNodeByPath(this.rootPath).replaceWith(this.oldRoot); - } -}); -//transformations['detach2'] = Detach2NodeTransformation; - -//2a. generyczna transformacja - -var createTransformation = function(desc) { - - var NodeTransformation = function(args) { - this.nodePath = args.node.getPath(); - this.document = args.node.document; - this.args = args; - }; - $.extend(NodeTransformation.prototype, { - run: function() { - var node = this.document.getNodeByPath(this.nodePath), - root; - - if(desc.getRoot) { - root = desc.getRoot(node); - } else { - root = this.document.root; - } - - this.rootPath = root.getPath(); - this.oldRoot = (root).clone(); - desc.impl.call(node, this.args); - }, - undo: function() { - this.document.getNodeByPath(this.rootPath).replaceWith(this.oldRoot); - } - }); - - return NodeTransformation; -} - - - -var contextTransformations = {}; -contextTransformations['setText'] = createContextTransformation({ - impl: function(args) { - this.setText(args.text); - }, - getChangeRoot: function() { - return this.context; - } -}); - -contextTransformations['setAttr'] = createContextTransformation({ - impl: function(args) { - this.setAttr(args.name, args.value); - }, - getChangeRoot: function() { - return this.context; - } -}); - -contextTransformations['split'] = createContextTransformation({ - impl: function(args) { - return this.split({offset: args.offset}); - }//, - // getChangeRoot: function() { - // return this.context.parent().parent(); - // } -}); - - -contextTransformations['before'] = createContextTransformation({ - getChangeRoot: function() { - return this.context.parent(); - }, - impl: function(args) { - this.before(args.node) - }, - -}); - -contextTransformations['before'] = createContextTransformation({ - impl: function(args) { - this.before(args.node) - }, - undo: function() { - this.context.detach(); - } -}); - - - -transformations['detach2'] = createTransformation({ - // impl: function() { - // //this.setAttr('class', 'cite'); // - // }, - impl: ElementNode.prototype.detach, - getRoot: function(node) { - return node.parent(); - } - -}); - -transformations['setText-old'] = createTransformation({ - impl: function(args) { - this.setText(args.text) - }, - getRoot: function(node) { - return node; - } - -}); - -transformations['setClass-old'] = createTransformation({ - impl: function(args) { - this.setClass(args.klass); - }, - getRoot: function(node) { - return node; - } -}) - -//3. detach z pełnym własnym redo - -var Detach3NodeTransformation = function(args) { - this.node = args.node; - this.document = this.node.document; -}; -$.extend(Detach3NodeTransformation.prototype, { - run: function() { - //this.index = this.node.getIndex(); - //this.parent = this.node.parent(); - - this.path = this.node.getPath(); - if(this.node.isSurroundedByTextElements()) { - this.prevText = this.node.prev().getText(); - this.nextText = this.node.next().getText(); - this.merge = true; - } else { - this.prevText = this.nextText = null; - this.merge = false; - } - - this.node.detach(); - }, - undo: function() { - var parent = this.document.getNodeByPath(this.path.slice(0,-1)), - idx = _.last(this.path); - var inserted = parent.insertAtIndex(this.node, idx); - if(this.merge) { - if(inserted.next()) { - inserted.before({text: this.prevText}); - inserted.next().setText(this.nextText); - } else { - inserted.prev().setText(this.prevText); - inserted.after({text: this.nextText}); - } - } - } -}); -transformations['detach3'] = Detach3NodeTransformation; - - -var registerTransformationsFromObject = function(object) { - _.pairs(object).filter(function(pair) { - var property = pair[1]; - return typeof property === 'function' && property._isTransformation; - }) - .forEach(function(pair) { - var name = pair[0], - method = pair[1]; - object.registerTransformation(name, createContextTransformation(method)); - }); -}; -registerTransformationsFromObject(ElementNode.prototype); -registerTransformationsFromObject(TextNode.prototype); -registerTransformationsFromObject(Document.prototype); \ No newline at end of file diff --git a/src/smartxml/transformations.js b/src/smartxml/transformations.js index 9aea150..25c0ec1 100644 --- a/src/smartxml/transformations.js +++ b/src/smartxml/transformations.js @@ -5,12 +5,12 @@ define(function(require) { var _ = require('libs/underscore'), toret = {}; -var getTransDesc = function(desc, name) { +var getTransDesc = function(desc) { if(typeof desc === 'function') { desc = {impl: desc}; } if(!desc.impl) { - throw new Error('Got transformation description without implementation.') + throw new Error('Got transformation description without implementation.'); } return desc; }; @@ -72,7 +72,7 @@ toret.createGenericTransformation = function(desc, name) { return value; } } - }); + }); } }); } @@ -94,7 +94,6 @@ toret.createGenericTransformation = function(desc, name) { this.snapshot = changeRoot.clone(); this.changeRootPath = changeRoot.getPath(); } - //var toret = desc.impl.call(this.context, this.args); // a argumenty do metody? var argsToPass = desc.undo ? [this].concat(this.args) : this.args; var toret = desc.impl.apply(this.context, argsToPass); this.hasRun = true; @@ -111,12 +110,8 @@ toret.createGenericTransformation = function(desc, name) { return GenericTransformation; }; -// var T = createGenericTransformation({impl: function() {}}); -// var t = T(doc, {a:1,b:2,c3:3}); - toret.createContextTransformation = function(desc, name) { - // mozna sie pozbyc przez przeniesienie object/context na koniec argumentow konstruktora generic transformation var GenericTransformation = toret.createGenericTransformation(desc, name); var ContextTransformation = function(document, object, args) { @@ -124,78 +119,24 @@ toret.createContextTransformation = function(desc, name) { if(document === object) { this.context = document; - } else { + } else { var contextPath = object.getPath(), transformation = this; Object.defineProperty(this, 'context', { get: function() { - // todo: to jakos inaczej, bo np. this.context w undo transformacji before to juz nie ten sam obiekt - // moze transformacja powinna zwracac zmodyfikowana sciezke do obiektu po dzialaniu run? - if(transformation.hasRun) { - //console.log('returning via path'); return transformation.document.getNodeByPath(contextPath); } else { - //console.log('returning original arg'); return object; - } } }); } - } + }; ContextTransformation.prototype = Object.create(GenericTransformation.prototype); return ContextTransformation; -} -// var T = createContextTransformation({impl: function() {}}); -// var t = T(doc, node, {a:1,b:2,c3:3}); -/// - - - -toret.TransformationStorage = function() { - this._transformations = {}; }; -_.extend(toret.TransformationStorage.prototype, { - - register: function(Transformation) { - var list = (this._transformations[Transformation.prototype.name] = this._transformations[Transformation.prototype.name] || []); - list.push(Transformation); - }, - - get: function(name) { - var transformations = this._transformations[name]; - if(!transformations) { - throw new Error('Transformation "' + name + '" not found!'); - } - // na razie zwraca pierwsza - return transformations[0]; - } -}); - - - -// var registerTransformationFromMethod = (object, methodName, desc) { -// if(!object[methodName]) { -// throw new Exeption('Cannot register transformation from unknown method ' + methodName + ' on ' + object); -// } -// desc.impl = object[name]; -// Transformation = createContextTransformation(desc); -// object.prototype.registerContextTransformation(name, createContextTransformation(method)); -// }; - - -// registerTransformationFromMethod(ElementNode, 'setAttr', { -// impl: function(args) { -// this.setAttr(args.name, args.value); -// }, -// getChangeRoot: function() { -// return this.context; -// } - -// }); - return toret; }); \ No newline at end of file diff --git a/src/wlxml/extensions/list/list.js b/src/wlxml/extensions/list/list.js index 02fe08d..8cfecc2 100644 --- a/src/wlxml/extensions/list/list.js +++ b/src/wlxml/extensions/list/list.js @@ -1,8 +1,9 @@ -define(function() { +define(function(require) { 'use strict'; -var extension = {document: {transformations: {}}, wlxmlClass: {list: {methods: {}}}}; +var _ = require('libs/underscore'), + extension = {document: {transformations: {}}, wlxmlClass: {list: {methods: {}}}}; extension.wlxmlClass.list.methods = { @@ -19,18 +20,19 @@ extension.wlxmlClass.list.methods = { getItem: function(index) { return this.contents('.item')[index]; } -} +}; extension.documentMethods = { areItemsOfSameList: function(params) { return params.node1.parent().sameNode(params.node2.parent()) && params.node2.parent().is('list'); } -} +}; extension.document.transformations.createList = { - impl: function(params) { + impl: function(params) { + /* globals Node */ var parent = params.node1.parent(), parentContents = parent.contents(), nodeIndexes = [params.node1.getIndex(), params.node2.getIndex()].sort(), @@ -41,9 +43,9 @@ extension.document.transformations.createList = { for(i = nodeIndexes[0]; i <= nodeIndexes[1]; i++) { node = parentContents[i]; if(node.nodeType === Node.TEXT_NODE) { - node = node.wrapWith({tagName: 'div', attrs: {'class': 'item'}}); //t + node = node.wrapWith({tagName: 'div', attrs: {'class': 'item'}}); } else { - node.setClass('item'); //t + node.setClass('item'); } nodesToWrap.push(node); } @@ -51,17 +53,17 @@ extension.document.transformations.createList = { var toInsert; if(parent.is('list') && parent.object.itemIndex(nodesToWrap[0]) > 0) { // object api // var prevItem = parent.object.getItem(parent.object.itemIndex(nodesToWrap[0])-1); // object api - // prevItem.append(listNode); //t + // prevItem.append(listNode); toInsert = listNode.wrapWith({tagName: 'div', attrs: {'class': 'item'}}); } else { - //nodesToWrap[0].before(listNode); //t + //nodesToWrap[0].before(listNode); toInsert = listNode; - } + } params.node1.before(toInsert); nodesToWrap.forEach(function(node) { - listNode.append(node); //t + listNode.append(node); }); }, getChangeRoot: function() { @@ -80,9 +82,9 @@ extension.document.transformations.extractItems = { precedingItems = [], extractedItems = [], succeedingItems = [], - items = list.contents(), // lub list.object.items() - listIsNested = list.parent().is('item'), - i; + items = list.contents(), + listIsNested = list.parent().is('item'); + items.forEach(function(item, idx) { if(idx < indexes[0]) { @@ -100,26 +102,28 @@ extension.document.transformations.extractItems = { if(succeedingItems.length === 0) { var reference_orig = reference; extractedItems.forEach(function(item) { - reference.after(item); //t + reference.after(item); reference = item; if(!listIsNested) { - item.setClass(null); //t + item.setClass(null); } }); - if(precedingItems.length === 0) - reference_orig.detach(); //t + if(precedingItems.length === 0) { + reference_orig.detach(); + } } else if(precedingItems.length === 0) { extractedItems.forEach(function(item) { - reference.before(item); //t + reference.before(item); if(!listIsNested) { - item.setClass(null); //t + item.setClass(null); } }); } else { extractedItems.forEach(function(item) { - reference.after(item); //t - if(!listIsNested) - item.setClass(null); //t + reference.after(item); + if(!listIsNested) { + item.setClass(null); + } reference = item; }); var secondList = params.item1.document.createDocumentNode({tagName: 'div', attrs: {'class':'list'}}), diff --git a/src/wlxml/extensions/list/list.test.js b/src/wlxml/extensions/list/list.test.js index faa85da..fd6089e 100644 --- a/src/wlxml/extensions/list/list.test.js +++ b/src/wlxml/extensions/list/list.test.js @@ -2,6 +2,9 @@ define(function(require) { 'use strict'; +/* jshint multistr:true */ +/* globals describe, it */ + var chai = require('libs/chai'), wlxml = require('wlxml/wlxml'), expect = chai.expect, @@ -16,6 +19,7 @@ var getDocumentFromXML = function(xml, options) { }; var removeEmptyTextNodes = function(xml) { + /* globals Node */ xml = $($.trim(xml)); xml.find(':not(iframe)') .addBack() @@ -233,9 +237,8 @@ describe('Lists extension', function() { doc.extractItems({item1: nestedListItem, item2: nestedListItem}); //@@ name! - var section = doc.root, - list = section.contents()[0], - item1 = list.contents()[0], + list = doc.root.contents()[0]; + var item1 = list.contents()[0], item2 = list.contents()[1], // item3 = list.contents()[2], item4 = list.contents()[3], // @@ -281,10 +284,8 @@ describe('Lists extension', function() { nestedListItem2 = nestedList.contents()[2]; doc.extractItems({item1: nestedListItem1, item2: nestedListItem2}); - - var section = doc.root, - list = section.contents()[0], - item1 = list.contents()[0], + list = doc.root.contents()[0]; + var item1 = list.contents()[0], item2 = list.contents()[1], item3 = list.contents()[2], item4 = list.contents()[3], @@ -323,9 +324,8 @@ describe('Lists extension', function() { doc.extractItems({item1: nestedListItem1, item2: nestedListItem2}); - var section = doc.root, - list = section.contents()[0], - item1 = list.contents()[0], + list = doc.root.contents()[0]; + var item1 = list.contents()[0], item2 = list.contents()[1], item3 = list.contents()[2], item4 = list.contents()[3], @@ -364,9 +364,8 @@ describe('Lists extension', function() { doc.extractItems({item1: nestedListItem1, item2: nestedListItem2}); - var section = doc.root, - list = section.contents()[0], - item1 = list.contents()[0], + list = doc.root.contents()[0]; + var item1 = list.contents()[0], item2 = list.contents()[1], item3 = list.contents()[2], item4 = list.contents()[3]; diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index 016f793..b3783af 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -7,6 +7,8 @@ define([ 'use strict'; +/* globals Node */ + // utils var isMetaAttribute = function(attrName) { @@ -29,7 +31,7 @@ var installObject = function(instance, klass) { _.keys(methods).concat(_.keys(transformations)).forEach(function(key) { instance.object[key] = _.bind(instance.object[key], instance); }); -} +}; var WLXMLElementNode = function(nativeNode, document) { smartxml.ElementNode.call(this, nativeNode, document); @@ -42,7 +44,6 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, { return this.getAttr('class') || ''; }, setClass: function(klass) { - var methods, object; if(klass !== this.klass) { installObject(this, klass); return this.setAttr('class', klass); @@ -141,7 +142,7 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, { var WLXMLDocumentNode = function() { smartxml.DocumentNode.apply(this, arguments); -} +}; WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype); var WLXMLDocument = function(xml, options) { @@ -239,7 +240,7 @@ $.extend(WLXMLDocument.prototype, { } //} } - + /* globals document */ el.replaceWith(document.createTextNode(text.transformed)); }); this.trigger('contentSet'); @@ -250,7 +251,7 @@ $.extend(WLXMLDocument.prototype, { thisClassTransformations[Transformation.prototype.name] = function(args) { var nodeInstance = this; return nodeInstance.transform(Transformation, args); - } + }; }, registerClassMethod: function(methodName, method, className) { @@ -277,7 +278,7 @@ $.extend(WLXMLDocument.prototype, { var name = pair[0], desc = pair[1]; doc.registerClassTransformation(transformations.createContextTransformation(desc, name), className); - }); + }); }); } diff --git a/src/wlxml/wlxml.test.js b/src/wlxml/wlxml.test.js index 3d1df03..9f846f6 100644 --- a/src/wlxml/wlxml.test.js +++ b/src/wlxml/wlxml.test.js @@ -6,7 +6,7 @@ define([ 'use strict'; /* jshint expr:true */ -/* global it, describe */ +/* global it, describe, beforeEach */ var expect = chai.expect; @@ -103,6 +103,8 @@ describe('WLXMLDocument', function() { }); describe('White space handling', function() { + /* globals Node */ + it('ignores white space surrounding block elements', function() { var node = nodeFromXML('
'), contents = node.contents();