lib/hanami/utils/escape.rb in hanami-utils-2.0.0.alpha1 vs lib/hanami/utils/escape.rb in hanami-utils-2.0.0.alpha2

- old
+ new

@@ -37,11 +37,11 @@ # Low hex codes lookup table # # @since 0.4.0 # @api private HEX_CODES = (0..255).each_with_object({}) do |c, codes| - if (c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A) # rubocop:disable Style/ConditionalAssignment + if (c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A) codes[c] = nil else codes[c] = c.to_s(HEX_BASE) end end.freeze @@ -53,12 +53,12 @@ # @since 0.4.0 # @api private # # @see https://gist.github.com/jodosha/ac5dd54416de744b9600 NON_PRINTABLE_CHARS = { - 0x0 => true, 0x1 => true, 0x2 => true, 0x3 => true, 0x4 => true, - 0x5 => true, 0x6 => true, 0x7 => true, 0x8 => true, 0x11 => true, + 0x0 => true, 0x1 => true, 0x2 => true, 0x3 => true, 0x4 => true, + 0x5 => true, 0x6 => true, 0x7 => true, 0x8 => true, 0x11 => true, 0x12 => true, 0x14 => true, 0x15 => true, 0x16 => true, 0x17 => true, 0x18 => true, 0x19 => true, 0x1a => true, 0x1b => true, 0x1c => true, 0x1d => true, 0x1e => true, 0x1f => true, 0x7f => true, 0x80 => true, 0x81 => true, 0x82 => true, 0x83 => true, 0x84 => true, 0x85 => true, 0x86 => true, 0x87 => true, 0x88 => true, 0x89 => true, 0x8a => true, @@ -102,170 +102,170 @@ # @since 0.4.0 # @api private # # @see Hanami::Utils::Escape.html_attribute HTML_ENTITIES = { - 34 => "quot", # quotation mark - 38 => "amp", # ampersand - 60 => "lt", # less-than sign - 62 => "gt", # greater-than sign - 160 => "nbsp", # no-break space - 161 => "iexcl", # inverted exclamation mark - 162 => "cent", # cent sign - 163 => "pound", # pound sign - 164 => "curren", # currency sign - 165 => "yen", # yen sign - 166 => "brvbar", # broken bar - 167 => "sect", # section sign - 168 => "uml", # diaeresis - 169 => "copy", # copyright sign - 170 => "ordf", # feminine ordinal indicator - 171 => "laquo", # left-pointing double angle quotation mark - 172 => "not", # not sign - 173 => "shy", # soft hyphen - 174 => "reg", # registered sign - 175 => "macr", # macron - 176 => "deg", # degree sign - 177 => "plusmn", # plus-minus sign - 178 => "sup2", # superscript two - 179 => "sup3", # superscript three - 180 => "acute", # acute accent - 181 => "micro", # micro sign - 182 => "para", # pilcrow sign - 183 => "middot", # middle dot - 184 => "cedil", # cedilla - 185 => "sup1", # superscript one - 186 => "ordm", # masculine ordinal indicator - 187 => "raquo", # right-pointing double angle quotation mark - 188 => "frac14", # vulgar fraction one quarter - 189 => "frac12", # vulgar fraction one half - 190 => "frac34", # vulgar fraction three quarters - 191 => "iquest", # inverted question mark - 192 => "Agrave", # Latin capital letter a with grave - 193 => "Aacute", # Latin capital letter a with acute - 194 => "Acirc", # Latin capital letter a with circumflex - 195 => "Atilde", # Latin capital letter a with tilde - 196 => "Auml", # Latin capital letter a with diaeresis - 197 => "Aring", # Latin capital letter a with ring above - 198 => "AElig", # Latin capital letter ae - 199 => "Ccedil", # Latin capital letter c with cedilla - 200 => "Egrave", # Latin capital letter e with grave - 201 => "Eacute", # Latin capital letter e with acute - 202 => "Ecirc", # Latin capital letter e with circumflex - 203 => "Euml", # Latin capital letter e with diaeresis - 204 => "Igrave", # Latin capital letter i with grave - 205 => "Iacute", # Latin capital letter i with acute - 206 => "Icirc", # Latin capital letter i with circumflex - 207 => "Iuml", # Latin capital letter i with diaeresis - 208 => "ETH", # Latin capital letter eth - 209 => "Ntilde", # Latin capital letter n with tilde - 210 => "Ograve", # Latin capital letter o with grave - 211 => "Oacute", # Latin capital letter o with acute - 212 => "Ocirc", # Latin capital letter o with circumflex - 213 => "Otilde", # Latin capital letter o with tilde - 214 => "Ouml", # Latin capital letter o with diaeresis - 215 => "times", # multiplication sign - 216 => "Oslash", # Latin capital letter o with stroke - 217 => "Ugrave", # Latin capital letter u with grave - 218 => "Uacute", # Latin capital letter u with acute - 219 => "Ucirc", # Latin capital letter u with circumflex - 220 => "Uuml", # Latin capital letter u with diaeresis - 221 => "Yacute", # Latin capital letter y with acute - 222 => "THORN", # Latin capital letter thorn - 223 => "szlig", # Latin small letter sharp sXCOMMAX German Eszett - 224 => "agrave", # Latin small letter a with grave - 225 => "aacute", # Latin small letter a with acute - 226 => "acirc", # Latin small letter a with circumflex - 227 => "atilde", # Latin small letter a with tilde - 228 => "auml", # Latin small letter a with diaeresis - 229 => "aring", # Latin small letter a with ring above - 230 => "aelig", # Latin lowercase ligature ae - 231 => "ccedil", # Latin small letter c with cedilla - 232 => "egrave", # Latin small letter e with grave - 233 => "eacute", # Latin small letter e with acute - 234 => "ecirc", # Latin small letter e with circumflex - 235 => "euml", # Latin small letter e with diaeresis - 236 => "igrave", # Latin small letter i with grave - 237 => "iacute", # Latin small letter i with acute - 238 => "icirc", # Latin small letter i with circumflex - 239 => "iuml", # Latin small letter i with diaeresis - 240 => "eth", # Latin small letter eth - 241 => "ntilde", # Latin small letter n with tilde - 242 => "ograve", # Latin small letter o with grave - 243 => "oacute", # Latin small letter o with acute - 244 => "ocirc", # Latin small letter o with circumflex - 245 => "otilde", # Latin small letter o with tilde - 246 => "ouml", # Latin small letter o with diaeresis - 247 => "divide", # division sign - 248 => "oslash", # Latin small letter o with stroke - 249 => "ugrave", # Latin small letter u with grave - 250 => "uacute", # Latin small letter u with acute - 251 => "ucirc", # Latin small letter u with circumflex - 252 => "uuml", # Latin small letter u with diaeresis - 253 => "yacute", # Latin small letter y with acute - 254 => "thorn", # Latin small letter thorn - 255 => "yuml", # Latin small letter y with diaeresis - 338 => "OElig", # Latin capital ligature oe - 339 => "oelig", # Latin small ligature oe - 352 => "Scaron", # Latin capital letter s with caron - 353 => "scaron", # Latin small letter s with caron - 376 => "Yuml", # Latin capital letter y with diaeresis - 402 => "fnof", # Latin small letter f with hook - 710 => "circ", # modifier letter circumflex accent - 732 => "tilde", # small tilde - 913 => "Alpha", # Greek capital letter alpha - 914 => "Beta", # Greek capital letter beta - 915 => "Gamma", # Greek capital letter gamma - 916 => "Delta", # Greek capital letter delta - 917 => "Epsilon", # Greek capital letter epsilon - 918 => "Zeta", # Greek capital letter zeta - 919 => "Eta", # Greek capital letter eta - 920 => "Theta", # Greek capital letter theta - 921 => "Iota", # Greek capital letter iota - 922 => "Kappa", # Greek capital letter kappa - 923 => "Lambda", # Greek capital letter lambda - 924 => "Mu", # Greek capital letter mu - 925 => "Nu", # Greek capital letter nu - 926 => "Xi", # Greek capital letter xi - 927 => "Omicron", # Greek capital letter omicron - 928 => "Pi", # Greek capital letter pi - 929 => "Rho", # Greek capital letter rho - 931 => "Sigma", # Greek capital letter sigma - 932 => "Tau", # Greek capital letter tau - 933 => "Upsilon", # Greek capital letter upsilon - 934 => "Phi", # Greek capital letter phi - 935 => "Chi", # Greek capital letter chi - 936 => "Psi", # Greek capital letter psi - 937 => "Omega", # Greek capital letter omega - 945 => "alpha", # Greek small letter alpha - 946 => "beta", # Greek small letter beta - 947 => "gamma", # Greek small letter gamma - 948 => "delta", # Greek small letter delta - 949 => "epsilon", # Greek small letter epsilon - 950 => "zeta", # Greek small letter zeta - 951 => "eta", # Greek small letter eta - 952 => "theta", # Greek small letter theta - 953 => "iota", # Greek small letter iota - 954 => "kappa", # Greek small letter kappa - 955 => "lambda", # Greek small letter lambda - 956 => "mu", # Greek small letter mu - 957 => "nu", # Greek small letter nu - 958 => "xi", # Greek small letter xi - 959 => "omicron", # Greek small letter omicron - 960 => "pi", # Greek small letter pi - 961 => "rho", # Greek small letter rho - 962 => "sigmaf", # Greek small letter final sigma - 963 => "sigma", # Greek small letter sigma - 964 => "tau", # Greek small letter tau - 965 => "upsilon", # Greek small letter upsilon - 966 => "phi", # Greek small letter phi - 967 => "chi", # Greek small letter chi - 968 => "psi", # Greek small letter psi - 969 => "omega", # Greek small letter omega - 977 => "thetasym", # Greek theta symbol - 978 => "upsih", # Greek upsilon with hook symbol - 982 => "piv", # Greek pi symbol + 34 => "quot", # quotation mark + 38 => "amp", # ampersand + 60 => "lt", # less-than sign + 62 => "gt", # greater-than sign + 160 => "nbsp", # no-break space + 161 => "iexcl", # inverted exclamation mark + 162 => "cent", # cent sign + 163 => "pound", # pound sign + 164 => "curren", # currency sign + 165 => "yen", # yen sign + 166 => "brvbar", # broken bar + 167 => "sect", # section sign + 168 => "uml", # diaeresis + 169 => "copy", # copyright sign + 170 => "ordf", # feminine ordinal indicator + 171 => "laquo", # left-pointing double angle quotation mark + 172 => "not", # not sign + 173 => "shy", # soft hyphen + 174 => "reg", # registered sign + 175 => "macr", # macron + 176 => "deg", # degree sign + 177 => "plusmn", # plus-minus sign + 178 => "sup2", # superscript two + 179 => "sup3", # superscript three + 180 => "acute", # acute accent + 181 => "micro", # micro sign + 182 => "para", # pilcrow sign + 183 => "middot", # middle dot + 184 => "cedil", # cedilla + 185 => "sup1", # superscript one + 186 => "ordm", # masculine ordinal indicator + 187 => "raquo", # right-pointing double angle quotation mark + 188 => "frac14", # vulgar fraction one quarter + 189 => "frac12", # vulgar fraction one half + 190 => "frac34", # vulgar fraction three quarters + 191 => "iquest", # inverted question mark + 192 => "Agrave", # Latin capital letter a with grave + 193 => "Aacute", # Latin capital letter a with acute + 194 => "Acirc", # Latin capital letter a with circumflex + 195 => "Atilde", # Latin capital letter a with tilde + 196 => "Auml", # Latin capital letter a with diaeresis + 197 => "Aring", # Latin capital letter a with ring above + 198 => "AElig", # Latin capital letter ae + 199 => "Ccedil", # Latin capital letter c with cedilla + 200 => "Egrave", # Latin capital letter e with grave + 201 => "Eacute", # Latin capital letter e with acute + 202 => "Ecirc", # Latin capital letter e with circumflex + 203 => "Euml", # Latin capital letter e with diaeresis + 204 => "Igrave", # Latin capital letter i with grave + 205 => "Iacute", # Latin capital letter i with acute + 206 => "Icirc", # Latin capital letter i with circumflex + 207 => "Iuml", # Latin capital letter i with diaeresis + 208 => "ETH", # Latin capital letter eth + 209 => "Ntilde", # Latin capital letter n with tilde + 210 => "Ograve", # Latin capital letter o with grave + 211 => "Oacute", # Latin capital letter o with acute + 212 => "Ocirc", # Latin capital letter o with circumflex + 213 => "Otilde", # Latin capital letter o with tilde + 214 => "Ouml", # Latin capital letter o with diaeresis + 215 => "times", # multiplication sign + 216 => "Oslash", # Latin capital letter o with stroke + 217 => "Ugrave", # Latin capital letter u with grave + 218 => "Uacute", # Latin capital letter u with acute + 219 => "Ucirc", # Latin capital letter u with circumflex + 220 => "Uuml", # Latin capital letter u with diaeresis + 221 => "Yacute", # Latin capital letter y with acute + 222 => "THORN", # Latin capital letter thorn + 223 => "szlig", # Latin small letter sharp sXCOMMAX German Eszett + 224 => "agrave", # Latin small letter a with grave + 225 => "aacute", # Latin small letter a with acute + 226 => "acirc", # Latin small letter a with circumflex + 227 => "atilde", # Latin small letter a with tilde + 228 => "auml", # Latin small letter a with diaeresis + 229 => "aring", # Latin small letter a with ring above + 230 => "aelig", # Latin lowercase ligature ae + 231 => "ccedil", # Latin small letter c with cedilla + 232 => "egrave", # Latin small letter e with grave + 233 => "eacute", # Latin small letter e with acute + 234 => "ecirc", # Latin small letter e with circumflex + 235 => "euml", # Latin small letter e with diaeresis + 236 => "igrave", # Latin small letter i with grave + 237 => "iacute", # Latin small letter i with acute + 238 => "icirc", # Latin small letter i with circumflex + 239 => "iuml", # Latin small letter i with diaeresis + 240 => "eth", # Latin small letter eth + 241 => "ntilde", # Latin small letter n with tilde + 242 => "ograve", # Latin small letter o with grave + 243 => "oacute", # Latin small letter o with acute + 244 => "ocirc", # Latin small letter o with circumflex + 245 => "otilde", # Latin small letter o with tilde + 246 => "ouml", # Latin small letter o with diaeresis + 247 => "divide", # division sign + 248 => "oslash", # Latin small letter o with stroke + 249 => "ugrave", # Latin small letter u with grave + 250 => "uacute", # Latin small letter u with acute + 251 => "ucirc", # Latin small letter u with circumflex + 252 => "uuml", # Latin small letter u with diaeresis + 253 => "yacute", # Latin small letter y with acute + 254 => "thorn", # Latin small letter thorn + 255 => "yuml", # Latin small letter y with diaeresis + 338 => "OElig", # Latin capital ligature oe + 339 => "oelig", # Latin small ligature oe + 352 => "Scaron", # Latin capital letter s with caron + 353 => "scaron", # Latin small letter s with caron + 376 => "Yuml", # Latin capital letter y with diaeresis + 402 => "fnof", # Latin small letter f with hook + 710 => "circ", # modifier letter circumflex accent + 732 => "tilde", # small tilde + 913 => "Alpha", # Greek capital letter alpha + 914 => "Beta", # Greek capital letter beta + 915 => "Gamma", # Greek capital letter gamma + 916 => "Delta", # Greek capital letter delta + 917 => "Epsilon", # Greek capital letter epsilon + 918 => "Zeta", # Greek capital letter zeta + 919 => "Eta", # Greek capital letter eta + 920 => "Theta", # Greek capital letter theta + 921 => "Iota", # Greek capital letter iota + 922 => "Kappa", # Greek capital letter kappa + 923 => "Lambda", # Greek capital letter lambda + 924 => "Mu", # Greek capital letter mu + 925 => "Nu", # Greek capital letter nu + 926 => "Xi", # Greek capital letter xi + 927 => "Omicron", # Greek capital letter omicron + 928 => "Pi", # Greek capital letter pi + 929 => "Rho", # Greek capital letter rho + 931 => "Sigma", # Greek capital letter sigma + 932 => "Tau", # Greek capital letter tau + 933 => "Upsilon", # Greek capital letter upsilon + 934 => "Phi", # Greek capital letter phi + 935 => "Chi", # Greek capital letter chi + 936 => "Psi", # Greek capital letter psi + 937 => "Omega", # Greek capital letter omega + 945 => "alpha", # Greek small letter alpha + 946 => "beta", # Greek small letter beta + 947 => "gamma", # Greek small letter gamma + 948 => "delta", # Greek small letter delta + 949 => "epsilon", # Greek small letter epsilon + 950 => "zeta", # Greek small letter zeta + 951 => "eta", # Greek small letter eta + 952 => "theta", # Greek small letter theta + 953 => "iota", # Greek small letter iota + 954 => "kappa", # Greek small letter kappa + 955 => "lambda", # Greek small letter lambda + 956 => "mu", # Greek small letter mu + 957 => "nu", # Greek small letter nu + 958 => "xi", # Greek small letter xi + 959 => "omicron", # Greek small letter omicron + 960 => "pi", # Greek small letter pi + 961 => "rho", # Greek small letter rho + 962 => "sigmaf", # Greek small letter final sigma + 963 => "sigma", # Greek small letter sigma + 964 => "tau", # Greek small letter tau + 965 => "upsilon", # Greek small letter upsilon + 966 => "phi", # Greek small letter phi + 967 => "chi", # Greek small letter chi + 968 => "psi", # Greek small letter psi + 969 => "omega", # Greek small letter omega + 977 => "thetasym", # Greek theta symbol + 978 => "upsih", # Greek upsilon with hook symbol + 982 => "piv", # Greek pi symbol 8194 => "ensp", # en space 8195 => "emsp", # em space 8201 => "thinsp", # thin space 8204 => "zwnj", # zero width non-joiner 8205 => "zwj", # zero width joiner @@ -368,12 +368,14 @@ # The output of an escape. # # It's marked with this special class for two reasons: # - # * Don't double escape the same string (this is for `Hanami::Helpers` compatibility) - # * Leave open the possibility to developers to mark a string as safe with an higher API (eg. `#raw` in `Hanami::View` or `Hanami::Helpers`) + # * Don't double escape the same string (this is for `Hanami::Helpers` + # compatibility) + # * Leave open the possibility to developers to mark a string as safe + # with an higher API (eg. `#raw` in `Hanami::View` or `Hanami::Helpers`) # # @since 0.4.0 # @api private class SafeString < ::String # @return [SafeString] the duped string @@ -397,11 +399,11 @@ def encode(*args) self.class.new super end end - # Escape HTML contents + # Escapes HTML contents # # This MUST be used only for tag contents. # Please use `html_attribute` for escaping HTML attributes. # # @param input [String] the input @@ -430,11 +432,11 @@ end result end - # Escape HTML attributes + # Escapes HTML attributes # # This can be used both for HTML attributes and contents. # Please note that this is more computational expensive. # If you need to escape only HTML contents, please use `.html`. # @@ -463,11 +465,11 @@ end result end - # Escape URL for HTML attributes (href, src, etc..). + # Escapes URL for HTML attributes (href, src, etc..). # # It extracts from the given input the first valid URL that matches the # whitelisted schemes (default: http, https and mailto). # # It's possible to pass a second optional argument to specify different @@ -521,11 +523,11 @@ end class << self private - # Encode the given string into UTF-8 + # Encodes the given string into UTF-8 # # @param input [String] the input # # @return [String] an UTF-8 encoded string # @@ -555,10 +557,10 @@ hex = hex_for_non_alphanumeric_code(code) return char if hex.nil? hex = REPLACEMENT_HEX if NON_PRINTABLE_CHARS[code] - if entity = HTML_ENTITIES[code] # rubocop:disable Lint/AssignmentInCondition + if entity = HTML_ENTITIES[code] "&#{entity};" else "&#x#{hex};" end end