-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(): nested duplicated clipPath causes infinite recursion #10774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Smrtnyk
wants to merge
5
commits into
fabricjs:master
Choose a base branch
from
Smrtnyk:fix()-nested-duplicated-clipPath-causes-infinite-recursion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+334
−41
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
096ec99
fix(): nested duplicated clipPath causes infinite recursion
Smrtnyk 42db4eb
update CHANGELOG.md
github-actions[bot] 668f502
Merge branch 'master' into fix()-nested-duplicated-clipPath-causes-in…
asturur f6707ef
example that does not work
AndreaBogazzi a3b8ce7
Merge branch 'master' into fix()-nested-duplicated-clipPath-causes-in…
AndreaBogazzi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,4 +20,4 @@ | |
| "vanilla", | ||
| "fabric" | ||
| ] | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 28 additions & 15 deletions
43
.codesandbox/templates/vanilla/src/testcases/loadingSvgs.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+11.1 KB
...output/rendering/index.spec.ts-snapshots/svg-import/nested-clippath-complex.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+710 Bytes
...tput/rendering/index.spec.ts-snapshots/svg-import/nested-clippath-different.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.62 KB
...-output/rendering/index.spec.ts-snapshots/svg-import/nested-clippath-simple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.89 KB
...-output/rendering/index.spec.ts-snapshots/svg-import/nested-clippath-triple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import { loadSVGFromString } from '../../fabric'; | ||
| import { describe, it, expect } from 'vitest'; | ||
|
|
||
| describe('ElementsParser', () => { | ||
| describe('clipPath handling', () => { | ||
| it('should not enter infinite loop with nested duplicated clipPath', async () => { | ||
| const svg = `<?xml version="1.0" encoding="UTF-8"?> | ||
| <svg version="1.1" width="100px" height="100px" viewBox="0 0 100 100" | ||
| xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> | ||
| <defs> | ||
| <clipPath id="i0"> | ||
| <circle cx="0" cy="0" r="50" /> | ||
| </clipPath> | ||
| </defs> | ||
|
|
||
| <g clip-path="url(#i0)"> | ||
| <g clip-path="url(#i0)"> | ||
| <rect | ||
| width="50" | ||
| height="50" | ||
| fill="red" | ||
| /> | ||
| </g> | ||
| </g> | ||
| </svg>`; | ||
| const { objects } = await loadSVGFromString(svg); | ||
| expect(objects).toHaveLength(1); | ||
| expect(objects[0]!.clipPath).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should handle triple nested clipPath references', async () => { | ||
| const svg = `<?xml version="1.0" encoding="UTF-8"?> | ||
| <svg version="1.1" width="100px" height="100px" viewBox="0 0 100 100" | ||
| xmlns="http://www.w3.org/2000/svg"> | ||
| <defs> | ||
| <clipPath id="clip1"> | ||
| <circle cx="50" cy="50" r="40" /> | ||
| </clipPath> | ||
| </defs> | ||
|
|
||
| <g clip-path="url(#clip1)"> | ||
| <g clip-path="url(#clip1)"> | ||
| <g clip-path="url(#clip1)"> | ||
| <rect | ||
| width="80" | ||
| height="80" | ||
| fill="blue" | ||
| /> | ||
| </g> | ||
| </g> | ||
| </g> | ||
| </svg>`; | ||
| const { objects } = await loadSVGFromString(svg); | ||
| expect(objects).toHaveLength(1); | ||
| expect(objects[0]!.clipPath).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should handle different clipPaths at different levels', async () => { | ||
| const svg = `<?xml version="1.0" encoding="UTF-8"?> | ||
| <svg version="1.1" width="100px" height="100px" viewBox="0 0 100 100" | ||
| xmlns="http://www.w3.org/2000/svg"> | ||
| <defs> | ||
| <clipPath id="clip1"> | ||
| <circle cx="50" cy="50" r="40" /> | ||
| </clipPath> | ||
| <clipPath id="clip2"> | ||
| <rect x="10" y="10" width="80" height="80" /> | ||
| </clipPath> | ||
| </defs> | ||
|
|
||
| <g clip-path="url(#clip1)"> | ||
| <g clip-path="url(#clip2)"> | ||
| <rect | ||
| width="60" | ||
| height="60" | ||
| fill="green" | ||
| /> | ||
| </g> | ||
| </g> | ||
| </svg>`; | ||
| const { objects } = await loadSVGFromString(svg); | ||
| expect(objects).toHaveLength(1); | ||
| expect(objects[0]!.clipPath).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should handle clipPath with multiple shapes', async () => { | ||
| const svg = `<?xml version="1.0" encoding="UTF-8"?> | ||
| <svg version="1.1" width="200px" height="200px" viewBox="0 0 200 200" | ||
| xmlns="http://www.w3.org/2000/svg"> | ||
| <defs> | ||
| <clipPath id="multiShape"> | ||
| <circle cx="50" cy="50" r="40" /> | ||
| <rect x="100" y="0" width="50" height="50" /> | ||
| </clipPath> | ||
| </defs> | ||
|
|
||
| <g clip-path="url(#multiShape)"> | ||
| <g clip-path="url(#multiShape)"> | ||
| <rect | ||
| width="200" | ||
| height="200" | ||
| fill="purple" | ||
| /> | ||
| </g> | ||
| </g> | ||
| </svg>`; | ||
| const { objects } = await loadSVGFromString(svg); | ||
| expect(objects).toHaveLength(1); | ||
| expect(objects[0]!.clipPath).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should handle missing clipPath gracefully', async () => { | ||
| const svg = `<?xml version="1.0" encoding="UTF-8"?> | ||
| <svg version="1.1" width="100px" height="100px" viewBox="0 0 100 100" | ||
| xmlns="http://www.w3.org/2000/svg"> | ||
| <g clip-path="url(#nonExistent)"> | ||
| <rect | ||
| width="50" | ||
| height="50" | ||
| fill="orange" | ||
| /> | ||
| </g> | ||
| </svg>`; | ||
| const { objects } = await loadSVGFromString(svg); | ||
| expect(objects).toHaveLength(1); | ||
| expect(objects[0]!.clipPath).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should handle clipPath on element directly', async () => { | ||
| const svg = `<?xml version="1.0" encoding="UTF-8"?> | ||
| <svg version="1.1" width="100px" height="100px" viewBox="0 0 100 100" | ||
| xmlns="http://www.w3.org/2000/svg"> | ||
| <defs> | ||
| <clipPath id="directClip"> | ||
| <circle cx="50" cy="50" r="45" /> | ||
| </clipPath> | ||
| </defs> | ||
|
|
||
| <rect | ||
| clip-path="url(#directClip)" | ||
| width="100" | ||
| height="100" | ||
| fill="yellow" | ||
| /> | ||
| </svg>`; | ||
| const { objects } = await loadSVGFromString(svg); | ||
| expect(objects).toHaveLength(1); | ||
| expect(objects[0]!.clipPath).toBeDefined(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,9 +30,7 @@ | |
| }; | ||
|
|
||
| type NotParsedFabricObject = FabricObject & { | ||
| fill: string; | ||
| stroke: string; | ||
| clipPath?: string; | ||
| clipPath?: string | FabricObject['clipPath']; | ||
| clipRule?: CanvasFillRule; | ||
| }; | ||
|
|
||
|
|
@@ -94,13 +92,26 @@ | |
| return null; | ||
| } | ||
|
|
||
| extractPropertyDefinition( | ||
| obj: NotParsedFabricObject, | ||
| property: 'fill' | 'stroke', | ||
| storage: Record<string, SVGGradientElement>, | ||
| ): { def: SVGGradientElement; id: string } | undefined; | ||
| extractPropertyDefinition( | ||
| obj: NotParsedFabricObject, | ||
| property: 'clipPath', | ||
| storage: Record<string, Element[]>, | ||
| ): { def: Element[]; id: string } | undefined; | ||
| extractPropertyDefinition( | ||
| obj: NotParsedFabricObject, | ||
| property: 'fill' | 'stroke' | 'clipPath', | ||
| storage: Record<string, StorageType[typeof property]>, | ||
| ): StorageType[typeof property] | undefined { | ||
| const value = obj[property]!, | ||
| regex = this.regexUrl; | ||
| ): { def: StorageType[typeof property]; id: string } | undefined { | ||
| const value = obj[property]; | ||
| if (typeof value !== 'string') { | ||
| return undefined; | ||
| } | ||
| const regex = this.regexUrl; | ||
| if (!regex.test(value)) { | ||
| return undefined; | ||
| } | ||
|
|
@@ -110,22 +121,22 @@ | |
| const id = regex.exec(value)![1]; | ||
| regex.lastIndex = 0; | ||
| // @todo fix this | ||
| return storage[id]; | ||
| return storage[id] ? { def: storage[id], id } : undefined; | ||
| } | ||
|
|
||
| resolveGradient( | ||
| obj: NotParsedFabricObject, | ||
| el: Element, | ||
| property: 'fill' | 'stroke', | ||
| ) { | ||
| const gradientDef = this.extractPropertyDefinition( | ||
| const gradientDefinition = this.extractPropertyDefinition( | ||
| obj, | ||
| property, | ||
| this.gradientDefs, | ||
| ) as SVGGradientElement; | ||
| if (gradientDef) { | ||
| ); | ||
| if (gradientDefinition) { | ||
| const opacityAttr = el.getAttribute(property + '-opacity'); | ||
| const gradient = Gradient.fromElement(gradientDef, obj, { | ||
| const gradient = Gradient.fromElement(gradientDefinition.def, obj, { | ||
| ...this.options, | ||
| opacity: opacityAttr, | ||
| } as SVGOptions); | ||
|
|
@@ -139,15 +150,19 @@ | |
| obj: NotParsedFabricObject, | ||
| usingElement: Element, | ||
| exactOwner?: Element, | ||
| processedClipPaths: Set<string> = new Set(), | ||
| ) { | ||
| const clipPathElements = this.extractPropertyDefinition( | ||
| const clipPathDefinition = this.extractPropertyDefinition( | ||
| obj, | ||
| 'clipPath', | ||
| this.clipPaths, | ||
| ) as Element[]; | ||
| if (clipPathElements) { | ||
| ); | ||
| if (clipPathDefinition) { | ||
| const clipPathElements = clipPathDefinition.def; | ||
| const objTransformInv = invertTransform(obj.calcTransformMatrix()); | ||
| const clipPathTag = clipPathElements[0].parentElement!; | ||
| const clipPathTag = clipPathElements[0].parentElement!.cloneNode( | ||
| true, | ||
| ) as HTMLElement; | ||
| let clipPathOwner = usingElement; | ||
| while ( | ||
| !exactOwner && | ||
|
|
@@ -163,6 +178,7 @@ | |
| // but i don't have an svg to test it | ||
| // at the first SVG that has a transform on both places and is misplaced | ||
| // try to invert this multiplication order | ||
| console.log(clipPathOwner.getAttribute('transform')); | ||
| const finalTransform = parseTransformAttribute( | ||
| `${clipPathOwner.getAttribute('transform') || ''} ${ | ||
| clipPathTag.getAttribute('originalTransform') || '' | ||
|
|
@@ -174,16 +190,28 @@ | |
| `matrix(${finalTransform.join(',')})`, | ||
| ); | ||
|
|
||
| const updatedProcessedClipPaths = new Set(processedClipPaths); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you remember why the new set each time?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm, don't really remember now |
||
| updatedProcessedClipPaths.add(clipPathDefinition.id); | ||
|
|
||
| const container = await Promise.all( | ||
| clipPathElements.map((clipPathElement) => { | ||
| return findTag(clipPathElement) | ||
| .fromElement(clipPathElement, this.options, this.cssRules) | ||
| .then((enlivedClippath: NotParsedFabricObject) => { | ||
| removeTransformMatrixForSvgParsing(enlivedClippath); | ||
| enlivedClippath.fillRule = enlivedClippath.clipRule!; | ||
| delete enlivedClippath.clipRule; | ||
| return enlivedClippath; | ||
| }); | ||
| clipPathElements.map(async (clipPathElement) => { | ||
| const enlivedClippath: NotParsedFabricObject = await findTag( | ||
| clipPathElement, | ||
| ).fromElement(clipPathElement, this.options, this.cssRules); | ||
| removeTransformMatrixForSvgParsing(enlivedClippath); | ||
| enlivedClippath.fillRule = enlivedClippath.clipRule!; | ||
| delete enlivedClippath.clipRule; | ||
|
|
||
| // Resolve clipPath on elements inside the clipPath definition | ||
| // This prevents nested elements from having unresolved clipPath strings | ||
| await this.resolveClipPath( | ||
| enlivedClippath, | ||
| clipPathElement, | ||
| undefined, | ||
| updatedProcessedClipPaths, | ||
| ); | ||
|
|
||
| return enlivedClippath; | ||
| }), | ||
| ); | ||
| const clipPath = | ||
|
|
@@ -192,6 +220,7 @@ | |
| objTransformInv, | ||
| clipPath.calcTransformMatrix(), | ||
| ); | ||
| // Only resolve if clipPath property is still a string (not already resolved) | ||
| if (clipPath.clipPath) { | ||
| await this.resolveClipPath( | ||
| clipPath, | ||
|
|
@@ -200,6 +229,7 @@ | |
| // it tries to differentiate from when clipPaths are inherited by outside groups | ||
| // or when are really clipPaths referencing other clipPaths | ||
| clipPathTag.getAttribute('clip-path') ? clipPathOwner : undefined, | ||
| updatedProcessedClipPaths, | ||
| ); | ||
| } | ||
| const { scaleX, scaleY, angle, skewX, translateX, translateY } = | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is new from me, i wanted to create a copy of the clipPath since we are adding to it transforms attributes depending on the element using it