Skip to content

Commit

Permalink
Merge branch 'cysp-feature/ignore-unknown-elements'
Browse files Browse the repository at this point in the history
  • Loading branch information
arielelkin committed Jun 25, 2018
2 parents 708c8b4 + d8b54cc commit fc4c0af
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file. Items under
- Add support for integer function SVG colors: rgb(int, int, int). Clément Beffa
### Internal Changes
- Add unit tests. Ariel Elkin.
- Deal with optional transformation arguments. Scott Talbot [#122](https://github.com/pocketsvg/PocketSVG/pull/122)
- Handle attributes on `<a>` elements and ignore unknown elements. Scott Talbot [#121](https://github.com/pocketsvg/PocketSVG/pull/121)

## [2.4.0]
### New Features
Expand Down
64 changes: 48 additions & 16 deletions PocketSVGTests/PocketSVGTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,50 @@ class PocketSVGTests: XCTestCase {
XCTAssertEqual(rectanglePath.svgRepresentation, representation)
}

func testIgnoresMaskElement() {
let svgString = """
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px">
<g>
<mask>
<g>
<rect width="100" height="100" style="fill: #FFFFFF" />
</g>
</mask>
<rect x="20" y="20" width="60" height="60" style="fill: #FF0000"/>
</g>
</svg>
"""

let paths = SVGBezierPath.paths(fromSVGString: svgString)
XCTAssertEqual(paths.count, 1)
let path = paths.first

XCTAssertEqual(path!.bounds, CGRect(x: 20, y: 20, width: 60, height: 60))
}

func testRespectsAttributesOnAElement() {
let svgString = """
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px">
<a transform="translate(5 10)">
<rect x="15" y="10" width="60" height="60"/>
</a>
</svg>
"""

let paths = SVGBezierPath.paths(fromSVGString: svgString)
XCTAssertEqual(paths.count, 1)
let path = paths.first!

let pathTransform = (path.svgAttributes["transform"]! as! NSValue).cgAffineTransformValue
let pathBounds = path.bounds
let translatedPathBounds = pathBounds.applying(pathTransform)

XCTAssertEqual(pathTransform, CGAffineTransform(translationX: 5, y: 10))
XCTAssertEqual(pathBounds, CGRect(x: 15, y: 10, width: 60, height: 60))
XCTAssertEqual(translatedPathBounds, CGRect(x: 20, y: 20, width: 60, height: 60))
}


func testTransformTranslate() {
let svgString = """
<svg xmlns="http://www.w3.org/2000/svg">
Expand All @@ -113,10 +157,7 @@ class PocketSVGTests: XCTestCase {
"""
let paths = SVGBezierPath.paths(fromSVGString: svgString)
XCTAssertEqual(paths.count, 1)

guard let path = paths.first else {
return
}
let path = paths.first!

let pathTransform = (path.svgAttributes["transform"]! as! NSValue).svg_CGAffineTransform();
XCTAssertEqual(pathTransform, CGAffineTransform(translationX: 10, y: 5))
Expand All @@ -132,10 +173,7 @@ class PocketSVGTests: XCTestCase {
"""
let paths = SVGBezierPath.paths(fromSVGString: svgString)
XCTAssertEqual(paths.count, 1)

guard let path = paths.first else {
return
}
let path = paths.first!

let pathTransform = (path.svgAttributes["transform"]! as! NSValue).svg_CGAffineTransform();
XCTAssertEqual(pathTransform, CGAffineTransform(translationX: 10, y: 0))
Expand All @@ -151,10 +189,7 @@ class PocketSVGTests: XCTestCase {
"""
let paths = SVGBezierPath.paths(fromSVGString: svgString)
XCTAssertEqual(paths.count, 1)

guard let path = paths.first else {
return
}
let path = paths.first!

let pathTransform = (path.svgAttributes["transform"]! as! NSValue).svg_CGAffineTransform();
XCTAssertEqual(pathTransform, CGAffineTransform(scaleX: 2, y: 2))
Expand All @@ -170,10 +205,7 @@ class PocketSVGTests: XCTestCase {
"""
let paths = SVGBezierPath.paths(fromSVGString: svgString)
XCTAssertEqual(paths.count, 1)

guard let path = paths.first else {
return
}
let path = paths.first!

let pathTransform = (path.svgAttributes["transform"]! as! NSValue).svg_CGAffineTransform();
XCTAssertEqual(pathTransform, CGAffineTransform(scaleX: 2, y: 2))
Expand Down
16 changes: 13 additions & 3 deletions SVGEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,21 @@ @interface SVGAttributeSet () {
valueOptions:NSMapTableStrongMemory];
NSMutableArray * const paths = [NSMutableArray new];

NSUInteger depthWithinUnknownElement = 0;

while(xmlTextReaderRead(_xmlReader) == 1) {
int const type = xmlTextReaderNodeType(_xmlReader);
const char * const tag = (char *)xmlTextReaderConstName(_xmlReader);

CGPathRef path = NULL;
if(type == XML_READER_TYPE_ELEMENT && strcasecmp(tag, "path") == 0)
if(depthWithinUnknownElement > 0) {
if(type == XML_READER_TYPE_ELEMENT && !xmlTextReaderIsEmptyElement(_xmlReader))
++depthWithinUnknownElement;
else if(type == XML_READER_TYPE_END_ELEMENT)
--depthWithinUnknownElement;
} else if(type == XML_READER_TYPE_ELEMENT && strcasecmp(tag, "svg") == 0) {
// recognize the root svg element but we don't need to do anything with it
} else if(type == XML_READER_TYPE_ELEMENT && strcasecmp(tag, "path") == 0)
path = readPathTag();
else if(type == XML_READER_TYPE_ELEMENT && strcasecmp(tag, "polyline") == 0)
path = readPolylineTag();
Expand All @@ -122,12 +131,13 @@ @interface SVGAttributeSet () {
path = readCircleTag();
else if(type == XML_READER_TYPE_ELEMENT && strcasecmp(tag, "ellipse") == 0)
path = readEllipseTag();
else if(strcasecmp(tag, "g") == 0) {
else if(strcasecmp(tag, "g") == 0 || strcasecmp(tag, "a") == 0) {
if(type == XML_READER_TYPE_ELEMENT)
pushGroup(readAttributes());
else if(type == XML_READER_TYPE_END_ELEMENT)
popGroup();
}
} else if(type == XML_READER_TYPE_ELEMENT && !xmlTextReaderIsEmptyElement(_xmlReader))
++depthWithinUnknownElement;
if(path) {
[paths addObject:CFBridgingRelease(path)];

Expand Down

0 comments on commit fc4c0af

Please sign in to comment.