|
| 1 | +// |
| 2 | +// Copyright 2020 The Matrix.org Foundation C.I.C |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import DTCoreText |
| 18 | + |
| 19 | +public extension DTHTMLElement { |
| 20 | + typealias ImageHandler = (_ sourceURL: String, _ width: CGFloat, _ height: CGFloat) -> URL? |
| 21 | + |
| 22 | + /// Sanitize the element using the given parameters. |
| 23 | + /// - Parameters: |
| 24 | + /// - allowedHTMLTags: An array of tags that are allowed. All other tags will be removed. |
| 25 | + /// - font: The default font to use when resetting the content of any unsupported tags. |
| 26 | + /// - imageHandler: An optional image handler to be run on `img` tags (if allowed) to update the `src` attribute. |
| 27 | + @objc func sanitize(with allowedHTMLTags: [String], bodyFont font: UIFont, imageHandler: ImageHandler?) { |
| 28 | + if let name = name, !allowedHTMLTags.contains(name) { |
| 29 | + |
| 30 | + // This is an unsupported tag. |
| 31 | + // Remove any attachments to fix rendering. |
| 32 | + textAttachment = nil |
| 33 | + |
| 34 | + // If the element has plain text content show that, |
| 35 | + // otherwise prevent the tag from displaying. |
| 36 | + if let stringContent = attributedString()?.string, |
| 37 | + !stringContent.isEmpty, |
| 38 | + let element = DTTextHTMLElement(name: nil, attributes: nil) { |
| 39 | + element.setText(stringContent) |
| 40 | + removeAllChildNodes() |
| 41 | + addChildNode(element) |
| 42 | + |
| 43 | + if let parent = parent() { |
| 44 | + element.inheritAttributes(from: parent) |
| 45 | + } else { |
| 46 | + fontDescriptor = DTCoreTextFontDescriptor() |
| 47 | + fontDescriptor.fontFamily = font.familyName |
| 48 | + fontDescriptor.fontName = font.fontName |
| 49 | + fontDescriptor.pointSize = font.pointSize |
| 50 | + paragraphStyle = DTCoreTextParagraphStyle.default() |
| 51 | + |
| 52 | + element.inheritAttributes(from: self) |
| 53 | + } |
| 54 | + element.interpretAttributes() |
| 55 | + |
| 56 | + } else if let parent = parent() { |
| 57 | + parent.removeChildNode(self) |
| 58 | + } else { |
| 59 | + didOutput = true |
| 60 | + } |
| 61 | + |
| 62 | + } else { |
| 63 | + // Process images with the handler when self is an image tag. |
| 64 | + if name == "img", let imageHandler = imageHandler { |
| 65 | + process(with: imageHandler) |
| 66 | + } |
| 67 | + |
| 68 | + // This element is a supported tag, but it may contain children that aren't, |
| 69 | + // so santize all child nodes to ensure correct tags. |
| 70 | + if let childNodes = childNodes as? [DTHTMLElement] { |
| 71 | + childNodes.forEach { $0.sanitize(with: allowedHTMLTags, bodyFont: font, imageHandler: imageHandler) } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /// Process the element with the supplied image handler. |
| 77 | + private func process(with imageHandler: ImageHandler) { |
| 78 | + // Get the values required to pass to the image handler |
| 79 | + guard let sourceURL = attributes["src"] as? String else { return } |
| 80 | + |
| 81 | + var width: CGFloat = -1 |
| 82 | + if let widthString = attributes["width"] as? String, |
| 83 | + let widthDouble = Double(widthString) { |
| 84 | + width = CGFloat(widthDouble) |
| 85 | + } |
| 86 | + |
| 87 | + var height: CGFloat = -1 |
| 88 | + if let heightString = attributes["height"] as? String, |
| 89 | + let heightDouble = Double(heightString) { |
| 90 | + height = CGFloat(heightDouble) |
| 91 | + } |
| 92 | + |
| 93 | + // If the handler returns an updated URL, update the text attachment. |
| 94 | + guard let localSourceURL = imageHandler(sourceURL, width, height) else { return } |
| 95 | + textAttachment.contentURL = localSourceURL |
| 96 | + } |
| 97 | +} |
0 commit comments