Skip to content

Commit 0f5c4eb

Browse files
authored
Merge pull request #1383 from wordpress-mobile/fix/crash-comment-attachment-renderer
Fix crash when attempting to render Gutenberg comment
2 parents 006acea + 4049aa5 commit 0f5c4eb

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

Aztec/Classes/Renderers/CommentAttachmentRenderer.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,20 @@ extension CommentAttachmentRenderer: TextViewAttachmentImageProvider {
3636
}
3737

3838
public func textView(_ textView: TextView, imageFor attachment: NSTextAttachment, with size: CGSize) -> UIImage? {
39-
UIGraphicsBeginImageContextWithOptions(size, false, 0)
40-
4139
// Either this is a comment attachment, or the logic is broken.
4240
let commentAttachment = attachment as! CommentAttachment
4341

4442
guard !isGutenbergComment(commentAttachment) else {
4543
return nil
4644
}
4745

46+
// Extra safety check to avoid crash when attempting to render image with size smaller than 0
47+
guard size.width > 0 && size.height > 0 else {
48+
return nil
49+
}
50+
51+
UIGraphicsBeginImageContextWithOptions(size, false, 0)
52+
4853
let message = messageAttributedString()
4954
let targetRect = boundingRect(for: message, size: size)
5055

AztecTests/Renderers/CommentAttachmentRendererTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,31 @@ class CommentAttachmentRendererTests: XCTestCase {
4646

4747
XCTAssertEqual(bounds, expectedBounds)
4848
}
49+
50+
func testImageForGutenbergCommentIsNil() {
51+
// Given
52+
let textView = TextViewStub()
53+
let gutenbergComment = CommentAttachment()
54+
gutenbergComment.text = "wp:paragraph"
55+
let renderer = CommentAttachmentRenderer(font: .systemFont(ofSize: 12))
56+
57+
// When
58+
let image = renderer.textView(textView, imageFor: gutenbergComment, with: .init(width: 2, height: 2))
59+
60+
// Then
61+
XCTAssertNil(image)
62+
}
63+
64+
func testRenderingImageWithIllegalSizeReturnsNil() {
65+
// Given
66+
let textView = TextViewStub()
67+
let attachment = CommentAttachment()
68+
let renderer = CommentAttachmentRenderer(font: .systemFont(ofSize: 12))
69+
70+
// When
71+
let image = renderer.textView(textView, imageFor: attachment, with: .init(width: -1, height: -1))
72+
73+
// Then
74+
XCTAssertNil(image)
75+
}
4976
}

Example/Example/SampleContent/content.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ <h1>Welcome To Aztec</h1>
33
<p>Aztec goal is to be an component that allows display and editition of HTML. <br/>
44
It aims to supports the most common character styles, paragraph styles and multimedia elements.
55
</p>
6+
<p><!-- wp:paragraph --></p>
67

78
<!--more-->
89

@@ -25,11 +26,19 @@ <h2>Paragraph Styles</h2>
2526
<h3>Lists</h3>
2627

2728
<h4>Unordered List:</h4>
29+
<!-- wp:list -->
2830
<ul>
31+
<!-- wp:list-item -->
2932
<li>One</li>
33+
<!-- /wp:list-item -->
34+
<!-- wp:list-item -->
3035
<li>Two</li>
36+
<!-- /wp:list-item -->
37+
<!-- wp:list-item -->
3138
<li>Three</li>
39+
<!-- wp:list-item -->
3240
</ul>
41+
<!-- /wp:list -->
3342

3443
<h4>Ordered List:</h4>
3544
<ol>

0 commit comments

Comments
 (0)