0001 // The MIT License 0002 // 0003 // Copyright (c) 2015 Gwendal Roué 0004 // 0005 // Permission is hereby granted, free of charge, to any person obtaining a copy 0006 // of this software and associated documentation files (the "Software"), to deal 0007 // in the Software without restriction, including without limitation the rights 0008 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0009 // copies of the Software, and to permit persons to whom the Software is 0010 // furnished to do so, subject to the following conditions: 0011 // 0012 // The above copyright notice and this permission notice shall be included in 0013 // all copies or substantial portions of the Software. 0014 // 0015 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0016 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0017 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0018 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0019 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0020 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 0021 // THE SOFTWARE. 0022 0023 final class HTMLEscapeHelper: MustacheBoxable { 0024 0025 var mustacheBox: MustacheBox { 0026 // Return a multi-facetted box, because HTMLEscape interacts in 0027 // various ways with Mustache rendering. 0028 return MustacheBox( 0029 // It has a value: 0030 value: self, 0031 0032 // HTMLEscape can be used as a filter: {{ HTMLEscape(x) }}: 0033 filter: Filter(filter), 0034 0035 // HTMLEscape escapes all variable tags: {{# HTMLEscape }}...{{ x }}...{{/ HTMLEscape }} 0036 willRender: willRender) 0037 } 0038 0039 // This function is used for evaluating `HTMLEscape(x)` expressions. 0040 private func filter
StandardLibrary.swift:57 public static let HTMLEscape: MustacheBoxable = HTMLEscapeHelper()(rendering: Rendering) throws -> Rendering { 0041 return Rendering(escapeHTML(rendering.string), rendering.contentType) 0042 } 0043 0044 // A WillRenderFunction: this function lets HTMLEscape change values that 0045 // are about to be rendered to their escaped counterpart. 0046 // 0047 // It is activated as soon as the formatter enters the context stack, when 0048 // used in a section {{# HTMLEscape }}...{{/ HTMLEscape }}. 0049 private func willRender
HTMLEscapeHelper.swift:33 filter: Filter(filter),HTMLEscapeHelper.swift:57 return try self.filter(rendering)(tag: Tag, box: MustacheBox) -> MustacheBox { 0050 switch tag.type { 0051 case .Variable: 0052 // {{ value }} 0053 // We don't know if the box contains a String, so let's escape its 0054 // rendering. 0055 return Box(render: { (info: RenderingInfo) -> Rendering in 0056 let rendering = try box.render(info: info) 0057 return try self.filter(rendering) 0058 }) 0059 case .Section: 0060 // {{# value }}...{{/ value }} 0061 // {{^ value }}...{{/ value }} 0062 // Leave sections untouched, so that loops and conditions are not 0063 // affected by the formatter. 0064 0065 return box 0066 } 0067 } 0068 } 0069
HTMLEscapeHelper.swift:36 willRender: willRender)