Swiftpack.co - Package - ucotta/BrilliantHTML5Parser

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: https://github.com/IBM-Swift/swift-html-entities.git This package will be replaced by the OWASP XSS Prevention Cheat Sheet.


  • Add XSS prevention cheat sheet by OWASP: https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet


First step: create a new directory with this files:


import PackageDescription

let package = Package(
    name: "MyTest",
    targets: [],
    dependencies: [
        .Package(url: "https://github.com/ucotta/BrilliantHTML5Parser.git", 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 = "https://raw.githubusercontent.com/ucotta/BrilliantHTML5Parser/master/examples/index.html"
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:



Stars: 0
Help us keep the lights on

Used By

Total: 1


0.95.10 - Oct 13, 2017

Fixed the use of some now obsolete methods. Fixed an error that breaks the application when processing an unclosed comment in an HTML file.

0.90.0 - Jan 9, 2017

0.87.0 - Nov 9, 2016

0.82 - Nov 8, 2016