1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// MARK: AttributedStringKey API
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
extension AttributedString {
public enum AttributeRunBoundaries : Hashable, Sendable {
case paragraph
// FIXME: This is semantically wrong. We do not ever want to constrain attributes on
// characters (i.e., grapheme clusters) -- they are way too vague, and way
// too eager to accidentally merge with neighboring string data. (And they're also way
// too slow for this use case.)
//
// The entire point of this feature is to anchor attributes that describe attachments like
// custom views that should be embedded in the text. We do not _ever_ want the anchor text
// to accidentally compose with a subsequent combining character, losing the attachment.
//
// This needs to be deprecated and replaced by `case unicodeScalar(UnicodeScalar)`.
//
// The current implementation already works like that -- it ignores all but the first scalar
// of the specified `Character`, and does not engage in normalization or grapheme breaking.
case character(Character)
}
}
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
extension AttributedString.AttributeRunBoundaries {
var _isScalarConstrained: Bool {
if case .character = self { return true }
return false
}
var _constrainedScalar: Unicode.Scalar? {
switch self {
case .character(let char): return char.unicodeScalars.first
default: return nil
}
}
}
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
extension AttributedString {
public struct AttributeInvalidationCondition : Hashable, Sendable {
private enum _Storage : Hashable {
case textChanged
case attributeChanged(String)
}
private let storage: _Storage
private init(_ storage: _Storage) {
self.storage = storage
}
var isAttribute: Bool {
guard case .attributeChanged = storage else { return false }
return true
}
var attributeKey: String? {
switch storage {
case .textChanged:
return nil
case .attributeChanged(let string):
return string
}
}
public static let textChanged = Self(.textChanged)
public static func attributeChanged<T: AttributedStringKey>(_ key: T.Type) -> Self {
Self(.attributeChanged(key.name))
}
public static func attributeChanged<T: AttributedStringKey>(_ key: KeyPath<AttributeDynamicLookup, T>) -> Self {
Self(.attributeChanged(T.name))
}
static func attributeChanged(_ name: String) -> Self {
Self(.attributeChanged(name))
}
}
}
// Developers define new attributes by implementing AttributeKey.
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
public protocol AttributedStringKey {
associatedtype Value : Hashable
static var name : String { get }
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
static var runBoundaries : AttributedString.AttributeRunBoundaries? { get }
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
static var inheritedByAddedText : Bool { get }
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
static var invalidationConditions : Set<AttributedString.AttributeInvalidationCondition>? { get }
}
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
extension AttributedStringKey {
public var description: String { Self.name }
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public static var runBoundaries : AttributedString.AttributeRunBoundaries? { nil }
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public static var inheritedByAddedText : Bool { true }
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public static var invalidationConditions : Set<AttributedString.AttributeInvalidationCondition>? { nil }
}
extension AttributedStringKey {
// FIXME: ☠️ Allocating an Array here is not a good idea.
static var _constraintsInvolved: [AttributedString.AttributeRunBoundaries] {
guard let rb = runBoundaries else { return [] }
return [rb]
}
}
// MARK: Attribute Scopes
@_nonSendable
@dynamicMemberLookup @frozen
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
public enum AttributeDynamicLookup {
public subscript<T: AttributedStringKey>(_: T.Type) -> T {
get { fatalError("Called outside of a dynamicMemberLookup subscript overload") }
}
}
@dynamicMemberLookup
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
public struct ScopedAttributeContainer<S: AttributeScope> : Sendable {
internal var storage : AttributedString._AttributeStorage
// Record the most recently deleted key for use in AttributedString mutation subscripts that use _modify
// Note: if ScopedAttributeContainer ever adds a mutating function that can mutate multiple attributes, this will need to record multiple removed keys
internal var removedKey : String?
@preconcurrency
public subscript<T: AttributedStringKey>(dynamicMember keyPath: KeyPath<S, T>) -> T.Value? where T.Value : Sendable {
get { storage[T.self] }
set {
storage[T.self] = newValue
if newValue == nil {
removedKey = T.name
}
}
}
internal init(_ storage : AttributedString._AttributeStorage = .init()) {
self.storage = storage
}
#if FOUNDATION_FRAMEWORK
// TODO: Support scope-specific equality/attributes in FoundationPreview
internal func equals(_ other: Self) -> Bool {
for (name, _) in S.attributeKeyTypes() {
if self.storage[name] != other.storage[name] {
return false
}
}
return true
}
internal var attributes : AttributeContainer {
var contents = AttributedString._AttributeStorage()
for (name, _) in S.attributeKeyTypes() {
contents[name] = self.storage[name]
}
return AttributeContainer(contents)
}
#endif // FOUNDATION_FRAMEWORK
}
// MARK: Internals
#if FOUNDATION_FRAMEWORK
internal extension AttributedStringKey {
static func _convertToObjectiveCValue(_ value: Value) throws -> AnyObject {
if let convertibleType = Self.self as? any ObjectiveCConvertibleAttributedStringKey.Type {
func project<K: ObjectiveCConvertibleAttributedStringKey>(_: K.Type) throws -> AnyObject {
try K.objectiveCValue(for: value as! K.Value)
}
return try project(convertibleType)
} else {
return value as AnyObject
}
}
static func _convertFromObjectiveCValue(_ value: AnyObject) throws -> Value {
if let convertibleType = Self.self as? any ObjectiveCConvertibleAttributedStringKey.Type {
func project<K: ObjectiveCConvertibleAttributedStringKey>(_: K.Type) throws -> Value {
guard let objcValue = value as? K.ObjectiveCValue else {
throw CocoaError(.coderInvalidValue)
}
return try K.value(for: objcValue) as! Value
}
return try project(convertibleType)
} else if let trueValue = value as? Value {
return trueValue
} else {
throw CocoaError(.coderInvalidValue)
}
}
}
#endif // FOUNDATION_FRAMEWORK
|