0

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&#8212;which hosts a compelling mix of fine and decorative art spanning the contemporary and the antique&#8212;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 :)

  • You need to debug. Have an input value, print all values if needed.-, like `imagTagRange`, `imageTag`, `result` inside the while loop (before/after modifications if needed). And check why it's failing. – Larme Feb 08 '21 at 10:16
  • But instead of a while loop, you could find allMatches, reverse the matches (so you modify the one in the end first in order to not mess with the matches ranges), and replaces the values. You don't use your regex groups, and fetch again the match. Also, you simply use replace with pattern https://stackoverflow.com/questions/28503449/swift-replace-substring-regex – Larme Feb 08 '21 at 10:19
  • Thanks Lame, much appreciatted. Could you please explain a bit more how you would go about it? – Angel Salinas ReeCast Feb 08 '21 at 10:33
  • Could you edit your question with a sample input? – Larme Feb 08 '21 at 10:41
  • Sure, I’ve updated it with an example input. Your help would be much appreciated – Angel Salinas ReeCast Feb 09 '21 at 09:38
  • Something like `htmlString.replacingOccurrences(of: "( – Larme Feb 09 '21 at 09:51
  • Hi Larme, thanks for providing the code. Do you mean that piece would replace the entire While block? I will try it out and let you know. Thank you ver much for your help – Angel Salinas ReeCast Feb 11 '21 at 09:50
  • Also, could you please post your comment as an answer so I can promote it to the accepted response? – Angel Salinas ReeCast Feb 11 '21 at 09:52

0 Answers0