Skip to content

Commit

Permalink
fix regex, also allow xlink:href
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzyma committed Apr 24, 2017
1 parent 1d532f2 commit 9660844
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
25 changes: 13 additions & 12 deletions dist/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @copyright Wout Fierens <wout@mick-wout.com>
* @license MIT
*
* BUILT: Sun Apr 23 2017 15:09:13 GMT+0200 (Mitteleuropäische Sommerzeit)
* BUILT: Mon Apr 24 2017 09:47:29 GMT+0200 (Mitteleuropäische Sommerzeit)
*/;
(function(root, factory) {
/* istanbul ignore next */
Expand Down Expand Up @@ -182,8 +182,8 @@ SVG.regex = {
// Parse rgb value
, rgb: /rgb\((\d+),(\d+),(\d+)\)/

// Parse reference id
, reference: /#([a-z0-9\-_]+)/i
// Parse reference url
, reference: /(url\()?([-\w:/.]+)?#([-\w]+)/

// splits a transformation chain
, transforms: /\)\s*,?\s*/
Expand Down Expand Up @@ -212,9 +212,6 @@ SVG.regex = {
// Test for image url
, isImage: /\.(jpg|jpeg|png|gif|svg)(\?[^=]+.*)?/i

// Test for url reference
, isUrl: /url\(([-\w:/]+(?:\.\w+)?)?#([-\w]+)\)/

// split at whitespace and comma
, delimiter: /[\s,]+/

Expand Down Expand Up @@ -4973,9 +4970,9 @@ function fullBox(b) {

// Get id from reference string
function idFromReference(url) {
var m = (url || '').toString().match(SVG.regex.reference)
var m = SVG.regex.reference.exec(url+'')

if (m) return m[1]
if (m) return m[3]
}

// creates an url reference out of nodes id
Expand All @@ -4990,8 +4987,10 @@ function removePrefixFromReferences(node) {

var v = node.attributes, match
for (n = v.length - 1; n >= 0; n--) {
if(match = SVG.regex.isUrl.exec(v[n].nodeValue)) {
if(match[1] == window.location) v[n].nodeValue = 'url(#' + match[2] + ')'
if(v[n].nodeName == 'xlink:href' && (match = SVG.regex.reference.exec(v[n].nodeValue))) {
if(match[2] == window.location) v[n].nodeValue = '#' + (match[3] || '')
}else if(match = SVG.regex.reference.exec(v[n].nodeValue)) {
if(match[1] && match[2] == window.location) v[n].nodeValue = 'url(#' + match[3] + ')'
}
}
}
Expand All @@ -5003,8 +5002,10 @@ function addPrefixToReferences(node) {

var v = node.attributes, match
for (n = v.length - 1; n >= 0; n--) {
if(match = SVG.regex.isUrl.exec(v[n].nodeValue)) {
if(!match[1]) v[n].nodeValue = 'url(' + window.location + '#' + match[2] + ')'
if(v[n].nodeName == 'xlink:href' && (match = SVG.regex.reference.exec(v[n].nodeValue))) {
if(!match[2]) v[n].nodeValue = window.location + '#' + (match[3] || '')
}else if(match = SVG.regex.reference.exec(v[n].nodeValue)) {
if(match[1] && !match[2]) v[n].nodeValue = 'url(' + window.location + '#' + match[3] + ')'
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions dist/svg.min.js

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ function fullBox(b) {

// Get id from reference string
function idFromReference(url) {
var m = (url || '').toString().match(SVG.regex.reference)
var m = SVG.regex.reference.exec(url+'')

if (m) return m[1]
if (m) return m[3]
}

// creates an url reference out of nodes id
Expand All @@ -197,8 +197,10 @@ function removePrefixFromReferences(node) {

var v = node.attributes, match
for (n = v.length - 1; n >= 0; n--) {
if(match = SVG.regex.isUrl.exec(v[n].nodeValue)) {
if(match[1] == window.location) v[n].nodeValue = 'url(#' + match[2] + ')'
if(v[n].nodeName == 'xlink:href' && (match = SVG.regex.reference.exec(v[n].nodeValue))) {
if(match[2] == window.location) v[n].nodeValue = '#' + (match[3] || '')
}else if(match = SVG.regex.reference.exec(v[n].nodeValue)) {
if(match[1] && match[2] == window.location) v[n].nodeValue = 'url(#' + match[3] + ')'
}
}
}
Expand All @@ -210,8 +212,10 @@ function addPrefixToReferences(node) {

var v = node.attributes, match
for (n = v.length - 1; n >= 0; n--) {
if(match = SVG.regex.isUrl.exec(v[n].nodeValue)) {
if(!match[1]) v[n].nodeValue = 'url(' + window.location + '#' + match[2] + ')'
if(v[n].nodeName == 'xlink:href' && (match = SVG.regex.reference.exec(v[n].nodeValue))) {
if(!match[2]) v[n].nodeValue = window.location + '#' + (match[3] || '')
}else if(match = SVG.regex.reference.exec(v[n].nodeValue)) {
if(match[1] && !match[2]) v[n].nodeValue = 'url(' + window.location + '#' + match[3] + ')'
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ SVG.regex = {
// Parse rgb value
, rgb: /rgb\((\d+),(\d+),(\d+)\)/

// Parse reference id
, reference: /#([a-z0-9\-_]+)/i
// Parse reference url
, reference: /(url\()?([-\w:/.]+)?#([-\w]+)/

// splits a transformation chain
, transforms: /\)\s*,?\s*/
Expand Down Expand Up @@ -39,9 +39,6 @@ SVG.regex = {
// Test for image url
, isImage: /\.(jpg|jpeg|png|gif|svg)(\?[^=]+.*)?/i

// Test for url reference
, isUrl: /url\(([-\w:/]+(?:\.\w+)?)?#([-\w]+)\)/

// split at whitespace and comma
, delimiter: /[\s,]+/

Expand Down

0 comments on commit 9660844

Please sign in to comment.