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:
parent
b5347e04b2
commit
52ec2bebd5
|
@ -60,6 +60,14 @@ class FieldTableViewCell: UITableViewCell {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var allowedCharset: CharacterSet? {
|
||||||
|
didSet {
|
||||||
|
illegalCharset = allowedCharset?.inverted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var illegalCharset: CharacterSet?
|
||||||
|
|
||||||
private(set) lazy var field = UITextField()
|
private(set) lazy var field = UITextField()
|
||||||
|
|
||||||
weak var delegate: FieldTableViewCellDelegate?
|
weak var delegate: FieldTableViewCellDelegate?
|
||||||
|
@ -96,6 +104,16 @@ class FieldTableViewCell: UITableViewCell {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension FieldTableViewCell: UITextFieldDelegate {
|
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) {
|
func textFieldDidEndEditing(_ textField: UITextField) {
|
||||||
delegate?.fieldCellDidEdit(self)
|
delegate?.fieldCellDidEdit(self)
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,8 @@ class WizardHostViewController: UITableViewController, TableModelHost, Wizard {
|
||||||
let url: URL
|
let url: URL
|
||||||
|
|
||||||
var filename: String {
|
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
|
let hostname: String
|
||||||
|
@ -215,6 +216,7 @@ extension WizardHostViewController {
|
||||||
let cell = Cells.field.dequeue(from: tableView, for: indexPath)
|
let cell = Cells.field.dequeue(from: tableView, for: indexPath)
|
||||||
cell.caption = L10n.Wizards.Host.Cells.TitleInput.caption
|
cell.caption = L10n.Wizards.Host.Cells.TitleInput.caption
|
||||||
cell.captionWidth = 100.0
|
cell.captionWidth = 100.0
|
||||||
|
cell.allowedCharset = AppConstants.Store.filenameCharset
|
||||||
cell.field.placeholder = L10n.Wizards.Host.Cells.TitleInput.placeholder
|
cell.field.placeholder = L10n.Wizards.Host.Cells.TitleInput.placeholder
|
||||||
cell.field.clearButtonMode = .always
|
cell.field.clearButtonMode = .always
|
||||||
cell.field.returnKeyType = .done
|
cell.field.returnKeyType = .done
|
||||||
|
|
|
@ -40,6 +40,16 @@ class AppConstants {
|
||||||
static let providersDirectory = "Providers"
|
static let providersDirectory = "Providers"
|
||||||
|
|
||||||
static let hostsDirectory = "Hosts"
|
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 {
|
class VPN {
|
||||||
|
|
Loading…
Reference in New Issue