Repository navigation
fix: prevent blockquote rendering and UIKit teardown crashes #24
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| #if canImport(UIKit) | ||
| import UIKit | ||
| #elseif canImport(AppKit) | ||
| import AppKit | ||
| #endif | ||
| import CoreText | ||
| import XCTest | ||
| @testable import Litext | ||
| @testable import MarkdownView | ||
|
|
||
| @MainActor | ||
| final class BlockquoteDrawingTests: XCTestCase { | ||
| func testSingleLineRTLQuoteDrawsBarOnFirstPassAndRedraw() throws { | ||
| try assertQuoteBars( | ||
| markdown: "> مرحبا بالعالم", | ||
| quotedTexts: ["مرحبا بالعالم"] | ||
| ) | ||
| } | ||
|
|
||
| func testSeparatedRTLQuotesDoNotDrawBarAcrossParagraph() throws { | ||
| try assertQuoteBars( | ||
| markdown: "> مرحبا بالعالم\n\n" + String(repeating: "Intervening paragraph. ", count: 100) | ||
| + "\n\n> صباح الخير", | ||
| quotedTexts: ["مرحبا بالعالم", "صباح الخير"] | ||
| ) | ||
| } | ||
|
|
||
| func testWrappedQuoteDrawsContinuousBarOnFullPass() throws { | ||
| let quote = String(repeating: "Quoted text. ", count: 100).trimmingCharacters(in: .whitespaces) | ||
| try assertQuoteBars(markdown: "> " + quote, quotedTexts: [quote]) | ||
| } | ||
|
|
||
| private func assertQuoteBars(markdown: String, quotedTexts: [String]) throws { | ||
| let view = MarkdownTextView() | ||
| view.document = .init(markdown: markdown, theme: .default) | ||
| let document = TextBuilder.build(view: view, viewProvider: .init()).document | ||
| let layout = LTXTextLayout(attributedString: document) | ||
| let size = CGSize(width: 320, height: 6000) | ||
| layout.containerSize = size | ||
| let frame = try XCTUnwrap(layout.ctFrame) | ||
| let lines = CTFrameGetLines(frame) as! [CTLine] | ||
| var origins = [CGPoint](repeating: .zero, count: lines.count) | ||
| CTFrameGetLineOrigins(frame, CFRange(location: 0, length: 0), &origins) | ||
| let expected = try makeContext(size: size) | ||
| let undecorated = NSMutableAttributedString(attributedString: document) | ||
| undecorated.removeAttribute(LTXLineDrawingCallbackName, range: NSRange(location: 0, length: document.length)) | ||
| let textOnly = LTXTextLayout(attributedString: undecorated) | ||
| textOnly.containerSize = size | ||
| textOnly.draw(in: expected) | ||
| expected.translateBy(x: 0, y: size.height) | ||
| expected.scaleBy(x: 1, y: -1) | ||
| expected.setFillColor(view.theme.colors.body.withAlphaComponent(0.1).cgColor) | ||
| for text in quotedTexts { | ||
| let range = (document.string as NSString).range(of: text) | ||
| XCTAssertNotEqual(range.location, NSNotFound) | ||
| let quoteLines = lines.indices.filter { index in | ||
| let lineRange = CTLineGetStringRange(lines[index]) | ||
| return NSIntersectionRange(range, NSRange(location: lineRange.location, length: lineRange.length)).length > 0 | ||
| } | ||
| let first = try XCTUnwrap(quoteLines.first) | ||
| let last = try XCTUnwrap(quoteLines.last) | ||
| let top = TextBuilder.lineBoundingBox(lines[first], lineOrigin: origins[first]).maxY | ||
| let bottom = TextBuilder.lineBoundingBox(lines[last], lineOrigin: origins[last]).minY | ||
| let rect = CGRect(x: 0, y: bottom, width: 4, height: top - bottom) | ||
| expected.addPath(CGPath(roundedRect: rect, cornerWidth: 2, cornerHeight: 2, transform: nil)) | ||
| expected.fillPath() | ||
| } | ||
| let expectedPixels = try barPixels(expected) | ||
| XCTAssertTrue(expectedPixels.contains { $0 != 0 }) | ||
| for pass in 1...2 { | ||
| let actual = try makeContext(size: size) | ||
| layout.draw(in: actual) | ||
| XCTAssertEqual(try barPixels(actual), expectedPixels, "Incorrect bar geometry on pass \(pass)") | ||
| } | ||
| } | ||
|
|
||
| private func makeContext(size: CGSize) throws -> CGContext { | ||
| try XCTUnwrap(CGContext( | ||
| data: nil, width: Int(size.width), height: Int(size.height), | ||
| bitsPerComponent: 8, bytesPerRow: Int(size.width) * 4, | ||
| space: CGColorSpaceCreateDeviceRGB(), | ||
| bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue | ||
| )) | ||
| } | ||
|
|
||
| private func barPixels(_ context: CGContext) throws -> Data { | ||
| let data = try XCTUnwrap(context.data) | ||
| var pixels = Data() | ||
| for row in 0..<context.height { | ||
| pixels.append(Data(bytes: data.advanced(by: row * context.bytesPerRow), count: 4 * 4)) | ||
| } | ||
| return pixels | ||
| } | ||
|
|
||
| func testQuoteEndCanBeDrawnWithoutItsStartMarker() throws { | ||
| let view = MarkdownTextView() | ||
| view.document = .init(markdown: "> " + String(repeating: "Quoted text. ", count: 200), theme: .default) | ||
| let document = TextBuilder.build(view: view, viewProvider: .init()).document | ||
| let layout = LTXTextLayout(attributedString: document) | ||
| let size = CGSize(width: 320, height: 6000) | ||
| layout.containerSize = size | ||
|
|
||
| let frame = try XCTUnwrap(layout.ctFrame) | ||
| let lines = CTFrameGetLines(frame) as! [CTLine] | ||
| var origins = [CGPoint](repeating: .zero, count: lines.count) | ||
| CTFrameGetLineOrigins(frame, CFRange(location: 0, length: 0), &origins) | ||
| let decoratedLines = lines.indices.filter { index in | ||
| let runs = CTLineGetGlyphRuns(lines[index]) as! [CTRun] | ||
| return runs.contains { run in | ||
| let attributes = CTRunGetAttributes(run) as NSDictionary | ||
| return attributes[LTXLineDrawingCallbackName] != nil | ||
| } | ||
| } | ||
| XCTAssertEqual(decoratedLines.count, 2) | ||
| let first = try XCTUnwrap(decoratedLines.first) | ||
| let last = try XCTUnwrap(decoratedLines.last) | ||
| let endBounds = TextBuilder.lineBoundingBox(lines[last], lineOrigin: origins[last]) | ||
| let visibleRect = CGRect(x: 0, y: size.height - endBounds.maxY, width: size.width, height: endBounds.height) | ||
| let startBounds = TextBuilder.lineBoundingBox(lines[first], lineOrigin: origins[first]) | ||
| XCTAssertGreaterThan(startBounds.minY, endBounds.maxY) | ||
|
|
||
| let context = try XCTUnwrap(CGContext( | ||
| data: nil, width: Int(size.width), height: Int(size.height), | ||
| bitsPerComponent: 8, bytesPerRow: Int(size.width) * 4, | ||
| space: CGColorSpaceCreateDeviceRGB(), | ||
| bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue | ||
| )) | ||
| // Start with the ending tile, as can happen when scrolling into a long quote. | ||
| layout.draw(in: context, visibleRect: visibleRect) | ||
| layout.draw(in: context) | ||
| layout.draw(in: context, visibleRect: visibleRect) | ||
|
Comment on lines
+129
to
+131
Collaborator
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. These three calls only prove that drawing does not crash. With the guard, the first call draws no bar for the quote's last line, and nothing here would catch a change in bar geometry. Consider asserting on the pixels in the bar column (x 0..<4), at least for the full |
||
| } | ||
| } | ||
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.
The stored start position survives between draw passes. It is only cleared when an end callback runs, so a pass that sees a start without its end leaves a value behind for the next pass. With clipped draws on a document that has two long quotes and a paragraph between them:
It is also a single captured
varshared by every quote in the document, so it is not safe if a Markdown layout is ever drawn from concurrent tile renders the wayDiffTileRendererdraws diffs.The description already scopes continuous bars out, so this can be a follow-up. A stateless version, where each quote line draws its own bar segment from its own bounds, would fix the pass-order dependence, the RTL case above and the missing tiles together.