|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +/// A set of one or more inline attributes. |
| 12 | +public struct InlineAttributes: InlineMarkup, InlineContainer { |
| 13 | + public var _data: _MarkupData |
| 14 | + |
| 15 | + init(_ raw: RawMarkup) throws { |
| 16 | + guard case .inlineAttributes = raw.data else { |
| 17 | + throw RawMarkup.Error.concreteConversionError(from: raw, to: InlineAttributes.self) |
| 18 | + } |
| 19 | + let absoluteRaw = AbsoluteRawMarkup(markup: raw, metadata: MarkupMetadata(id: .newRoot(), indexInParent: 0)) |
| 20 | + self.init(_MarkupData(absoluteRaw)) |
| 21 | + } |
| 22 | + |
| 23 | + init(_ data: _MarkupData) { |
| 24 | + self._data = data |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// MARK: - Public API |
| 29 | + |
| 30 | +public extension InlineAttributes { |
| 31 | + /// Create a set of custom inline attributes applied to zero or more child inline elements. |
| 32 | + init<Children: Sequence>(attributes: String, _ children: Children) where Children.Element == RecurringInlineMarkup { |
| 33 | + try! self.init(.inlineAttributes(attributes: attributes, parsedRange: nil, children.map { $0.raw.markup })) |
| 34 | + } |
| 35 | + |
| 36 | + /// Create a set of custom attributes applied to zero or more child inline elements. |
| 37 | + init(attributes: String, _ children: RecurringInlineMarkup...) { |
| 38 | + self.init(attributes: attributes, children) |
| 39 | + } |
| 40 | + |
| 41 | + /// The specified attributes in JSON5 format. |
| 42 | + var attributes: String { |
| 43 | + get { |
| 44 | + guard case let .inlineAttributes(attributes) = _data.raw.markup.data else { |
| 45 | + fatalError("\(self) markup wrapped unexpected \(_data.raw)") |
| 46 | + } |
| 47 | + return attributes |
| 48 | + } |
| 49 | + set { |
| 50 | + _data = _data.replacingSelf(.inlineAttributes(attributes: newValue, parsedRange: nil, _data.raw.markup.copyChildren())) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // MARK: Visitation |
| 55 | + |
| 56 | + func accept<V: MarkupVisitor>(_ visitor: inout V) -> V.Result { |
| 57 | + return visitor.visitInlineAttributes(self) |
| 58 | + } |
| 59 | +} |
0 commit comments