--- /dev/null
+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,
+ }
+};
+
+});
+
--- /dev/null
+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('<section></section>');
+ 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('<section><metadata><dc:key>value</dc:key></metadata></section>');
+ 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('<section></section>');
+
+ 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('<section><metadata><dc:key>value</dc:key></metadata></section>');
+ expect(doc.root.contents()).to.have.length(0);
+ });
+});
+
+
+});
\ No newline at end of file
'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() {};
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;
var WLXMLDocument = function(xml, options) {
this.classMethods = {};
this.classTransformations = {};
- smartxml.Document.call(this, xml);
+ smartxml.Document.call(this, xml, [metadataExtension]);
this.options = options;
};
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('<section></section>');
- 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('<section><metadata><dc:key>value</dc:key></metadata></section>');
- 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('<section></section>');
-
- 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('<section><metadata><dc:key>value</dc:key></metadata></section>');
- expect(doc.root.contents()).to.have.length(0);
- });
- });
-
});
});
\ No newline at end of file