diff --git a/Maccy/HistoryMenuItem.swift b/Maccy/HistoryMenuItem.swift index 00bb404d..11af8ad1 100644 --- a/Maccy/HistoryMenuItem.swift +++ b/Maccy/HistoryMenuItem.swift @@ -126,7 +126,9 @@ class HistoryMenuItem: NSMenuItem { let rangeLength = range.upperBound - range.lowerBound + 1 let highlightRange = NSRange(location: range.lowerBound, length: rangeLength) - attributedTitle.addAttribute(.font, value: highlightFont, range: highlightRange) + if Range(highlightRange, in: title) != nil { + attributedTitle.addAttribute(.font, value: highlightFont, range: highlightRange) + } } self.attributedTitle = attributedTitle diff --git a/Maccy/Preview/Preview.swift b/Maccy/Preview/Preview.swift index fae837be..05255368 100644 --- a/Maccy/Preview/Preview.swift +++ b/Maccy/Preview/Preview.swift @@ -49,7 +49,7 @@ class Preview: NSViewController { applicationValueLabel.removeFromSuperview() } - if textView.stringValue.count > maxTextSize { + if textView.stringValue.count > maxTextSize { textView.stringValue = textView.stringValue.shortened(to: maxTextSize) } diff --git a/MaccyTests/HistoryMenuItemTests.swift b/MaccyTests/HistoryMenuItemTests.swift index bcaa78f5..6adcf575 100644 --- a/MaccyTests/HistoryMenuItemTests.swift +++ b/MaccyTests/HistoryMenuItemTests.swift @@ -130,6 +130,12 @@ class HistoryMenuItemTests: XCTestCase { XCTAssertEqual(menuItem.attributedTitle, nil) } + func testHighlightInvalidRange() { + let menuItem = historyMenuItem("") + menuItem.highlight([0...0]) + XCTAssertEqual(menuItem.attributedTitle?.string, "") + } + private func historyMenuItem(_ value: String?, application: String? = "com.apple.finder") -> HistoryMenuItem { let content = HistoryItemContent(type: NSPasteboard.PasteboardType.string.rawValue, value: value?.data(using: .utf8))