1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 24x 840x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x | /** * @copyright 2016, Miles Johnson * @license https://opensource.org/licenses/MIT * @flow */ /* eslint-disable no-useless-escape, max-len */ import type { NodeConfig } from './types'; // https://blog.codinghorror.com/the-problem-with-urls/ // http://www.regular-expressions.info/email.html type ConfigMap = { [key: string]: NodeConfig }; type FilterMap = { [key: string]: number }; // This pattern will be used at the start or end of other patterns, // making it easier to apply matches without capturing special chars. export const ALNUM_CHAR: string = '[a-z0-9]{1}'; export const HASHTAG_PATTERN: string = '#([-a-z0-9_]+)'; export const URL_CHAR_PART: string = 'a-z0-9-_~%!$&\'*+;:@'; // Disallow . , ( ) export const URL_SCHEME_PATTERN: string = '(https?://)?'; export const URL_AUTH_PATTERN: string = `([${URL_CHAR_PART}]+@)?`; export const URL_HOST_PATTERN: string = `((?:www\\.)?${ALNUM_CHAR}[-a-z0-9.]*[-a-z0-9]+\\.[a-z]{2,63})`; export const URL_PORT_PATTERN: string = '(:[0-9]+)?'; export const URL_PATH_PATTERN: string = `(/[${URL_CHAR_PART}/]*(?:\\.[a-z]{2,8})?)?`; export const URL_QUERY_PATTERN: string = `(\\?[${URL_CHAR_PART}=\\[\\]/\\\\\]*)?`; export const URL_FRAGMENT_PATTERN: string = '(#[\\w%/]*)?'; export const URL_PATTERN: string = [ URL_SCHEME_PATTERN, URL_AUTH_PATTERN, URL_HOST_PATTERN, URL_PORT_PATTERN, URL_PATH_PATTERN, URL_QUERY_PATTERN, URL_FRAGMENT_PATTERN, ].join(''); export const IP_V4_PART: string = '(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'; export const IP_V4_PATTERN: string = `(${IP_V4_PART}\\.${IP_V4_PART}\\.${IP_V4_PART}\\.${IP_V4_PART})`; export const IP_PATTERN: string = [ URL_SCHEME_PATTERN, URL_AUTH_PATTERN, IP_V4_PATTERN, URL_PORT_PATTERN, URL_PATH_PATTERN, URL_QUERY_PATTERN, URL_FRAGMENT_PATTERN, ].join(''); export const EMAIL_CLASS_PART: string = '[a-z0-9!#$%&?*+=_{|}~-]+'; export const EMAIL_USERNAME_PATTERN: string = `(${ALNUM_CHAR}${EMAIL_CLASS_PART}(?:\\.${EMAIL_CLASS_PART})*${ALNUM_CHAR})`; export const EMAIL_PATTERN: string = `${EMAIL_USERNAME_PATTERN}@${URL_HOST_PATTERN}`; // Parser rules for HTML tags export const PARSER_ALLOW: number = 1; // Allow element and children export const PARSER_DENY: number = 2; // Do not render this element or its children export const TYPE_INLINE: string = 'inline'; export const TYPE_BLOCK: string = 'block'; export const TYPE_INLINE_BLOCK: string = 'inline-block'; // Special case export const CONFIG_INLINE: NodeConfig = { rule: PARSER_ALLOW, type: TYPE_INLINE, inline: true, block: false, self: false, void: false, parent: [], children: [], }; export const CONFIG_BLOCK: NodeConfig = { rule: PARSER_ALLOW, type: TYPE_BLOCK, inline: true, block: true, self: true, void: false, parent: [], children: [], }; // Tags not listed here will be marked as pass-through // https://developer.mozilla.org/en-US/docs/Web/HTML/Element const tagConfigs = { a: { type: TYPE_INLINE_BLOCK, block: true, }, address: { self: false, }, audio: { children: ['track', 'source'], }, br: { inline: false, void: true, }, button: { type: TYPE_INLINE_BLOCK, }, dd: { parent: ['dl'], }, dl: { children: ['dt'], }, dt: { parent: ['dl'], children: ['dd'], }, figcaption: { parent: ['figure'], }, footer: { self: false, }, header: { self: false, }, h1: { self: false, }, h2: { self: false, }, h3: { self: false, }, h4: { self: false, }, h5: { self: false, }, h6: { self: false, }, hr: { inline: false, block: false, void: true, }, img: { inline: false, void: true, }, li: { self: false, parent: ['ul', 'ol'], }, main: { self: false, }, ol: { children: ['li'], }, picture: { children: ['source', 'img'], }, source: { inline: false, parent: ['audio', 'video', 'picture'], void: true, }, span: { self: true, }, table: { children: ['thead', 'tbody', 'tfoot', 'tr'], }, tbody: { parent: ['table'], children: ['tr'], }, td: { parent: ['tr'], }, tfoot: { parent: ['table'], children: ['tr'], }, th: { parent: ['tr'], }, thead: { parent: ['table'], children: ['tr'], }, tr: { parent: ['table', 'tbody', 'thead', 'tfoot'], children: ['th', 'td'], }, track: { inline: false, parent: ['audio', 'video'], void: true, }, ul: { children: ['li'], }, video: { children: ['track', 'source'], }, }; function createConfigBuilder(config: NodeConfig) { return (tagName: string) => { tagConfigs[tagName] = { ...config, ...(tagConfigs[tagName] || {}), }; }; } // Add inline tags [ 'a', 'abbr', 'b', 'br', 'button', 'cite', 'code', 'del', 'dfn', 'em', 'i', 'img', 'ins', 'kbd', 'label', 'mark', 'output', 'picture', 'q', 's', 'samp', 'source', 'span', 'strong', 'sub', 'sup', 'time', 'track', 'u', 'var', 'video', ].forEach( createConfigBuilder(CONFIG_INLINE), ); // Add block tags [ 'address', 'article', 'aside', 'audio', 'blockquote', 'dd', 'details', 'div', 'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'header', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'legend', 'li', 'main', 'nav', 'ol', 'p', 'pre', 'section', 'summary', 'table', 'thead', 'tbody', 'tfoot', 'tr', 'th', 'td', 'ul', ].forEach( createConfigBuilder(CONFIG_BLOCK), ); // Disable this map from being modified export const TAGS: ConfigMap = Object.freeze(tagConfigs); // Tags that should never be allowed, even if the whitelist is disabled export const TAGS_BLACKLIST: { [tagName: string]: boolean } = { applet: true, body: true, canvas: true, embed: true, frame: true, frameset: true, head: true, html: true, iframe: true, object: true, script: true, style: true, }; // Filters apply to HTML attributes export const FILTER_ALLOW: number = 1; export const FILTER_DENY: number = 2; export const FILTER_CAST_NUMBER: number = 3; export const FILTER_CAST_BOOL: number = 4; // Attributes not listed here will be denied // https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes export const ATTRIBUTES: FilterMap = Object.freeze({ alt: FILTER_ALLOW, cite: FILTER_ALLOW, class: FILTER_ALLOW, colspan: FILTER_CAST_NUMBER, controls: FILTER_CAST_BOOL, datetime: FILTER_ALLOW, default: FILTER_CAST_BOOL, disabled: FILTER_CAST_BOOL, dir: FILTER_ALLOW, height: FILTER_ALLOW, href: FILTER_ALLOW, id: FILTER_ALLOW, kind: FILTER_ALLOW, label: FILTER_ALLOW, lang: FILTER_ALLOW, loop: FILTER_CAST_BOOL, muted: FILTER_CAST_BOOL, poster: FILTER_ALLOW, role: FILTER_ALLOW, rowspan: FILTER_CAST_NUMBER, span: FILTER_CAST_NUMBER, src: FILTER_ALLOW, target: FILTER_ALLOW, title: FILTER_ALLOW, width: FILTER_ALLOW, }); // Attributes to camel case for React props export const ATTRIBUTES_TO_PROPS: { [key: string]: string } = Object.freeze({ class: 'className', colspan: 'colSpan', datetime: 'dateTime', rowspan: 'rowSpan', }); |