An HTML 5 processor to parse, edit or create HTML5 files in Swift. Designed to be used together with BrilliantTemplate

What is this?

This package was designed to parse HTML 5 files for Brilliant Template. However, it can also be used independently.

Attention, this package uses IBM HTML Entities: This package will be replaced by the OWASP XSS Prevention Cheat Sheet.



First step: create a new directory with this files:


import PackageDescription

let package = Package(
    name: "MyTest",
    targets: [],
    dependencies: [
        .Package(url: "", majorVersion: 0)
    exclude: []


import Foundation

import BrilliantHTML5Parser

var data: [String: Any] = [
    "title": "This is the title",
    "welcome": "this is your first example!!",
    "more": "and you can do a lot of stuff",
    "buttonColor": "btn-blue btn-sm btn"

let URLINDEX = ""
if let url = URL(string: URLINDEX) {
    var html = try String(contentsOf: url)
    print("Input HTML:")

    let parser = ParserHTML5(html: html)
    while let item = parser.root.getNextTid() {
        if let val = data[item["tid"]!] {
            item.addNode(node: TextHTML(text: val as! String))
        item["tid"] = nil

    while let item = parser.root.getNextAid() {
        if let val = data[item["aid"]!] {
            item["class"] = val as! String
        item["aid"] = nil

Second step: build the project

swift package generate-xcodeproj 

Open the Xcode project, select the correct scheme and run!


Create an HTML5 from scratch

let parser = ParserHTML5()

let html = TagHTML(tagName: "html")
let head = TagHTML(tagName: "head")
let body = TagHTML(tagName: "body")
let div = TagHTML(tagName: "div")
div["dupl"] = "3"

html["lang"] = "en"
head.addNode(node: TagHTML(tagName: "title", content: "Welcome title!!"))
body.addNode(node: TagHTML(tagName: "h1", content: "Welcome!"))
body.addNode(node: div)

// Add nodes
html.addNode(node: head)
html.addNode(node: body)
parser.root.addNode(node: html)

// Now print result.

You will get this (but you won't see it tabbed)

<!DOCTYPE html>
<html lang="en">
        <title>Welcome title!!</title>
    <div dupl="3"></div>

If you'd like to have a tabbed result, please feel free to request this feature in ISSUES.

Parsing an HTML5 and commenting a div

let parser = ParserHTML5(html: "somehtmlhere with a div with attribute dupl")

if let div = parser.root.getNextNodeWithAtt(att: "dupl") {
    // first comment the div with att dupl=3
    div.parentNode?.addNode(node: CommentHTML(comment: div.toHTML))
    // Delete the node.
    div.parentNode = nil
} else {
    print("The HTML doesnt contains a div with an attribute named *dupl*") 

More examples in testing unit:



  • Swift Tools 4.0.0
View More Packages from this Author


Last updated: Sat Nov 25 2023 22:12:45 GMT-1000 (Hawaii-Aleutian Standard Time)