Skip to content

Commit f946d7b

Browse files
authored
Merge pull request #96 from swhitty/resolve-clipPath
Resolve clipPaths via stylesheet
2 parents 5886ae2 + f2cb554 commit f946d7b

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

Samples.bundle/amex.svg

Lines changed: 1 addition & 0 deletions
Loading

SwiftDraw/Sources/LayerTree/LayerTree.Builder.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ extension LayerTree {
167167
}
168168

169169
func makeClipShapes(for element: DOM.GraphicsElement) -> [ClipShape] {
170-
guard let clipId = element.attributes.clipPath?.fragmentID,
171-
let clip = svg.defs.clipPaths.first(where: { $0.id == clipId }) else { return [] }
172-
170+
let attributes = DOM.presentationAttributes(for: element, styles: svg.styles)
171+
guard let clipID = attributes.clipPath?.fragmentID,
172+
let clip = svg.defs.clipPaths.first(where: { $0.id == clipID }) else { return [] }
173173
return clip.childElements.compactMap(makeClipShape)
174174
}
175175

SwiftDraw/Tests/LayerTree/LayerTree.BuilderTests.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,30 @@ final class LayerTreeBuilderTests: XCTestCase {
7171
let circle = DOM.Circle(cx: 5, cy: 5, r: 5)
7272
let svg = DOM.SVG(width: 10, height: 10)
7373
svg.defs.clipPaths.append(DOM.ClipPath(id: "clip1", childElements: [circle]))
74+
75+
var attributes = DOM.PresentationAttributes()
76+
attributes.clipPath = URL(string: "#clip1")
77+
svg.styles = [DOM.StyleSheet(attributes: [.class("a"): attributes])]
78+
7479
let builder = LayerTree.Builder(svg: svg)
7580

76-
let element = DOM.GraphicsElement()
81+
var element = DOM.GraphicsElement()
7782
element.attributes.clipPath = URL(string: "#clip1")
7883

79-
let shapes = builder.createClipShapes(for: element)
80-
XCTAssertEqual(shapes, [.ellipse(within: LayerTree.Rect(x: 0, y: 0, width: 10, height: 10))])
84+
var shapes = builder.createClipShapes(for: element)
85+
XCTAssertEqual(
86+
builder.createClipShapes(for: element),
87+
[.ellipse(within: LayerTree.Rect(x: 0, y: 0, width: 10, height: 10))]
88+
)
89+
90+
element = DOM.GraphicsElement()
91+
element.class = "a"
92+
XCTAssertEqual(
93+
builder.createClipShapes(for: element),
94+
[.ellipse(within: LayerTree.Rect(x: 0, y: 0, width: 10, height: 10))]
95+
)
8196
}
82-
97+
8398
func testDOMGroupMakesChildContents() {
8499
let builder = LayerTree.Builder(svg: DOM.SVG(width: 10, height: 10))
85100

0 commit comments

Comments
 (0)