Skip to content

Commit

Permalink
Merge pull request #106 from RedMadRobot/range-sanitisation
Browse files Browse the repository at this point in the history
Range sanitisation
  • Loading branch information
taflanidi committed Apr 16, 2023
2 parents d5649f0 + e44e5f7 commit 41f93e2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# 𝌡Changelog

## 7.2.0

**🔄 Modified:**
* `MaskedTextInputListener.replaceCharacters()`: apply a patch to counter iOS Undo Manager, see https://github.com/RedMadRobot/input-mask-ios/issues/84

## 7.1.1

**🔄 Modified:**
Expand Down
2 changes: 1 addition & 1 deletion InputMask.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "InputMask"
spec.version = "7.1.1"
spec.version = "7.2.0"
spec.summary = "InputMask"
spec.description = "User input masking library."
spec.homepage = "https://github.com/RedMadRobot/input-mask-ios"
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -148,6 +148,7 @@ These folks rock:
* Ivan [vani](https://github.com/vani2) Vavilov
* Diego [diegotl](https://github.com/diegotl) Trevisan
* Martin [martintreurnicht](https://github.com/martintreurnicht) Treurnicht
* Alexander [CFIFok](https://github.com/CFIFok) Kurilovich

<a name="license" />

Expand Down
Expand Up @@ -225,13 +225,36 @@ open class MaskedTextInputListener: NSObject {
}

open func replaceCharacters(inText text: String, range: NSRange, withCharacters newText: String) -> String {
if 0 < range.length {
/**
https://github.com/RedMadRobot/input-mask-ios/issues/84
iOS Undo Manager might not consider text adjustments made by the library.
Thus, the `range` might be out of bounds compared to the actual text.
*/
var sanitisedRangeLocation = range.location
var sanitisedRangeLength = range.length

if text.count < sanitisedRangeLocation {
sanitisedRangeLocation = text.count
}

if text.count < sanitisedRangeLocation + sanitisedRangeLength {
sanitisedRangeLength = text.count - sanitisedRangeLocation
}

let sanitisedRange = NSRange(
location: sanitisedRangeLocation,
length: sanitisedRangeLength
)
// end

if 0 < sanitisedRange.length {
let result = NSMutableString(string: text)
result.replaceCharacters(in: range, with: newText)
result.replaceCharacters(in: sanitisedRange, with: newText)
return result as String
} else {
let result = NSMutableString(string: text)
result.insert(newText, at: range.location)
result.insert(newText, at: sanitisedRange.location)
return result as String
}
}
Expand Down

0 comments on commit 41f93e2

Please sign in to comment.