Restrict charset for host profile title

It's used now as a filename. Remember to also normalize pre-filled
title from imported filename by replacing illegal characters.
This commit is contained in:
Davide De Rosa 2018-10-26 17:13:30 +02:00
parent b5347e04b2
commit 52ec2bebd5
3 changed files with 31 additions and 1 deletions

View File

@ -60,6 +60,14 @@ class FieldTableViewCell: UITableViewCell {
}
}
var allowedCharset: CharacterSet? {
didSet {
illegalCharset = allowedCharset?.inverted
}
}
private var illegalCharset: CharacterSet?
private(set) lazy var field = UITextField()
weak var delegate: FieldTableViewCellDelegate?
@ -96,6 +104,16 @@ class FieldTableViewCell: UITableViewCell {
}
extension FieldTableViewCell: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let illegalCharset = illegalCharset else {
return true
}
guard string.rangeOfCharacter(from: illegalCharset) == nil else {
return false
}
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
delegate?.fieldCellDidEdit(self)
}

View File

@ -34,7 +34,8 @@ class WizardHostViewController: UITableViewController, TableModelHost, Wizard {
let url: URL
var filename: String {
return url.deletingPathExtension().lastPathComponent
let raw = url.deletingPathExtension().lastPathComponent
return raw.components(separatedBy: AppConstants.Store.filenameCharset.inverted).joined(separator: "_")
}
let hostname: String
@ -215,6 +216,7 @@ extension WizardHostViewController {
let cell = Cells.field.dequeue(from: tableView, for: indexPath)
cell.caption = L10n.Wizards.Host.Cells.TitleInput.caption
cell.captionWidth = 100.0
cell.allowedCharset = AppConstants.Store.filenameCharset
cell.field.placeholder = L10n.Wizards.Host.Cells.TitleInput.placeholder
cell.field.clearButtonMode = .always
cell.field.returnKeyType = .done

View File

@ -40,6 +40,16 @@ class AppConstants {
static let providersDirectory = "Providers"
static let hostsDirectory = "Hosts"
static let filenameCharset: CharacterSet = {
var chars: CharacterSet = .decimalDigits
let english = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let symbols = "-_"
chars.formUnion(CharacterSet(charactersIn: english))
chars.formUnion(CharacterSet(charactersIn: english.lowercased()))
chars.formUnion(CharacterSet(charactersIn: symbols))
return chars
}()
}
class VPN {