Permalink
Browse files

Fiking regex for uniquing id’s

  • Loading branch information...
nvh committed Feb 1, 2018
1 parent 2d58487 commit 08c3304c7b6de9fb0a0d2577fd2e74354ca6472c
Showing with 12 additions and 0 deletions.
  1. +1 −0 framer/SVGLayer.coffee
  2. +3 −0 framer/Utils.coffee
  3. +8 −0 test/tests/SVGLayerTest.coffee
View
@@ -11,6 +11,7 @@ updateIdsToBeUnique = (htmlString) ->
for id in ids
uniqueId = Utils.getUniqueId(id)
if id isnt uniqueId
id = Utils.escapeForRegex(id)
htmlString = htmlString.replace(///((id|xlink:href)=["'']\#?)#{id}(["'])///g, "$1#{uniqueId}$3")
htmlString = htmlString.replace(///(["'']url\(\#)#{id}(\)["'])///g, "$1#{uniqueId}$2")
return htmlString
View
@@ -1441,6 +1441,9 @@ Utils.getUniqueId = (prefix = 'id') ->
count++
return id
Utils.escapeForRegex = (string) ->
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
_.extend exports, Utils
@@ -104,6 +104,14 @@ describe "SVGLayer", ->
expect(b.html).to.equal '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><path d="M 100 50 C 100 77.614 77.614 100 50 100 C 22.386 100 0 77.614 0 50 C 0 22.386 22.386 0 50 0" id="path1" name="path" fill="transparent" stroke="#0AF" style="-webkit-perspective: none; pointer-events: none; display: block; opacity: 1; overflow: visible; -webkit-transform-style: preserve-3d; -webkit-backface-visibility: visible; fill: rgba(0, 0, 0, 0); stroke: #00aaff; -webkit-perspective-origin-x: 50%; -webkit-perspective-origin-y: 50%;"></path></svg>'
a.destroy()
it "should unique the id's even if theyre complex", ->
string = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" xmlns:xlink=\"http:\/\/www.w3.org\/1999\/xlink\" width=\"22px\" height=\"22px\" viewBox=\"0 0 22 22\" version=\"1.1\"><!-- Generator: Sketch 48.2 (47327) - http:\/\/www.bohemiancoding.com\/sketch --><title>Combined Shape Copy 3<\/title><desc>Created with Sketch.<\/desc><defs><\/defs><g id=\"Controls\/TableViewCell\/_Resources\/Right-Accessories\/Move-+-Info-+-Detail\" stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(-99.000000, -11.000000)\"><g id=\"Group-3\" transform=\"translate(21.000000, 0.000000)\" fill=\"#007AFF\" fill-rule=\"nonzero\"><path d=\"M89,32 C94.5228475,32 99,27.5228475 99,22 C99,16.4771525 94.5228475,12 89,12 C83.4771525,12 79,16.4771525 79,22 C79,27.5228475 83.4771525,32 89,32 Z M89,33 C82.9248678,33 78,28.0751322 78,22 C78,15.9248678 82.9248678,11 89,11 C95.0751322,11 100,15.9248678 100,22 C100,28.0751322 95.0751322,33 89,33 Z M88.75,18.5 C88.0595,18.5 87.5,17.9405 87.5,17.25 C87.5,16.5595 88.0595,16 88.75,16 C89.4405,16 90,16.5595 90,17.25 C90,17.9405 89.4405,18.5 88.75,18.5 Z M87,28.5 L87,28 L88,28 L88,20.5 L87,20.5 L87,20 L88,20 L90,20 L90,20.5 L90,28 L91,28 L91,28.5 L87,28.5 Z\" id=\"Combined-Shape-Copy-3\"><\/path><\/g><\/g><\/svg>"
svg1 = new SVGLayer({"name": "icon", "backgroundColor": null, "width": 22, "x": 11, "htmlIntrinsicSize": {"height": 22, "width": 22}, "color": "rgba(255, 149, 0, 1.00)", "height": 22, "constraintValues": {"left": null, "aspectRatioLocked": true, "height": 22, "centerAnchorX": 0.5, "width": 22, "top": null, "centerAnchorY": 0.5}, "blending": "normal", "y": 11, "svg": string})
svg2 = new SVGLayer({"name": "icon", "backgroundColor": null, "width": 22, "x": 11, "htmlIntrinsicSize": {"height": 22, "width": 22}, "color": "rgba(255, 149, 0, 1.00)", "height": 22, "constraintValues": {"left": null, "aspectRatioLocked": true, "height": 22, "centerAnchorX": 0.5, "width": 22, "top": null, "centerAnchorY": 0.5}, "blending": "normal", "y": 11, "svg": string})
expect(svg2.html).to.not.be.null
svg2.html.should.not.equal ""
svg1.html.should.not.equal svg2.html
it "should be able to copy a layer twice", ->
string = '<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" id="rect" name="Rectangle"></path></svg>'
a = new SVGLayer

0 comments on commit 08c3304

Please sign in to comment.