Skip to content

Commit

Permalink
HTML escape matching & sorting quizzes (#492)
Browse files Browse the repository at this point in the history
* Remove unused

* Add String+HTMLEscape

* Add HTML-escaping for MatchingQuizViewController

* Remove unused

* Add HTML-escaping for SortingQuizViewController
  • Loading branch information
ivan-magda authored Jul 8, 2019
1 parent dd69b71 commit d071d1c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 25 deletions.
8 changes: 6 additions & 2 deletions Stepic.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3132,6 +3132,7 @@
2C97E00F215E482B005684A1 /* SearchResultsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08C1BF301FBA0CA1008F342F /* SearchResultsViewController.swift */; };
2C97E010215E5246005684A1 /* Explore.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 08CB0D391FB5FB28001A1E02 /* Explore.storyboard */; };
2C98B6B61FDFD74C005AB72C /* OnboardingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C98B6B51FDFD74C005AB72C /* OnboardingViewController.swift */; };
2C9A8D2E22D348A5009434DB /* String+HTMLEscape.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C9A8D2D22D348A5009434DB /* String+HTMLEscape.swift */; };
2C9BD78E1FC43C6B00F89CBE /* NotificationsBadgesManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C9BD78D1FC43C6B00F89CBE /* NotificationsBadgesManager.swift */; };
2C9E3F3C1F7A80A300DDF1AA /* Notification+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C9E3F3B1F7A80A300DDF1AA /* Notification+CoreDataProperties.swift */; };
2C9E3F3E1F7A930100DDF1AA /* NotificationsAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C9E3F3D1F7A930100DDF1AA /* NotificationsAPI.swift */; };
Expand Down Expand Up @@ -5897,6 +5898,7 @@
2C9732F71F4C391900AC9301 /* Icons.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Icons.xcassets; path = Content/3150/Icons.xcassets; sourceTree = "<group>"; };
2C9732F91F4C391900AC9301 /* Tokens.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Tokens.plist; path = Content/3150/Tokens.plist; sourceTree = "<group>"; };
2C98B6B51FDFD74C005AB72C /* OnboardingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingViewController.swift; sourceTree = "<group>"; };
2C9A8D2D22D348A5009434DB /* String+HTMLEscape.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+HTMLEscape.swift"; sourceTree = "<group>"; };
2C9BD78D1FC43C6B00F89CBE /* NotificationsBadgesManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationsBadgesManager.swift; sourceTree = "<group>"; };
2C9E3F351F7A79E600DDF1AA /* Model_notifications.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Model_notifications.xcdatamodel; sourceTree = "<group>"; };
2C9E3F3B1F7A80A300DDF1AA /* Notification+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notification+CoreDataProperties.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -11147,10 +11149,11 @@
62E986D974D797A3BDF8DA59 /* Extensions */ = {
isa = PBXGroup;
children = (
2C20777F22BB897200D44DC0 /* DispatchQueue+promise.swift */,
2C48D5FB228D865E00739477 /* Guarantee+thenableWithFallback.swift */,
62E9845643D7541307856023 /* UITableViewExtensions.swift */,
2C9A8D2D22D348A5009434DB /* String+HTMLEscape.swift */,
62E9866C8BA98FF768F487E2 /* UICollectionViewExtensions.swift */,
2C20777F22BB897200D44DC0 /* DispatchQueue+promise.swift */,
62E9845643D7541307856023 /* UITableViewExtensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -16083,6 +16086,7 @@
B8042A238380ACC1F32082E1 /* NewStepAssembly.swift in Sources */,
60B1FF909EC34FCFD6D2502D /* NewStepViewController.swift in Sources */,
FE43FE17249BB2B4F6890F8D /* NewStepInteractor.swift in Sources */,
2C9A8D2E22D348A5009434DB /* String+HTMLEscape.swift in Sources */,
36D02A00641C747F6D18DB60 /* NewStepPresenter.swift in Sources */,
7FBB25F3FD251734F7E049DD /* NewStepDataFlow.swift in Sources */,
E68F5C66A733F2B9F2C18920 /* NewStepProvider.swift in Sources */,
Expand Down
16 changes: 5 additions & 11 deletions Stepic/MatchingQuizViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class MatchingQuizViewController: QuizViewController {

fileprivate var orderedOptions: [String] = []
fileprivate var optionsPermutation: [Int] = []
fileprivate var positionForOptionInAttempt: [String : Int] = [:]
fileprivate var firstCellHeights: [CGFloat?] = []
fileprivate var secondCellHeights: [CGFloat?] = []

Expand Down Expand Up @@ -129,13 +128,11 @@ class MatchingQuizViewController: QuizViewController {

fileprivate func resetOptionsToDataset() {
orderedOptions = []
positionForOptionInAttempt = [:]
optionsPermutation = []
if let dataset = dataset {
self.orderedOptions = dataset.secondValues
for (index, option) in dataset.secondValues.enumerated() {
for (index, _) in dataset.secondValues.enumerated() {
optionsPermutation += [index]
positionForOptionInAttempt[option] = index
}
}
}
Expand All @@ -160,11 +157,6 @@ class MatchingQuizViewController: QuizViewController {
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func maxCellHeight(options: [String], heights: [CGFloat?], sortable: Bool) -> CGFloat {

var max: CGFloat = 0
Expand Down Expand Up @@ -249,7 +241,8 @@ extension MatchingQuizViewController : UITableViewDataSource {

switch tableView.tag {
case 1:
cell.setHTMLText(dataset.firstValues[indexPath.row], width: cellWidth(forTableView: firstTableView), finishedBlock: {
let htmlEscapedText = dataset.firstValues[indexPath.row].addingHTMLEntities()
cell.setHTMLText(htmlEscapedText, width: cellWidth(forTableView: firstTableView), finishedBlock: {
[weak self]
newHeight in

Expand All @@ -272,8 +265,9 @@ extension MatchingQuizViewController : UITableViewDataSource {
}
})
case 2:
let htmlEscapedText = self.orderedOptions[indexPath.row].addingHTMLEntities()
cell.sortable = true
cell.setHTMLText(orderedOptions[indexPath.row], width: cellWidth(forTableView: secondTableView), finishedBlock: {
cell.setHTMLText(htmlEscapedText, width: cellWidth(forTableView: secondTableView), finishedBlock: {
[weak self]
newHeight in

Expand Down
14 changes: 2 additions & 12 deletions Stepic/SortingQuizViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,6 @@ class SortingQuizViewController: QuizViewController {
s.tableView.reloadData()
}
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

extension SortingQuizViewController : UITableViewDelegate {
Expand Down Expand Up @@ -171,7 +160,8 @@ extension SortingQuizViewController : UITableViewDataSource {

let cell = tableView.dequeueReusableCell(withIdentifier: "SortingQuizTableViewCell", for: indexPath) as! SortingQuizTableViewCell

cell.setHTMLText(orderedOptions[indexPath.row], width: cellWidth, finishedBlock: {
let htmlEscapedText = self.orderedOptions[indexPath.row].addingHTMLEntities()
cell.setHTMLText(htmlEscapedText, width: cellWidth, finishedBlock: {
[weak self]
newHeight in

Expand Down
53 changes: 53 additions & 0 deletions Stepic/Sources/Extensions/String+HTMLEscape.swift
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)
}
}
}

0 comments on commit d071d1c

Please sign in to comment.