Skip to content

Commit

Permalink
Merge pull request #1383 from wordpress-mobile/fix/crash-comment-atta…
Browse files Browse the repository at this point in the history
…chment-renderer

Fix crash when attempting to render Gutenberg comment
itsmeichigo authored Feb 1, 2024
2 parents 006acea + 4049aa5 commit 0f5c4eb
Showing 3 changed files with 43 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Aztec/Classes/Renderers/CommentAttachmentRenderer.swift
Original file line number Diff line number Diff line change
@@ -36,15 +36,20 @@ extension CommentAttachmentRenderer: TextViewAttachmentImageProvider {
}

public func textView(_ textView: TextView, imageFor attachment: NSTextAttachment, with size: CGSize) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, 0)

// Either this is a comment attachment, or the logic is broken.
let commentAttachment = attachment as! CommentAttachment

guard !isGutenbergComment(commentAttachment) else {
return nil
}

// Extra safety check to avoid crash when attempting to render image with size smaller than 0
guard size.width > 0 && size.height > 0 else {
return nil
}

UIGraphicsBeginImageContextWithOptions(size, false, 0)

let message = messageAttributedString()
let targetRect = boundingRect(for: message, size: size)

27 changes: 27 additions & 0 deletions AztecTests/Renderers/CommentAttachmentRendererTests.swift
Original file line number Diff line number Diff line change
@@ -46,4 +46,31 @@ class CommentAttachmentRendererTests: XCTestCase {

XCTAssertEqual(bounds, expectedBounds)
}

func testImageForGutenbergCommentIsNil() {
// Given
let textView = TextViewStub()
let gutenbergComment = CommentAttachment()
gutenbergComment.text = "wp:paragraph"
let renderer = CommentAttachmentRenderer(font: .systemFont(ofSize: 12))

// When
let image = renderer.textView(textView, imageFor: gutenbergComment, with: .init(width: 2, height: 2))

// Then
XCTAssertNil(image)
}

func testRenderingImageWithIllegalSizeReturnsNil() {
// Given
let textView = TextViewStub()
let attachment = CommentAttachment()
let renderer = CommentAttachmentRenderer(font: .systemFont(ofSize: 12))

// When
let image = renderer.textView(textView, imageFor: attachment, with: .init(width: -1, height: -1))

// Then
XCTAssertNil(image)
}
}
9 changes: 9 additions & 0 deletions Example/Example/SampleContent/content.html
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ <h1>Welcome To Aztec</h1>
<p>Aztec goal is to be an component that allows display and editition of HTML. <br/>
It aims to supports the most common character styles, paragraph styles and multimedia elements.
</p>
<p><!-- wp:paragraph --></p>

<!--more-->

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

<h4>Unordered List:</h4>
<!-- wp:list -->
<ul>
<!-- wp:list-item -->
<li>One</li>
<!-- /wp:list-item -->
<!-- wp:list-item -->
<li>Two</li>
<!-- /wp:list-item -->
<!-- wp:list-item -->
<li>Three</li>
<!-- wp:list-item -->
</ul>
<!-- /wp:list -->

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

0 comments on commit 0f5c4eb

Please sign in to comment.