Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added ability to replace elements and generate elements from objects

  • Loading branch information...
commit 44e6e94ed15972330faaf5ec94548f179cf1b935 1 parent 481e712
@vgrichina authored
Showing with 77 additions and 2 deletions.
  1. +31 −2 main.js
  2. +46 −0 test.js
View
33 main.js
@@ -68,6 +68,10 @@ function createProxy(converted, attrGetter, elemGetter, attrSetter, elemSetter)
return converted[name];
}
+ if (name == "$") {
+ return undefined;
+ }
+
if (name[0] == "$") {
var attrName = name.slice(1);
return attrGetter.call(target, attrName);
@@ -150,7 +154,7 @@ function convertElement(elem) {
},
function(name, value) {
// Child elements
- if (typeof value == "string") {
+ if (value != null && value.constructor == String) {
var matchingElements = this[name];
if (matchingElements.length > 0) {
// Set text of existing elements
@@ -172,7 +176,32 @@ function convertElement(elem) {
}
}
- // TODO: Add new elements
+ if (value == null) {
+ return;
+ }
+
+ function addChildElement(obj) {
+ if (obj.$) {
+ converted.$.addChild(obj.$);
+ converted.push(convertElement(obj.$));
+ } else {
+ // Create new element
+ var child = convertElement(elem.node(name));
+ converted.push(child);
+ // Create child elements
+ Object.keys(obj).forEach(function(it) {
+ child[it] = obj[it];
+ });
+ }
+ }
+
+ if (!value.$ && value.constructor == Array) {
+ value.forEach(function(it) {
+ addChildElement(it);
+ });
+ } else {
+ addChildElement(value);
+ }
}
}
);
View
46 test.js
@@ -20,6 +20,7 @@ describe("easy", function() {
it("should save original DOM element in '$' property", function() {
var xml = easy.parse(sampleXml);
assert.ok(xml.$);
+ assert.equal(xml.$.name(), "books");
assert.equal(xml.book[0].language[0].$.text(), "English");
});
@@ -68,6 +69,30 @@ describe("easy", function() {
'</book></books>');
});
+ it("should allow to replace elements with other elements", function() {
+ var xml = easy.parse("<books/>");
+ xml.book = easy.parse(sampleXml).book;
+ assert.equal(xml.$.toString(),
+ '<books><book name="Lord of the Rings">' +
+ '<author name="J. R. R. Tolkien"/>' +
+ '<language>English</language>' +
+ '</book></books>');
+ });
+
+ it("should allow to create new elements from objects", function() {
+ var xml = easy.parse("<books/>");
+ xml.book = [{
+ $name: "Lord of the Rings",
+ author: {$name:"J. R. R. Tolkien"},
+ language: "English"
+ }];
+ assert.equal(xml.$.toString(),
+ '<books><book name="Lord of the Rings">' +
+ '<author name="J. R. R. Tolkien"/>' +
+ '<language>English</language>' +
+ '</book></books>');
+ });
+
describe("shorthands", function() {
it("should allow access without indices", function() {
var xml = easy.parse(sampleXml);
@@ -114,5 +139,26 @@ describe("easy", function() {
'<genre>Fantasy</genre>' +
'</book></books>');
});
+
+ it("should allow to replace elements with other elements", function() {
+ var xml = easy.parse(sampleXml);
+ xml.book.language = easy.parse("<language>Russian</language>");
+ assert.equal(xml.$.toString(),
+ '<books><book name="Lord of the Rings">' +
+ '<author name="J. R. R. Tolkien"/>' +
+ '<language>Russian</language>' +
+ '</book></books>');
+ });
+
+ it("should allow to create new elements from objects", function() {
+ var xml = easy.parse('<books><book name="Lord of the Rings" /></books>');
+ xml.book.author = {$name:"J. R. R. Tolkien"};
+ xml.book.language = "English";
+ assert.equal(xml.$.toString(),
+ '<books><book name="Lord of the Rings">' +
+ '<author name="J. R. R. Tolkien"/>' +
+ '<language>English</language>' +
+ '</book></books>');
+ });
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.