Merge pull request #9 from JohnSundell/html-escaping

HTMLOutputFormat: Escape < and > characters
This commit is contained in:
John Sundell 2018-08-27 00:22:42 +02:00 committed by GitHub
commit b61a641be7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 16 deletions

View File

@ -33,11 +33,11 @@ public extension HTMLOutputFormat {
}
public mutating func addToken(_ token: String, ofType type: TokenType) {
html.append("<span class=\"\(classPrefix)\(type.rawValue)\">\(token)</span>")
html.append("<span class=\"\(classPrefix)\(type.rawValue)\">\(token.escaped)</span>")
}
public mutating func addPlainText(_ text: String) {
html.append(text)
html.append(text.escaped)
}
public mutating func addWhitespace(_ whitespace: String) {
@ -49,3 +49,10 @@ public extension HTMLOutputFormat {
}
}
}
private extension String {
var escaped: String {
return replacingOccurrences(of: "<", with: "&lt;")
.replacingOccurrences(of: ">", with: "&gt;")
}
}

View File

@ -0,0 +1,22 @@
import Foundation
import XCTest
/// Abstract base class for all Splash tests
class SplashTestCase: XCTestCase {
#if !os(Linux)
func testHasLinuxVerificationTest() {
let concreteType = type(of: self)
guard concreteType != SyntaxHighlighterTestCase.self else {
return
}
guard concreteType != SplashTestCase.self else {
return
}
XCTAssertTrue(concreteType.testNames.contains("testAllTestsRunOnLinux"),
"\(concreteType) doesn't have a test that verifies that its tests run on Linux")
}
#endif
}

View File

@ -10,7 +10,7 @@ import Splash
/// Test case used as an abstract base class for all tests relating to
/// syntax highlighting. For all such tests, the Swift grammar is used.
class SyntaxHighlighterTestCase: XCTestCase {
class SyntaxHighlighterTestCase: SplashTestCase {
private(set) var highlighter: SyntaxHighlighter<OutputFormatMock>!
private(set) var builder: OutputBuilderMock!
@ -19,17 +19,4 @@ class SyntaxHighlighterTestCase: XCTestCase {
builder = OutputBuilderMock()
highlighter = SyntaxHighlighter(format: OutputFormatMock(builder: builder))
}
#if os(macOS)
func testHasLinuxVerificationTest() {
let concreteType = type(of: self)
guard concreteType != SyntaxHighlighterTestCase.self else {
return
}
XCTAssertTrue(concreteType.testNames.contains("testAllTestsRunOnLinux"),
"All test cases should have a test that verify that their tests run on Linux")
}
#endif
}

View File

@ -0,0 +1,32 @@
import Foundation
import XCTest
import Splash
final class HTMLOutputFormatTests: SplashTestCase {
private var highlighter: SyntaxHighlighter<HTMLOutputFormat>!
override func setUp() {
super.setUp()
highlighter = SyntaxHighlighter(format: HTMLOutputFormat())
}
func testStrippingGreaterAndLessThanCharactersFromOutput() {
let html = highlighter.highlight("Array<String>")
XCTAssertEqual(html, """
<span class="type">Array</span>&lt;<span class="type">String</span>&gt;
""")
}
func testAllTestsRunOnLinux() {
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
}
}
extension HTMLOutputFormatTests {
static var allTests: [(String, TestClosure<HTMLOutputFormatTests>)] {
return [
("testStrippingGreaterAndLessThanCharactersFromOutput", testStrippingGreaterAndLessThanCharactersFromOutput)
]
}
}