Skip to content

Commit 2dc91ff

Browse files
committed
Restrict typing attributes to allowed keys and initialize default attributes inline
This change enforces filtering of typing attributes to a defined allowed list in both AppKit and UIKit implementations. It also moves the initialization of default typing attributes to the declaration line, simplifying the init logic.
1 parent fe5feb1 commit 2dc91ff

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

Sources/STTextViewAppKit/STTextView.swift

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ import AVFoundation
135135
}
136136

137137
/// Default typing attributes used in place of missing attributes of font, color and paragraph
138-
internal var _defaultTypingAttributes: [NSAttributedString.Key: Any]
138+
internal var _defaultTypingAttributes: [NSAttributedString.Key: Any] = [
139+
.paragraphStyle: NSParagraphStyle.default,
140+
.font: NSFont.preferredFont(forTextStyle: .body),
141+
.foregroundColor: NSColor.textColor
142+
]
139143

140144
/// The attributes to apply to new text that the user enters.
141145
///
@@ -147,12 +151,38 @@ import AVFoundation
147151
}
148152

149153
set {
150-
_typingAttributes = newValue
154+
_typingAttributes = newValue.filter {
155+
_allowedTypingAttributes.contains($0.key)
156+
}
151157
needsDisplay = true
152158
}
153159
}
154160

155161
private var _typingAttributes: [NSAttributedString.Key: Any]
162+
private var _allowedTypingAttributes: [NSAttributedString.Key] = [
163+
.paragraphStyle,
164+
.font,
165+
.foregroundColor,
166+
.baselineOffset,
167+
.kern,
168+
.ligature,
169+
.shadow,
170+
.strikethroughColor,
171+
.strikethroughStyle,
172+
.superscript,
173+
.languageIdentifier,
174+
.tracking,
175+
.writingDirection,
176+
.textEffect,
177+
.accessibilityFont,
178+
.accessibilityForegroundColor,
179+
.backgroundColor,
180+
.baselineOffset,
181+
.underlineColor,
182+
.underlineStyle,
183+
.accessibilityUnderline,
184+
.accessibilityUnderlineColor
185+
]
156186

157187
internal func updateTypingAttributes(at location: NSTextLocation? = nil) {
158188
if let location {
@@ -613,12 +643,6 @@ import AVFoundation
613643
textFinder = NSTextFinder()
614644
textFinder.client = textFinderClient
615645

616-
_defaultTypingAttributes = [
617-
.paragraphStyle: NSParagraphStyle.default,
618-
.font: NSFont.preferredFont(forTextStyle: .body),
619-
.foregroundColor: NSColor.textColor
620-
]
621-
622646
_typingAttributes = [:]
623647

624648
super.init(frame: frameRect)

Sources/STTextViewUIKit/STTextView.swift

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,11 @@ import STTextViewCommon
329329
}
330330

331331
/// Default typing attributes used in place of missing attributes of font, color and paragraph
332-
internal var _defaultTypingAttributes: [NSAttributedString.Key: Any]
332+
internal var _defaultTypingAttributes: [NSAttributedString.Key: Any] = [
333+
.paragraphStyle: NSParagraphStyle.default,
334+
.font: UIFont.preferredFont(forTextStyle: .body),
335+
.foregroundColor: UIColor.label
336+
]
333337

334338
/// The attributes to apply to new text that the user enters.
335339
///
@@ -341,12 +345,33 @@ import STTextViewCommon
341345
}
342346

343347
set {
344-
_typingAttributes = newValue
348+
_typingAttributes = newValue.filter {
349+
_allowedTypingAttributes.contains($0.key)
350+
}
345351
setNeedsDisplay()
346352
}
347353
}
348354

349355
private var _typingAttributes: [NSAttributedString.Key: Any]
356+
private var _allowedTypingAttributes: [NSAttributedString.Key] = [
357+
.paragraphStyle,
358+
.font,
359+
.foregroundColor,
360+
.baselineOffset,
361+
.kern,
362+
.ligature,
363+
.shadow,
364+
.strikethroughColor,
365+
.strikethroughStyle,
366+
.languageIdentifier,
367+
.tracking,
368+
.writingDirection,
369+
.textEffect,
370+
.backgroundColor,
371+
.baselineOffset,
372+
.underlineColor,
373+
.underlineStyle
374+
]
350375

351376
internal func updateTypingAttributes(at location: NSTextLocation? = nil) {
352377
if let location {
@@ -436,12 +461,6 @@ import STTextViewCommon
436461
allowsUndo = true
437462
_undoManager = CoalescingUndoManager()
438463

439-
_defaultTypingAttributes = [
440-
.paragraphStyle: NSParagraphStyle.default,
441-
.font: UIFont.preferredFont(forTextStyle: .body),
442-
.foregroundColor: UIColor.label
443-
]
444-
445464
_typingAttributes = [:]
446465

447466
super.init(frame: frame)

0 commit comments

Comments
 (0)