Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SP-8330 appletv html crash #352

Merged
merged 6 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions ConsentViewController/Classes/Extensions/SPString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import Foundation

extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return nil }
guard let data = self.stripOutHtml()?.data(using: .utf8) else { return nil }
do {
return try NSAttributedString(
data: data,
options: [
.documentType: NSAttributedString.DocumentType.html,
.documentType: NSAttributedString.DocumentType.plain,
.characterEncoding: String.Encoding.utf8.rawValue
],
documentAttributes: nil
Expand All @@ -24,4 +24,13 @@ extension String {
}
}
var htmlToString: String { htmlToAttributedString?.string ?? "" }
func stripOutHtml() -> String? {
// replace &lt; with < and &gt; with >
let decoded = self.replacingOccurrences(of: "&lt;", with: "<").replacingOccurrences(of: "&gt;", with: ">")
// removes any CSS inline styling
let noCSS = decoded.replacingOccurrences(of: "<style>[^>]+</style>", with: "", options: .regularExpression, range: nil)
// removes any HTML tags
let noHTML = noCSS.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
return noHTML
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// SPString.swift
// ConsentViewController_ExampleTests
//
// Created by Dmytro Fedko on 17.12.2021.
// Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import Quick
import Nimble
@testable import ConsentViewController

class SPStringExtensionsSpec: QuickSpec {
override func spec() {
describe("stripOutHtml") {
it("should strip any HTML and CSS markup tags from string") {
let cssHtml = """
<head>
<title>HTML - head tag</title>
<strong><i><b>strongbolditalic</b></i></strong>
<meta name="author" content="john smith">
<style>body {color: black}</style>
<link rel="stylesheet" href="stylesheet.css">
<base href="https://www.tutorialstonight.com">
<script src="script.js"></script>
<noscript>Your browser does not support javascript.</noscript>
</head>
"""
let stripped = cssHtml.stripOutHtml()
expect(stripped).to(equal("\nHTML - head tag\nstrongbolditalic\n\n\n\n\n\nYour browser does not support javascript.\n"))
}
}
}
}