Skip to content

Commit 23da2bf

Browse files
committed
Also updating the id’s, when setting svg
1 parent 0b25219 commit 23da2bf

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

framer/SVGLayer.coffee

+11-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
{SVGPath} = require "./SVGPath"
77
Utils = require "./Utils"
88

9+
updateIdsToBeUnique = (htmlString) ->
10+
ids = Utils.getIdAttributesFromString(htmlString)
11+
for id in ids
12+
uniqueId = Utils.getUniqueId(id)
13+
if id isnt uniqueId
14+
htmlString = htmlString.replace(///((id|xlink:href)=["'']\#?)#{id}(["'])///g, "$1#{uniqueId}$3")
15+
htmlString = htmlString.replace(///(["'']url\(\#)#{id}(\)["'])///g, "$1#{uniqueId}$2")
16+
return htmlString
17+
918
class exports.SVGLayer extends Layer
1019

1120
@DenyCopyMessage: "SVGLayer doesn't support `copy` when the layer has one more children"
@@ -60,7 +69,7 @@ class exports.SVGLayer extends Layer
6069
return null
6170
set: (value) ->
6271
if typeof value is "string"
63-
@html = value
72+
@html = updateIdsToBeUnique(value)
6473
else if value instanceof SVGElement
6574
idElements = value.querySelectorAll('[id]')
6675
for element in idElements
@@ -83,14 +92,7 @@ class exports.SVGLayer extends Layer
8392
props = @props
8493
if props.html? and props.svg?
8594
delete props.svg
86-
ids = Utils.getIdAttributesFromString(props.html)
87-
html = props.html
88-
for id in ids
89-
uniqueId = Utils.getUniqueId(id)
90-
if id isnt uniqueId
91-
html = html.replace(///((id|xlink:href)=["'']\#?)#{id}(["'])///g, "$1#{uniqueId}$3")
92-
html = html.replace(///(["'']url\(\#)#{id}(\)["'])///g, "$1#{uniqueId}$2")
93-
props.html = html
95+
props.html = updateIdsToBeUnique(props.html)
9496
copy = new @constructor(props)
9597
copy.style = @style
9698
copy

test/tests/SVGLayerTest.coffee

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ describe "SVGLayer", ->
136136
html: svgString
137137
expect(a.backgroundColor).to.be.null
138138

139-
it "should warn when adding an svg string with id's that already exist in the document", ->
139+
it "should unique the id's in a string that already exist in the document", ->
140140
svgWithIds = '<svg xmlns="http://www.w3.org/2000/svg" width="182" height="182"><path d="M 0 0 L 182 0 L 182 182 L 0 182 Z" name="Rectangle" id="test-bla/hoera"></path></svg>'
141141
a = new SVGLayer
142142
svg: svgWithIds
143143
b = new SVGLayer
144144
svg: svgWithIds
145145
a.html.should.equal '<svg xmlns="http://www.w3.org/2000/svg" width="182" height="182"><path d="M 0 0 L 182 0 L 182 182 L 0 182 Z" name="Rectangle" id="test-bla/hoera" style="-webkit-perspective: none; pointer-events: none; display: block; opacity: 1; overflow: visible; -webkit-transform-style: preserve-3d; -webkit-backface-visibility: visible; -webkit-perspective-origin-x: 50%; -webkit-perspective-origin-y: 50%;"></path></svg>'
146-
b.html.should.equal ''
146+
b.html.should.equal '<svg xmlns="http://www.w3.org/2000/svg" width="182" height="182"><path d="M 0 0 L 182 0 L 182 182 L 0 182 Z" name="Rectangle" id="test-bla/hoera1" style="-webkit-perspective: none; pointer-events: none; display: block; opacity: 1; overflow: visible; -webkit-transform-style: preserve-3d; -webkit-backface-visibility: visible; -webkit-perspective-origin-x: 50%; -webkit-perspective-origin-y: 50%;"></path></svg>'
147147
a.destroy()
148148
b.destroy()
149149

0 commit comments

Comments
 (0)