ctf-server
old server for hosting capture-the-flag
git clone https://9o.is/git/ctf-server.git
svg.addclass.js
(1276B)
1 /**
2 * This method allow to easily add a CSS class to any SVG element
3 *
4 * The classList parameter is a string of white space separated CSS class name.
5 *
6 * Conveniently, this method return the object itself in order to easily chain
7 * method call.
8 *
9 * @param classList string
10 */
11
12 // Testing the existence of the global SVGElement object to safely extend it.
13 if (SVGElement && SVGElement.prototype) {
14 SVGElement.prototype.addClass = function addClass(classList) {
15 "use strict";
16
17 // Because the className property can be animated through SVG, we have to reach
18 // the baseVal property of the className SVGAnimatedString object.
19 var currentClass = this.className.baseVal;
20
21 // Note that all browsers which currently support SVG also support Array.forEach()
22 classList.split(' ').forEach(function (newClass) {
23 var tester = new RegExp('\\b' + newClass + '\\b', 'g');
24
25 if (-1 === currentClass.search(tester)) {
26 currentClass += ' ' + newClass;
27 }
28 });
29
30 // The SVG className property is a readonly property so
31 // we must use the regular DOM API to write our new classes.
32 this.setAttribute('class', currentClass);
33
34 return this;
35 };
36 }