Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Temporarily avoid using FoundationXML to unblock CI" #1092

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions Sources/SwiftDocC/Infrastructure/Bundle Assets/SVGIDExtractor.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2022-2024 Apple Inc. and the Swift project authors
Copyright (c) 2022 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
Expand All @@ -10,6 +10,12 @@

import Foundation

// On non-Darwin platforms, Foundation's XML support is vended as a separate module:
// https://github.com/apple/swift-corelibs-foundation/blob/main/Docs/ReleaseNotes_Swift5.md#dependency-management
#if canImport(FoundationXML)
import FoundationXML
#endif

/// A basic XML parser that extracts the first `id` attribute found in the given SVG.
///
/// This is a single-purpose tool and should not be used for general-purpose SVG parsing.
Expand All @@ -19,19 +25,14 @@ enum SVGIDExtractor {
/// Exposed for testing. The sibling `extractID(from: URL)` method is intended to be
/// used within SwiftDocC.
static func _extractID(from data: Data) -> String? {
// FIXME: Revert this and resume using XMLParser when rdar://138726860 is integrated into a Swift toolchain.
for capitalization in ["id", "ID", "Id", "iD"] {
guard let idAttributeRange = data.firstRange(of: Data(" \(capitalization)=\"".utf8), in: data.indices) else {
continue
}

guard let endQuote = data.firstRange(of: Data("\"".utf8), in: idAttributeRange.upperBound...) else {
continue
}

return String(data: data[idAttributeRange.endIndex ..< endQuote.lowerBound], encoding: .utf8)
}
return nil
let delegate = SVGIDParserDelegate()
let svgParser = XMLParser(data: data)
svgParser.delegate = delegate

// The delegate aborts the parsing when it finds the ID so the larger parsing operation is not "successful"
_ = svgParser.parse()

return delegate.id
}

/// Returns the first `id` attribute found in the given SVG, if any.
Expand All @@ -46,3 +47,22 @@ enum SVGIDExtractor {
return _extractID(from: data)
}
}

private class SVGIDParserDelegate: NSObject, XMLParserDelegate {
var id: String?

func parser(
_ parser: XMLParser,
didStartElement elementName: String,
namespaceURI: String?,
qualifiedName qName: String?,
attributes attributeDict: [String : String] = [:]
) {
guard let id = attributeDict["id"] ?? attributeDict["ID"] ?? attributeDict["iD"] ?? attributeDict["Id"] else {
return
}

self.id = id
parser.abortParsing()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,6 @@ public enum JSONPatchOperation: Codable {
/// - variantPatchOperation: The patch to apply.
/// - pointer: The pointer to the value to update.
public init<Value>(variantPatchOperation: VariantPatchOperation<Value>, pointer: JSONPointer) {
// FIXME: The latest Swift development snapshots (2024-10-30-a and later) is missing the "references" path component. (rdar://139446585)
// AFAICT it's the only path that's missing components, so we're working around that issue here.
// Since the only RenderNode coding paths that have include a topic reference (as its 2nd component) are
// modifications of topics in the "references" section, we can detect and workaround this issue by checking for a "doc://" prefix.
var pointer = pointer
if pointer.pathComponents.first?.hasPrefix("doc://") == true {
pointer = pointer.prependingPathComponents(["references"])
}

switch variantPatchOperation {
case .replace(let value):
self = .replace(pointer: pointer, encodableValue: value)
Expand Down
13 changes: 0 additions & 13 deletions Tests/SwiftDocCTests/Infrastructure/SVGIDExtractorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ class SVGIDExtractorTests: XCTestCase {
XCTAssertEqual(id, "plus-id")
}

do {
let id = extractIDFromSVG(
"""
<?xml version="1.0" encoding="UTF-8"?>
<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" id="plus-id">
<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>
</svg>
"""
)

XCTAssertEqual(id, "plus-id")
}

do {
let id = extractIDFromSVG(
"""
Expand Down