From 2e65999ad3791878b245ead3d0fa8442d74fd1b2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Wed, 14 May 2014 16:20:14 +0200 Subject: [PATCH] wlxml: comments --- src/wlxml/extensions/comments/comments.js | 36 +++++++++++++++++++++++ src/wlxml/wlxml.js | 7 +++-- 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/wlxml/extensions/comments/comments.js diff --git a/src/wlxml/extensions/comments/comments.js b/src/wlxml/extensions/comments/comments.js new file mode 100644 index 0000000..521bad8 --- /dev/null +++ b/src/wlxml/extensions/comments/comments.js @@ -0,0 +1,36 @@ +define(function() { + +'use strict'; + +var extension = {wlxmlClass: {comment: { + methods: { + describesParent: true, + getText: function() { + var text = ''; + this.contents() + .filter(function(node) { + /* globals Node */ + return node && node.nodeType === Node.TEXT_NODE; + }) + .forEach(function(node) { + text = text + node.getText(); + }); + return text; + }, + setText: function(text) { + var contents = this.contents(); + if(contents.length === 1 && contents[0].nodeType === Node.TEXT_NODE) { + contents[0].setText(text); + } else { + contents.forEach(function(node) { + node.detach(); + }); + this.append({text: text}); + } + } + } +}}}; + +return extension; + +}); \ No newline at end of file diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index bbd6233..e9e5d5b 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -3,8 +3,9 @@ define([ 'libs/underscore', 'smartxml/smartxml', 'smartxml/transformations', - 'wlxml/extensions/metadata/metadata' -], function($, _, smartxml, transformations, metadataExtension) { + 'wlxml/extensions/metadata/metadata', + 'wlxml/extensions/comments/comments' +], function($, _, smartxml, transformations, metadataExtension, commentExtension) { 'use strict'; @@ -213,7 +214,7 @@ $.extend(WLXMLTextNode.prototype, WLXMLDocumentNodeMethods); var WLXMLDocument = function(xml, options) { this.classMethods = {}; this.classTransformations = {}; - smartxml.Document.call(this, xml, [metadataExtension]); + smartxml.Document.call(this, xml, [metadataExtension, commentExtension]); this.options = options; }; -- 2.20.1