// Copyright (c) 2017 The Go Authors. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd package safehtml import ( "bytes" "fmt" "regexp" "strings" ) // A Style is an immutable string-like type which represents a sequence of CSS // declarations (property_name1: property_value1; property_name2: property_value2; ...) // and guarantees that its value will not cause untrusted script execution // (cross-site scripting) when evaluated as CSS in a browser. // // Style's string representation can safely be: // * Interpolated as the content of a quoted HTML style attribute. However, the // Style string must be HTML-attribute-escaped before interpolation. // * Interpolated as the content of a {}-wrapped block within a StyleSheet. // '<' runes in the Style string must be CSS-escaped before interpolation. // The Style string is also guaranteed not to be able to introduce new // properties or elide existing ones. // * Interpolated as the content of a {}-wrapped block within an HTML `. Escape this in case the Style user forgets to. c == '"', c == '\\', // Must be CSS-escaped in . U+000A line feed is handled in the next case. c <= '\u001F', c == '\u007F', // C0 control codes c >= '\u0080' && c <= '\u009F', // C1 control codes c == '\u2028', c == '\u2029': // Unicode newline characters // See CSS escape sequence syntax at https://www.w3.org/TR/css-syntax-3/#escape-diagram. fmt.Fprintf(&b, "\\%06X", c) default: b.WriteRune(c) } } return b.String() }