# scinfu/swiftsoup architecture

> Architecture diagram of the GitHub repository scinfu/swiftsoup, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/scinfu/swiftsoup
- Repository: https://github.com/scinfu/swiftsoup

## Overview

SwiftSoup is a Swift HTML/XML parsing and DOM-manipulation library. Its main workflow accepts markup through the public API, parses it into a document tree, supports CSS selection and DOM edits, then serializes output; it also provides whitelist-based sanitization. The graph preserves HTML and XML parsing as distinct branches and includes the selector and DOM layers. Relationships are grounded in the README or source-index references; some internal dispatch details are not shown in the excerpts, so those links are omitted.

Main entry points: [`Sources/Element.swift`](https://github.com/scinfu/swiftsoup/blob/master/Sources/Element.swift) (Elements and attributes), [`Sources/Document.swift`](https://github.com/scinfu/swiftsoup/blob/master/Sources/Document.swift) (Document), [`Sources/Parser.swift`](https://github.com/scinfu/swiftsoup/blob/master/Sources/Parser.swift) (Parser), [`Sources/SwiftSoup.swift`](https://github.com/scinfu/swiftsoup/blob/master/Sources/SwiftSoup.swift) (SwiftSoup API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Public API"]
  node_api["SwiftSoup API<br/>[SwiftSoup.swift]"]
end

subgraph group_parsing["Parsing"]
  node_parser["Parser<br/>[Parser.swift]"]
  node_htmlbuilder["HTML tree builder"]
  node_htmlstate["HTML builder states"]
  node_xmlbuilder["XML tree builder"]
  node_tokenizer["Tokeniser<br/>[Tokeniser.swift]"]
  node_tokenstate["Tokeniser states"]
  node_reader["Character reader"]
end

subgraph group_dom["DOM and Queries"]
  node_document["Document<br/>[Document.swift]"]
  node_node["DOM nodes<br/>[Node.swift]"]
  node_element["Elements and attributes<br/>[Element.swift]"]
  node_attributes["Attribute collections<br/>[Attributes.swift]"]
  node_queryparser["CSS query parser<br/>[QueryParser.swift]"]
  node_evaluator["Selector evaluators<br/>[Evaluator.swift]"]
  node_elements["Selection results<br/>[Elements.swift]"]
  node_tag["Tag definitions<br/>[Tag.swift]"]
  node_textnode["Text nodes<br/>[TextNode.swift]"]
end

subgraph group_output["Output and Safety"]
  node_cleaner["HTML cleaner<br/>[Cleaner.swift]"]
  node_whitelist["Safety whitelist<br/>[Whitelist.swift]"]
  node_entities["Entity encoding<br/>[Entities.swift]"]
  node_stringbuilder["String builder"]
end

node_caller(("Library caller"))

node_caller -->|"submits markup"| node_api
node_api -->|"parses input"| node_parser
node_parser -->|"selects HTML mode"| node_htmlbuilder
node_parser -->|"selects XML mode"| node_xmlbuilder
node_parser -->|"uses tokenizer"| node_tokenizer
node_htmlbuilder -->|"uses states"| node_htmlstate
node_tokenizer -->|"uses states"| node_tokenstate
node_tokenizer -->|"reads characters"| node_reader
node_htmlbuilder -.->|"builds tree"| node_document
node_xmlbuilder -.->|"builds tree"| node_document
node_parser -->|"returns document"| node_document
node_document -->|"uses node tree"| node_node
node_node -->|"supports DOM elements"| node_element
node_element -->|"reads and edits"| node_attributes
node_element -->|"uses tag metadata"| node_tag
node_element -->|"parses selectors"| node_queryparser
node_queryparser -->|"builds evaluators"| node_evaluator
node_element -->|"matches elements"| node_evaluator
node_element -->|"returns selections"| node_elements
node_document -->|"contains text nodes"| node_textnode
node_caller -->|"queries and edits"| node_element
node_caller -->|"requests serialized output"| node_api
node_document -->|"serializes output"| node_stringbuilder
node_caller -->|"requests sanitization"| node_api
node_api -->|"cleans markup"| node_cleaner
node_cleaner -.->|"applies rules"| node_whitelist
node_document -->|"encodes output"| node_entities

click node_api "https://github.com/scinfu/swiftsoup/blob/master/Sources/SwiftSoup.swift"
click node_parser "https://github.com/scinfu/swiftsoup/blob/master/Sources/Parser.swift"
click node_htmlbuilder "https://github.com/scinfu/swiftsoup/blob/master/Sources/HtmlTreeBuilder.swift"
click node_htmlstate "https://github.com/scinfu/swiftsoup/blob/master/Sources/HtmlTreeBuilderState.swift"
click node_xmlbuilder "https://github.com/scinfu/swiftsoup/blob/master/Sources/XmlTreeBuilder.swift"
click node_tokenizer "https://github.com/scinfu/swiftsoup/blob/master/Sources/Tokeniser.swift"
click node_tokenstate "https://github.com/scinfu/swiftsoup/blob/master/Sources/TokeniserState.swift"
click node_reader "https://github.com/scinfu/swiftsoup/blob/master/Sources/CharacterReader.swift"
click node_document "https://github.com/scinfu/swiftsoup/blob/master/Sources/Document.swift"
click node_node "https://github.com/scinfu/swiftsoup/blob/master/Sources/Node.swift"
click node_element "https://github.com/scinfu/swiftsoup/blob/master/Sources/Element.swift"
click node_attributes "https://github.com/scinfu/swiftsoup/blob/master/Sources/Attributes.swift"
click node_queryparser "https://github.com/scinfu/swiftsoup/blob/master/Sources/QueryParser.swift"
click node_evaluator "https://github.com/scinfu/swiftsoup/blob/master/Sources/Evaluator.swift"
click node_elements "https://github.com/scinfu/swiftsoup/blob/master/Sources/Elements.swift"
click node_tag "https://github.com/scinfu/swiftsoup/blob/master/Sources/Tag.swift"
click node_textnode "https://github.com/scinfu/swiftsoup/blob/master/Sources/TextNode.swift"
click node_cleaner "https://github.com/scinfu/swiftsoup/blob/master/Sources/Cleaner.swift"
click node_whitelist "https://github.com/scinfu/swiftsoup/blob/master/Sources/Whitelist.swift"
click node_entities "https://github.com/scinfu/swiftsoup/blob/master/Sources/Entities.swift"
click node_stringbuilder "https://github.com/scinfu/swiftsoup/blob/master/Sources/StringBuilder.swift"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api toneBlue
class node_parser,node_htmlbuilder,node_htmlstate,node_xmlbuilder,node_tokenizer,node_tokenstate,node_reader toneAmber
class node_document,node_node,node_element,node_attributes,node_queryparser,node_evaluator,node_elements,node_tag,node_textnode toneMint
class node_cleaner,node_whitelist,node_entities,node_stringbuilder toneRose
class node_caller toneIndigo
```

## Components

### Public API

- [SwiftSoup API](https://github.com/scinfu/swiftsoup/blob/master/Sources/SwiftSoup.swift): `Sources/SwiftSoup.swift`

### Parsing

- [Parser](https://github.com/scinfu/swiftsoup/blob/master/Sources/Parser.swift): `Sources/Parser.swift`
- [HTML tree builder](https://github.com/scinfu/swiftsoup/blob/master/Sources/HtmlTreeBuilder.swift): `Sources/HtmlTreeBuilder.swift`
- [HTML builder states](https://github.com/scinfu/swiftsoup/blob/master/Sources/HtmlTreeBuilderState.swift): `Sources/HtmlTreeBuilderState.swift`
- [XML tree builder](https://github.com/scinfu/swiftsoup/blob/master/Sources/XmlTreeBuilder.swift): `Sources/XmlTreeBuilder.swift`
- [Tokeniser](https://github.com/scinfu/swiftsoup/blob/master/Sources/Tokeniser.swift): `Sources/Tokeniser.swift`
- [Tokeniser states](https://github.com/scinfu/swiftsoup/blob/master/Sources/TokeniserState.swift): `Sources/TokeniserState.swift`
- [Character reader](https://github.com/scinfu/swiftsoup/blob/master/Sources/CharacterReader.swift): `Sources/CharacterReader.swift`

### DOM and Queries

- [Document](https://github.com/scinfu/swiftsoup/blob/master/Sources/Document.swift): `Sources/Document.swift`
- [DOM nodes](https://github.com/scinfu/swiftsoup/blob/master/Sources/Node.swift): `Sources/Node.swift`
- [Elements and attributes](https://github.com/scinfu/swiftsoup/blob/master/Sources/Element.swift): `Sources/Element.swift`
- [Attribute collections](https://github.com/scinfu/swiftsoup/blob/master/Sources/Attributes.swift): `Sources/Attributes.swift`
- [CSS query parser](https://github.com/scinfu/swiftsoup/blob/master/Sources/QueryParser.swift): `Sources/QueryParser.swift`
- [Selector evaluators](https://github.com/scinfu/swiftsoup/blob/master/Sources/Evaluator.swift): `Sources/Evaluator.swift`
- [Selection results](https://github.com/scinfu/swiftsoup/blob/master/Sources/Elements.swift): `Sources/Elements.swift`
- [Tag definitions](https://github.com/scinfu/swiftsoup/blob/master/Sources/Tag.swift): `Sources/Tag.swift`
- [Text nodes](https://github.com/scinfu/swiftsoup/blob/master/Sources/TextNode.swift): `Sources/TextNode.swift`

### Output and Safety

- [HTML cleaner](https://github.com/scinfu/swiftsoup/blob/master/Sources/Cleaner.swift): `Sources/Cleaner.swift`
- [Safety whitelist](https://github.com/scinfu/swiftsoup/blob/master/Sources/Whitelist.swift): `Sources/Whitelist.swift`
- [Entity encoding](https://github.com/scinfu/swiftsoup/blob/master/Sources/Entities.swift): `Sources/Entities.swift`
- [String builder](https://github.com/scinfu/swiftsoup/blob/master/Sources/StringBuilder.swift): `Sources/StringBuilder.swift`

### Other components

- **Library caller**

## Connections

- Library caller → SwiftSoup API: submits markup (evidence: `README.md`)
- SwiftSoup API → Parser: parses input (evidence: `Sources/SwiftSoup.swift`)
- Parser → HTML tree builder: selects HTML mode (evidence: `Sources/Parser.swift`)
- Parser → XML tree builder: selects XML mode (evidence: `Sources/Parser.swift`)
- Parser → Tokeniser: uses tokenizer (evidence: `Sources/Parser.swift`)
- HTML tree builder → HTML builder states: uses states (evidence: `Sources/HtmlTreeBuilderState.swift`)
- Tokeniser → Tokeniser states: uses states (evidence: `Sources/TokeniserState.swift`)
- Tokeniser → Character reader: reads characters (evidence: `Sources/TokeniserState.swift`)
- HTML tree builder → Document: builds tree
- XML tree builder → Document: builds tree
- Parser → Document: returns document (evidence: `Sources/Parser.swift`)
- Document → DOM nodes: uses node tree (evidence: `Sources/Document.swift`)
- DOM nodes → Elements and attributes: supports DOM elements (evidence: `Sources/Node.swift`)
- Elements and attributes → Attribute collections: reads and edits (evidence: `Sources/Element.swift`)
- Elements and attributes → Tag definitions: uses tag metadata (evidence: `Sources/Element.swift`)
- Elements and attributes → CSS query parser: parses selectors (evidence: `Sources/Element.swift`)
- CSS query parser → Selector evaluators: builds evaluators (evidence: `Sources/QueryParser.swift`)
- Elements and attributes → Selector evaluators: matches elements (evidence: `Sources/Evaluator.swift`)
- Elements and attributes → Selection results: returns selections (evidence: `Sources/Element.swift`)
- Document → Text nodes: contains text nodes (evidence: `Sources/Document.swift`)
- Library caller → Elements and attributes: queries and edits (evidence: `README.md`)
- Library caller → SwiftSoup API: requests serialized output (evidence: `README.md`)
- Document → String builder: serializes output (evidence: `Sources/Document.swift`)
- Library caller → SwiftSoup API: requests sanitization (evidence: `README.md`)
- SwiftSoup API → HTML cleaner: cleans markup (evidence: `Sources/SwiftSoup.swift`)
- HTML cleaner → Safety whitelist: applies rules
- Document → Entity encoding: encodes output (evidence: `Sources/Document.swift`)
