Skip to content

Conversation

@d-ronnqvist
Copy link
Contributor

Bug/issue #, if applicable:

Summary

This is a follow up change from #1369

It adds tests for how the DocCHTML/MarkdownRenderer processes inline HTML—skipping any comments while preserving the rest. With these tests in place, I could refactor the implementation with more confidence and extract the inner loop into a private function.

Dependencies

None.

Testing

Nothing in particular for this PR. It only adds tests and makes minor code cleanup to functionality that is only exercised in tests so far. See #1366 for how it eventually does get used.

Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

  • Added tests
  • Ran the ./bin/test script and it succeeded
  • Updated documentation if necessary

@d-ronnqvist
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@patshaughnessy patshaughnessy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks much better now - thanks for refactoring this! With the new comments and extracted functions it's much easier for me to follow the algorithm 👍


// On non-Darwin platforms, `XMLElement(xmlString:)` sometimes crashes for certain invalid / incomplete XML strings.
// To minimize the risk of this happening, don't try to parse the XML string as an empty HTML element unless it ends with "/>"
if rawHTML.hasSuffix("/>"), let parsed = try? XMLElement(xmlString: rawHTML) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you run into some crashes while running your new tests in Linux CI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the Linux CI doesn't encounter any crashes with these changes. Locally, a Swift 6.0.3 container also didn't have any issues with the implementation in either 84dbf48 or e540ba3.

let element = elements.removeFirst()

guard let start = element as? InlineHTML else {
var remainder = Array(container)[...]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're using a slice here, I imagine this is quite efficient, even though we are mutating remainder.

Copy link
Contributor Author

@d-ronnqvist d-ronnqvist Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because a slice is a portion of a buffer that's owned by another object, mutating the slice can be thought of as updating the start and end indices, which is very cheap.

If you use the Godbolt Compiler Explorer you can see that these two implementations produce rather comparable assembly code:

// Iterating over an array
func iterate(over numbers: [Int], doing work: (Int) -> Void) {
    for number in numbers {
        work(number)
    }
}
// Mutating a slice by repeatedly popping the first element
func iterate(over numbers: [Int], doing work: (Int) -> Void) {
    var remainder = numbers[...]
    while let number = remainder.popFirst() {
        work(number)
    }
}

Further; if the "work" is known to the compiler, like in the comparison below, then the Swift compiler is more likely to be able to optimize away those extra retain and release calls:

// Iterating over an array
func sum(numbers: [Int]) -> Int {
    var total = 0
    for number in numbers {
        total += number
    }
    return total
}
// Mutating a slice by repeatedly popping the first element
func sum(numbers: [Int]) -> Int {
    var total = 0
    var remainder = numbers[...]
    while let number = remainder.popFirst() {
        total += number
    }
    return total
}

d-ronnqvist and others added 2 commits December 8, 2025 10:53
@d-ronnqvist
Copy link
Contributor Author

@swift-ci please test

@d-ronnqvist d-ronnqvist merged commit f561a86 into swiftlang:main Dec 8, 2025
2 checks passed
@d-ronnqvist d-ronnqvist deleted the output-html-parse-inline branch December 8, 2025 10:13
d-ronnqvist added a commit to d-ronnqvist/swift-docc that referenced this pull request Dec 17, 2025
…wiftlang#1379)

* Add test about parsing inline HTML except for comments

* Extract inner HTML parsing code into a private function

* Fix minor spelling in code comment

Co-authored-by: Pat Shaughnessy <pat_shaughnessy@apple.com>

---------

Co-authored-by: Pat Shaughnessy <pat_shaughnessy@apple.com>
d-ronnqvist added a commit that referenced this pull request Dec 17, 2025
…1402)

* Add a new target for rendering Markdown content into static HTML (#1369)

* Add a new target for rendering content into static HTML

rdar://163326857

* Include documentation comments with examples for the various `visit(_:)` methods.

* Document what a `LinkedAsset` represents

* Add code comments to explain how the inline HTML markup is parsed

* Don't lowercase anchors for self-referencing headings

* Avoid nested scopes when switching over names

* Add a code comment about what an "autolink" is

* Add a helper function for rendering availability information as HTML (#1376)

rdar://163326857

* Add a helper function for rendering page breadcrumbs as HTML (#1378)

* Add a helper function for rendering page breadcrumbs as HTML

rdar://163326857

* Apply wording suggestion in documentation comment

Co-authored-by: Joseph Heck <j_heck@apple.com>

---------

Co-authored-by: Joseph Heck <j_heck@apple.com>

* Add a helper function for rendering a parameters section as HTML (#1377)

* Add a helper function for rendering a parameters section as HTML

rdar://163326857

* Apply wording suggestions in documentation comments

Co-authored-by: Pat Shaughnessy <pat_shaughnessy@apple.com>
Co-authored-by: Joseph Heck <j_heck@apple.com>

* Add TODO comment about a debug assertion for the parameter name order

---------

Co-authored-by: Pat Shaughnessy <pat_shaughnessy@apple.com>
Co-authored-by: Joseph Heck <j_heck@apple.com>

* Add tests for parsing of inline HTML when rendering markdown as HTML (#1379)

* Add test about parsing inline HTML except for comments

* Extract inner HTML parsing code into a private function

* Fix minor spelling in code comment

Co-authored-by: Pat Shaughnessy <pat_shaughnessy@apple.com>

---------

Co-authored-by: Pat Shaughnessy <pat_shaughnessy@apple.com>

* Add a helper function for rendering a returns sections as HTML (#1381)

* Add a helper function for rendering a returns sections as HTML

rdar://163326857

* Minor phrasing updates to the parameter section helper's documentation

* Add new source file to Windows CMake file list

* Correct grammar in documentation comments

Co-authored-by: Pat Shaughnessy <pat_shaughnessy@apple.com>

---------

Co-authored-by: Pat Shaughnessy <pat_shaughnessy@apple.com>

* Add a helper function for rendering symbol declarations as HTML (#1384)

* Add a helper function for rendering symbol declarations as HTML

rdar://163326857

* Add code comments to describe why the concise declaration:
  - Only includes the primary language
  - Joins the declaration fragments into a single string

* Add a helper function for rendering a Topics/SeeAlso section as HTML (#1382)

* Add a helper function for rendering a Topics/SeeAlso section as HTML

rdar://163326857

* Add more code comments and internal documentation comments

* Very slightly correct the test data

* Minimally integrate per-page HTML content into each "index.html" file (#1383)

* Minimally integrate per-page HTML content into each "index.html" file

rdar://163326857

* Extract some common code into private helpers

* Move a few common checks into the new private helpers

* Add a helper function for rendering a discussion section as HTML (#1388)

* Add a helper function for rendering a discussion section as HTML

rdar://163326857

* Integrate the discussion section in the HTML renderer

* Include Mentioned In and Relationships sections in the HTML output (#1391)

* Include more content in the HTML output (#1390)

* Include more page content in the HTML output

* Also add deprecation summaries to the HTML output

---------

Co-authored-by: Joseph Heck <j_heck@apple.com>
Co-authored-by: Pat Shaughnessy <pat_shaughnessy@apple.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants