Skip to content

Commit

Permalink
fix broken jsapi clone method (svg#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjlockwood authored and GreLI committed Sep 15, 2018
1 parent dd2a21f commit 828a72a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
22 changes: 22 additions & 0 deletions lib/svgo/css-class-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ var CSSClassList = function(node) {
//this.classValue = null;
};

/**
* Performs a deep clone of this object.
*
* @param parentNode the parentNode to assign to the cloned result
*/
CSSClassList.prototype.clone = function(parentNode) {
var node = this;
var nodeData = {};

Object.keys(node).forEach(function(key) {
if (key !== 'parentNode') {
nodeData[key] = node[key];
}
});

// Deep-clone node data.
nodeData = JSON.parse(JSON.stringify(nodeData));

var clone = new CSSClassList(parentNode);
Object.assign(clone, nodeData);
return clone;
};

CSSClassList.prototype.hasClass = function() {
this.classAttr = { // empty class attr
Expand Down
22 changes: 22 additions & 0 deletions lib/svgo/css-style-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ var CSSStyleDeclaration = function(node) {
this.parseError = false;
};

/**
* Performs a deep clone of this object.
*
* @param parentNode the parentNode to assign to the cloned result
*/
CSSStyleDeclaration.prototype.clone = function(parentNode) {
var node = this;
var nodeData = {};

Object.keys(node).forEach(function(key) {
if (key !== 'parentNode') {
nodeData[key] = node[key];
}
});

// Deep-clone node data.
nodeData = JSON.parse(JSON.stringify(nodeData));

var clone = new CSSStyleDeclaration(parentNode);
Object.assign(clone, nodeData);
return clone;
};

CSSStyleDeclaration.prototype.hasStyle = function() {
this.addStyleHandler();
Expand Down
10 changes: 8 additions & 2 deletions lib/svgo/jsAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,25 @@ JSAPI.prototype.clone = function() {
var nodeData = {};

Object.keys(node).forEach(function(key) {
if (key !== 'content') {
if (key !== 'class' && key !== 'style' && key !== 'content') {
nodeData[key] = node[key];
}
});

// Deep-clone node data
// Deep-clone node data.
nodeData = JSON.parse(JSON.stringify(nodeData));

// parentNode gets set to a proper object by the parent clone,
// but it needs to be true/false now to do the right thing
// in the constructor.
var clonedNode = new JSAPI(nodeData, !!node.parentNode);

if (node.class) {
clonedNode.class = node.class.clone(clonedNode);
}
if (node.style) {
clonedNode.style = node.style.clone(clonedNode);
}
if (node.content) {
clonedNode.content = node.content.map(function(childNode) {
var clonedChild = childNode.clone();
Expand Down
29 changes: 29 additions & 0 deletions test/svg2js/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
var SHOULD = require('should'),
FS = require('fs'),
PATH = require('path'),
JSAPI = require(process.env.COVERAGE ?
'../../lib-cov/svgo/jsAPI' :
'../../lib/svgo/jsAPI'),
CSSClassList = require(process.env.COVERAGE ?
'../../lib-cov/svgo/css-class-list' :
'../../lib/svgo/css-class-list'),
CSSStyleDeclaration = require(process.env.COVERAGE ?
'../../lib-cov/svgo/css-style-declaration' :
'../../lib/svgo/css-style-declaration'),
SVG2JS = require(process.env.COVERAGE ?
'../../lib-cov/svgo/svg2js' :
'../../lib/svgo/svg2js');
Expand Down Expand Up @@ -179,6 +188,26 @@ describe('svg2js', function() {

describe('API', function() {

describe('clone()', function() {

it('svg should have property "clone"', function() {
return root.content[3].should.have.property('clone');
});

it('svg.clone() should be an instance of JSAPI', function() {
return root.content[3].clone().should.be.instanceOf(JSAPI);
});

it('root.content[3].content[0].clone() has a valid style property', function() {
return root.content[3].content[0].clone().style.should.be.instanceof(CSSStyleDeclaration);
});

it('root.content[3].content[2].clone() has a valid class property', function() {
return root.content[3].content[2].clone().class.should.be.instanceof(CSSClassList);
});

});

describe('isElem()', function() {

it('svg should have property "isElem"', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/svg2js/test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 828a72a

Please sign in to comment.