• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

svg-sprite / svg-sprite / 675130967

pending completion
675130967

Pull #436

github

GitHub
Merge d9060bd83 into ebff837d7
Pull Request #436: Enforce Lint and assorted tweaks

313 of 433 branches covered (72.29%)

Branch coverage included in aggregate %.

1952 of 1952 new or added lines in 12 files covered. (100.0%)

2877 of 3251 relevant lines covered (88.5%)

75.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.85
/lib/svg-sprite/sprite.js
1
'use strict';
1✔
2

1✔
3
/**
1✔
4
 * svg-sprite is a Node.js module for creating SVG sprites
1✔
5
 *
1✔
6
 * @see https://github.com/svg-sprite/svg-sprite
1✔
7
 *
1✔
8
 * @author Joschi Kuphal <joschi@kuphal.net> (https://github.com/jkphl)
1✔
9
 * @copyright © 2018 Joschi Kuphal
1✔
10
 * @license MIT https://github.com/svg-sprite/svg-sprite/blob/master/LICENSE
1✔
11
 */
1✔
12

1✔
13
const _ = require('lodash');
1✔
14
const Vinyl = require('vinyl');
1✔
15

1✔
16
/**
1✔
17
 * SVGSprite
1✔
18
 *
1✔
19
 * @param {String} xmlDeclaration       XML declaration
1✔
20
 * @param {String} doctypeDeclaration   Doctype declaration
1✔
21
 * @param {Object} rootAttributes       Root attributes
1✔
22
 * @param {Boolean} addSVGNamespaces    Add default SVG namespaces
1✔
23
 * @param {Array} transform             List of post-processing transform callbacks
1✔
24
 */
1✔
25
function SVGSprite(xmlDeclaration, doctypeDeclaration, rootAttributes, addSVGNamespaces, transform) {
11✔
26
    this.xmlDeclaration = xmlDeclaration || '';
11!
27
    this.doctypeDeclaration = doctypeDeclaration || '';
11!
28
    this.rootAttributes = { ...rootAttributes };
11✔
29
    this.transform = transform;
11✔
30
    this.content = [];
11✔
31
    this._serialized = null;
11✔
32

11✔
33
    if (addSVGNamespaces) {
11✔
34
        this.rootAttributes.xmlns = this.DEFAULT_SVG_NAMESPACE;
11✔
35
        this.rootAttributes['xmlns:xlink'] = this.XLINK_NAMESPACE;
11✔
36
    }
11✔
37
}
11✔
38

1✔
39
/**
1✔
40
 * Prototype properties
1✔
41
 *
1✔
42
 * @type {Object}
1✔
43
 */
1✔
44
SVGSprite.prototype = {};
1✔
45

1✔
46
/**
1✔
47
 * Default SVG namespace
1✔
48
 *
1✔
49
 * @type {String}
1✔
50
 */
1✔
51
SVGSprite.prototype.DEFAULT_SVG_NAMESPACE = 'http://www.w3.org/2000/svg';
1✔
52

1✔
53
/**
1✔
54
 * Xlink namespace
1✔
55
 *
1✔
56
 * @type {String}
1✔
57
 */
1✔
58
SVGSprite.prototype.XLINK_NAMESPACE = 'http://www.w3.org/1999/xlink';
1✔
59

1✔
60
/**
1✔
61
 * Add a content string
1✔
62
 *
1✔
63
 * @param {String} content          Content string
1✔
64
 * @return {SVGSprite}              Self reference
1✔
65
 */
1✔
66
SVGSprite.prototype.add = function(content) {
1✔
67
    if (Array.isArray(content)) {
61✔
68
        this.content.push(...content);
7✔
69
    } else {
61✔
70
        this.content.push(content);
54✔
71
    }
54✔
72

61✔
73
    this._serialized = null;
61✔
74
};
1✔
75

1✔
76
/**
1✔
77
 * Serialize the SVG sprite
1✔
78
 *
1✔
79
 * @return {String}                 SVG sprite
1✔
80
 */
1✔
81
SVGSprite.prototype.toString = function() {
1✔
82
    if (this._serialized === null) {
22✔
83
        let svg = this.xmlDeclaration + this.doctypeDeclaration;
11✔
84
        svg += '<svg';
11✔
85
        for (const attr in this.rootAttributes) {
11✔
86
            svg += ' ' + attr + '="' + _.escape(this.rootAttributes[attr]) + '"';
55✔
87
        }
55✔
88

11✔
89
        svg += '>';
11✔
90
        svg += this.content.join('');
11✔
91
        svg += '</svg>';
11✔
92

11✔
93
        // Apply post-processing transformations
11✔
94
        for (let t = 0; t < this.transform.length; t++) {
11!
95
            if (_.isFunction(this.transform[t])) {
×
96
                svg = this.transform[t](svg) || '';
×
97
            }
×
98
        }
×
99

11✔
100
        this._serialized = svg;
11✔
101
    }
11✔
102

22✔
103
    return this._serialized;
22✔
104
};
1✔
105

1✔
106
/**
1✔
107
 * Return as vinyl file
1✔
108
 *
1✔
109
 * @param {String} base             Base path
1✔
110
 * @param {String} path             Path
1✔
111
 * @return {Vinyl}                  Vinyl file
1✔
112
 */
1✔
113
SVGSprite.prototype.toFile = function(base, path) {
1✔
114
    return new Vinyl({
11✔
115
        base,
11✔
116
        path,
11✔
117
        contents: Buffer.from(this.toString())
11✔
118
    });
11✔
119
};
1✔
120

1✔
121
/**
1✔
122
 * Module export
1✔
123
 */
1✔
124
module.exports = SVGSprite;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc