In my iOS app, I have an auxiliary function that receives an html-formatted string and removes the explicit height tags from images.
This is the complete function:
/// This function removes any explicit height in image tag
/// - Parameter htmlString: target HTML
/// - Returns: string HTML after removing height from any aimge tag
private func removeExplicitHeightFromImgTag(for htmlString: String) -> String {
var result = NSString(string: htmlString)
/// Potential breakpoint / Infinite look: This while loop seems to be causing infinite loop behaviour.
while let imagTagrange = (result as String).firstRangeThatMatches(for: "<img.*height\\s*=\\s*(\\\")?[0-9]+(\\\")?") {
let imageTag = result.substring(with: imagTagrange)
if let heightRange = imageTag.firstRangeThatMatches(for: "height\\s*=\\s*(\\\")[0-9]+(\\\")?") {
let tagWithoutHeight = NSString(string: imageTag).replacingCharacters(in: heightRange, with: "")
result = result.replacingCharacters(in: imagTagrange, with: tagWithoutHeight) as NSString
}
}
return result as String
}
The specific block that seems to be causing havoc is the following:
/// Potential breakpoint / Infinite look: This while loop seems to be causing infinite loop behaviour.
while let imagTagrange = (result as String).firstRangeThatMatches(for: "<img.*height\\s*=\\s*(\\\")?[0-9]+(\\\")?") {
let imageTag = result.substring(with: imagTagrange)
if let heightRange = imageTag.firstRangeThatMatches(for: "height\\s*=\\s*(\\\")[0-9]+(\\\")?") {
let tagWithoutHeight = NSString(string: imageTag).replacingCharacters(in: heightRange, with: "")
result = result.replacingCharacters(in: imagTagrange, with: tagWithoutHeight) as NSString
}
}
The input for this function would be an html block encoded as string, which represents the body of an online article.
For example, the following url: https://www.architecturaldigest.com/story/inside-an-art-filled-hollywood-regency-pied-a-terre
Would be parsed and assigned to the htmlString parameter as:
<div><p>Busting a move to Los Angeles seemed only natural for Houston-based interior designer Garrett Hunter and architect Michael Landrum. Although the two friends maintain independent practices, they share an office space and frequently collaborate on projects. The two are also partners in an ever-evolving, experimental gallery/showroom project that first came to life in Houston in 2016 named Tienda X. Two years later, Hunter and Landrum moved the gallery—which hosts a compelling mix of fine and decorative art spanning the contemporary and the antique—to a Mediterranean-style stone house in Austin. They dubbed the operation Villa X.</p><span><img alt=\"pIn a sitting room a Maison Jansen sofa is accompanied by indigo pillows by Christopher Wrobleski a Spanish Majolica oil...\" src=\"https://media.architecturaldigest.com/photos/601c24457d77c6f2f298922d/master/w_1600%2Cc_limit/2020-11-12-Watsonia-AD0080_r2.jpg\"></span><span><p>In a sitting room...
Could you please help in understanding how to solve this and if there is a way to proceed? I am really stuck and would be enormously grateful to you :)