summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2017-03-14 18:43:22 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2017-03-14 18:43:22 +0100
commitaf45360b7b2a8dc3f3eaed553f9544b4318e7348 (patch)
tree8da54a60d1c23854239996bc5318ccb87e8f1b15 /src
parent4b6e28689412a3fda9a52f2e665dbc536c87cd8f (diff)
downloadsvg.js-af45360b7b2a8dc3f3eaed553f9544b4318e7348.tar.gz
svg.js-af45360b7b2a8dc3f3eaed553f9544b4318e7348.zip
move most regexe to SVG.regex, fix matrix constructor for string argument
Diffstat (limited to 'src')
-rw-r--r--src/color.js2
-rw-r--r--src/element.js2
-rw-r--r--src/helpers.js16
-rw-r--r--src/matrix.js2
-rw-r--r--src/pointarray.js4
-rw-r--r--src/regex.js4
-rw-r--r--src/transform.js6
7 files changed, 10 insertions, 26 deletions
diff --git a/src/color.js b/src/color.js
index 25f41d6..9d77d4e 100644
--- a/src/color.js
+++ b/src/color.js
@@ -13,7 +13,7 @@ SVG.Color = function(color) {
if (typeof color === 'string') {
if (SVG.regex.isRgb.test(color)) {
// get rgb values
- match = SVG.regex.rgb.exec(color.replace(/\s/g,''))
+ match = SVG.regex.rgb.exec(color.replace(SVG.regex.whitespace,''))
// parse numeric values
this.r = parseInt(match[1])
diff --git a/src/element.js b/src/element.js
index d22e742..9bb8a0a 100644
--- a/src/element.js
+++ b/src/element.js
@@ -129,7 +129,7 @@ SVG.Element = SVG.invent({
, classes: function() {
var attr = this.attr('class')
- return attr == null ? [] : attr.trim().split(/\s+/)
+ return attr == null ? [] : attr.trim().split(SVG.regex.delimiter)
}
// Return true if class exists on the node, false otherwise
, hasClass: function(name) {
diff --git a/src/helpers.js b/src/helpers.js
index 4974517..e3ad6f0 100644
--- a/src/helpers.js
+++ b/src/helpers.js
@@ -95,22 +95,6 @@ function ensureCentre(o, target) {
o.cy = o.cy == null ? target.bbox().cy : o.cy
}
-// Convert string to matrix
-function stringToMatrix(source) {
- // remove matrix wrapper and split to individual numbers
- source = source
- .replace(SVG.regex.whitespace, '')
- .replace(SVG.regex.matrix, '')
- .split(SVG.regex.matrixElements)
-
- // convert string values to floats and convert to a matrix-formatted object
- return arrayToMatrix(
- SVG.utils.map(source, function(n) {
- return parseFloat(n)
- })
- )
-}
-
// PathArray Helpers
function arrayToString(a) {
for (var i = 0, il = a.length, s = ''; i < il; i++) {
diff --git a/src/matrix.js b/src/matrix.js
index 60f3531..970e3be 100644
--- a/src/matrix.js
+++ b/src/matrix.js
@@ -7,7 +7,7 @@ SVG.Matrix = SVG.invent({
source = source instanceof SVG.Element ?
source.matrixify() :
typeof source === 'string' ?
- stringToMatrix(source) :
+ arrayToMatrix(source.split(SVG.regex.delimiter).map(parseFloat)) :
arguments.length == 6 ?
arrayToMatrix([].slice.call(arguments)) :
Array.isArray(source) ?
diff --git a/src/pointarray.js b/src/pointarray.js
index 58166a3..b0a3d54 100644
--- a/src/pointarray.js
+++ b/src/pointarray.js
@@ -53,7 +53,7 @@ SVG.extend(SVG.PointArray, {
}
} else { // Else, it is considered as a string
// parse points
- array = array.trim().split(/[\s,]+/)
+ array = array.trim().split(SVG.regex.delimiter).map(parseFloat)
}
// validate points - https://svgwg.org/svg2-draft/shapes.html#DataTypePoints
@@ -62,7 +62,7 @@ SVG.extend(SVG.PointArray, {
// wrap points in two-tuples and parse points as floats
for(var i = 0, len = array.length; i < len; i = i + 2)
- points.push([ parseFloat(array[i]), parseFloat(array[i+1]) ])
+ points.push([ array[i], array[i+1] ])
return points
}
diff --git a/src/regex.js b/src/regex.js
index c953805..28343d2 100644
--- a/src/regex.js
+++ b/src/regex.js
@@ -15,8 +15,8 @@ SVG.regex = {
// Parse matrix wrapper
, matrix: /matrix\(|\)/g
- // Elements of a matrix
-, matrixElements: /,*\s+|,/
+ // splits a transformation chain
+, transforms: /\)\s*,?\s*/
// Whitespace
, whitespace: /\s/g
diff --git a/src/transform.js b/src/transform.js
index 170c205..f2061db 100644
--- a/src/transform.js
+++ b/src/transform.js
@@ -180,12 +180,12 @@ SVG.extend(SVG.Element, {
var matrix = (this.attr('transform') || '')
// split transformations
- .split(/\)\s*,?\s*/).slice(0,-1).map(function(str){
+ .split(SVG.regex.transforms).slice(0,-1).map(function(str){
// generate key => value pairs
var kv = str.trim().split('(')
- return [kv[0], kv[1].split(SVG.regex.matrixElements).map(function(str){ return parseFloat(str) })]
+ return [kv[0], kv[1].split(SVG.regex.delimiter).map(function(str){ return parseFloat(str) })]
})
- // calculate every transformation into one matrix
+ // merge every transformation into one matrix
.reduce(function(matrix, transform){
if(transform[0] == 'matrix') return matrix.multiply(arrayToMatrix(transform[1]))