-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTML escape matching & sorting quizzes (#492)
* Remove unused * Add String+HTMLEscape * Add HTML-escaping for MatchingQuizViewController * Remove unused * Add HTML-escaping for SortingQuizViewController
- Loading branch information
1 parent
dd69b71
commit d071d1c
Showing
4 changed files
with
66 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Foundation | ||
|
||
extension String { | ||
private static let requiredEscapes: Set<Character> = ["<", ">"] | ||
private static let escapeMap: [Character: String] = [ | ||
"<": "lt", | ||
">": "gt" | ||
] | ||
|
||
func addingHTMLEntities() -> String { | ||
var copy = self | ||
copy.addHTMLEntities() | ||
return copy | ||
} | ||
|
||
mutating func addHTMLEntities() { | ||
var position: String.Index? = self.startIndex | ||
|
||
while let cursorPosition = position { | ||
guard cursorPosition != self.endIndex else { | ||
break | ||
} | ||
|
||
let character = self[cursorPosition] | ||
|
||
if String.requiredEscapes.contains(character), let entity = String.escapeMap[character] { | ||
let escape = "&\(entity);" | ||
position = self.positionAfterReplacingCharacter(at: cursorPosition, with: escape) | ||
} else { | ||
position = self.index(cursorPosition, offsetBy: 1, limitedBy: self.endIndex) | ||
} | ||
} | ||
} | ||
|
||
/// Replaces the character at the given position with the escape and returns the new position. | ||
private mutating func positionAfterReplacingCharacter( | ||
at position: String.Index, | ||
with escape: String | ||
) -> String.Index? { | ||
let nextIndex = self.index(position, offsetBy: 1) | ||
|
||
if let fittingPosition = self.index(position, offsetBy: escape.count, limitedBy: self.endIndex) { | ||
// Check if we can fit the whole escape in the receiver | ||
self.replaceSubrange(position..<nextIndex, with: escape) | ||
return fittingPosition | ||
} else { | ||
// If we can't, remove the character and insert the escape to make it fit. | ||
self.remove(at: position) | ||
self.insert(contentsOf: escape, at: position) | ||
return self.index(position, offsetBy: escape.count, limitedBy: self.endIndex) | ||
} | ||
} | ||
} |