Skip to content

Commit d74887a

Browse files
authoredNov 8, 2024
Revert "Temporarily avoid using FoundationXML to unblock CI (#1090)"
This reverts commit 6ace3ef.
1 parent 3b39d07 commit d74887a

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2022-2024 Apple Inc. and the Swift project authors
4+
Copyright (c) 2022 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -10,6 +10,12 @@
1010

1111
import Foundation
1212

13+
// On non-Darwin platforms, Foundation's XML support is vended as a separate module:
14+
// https://github.com/apple/swift-corelibs-foundation/blob/main/Docs/ReleaseNotes_Swift5.md#dependency-management
15+
#if canImport(FoundationXML)
16+
import FoundationXML
17+
#endif
18+
1319
/// A basic XML parser that extracts the first `id` attribute found in the given SVG.
1420
///
1521
/// This is a single-purpose tool and should not be used for general-purpose SVG parsing.
@@ -19,19 +25,14 @@ enum SVGIDExtractor {
1925
/// Exposed for testing. The sibling `extractID(from: URL)` method is intended to be
2026
/// used within SwiftDocC.
2127
static func _extractID(from data: Data) -> String? {
22-
// FIXME: Revert this and resume using XMLParser when rdar://138726860 is integrated into a Swift toolchain.
23-
for capitalization in ["id", "ID", "Id", "iD"] {
24-
guard let idAttributeRange = data.firstRange(of: Data(" \(capitalization)=\"".utf8), in: data.indices) else {
25-
continue
26-
}
27-
28-
guard let endQuote = data.firstRange(of: Data("\"".utf8), in: idAttributeRange.upperBound...) else {
29-
continue
30-
}
31-
32-
return String(data: data[idAttributeRange.endIndex ..< endQuote.lowerBound], encoding: .utf8)
33-
}
34-
return nil
28+
let delegate = SVGIDParserDelegate()
29+
let svgParser = XMLParser(data: data)
30+
svgParser.delegate = delegate
31+
32+
// The delegate aborts the parsing when it finds the ID so the larger parsing operation is not "successful"
33+
_ = svgParser.parse()
34+
35+
return delegate.id
3536
}
3637

3738
/// Returns the first `id` attribute found in the given SVG, if any.
@@ -46,3 +47,22 @@ enum SVGIDExtractor {
4647
return _extractID(from: data)
4748
}
4849
}
50+
51+
private class SVGIDParserDelegate: NSObject, XMLParserDelegate {
52+
var id: String?
53+
54+
func parser(
55+
_ parser: XMLParser,
56+
didStartElement elementName: String,
57+
namespaceURI: String?,
58+
qualifiedName qName: String?,
59+
attributes attributeDict: [String : String] = [:]
60+
) {
61+
guard let id = attributeDict["id"] ?? attributeDict["ID"] ?? attributeDict["iD"] ?? attributeDict["Id"] else {
62+
return
63+
}
64+
65+
self.id = id
66+
parser.abortParsing()
67+
}
68+
}

‎Sources/SwiftDocC/Model/Rendering/Variants/JSONPatchOperation.swift

-9
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,6 @@ public enum JSONPatchOperation: Codable {
7474
/// - variantPatchOperation: The patch to apply.
7575
/// - pointer: The pointer to the value to update.
7676
public init<Value>(variantPatchOperation: VariantPatchOperation<Value>, pointer: JSONPointer) {
77-
// FIXME: The latest Swift development snapshots (2024-10-30-a and later) is missing the "references" path component. (rdar://139446585)
78-
// AFAICT it's the only path that's missing components, so we're working around that issue here.
79-
// Since the only RenderNode coding paths that have include a topic reference (as its 2nd component) are
80-
// modifications of topics in the "references" section, we can detect and workaround this issue by checking for a "doc://" prefix.
81-
var pointer = pointer
82-
if pointer.pathComponents.first?.hasPrefix("doc://") == true {
83-
pointer = pointer.prependingPathComponents(["references"])
84-
}
85-
8677
switch variantPatchOperation {
8778
case .replace(let value):
8879
self = .replace(pointer: pointer, encodableValue: value)

‎Tests/SwiftDocCTests/Infrastructure/SVGIDExtractorTests.swift

-13
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,6 @@ class SVGIDExtractorTests: XCTestCase {
2525
XCTAssertEqual(id, "plus-id")
2626
}
2727

28-
do {
29-
let id = extractIDFromSVG(
30-
"""
31-
<?xml version="1.0" encoding="UTF-8"?>
32-
<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" id="plus-id">
33-
<path d="M9.99 0c-5.461 0-9.99 4.539-9.99 10s4.539 10 10 10 10-4.53 10-10-4.539-10-10.010-10zM14.079 10.863h-3.226v3.049c0 0.589-0.323 0.97-0.873 0.97-0.559 0-0.853-0.401-0.853-0.97v-3.049h-3.216c-0.579 0-0.98-0.304-0.98-0.843 0-0.559 0.383-0.873 0.98-0.873h3.216v-3.246c0-0.569 0.294-0.97 0.853-0.97 0.549 0 0.873 0.383 0.873 0.97v3.246h3.226c0.599 0 0.97 0.314 0.97 0.873 0 0.539-0.391 0.843-0.97 0.843z"></path>
34-
</svg>
35-
"""
36-
)
37-
38-
XCTAssertEqual(id, "plus-id")
39-
}
40-
4128
do {
4229
let id = extractIDFromSVG(
4330
"""

0 commit comments

Comments
 (0)