Skip to content

Commit b248f8e

Browse files
committed
Add support for G tags
1 parent 4e1ea5f commit b248f8e

7 files changed

Lines changed: 34 additions & 9 deletions

File tree

dist/vivus.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ function Pathformer(element) {
4040
throw new Error('Pathformer [constructor]: "element" parameter is not related to an existing ID');
4141
}
4242
}
43-
if (element.constructor instanceof window.SVGElement || /^svg$/i.test(element.nodeName)) {
43+
if (element instanceof window.SVGElement ||
44+
element instanceof window.SVGGElement ||
45+
/^svg$/i.test(element.nodeName)) {
4446
this.el = element;
4547
} else {
4648
throw new Error('Pathformer [constructor]: "element" parameter must be a string or a SVGelement');
@@ -394,6 +396,7 @@ Vivus.prototype.setElement = function (element, options) {
394396
switch (element.constructor) {
395397
case window.SVGSVGElement:
396398
case window.SVGElement:
399+
case window.SVGGElement:
397400
this.el = element;
398401
this.isReady = true;
399402
break;

dist/vivus.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vivus",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"description": "JavaScript library to make drawing animation on SVG",
55
"main": "dist/vivus.js",
66
"scripts": {

src/pathformer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ function Pathformer(element) {
2929
throw new Error('Pathformer [constructor]: "element" parameter is not related to an existing ID');
3030
}
3131
}
32-
if (element.constructor instanceof window.SVGElement || /^svg$/i.test(element.nodeName)) {
32+
if (element instanceof window.SVGElement ||
33+
element instanceof window.SVGGElement ||
34+
/^svg$/i.test(element.nodeName)) {
3335
this.el = element;
3436
} else {
3537
throw new Error('Pathformer [constructor]: "element" parameter must be a string or a SVGelement');

src/vivus.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Vivus.prototype.setElement = function (element, options) {
126126
switch (element.constructor) {
127127
case window.SVGSVGElement:
128128
case window.SVGElement:
129+
case window.SVGGElement:
129130
this.el = element;
130131
this.isReady = true;
131132
break;

test/unit/pathformer.spec.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
describe('Pathformer', function () {
88

99
var svgTag,
10-
svgTagId = 'my-svg';
10+
svgTagId = 'my-svg',
11+
svgGroupTag,
12+
svgGroupTagId = 'my-svg-group';
1113

1214
beforeEach(function () {
1315
// Remove tag if existing
@@ -18,13 +20,17 @@ describe('Pathformer', function () {
1820

1921
// Create the SVG
2022
svgTag = document.createElementNS('http://www.w3.org/2000/svg','svg');
21-
svgTag.id = 'my-svg';
23+
svgTag.id = svgTagId;
2224
svgTag.innerHTML = '<circle fill="none" stroke="#f9f9f9" stroke-width="3" stroke-miterlimit="10" cx="100" cy="100" r="72.947"/>' +
2325
'<circle fill="none" stroke="#f9f9f9" stroke-width="3" stroke-miterlimit="10" cx="100" cy="100" r="39.74"/>' +
26+
'<g id="' + svgGroupTagId + '">' +
2427
'<line fill="none" stroke="#f9f9f9" stroke-width="3" stroke-miterlimit="10" x1="34.042" y1="131.189" x2="67.047" y2="77.781"/>' +
2528
'<line fill="none" stroke="#f9f9f9" stroke-width="3" stroke-miterlimit="10" x1="165.957" y1="68.809" x2="132.953" y2="122.219"/>' +
2629
'<line fill="none" stroke="#f9f9f9" stroke-width="3" stroke-miterlimit="10" x1="131.19" y1="165.957" x2="77.781" y2="132.953"/>' +
27-
'<line fill="none" stroke="#f9f9f9" stroke-width="3" stroke-miterlimit="10" x1="68.81" y1="34.042" x2="122.219" y2="67.046"/>';
30+
'<line fill="none" stroke="#f9f9f9" stroke-width="3" stroke-miterlimit="10" x1="68.81" y1="34.042" x2="122.219" y2="67.046"/>' +
31+
'</g>';
32+
33+
svgGroupTag = svgTag.querySelector('#'+svgGroupTagId);
2834

2935
// Insert it to the body
3036
document.body.appendChild(svgTag);
@@ -51,6 +57,12 @@ describe('Pathformer', function () {
5157
}).not.toThrow();
5258
});
5359

60+
it('should work with only the SVG group object', function () {
61+
expect(function () {
62+
new Pathformer(svgGroupTag);
63+
}).not.toThrow();
64+
});
65+
5466
it('should throw an error if the SVG ID given is invalid', function () {
5567
expect(function () {
5668
new Pathformer('my-unexisting-svg');

test/unit/vivus.spec.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ describe('Vivus', function () {
1212
objTag,
1313
wrapTag,
1414
svgTag,
15-
svgTagId = 'my-svg';
15+
svgTagId = 'my-svg',
16+
svgGroupTagId = 'my-svg-group';
1617

1718
// Mock ObjectElement and it's constructor via createElement
1819
ObjectElementMock = function () {
@@ -60,7 +61,7 @@ describe('Vivus', function () {
6061
svgTag.id = svgTagId;
6162
svgTag.innerHTML = '<circle fill="none" stroke="#f9f9f9" stroke-width="3" stroke-miterlimit="10" cx="100" cy="100" r="72.947"/>' +
6263
'<circle fill="none" stroke="#f9f9f9" stroke-width="3" stroke-miterlimit="10" cx="100" cy="100" r="39.74"/>' +
63-
'<g>' +
64+
'<g id="' + svgGroupTagId + '">' +
6465
'<line fill="none" stroke="#f9f9f9" stroke-width="3" stroke-miterlimit="10" x1="34.042" y1="131.189" x2="67.047" y2="77.781"/>' +
6566
'<line fill="none" stroke="#f9f9f9" stroke-width="3" stroke-miterlimit="10" x1="165.957" y1="68.809" x2="132.953" y2="122.219"/>' +
6667
'<line fill="none" stroke="#f9f9f9" stroke-width="3" stroke-miterlimit="10" x1="131.19" y1="165.957" x2="77.781" y2="132.953"/>' +
@@ -131,6 +132,12 @@ describe('Vivus', function () {
131132
}).not.toThrow();
132133
});
133134

135+
it('should work with the SVG group object', function () {
136+
expect(function () {
137+
new Vivus(svgGroupTagId);
138+
}).not.toThrow();
139+
});
140+
134141
it('should throw an error if the SVG ID given is invalid', function () {
135142
expect(function () {
136143
new Vivus('my-unexisting-svg');

0 commit comments

Comments
 (0)