'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);
+ var DOM = this._$.clone(true, true),
+ doc = this.document;
DOM.find('*').addBack().each(function() {
var el = $(this),
data = el.data();
- var txt;
+ var txt, documentNode, metaNode;
if(data[formatter_prefix+ 'orig_before']) {
txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
el.append(data[formatter_prefix+ 'orig_end']);
}
}
+
+
+ if(this.nodeType === Node.ELEMENT_NODE) {
+ documentNode = doc.createDocumentNode(this);
+ metaNode = $('<metadata>');
+ documentNode.getMetadata().forEach(function(row) {
+ metaNode.append('<dc:'+ row.key + '>' + row.value + '</dc:' + row.key + '>');
+ });
+ if(metaNode.children().length) {
+ $(this).prepend(metaNode);
+ }
+ }
+
});
+
+
return DOM;
}
});
WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
var WLXMLDocument = function(xml, options) {
- smartxml.Document.call(this, xml);
- this.options = options;
-
this.classMethods = {};
this.classTransformations = {};
+ smartxml.Document.call(this, xml);
+ this.options = options;
};
var formatter_prefix = '_wlxml_formatter_';
},
normalizeXML: function(nativeNode) {
+ var doc = this,
+ prefixLength = 'dc:'.length;
+
$(nativeNode).find(':not(iframe)').addBack().contents()
.filter(function() {return this.nodeType === Node.TEXT_NODE;})
.each(function() {
/* globals document */
el.replaceWith(document.createTextNode(text.transformed));
});
+
+ $(nativeNode).find('metadata').each(function() {
+ var metadataNode = $(this),
+ owner = doc.createDocumentNode(metadataNode.parent()[0]);
+
+ metadataNode.children().each(function() {
+ owner.addMetadataRow({key: (this.tagName).toLowerCase().substr(prefixLength), value: $(this).text()});
+ });
+ metadataNode.remove();
+ });
},
registerClassTransformation: function(Transformation, className) {
define([
+ 'libs/jquery',
'libs/chai',
'./wlxml.js'
-], function(chai, wlxml) {
+], function($, chai, wlxml) {
'use strict';
expect(testClassNode.object.testTransformation().sameNode(testClassNode)).to.equal(true, '1');
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);
+ });
});
});