Start work on HTMLEntityDecoder port.
This commit is contained in:
parent
96cbc51d26
commit
be09d2cda2
51
Modules/Parser/Sources/SAX/HTMLEntityDecoder.swift
Normal file
51
Modules/Parser/Sources/SAX/HTMLEntityDecoder.swift
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
//
|
||||||
|
// HTMLEntityDecoder.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Brent Simmons on 9/14/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
public final class HTMLEntityDecoder {
|
||||||
|
|
||||||
|
static func decodedString(withEncodedString encodedString: String) -> String {
|
||||||
|
|
||||||
|
let scanner = Scanner(string: encodedString)
|
||||||
|
scanner.charactersToBeSkipped = nil
|
||||||
|
var result = ""
|
||||||
|
var didDecodeAtLeastOneEntity = false
|
||||||
|
|
||||||
|
while true {
|
||||||
|
|
||||||
|
var scannedString = nil
|
||||||
|
if scanner.scanUpToString("&" intoString: &scannedString) {
|
||||||
|
result.append(scannedString)
|
||||||
|
}
|
||||||
|
if scanner.isAtEnd {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
let savedScanLocation = scanner.scanLocation
|
||||||
|
|
||||||
|
var decodedEntity: String? = nil
|
||||||
|
if scanner.scanEntityValue(&decodedEntity) {
|
||||||
|
result.append(decodedEntity)
|
||||||
|
didDecodeAtLeastOneEntity = true
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.append("&")
|
||||||
|
scanner.scanLocation = savedScanLocation + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if scanner.isAtEnd {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !didDecodeAtLeastOneEntity { // No changes made?
|
||||||
|
return encodedString
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user