From: Aleksander Ɓukasz Date: Wed, 15 Jan 2014 15:04:14 +0000 (+0100) Subject: wlxml: metadata wip - as extension X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/266a16ec421df5bc8f2565884dd410918f1f1df4 wlxml: metadata wip - as extension --- diff --git a/src/wlxml/extensions/metadata/metadata.js b/src/wlxml/extensions/metadata/metadata.js new file mode 100644 index 0000000..4136673 --- /dev/null +++ b/src/wlxml/extensions/metadata/metadata.js @@ -0,0 +1,41 @@ +define(function(require) { + +'use strict'; + +var _ = require('libs/underscore'), + metadataKey = 'wlxml.metadata'; + + +var methods = { + getMetadata: function() { + return this.getData(metadataKey) || []; + } +}; + +var transformations = { + addMetadataRow: function(row) { + this.setMetadataRow(null, row); + }, + + setMetadataRow: function(index, row) { + var metadata = this.getData(metadataKey) || []; + if(typeof index !== 'number' || index > metadata.length - 1) { + metadata.push(row); + index = metadata.length - 1; + } else { + metadata[index] = _.extend(metadata[index], row); + } + this.setData(metadataKey, metadata); + this.triggerChangeEvent('metadataChange', {index: index}); + } +}; + +return { + elementNode: { + methods: methods, + transformations: transformations, + } +}; + +}); + diff --git a/src/wlxml/extensions/metadata/metadata.test.js b/src/wlxml/extensions/metadata/metadata.test.js new file mode 100644 index 0000000..00e5c17 --- /dev/null +++ b/src/wlxml/extensions/metadata/metadata.test.js @@ -0,0 +1,51 @@ +define(function(require) { + +'use strict'; + +/* jshint multistr:true */ +/* globals describe, it */ + +var chai = require('libs/chai'), + wlxml = require('wlxml/wlxml'), + expect = chai.expect, + $ = require('libs/jquery'); + +var getDocumentFromXML = function(xml, options) { + return wlxml.WLXMLDocumentFromXML(xml, options || {}); +}; + + +describe.only('Metadata API', function() { + it('allows to set metadata on an element node', function() { + var doc = getDocumentFromXML('
'); + expect(doc.root.getMetadata()).to.deep.equal([]); + doc.root.addMetadataRow({key: 'key', value: 'value'}); + expect(doc.root.getMetadata()).to.deep.equal([{key: 'key', value: 'value'}]); + }); + + it('reads node\'s metadata from its metadata child node', function() { + var doc = getDocumentFromXML('
value
'); + expect(doc.root.getMetadata()).to.deep.equal([{key: 'key', value: 'value'}]); + }); + + it('serializes node\'s metadata to its metadata child node', function() { + var doc = getDocumentFromXML('
'); + + doc.root.addMetadataRow({key: 'key', value: 'value'}); + + var metadataNodes = $(doc.toXML()).children('metadata'), + keyNodes = metadataNodes.children(); + + expect(metadataNodes).to.have.length(1); + expect(keyNodes).to.have.length(1); + expect(keyNodes[0].tagName.toLowerCase()).to.equal('dc:key'); + expect($(keyNodes[0]).text()).to.equal('value'); + }); + it('doesnt show metadata node on nodes contents', function() { + var doc = getDocumentFromXML('
value
'); + expect(doc.root.contents()).to.have.length(0); + }); +}); + + +}); \ No newline at end of file diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index c3ed84f..6c6c40c 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -2,13 +2,13 @@ define([ 'libs/jquery', 'libs/underscore', 'smartxml/smartxml', - 'smartxml/transformations' -], function($, _, smartxml, transformations) { + 'smartxml/transformations', + 'wlxml/extensions/metadata/metadata' +], function($, _, smartxml, transformations, metadataExtension) { 'use strict'; /* globals Node */ -var metadataKey = 'wlxml.metadata'; var AttributesList = function() {}; @@ -79,26 +79,6 @@ $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, { return attrName !== 'class' &&_.contains(_.keys(this.getMetaAttributes()), attrName); }, - getMetadata: function() { - return this.getData(metadataKey) || []; - }, - - addMetadataRow: function(row) { - this.setMetadataRow(null, row); - }, - - setMetadataRow: function(index, row) { - var metadata = this.getData(metadataKey) || []; - if(typeof index !== 'number' || index > metadata.length - 1) { - metadata.push(row); - index = metadata.length - 1; - } else { - metadata[index] = _.extend(metadata[index], row); - } - this.setData(metadataKey, metadata); - this.triggerChangeEvent('metadataChange', {index: index}); - }, - _getXMLDOMToDump: function() { var DOM = this._$.clone(true, true), doc = this.document; @@ -182,7 +162,7 @@ WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype); var WLXMLDocument = function(xml, options) { this.classMethods = {}; this.classTransformations = {}; - smartxml.Document.call(this, xml); + smartxml.Document.call(this, xml, [metadataExtension]); this.options = options; }; diff --git a/src/wlxml/wlxml.test.js b/src/wlxml/wlxml.test.js index 7eb8efb..18a7332 100644 --- a/src/wlxml/wlxml.test.js +++ b/src/wlxml/wlxml.test.js @@ -293,39 +293,6 @@ describe('WLXMLDocument', function() { expect(testClassNode.object.testTransformation2().sameNode(testClassNode)).to.equal(true, '1'); }); }); - - describe.only('Metadata API', function() { - it('allows to set metadata on an element node', function() { - var doc = getDocumentFromXML('
'); - expect(doc.root.getMetadata()).to.deep.equal([]); - doc.root.addMetadataRow({key: 'key', value: 'value'}); - expect(doc.root.getMetadata()).to.deep.equal([{key: 'key', value: 'value'}]); - }); - - it('reads node\'s metadata from its metadata child node', function() { - var doc = getDocumentFromXML('
value
'); - expect(doc.root.getMetadata()).to.deep.equal([{key: 'key', value: 'value'}]); - }); - - it('serializes node\'s metadata to its metadata child node', function() { - var doc = getDocumentFromXML('
'); - - doc.root.addMetadataRow({key: 'key', value: 'value'}); - - var metadataNodes = $(doc.toXML()).children('metadata'), - keyNodes = metadataNodes.children(); - - expect(metadataNodes).to.have.length(1); - expect(keyNodes).to.have.length(1); - expect(keyNodes[0].tagName.toLowerCase()).to.equal('dc:key'); - expect($(keyNodes[0]).text()).to.equal('value'); - }); - it('doesnt show metadata node on nodes contents', function() { - var doc = getDocumentFromXML('
value
'); - expect(doc.root.contents()).to.have.length(0); - }); - }); - }); }); \ No newline at end of file