Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions Modules/Sources/WordPressShared/Utility/RichContentFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ import Foundation

content = RegEx.styleTags.stringByReplacingMatches(in: content,
options: .reportCompletion,
range: NSRange(location: 0, length: content.count),
range: content.fullNSRange,
withTemplate: "")

content = RegEx.scriptTags.stringByReplacingMatches(in: content,
options: .reportCompletion,
range: NSRange(location: 0, length: content.count),
range: content.fullNSRange,
withTemplate: "")

content = RegEx.gutenbergComments.stringByReplacingMatches(in: content,
options: .reportCompletion,
range: NSRange(location: 0, length: content.count),
range: content.fullNSRange,
withTemplate: "")

return content
Expand All @@ -84,23 +84,23 @@ import Foundation
// Convert div tags to p tags
content = RegEx.divTagsStart.stringByReplacingMatches(in: content,
options: .reportCompletion,
range: NSRange(location: 0, length: content.count),
range: content.fullNSRange,
withTemplate: openPTag)

content = RegEx.divTagsEnd.stringByReplacingMatches(in: content,
options: .reportCompletion,
range: NSRange(location: 0, length: content.count),
range: content.fullNSRange,
withTemplate: closePTag)

// Remove duplicate/redundant p tags.
content = RegEx.pTagsStart.stringByReplacingMatches(in: content,
options: .reportCompletion,
range: NSRange(location: 0, length: content.count),
range: content.fullNSRange,
withTemplate: openPTag)

content = RegEx.pTagsEnd.stringByReplacingMatches(in: content,
options: .reportCompletion,
range: NSRange(location: 0, length: content.count),
range: content.fullNSRange,
withTemplate: closePTag)

content = filterNewLines(content)
Expand All @@ -114,11 +114,11 @@ import Foundation
var ranges = [NSRange]()
// We don't want to remove new lines from preformatted tag blocks,
// so get the ranges of such blocks.
let matches = RegEx.preTags.matches(in: content, options: .reportCompletion, range: NSRange(location: 0, length: content.count))
let matches = RegEx.preTags.matches(in: content, options: .reportCompletion, range: content.fullNSRange)
if matches.isEmpty {

// No blocks found, so we'll parse the whole string.
ranges.append(NSRange(location: 0, length: content.count))
ranges.append(content.fullNSRange)
} else {

// One or more preformatted blocks found, we don't want to remove new lines
Expand All @@ -133,7 +133,7 @@ import Foundation
location = match.range.location + match.range.length
}

length = content.count - location
length = content.utf16.count - location
ranges.append(NSRange(location: location, length: length))
}

Expand Down Expand Up @@ -163,7 +163,7 @@ import Foundation

content = RegEx.styleAttr.stringByReplacingMatches(in: content,
options: .reportCompletion,
range: NSRange(location: 0, length: content.count),
range: content.fullNSRange,
withTemplate: "")

return content
Expand All @@ -187,7 +187,9 @@ import Foundation
let location = attrRange.location + attrRange.length
let length = elementStr.length - location
let ending = elementStr.range(of: "\"", options: .caseInsensitive, range: NSRange(location: location, length: length))
value = elementStr.substring(with: NSRange(location: location, length: ending.location - location))
if ending.location != NSNotFound {
value = elementStr.substring(with: NSRange(location: location, length: ending.location - location))
}
}

return value
Expand All @@ -206,10 +208,9 @@ import Foundation
}

var content = string.trim()
let matches = RegEx.trailingBRTags.matches(in: content, options: .reportCompletion, range: NSRange(location: 0, length: content.count))
if let match = matches.first {
let index = content.index(content.startIndex, offsetBy: match.range.location)
content = String(content.prefix(upTo: index))
let matches = RegEx.trailingBRTags.matches(in: content, options: .reportCompletion, range: content.fullNSRange)
if let match = matches.first, let matchRange = Range(match.range, in: content) {
content = String(content[..<matchRange.lowerBound])
}

return content
Expand Down
14 changes: 14 additions & 0 deletions Modules/Sources/WordPressShared/Utility/String+FullNSRange.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

extension String {

/// The `NSRange` spanning the whole string in UTF-16 code units — the unit
/// `NSRegularExpression` and other `NSString`-based APIs operate on.
///
/// Prefer this to `NSRange(location: 0, length: count)`: `count` is the grapheme-cluster
/// count, which is shorter than the UTF-16 length for emoji, flags, and combining sequences,
/// so a grapheme-count range silently truncates the search near the end of the string.
var fullNSRange: NSRange {
NSRange(location: 0, length: utf16.count)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ extension RichContentFormatter {
of: srcImgURLStr,
with: modifiedURL.absoluteString,
options: .literal,
range: NSRange(location: 0, length: imgElementStr.count)
range: NSRange(location: 0, length: imgElementStr.utf16.count)
)

mContent.replaceCharacters(in: match.range, with: mImageStr as String)
Expand Down
Loading