Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 7x 1x 1x 1x 1x 6x 7x 4x 4x 4x 4x 2x 4x 2x 2x 2x 4x 6x 7x 1x 1x 6x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 6x 6x 4x 4x 4x 4x 4x 4x | /* eslint @cspell/spellchecker: ['warn', {cspell: {dictionaries: ['html']}}] */ /** * A stricter sanitizer that allows only text related tags and attributes as to allow any sort of content formatting, * makes no sense to use pictures or forms as html content in i18n, you translate mostly text, so you use data-i18n on the * form elements instead of using html as translation value. */ /** * A set of allowed tags for the sanitizer decide whether to remove an element or not * Any element where its tag name is absent on the set is to be removed */ export const ALLOWED_TAGS = new Set([ 'a', 'abbr', 'acronym', 'address', 'area', 'article', 'aside', 'b', 'bdi', 'bdo', 'big', 'blockquote', 'br', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'content', 'data', 'datalist', 'dd', 'decorator', 'del', 'details', 'dfn', 'dir', 'div', 'dl', 'dt', 'element', 'em', 'fieldset', 'figcaption', 'figure', 'font', 'footer', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'i', 'ins', 'kbd', 'label', 'legend', 'li', 'main', 'map', 'mark', 'marquee', 'meter', 'nav', 'nobr', 'ol', 'p', 'pre', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'section', 'small', 'source', 'spacer', 'span', 'strike', 'strong', 'style', 'sub', 'summary', 'sup', 'time', 'tr', 'track', 'tt', 'u', 'ul', 'var', 'wbr', ]) /** * A set of allowed attribute for the sanitizer decide whether to remove an attribute from an element or not. * Any attribute where its name is absent on the set is to be removed except data-* attribute not related to i18n, * and aria-* attributes */ export const ALLOWED_STANDARD_ATTRS = new Set([ 'accept', 'action', 'align', 'alt', 'autocapitalize', 'autocomplete', 'autoplay', 'background', 'bgcolor', 'border', 'capture', 'cellpadding', 'cellspacing', 'checked', 'cite', 'class', 'clear', 'color', 'cols', 'colspan', 'controls', 'coords', 'crossorigin', 'datetime', 'decoding', 'default', 'dir', 'disabled', 'download', 'draggable', 'enctype', 'enterkeyhint', 'face', 'for', 'headers', 'height', 'hidden', 'high', 'href', 'hreflang', 'id', 'inputmode', 'integrity', 'ismap', 'kind', 'label', 'lang', 'list', 'loading', 'loop', 'low', 'max', 'maxlength', 'media', 'method', 'min', 'minlength', 'multiple', 'muted', 'name', 'nonce', 'noshade', 'novalidate', 'nowrap', 'open', 'optimum', 'pattern', 'placeholder', 'playsinline', 'poster', 'preload', 'radiogroup', 'readonly', 'rel', 'required', 'rev', 'reversed', 'role', 'rows', 'rowspan', 'spellcheck', 'scope', 'selected', 'shape', 'size', 'sizes', 'span', 'srclang', 'start', 'src', 'srcset', 'step', 'style', 'summary', 'slot', 'tabindex', 'title', 'translate', 'type', 'usemap', 'valign', 'value', 'width', 'xmlns', ]) /** * @typedef RemovedAttr * @property {string} name - removed attribute name * @property {Element} from - target Element where the attribute was removed */ /** * @typedef SanitizeHtmlResult * * The result of `sanitizeI18nHtml`, show the sanitized html as well as the actions performed during sanitization * @property {string} html - sanitized html result * @property {Element[]} removedElements - list of removed elements * @property {RemovedAttr[]} removedAttributes - list of removed attributes */ /** * Initializes a Document and shows its body * @param {string} html - HTML input string to sanitize * @returns {HTMLElement} Sanitized Html string */ const _initDocument = (html) => { const doc = new DOMParser().parseFromString('<remove></remove>' + html, 'text/html') doc.body.firstChild?.remove() return doc.body } /** * @param {object} param - function parameters * @param {Element} param.currentElement - current element being sanitized * @param {Element[]} param.removedElements - list of removed elements, mutable * @param {RemovedAttr[]} param.removedAttributes - list of removed attributes, mutable */ const sanitizeElement = ({ currentElement, removedElements, removedAttributes }) => { if (!ALLOWED_TAGS.has(currentElement.tagName.toLowerCase())) { removedElements.push(currentElement) currentElement.remove() return } const { attributes } = currentElement for (const attribute of Array.from(attributes)) { const name = attribute.name.toLowerCase() if ( name === 'data-i18n' || name.startsWith('data-i18n-') || (!name.startsWith('data-') && !name.startsWith('aria-') && !ALLOWED_STANDARD_ATTRS.has(name)) ) { removedAttributes.push({ name, from: currentElement }) attributes.removeNamedItem(name) } } for (const child of Array.from(currentElement.children)) { sanitizeElement({ removedElements, removedAttributes, currentElement: child }) } } /** * Sanitizes The HTML input * @param {string} html - HTML input string to sanitize * @returns {SanitizeHtmlResult} Sanitization result */ export function sanitizeI18nHtml (html) { /** @type {Element[]} */ const removedElements = [] /** @type {RemovedAttr[]} */ const removedAttributes = [] const doc = _initDocument(html) for (const currentElement of Array.from(doc.children)) { sanitizeElement({ removedElements, removedAttributes, currentElement }) } return { html: doc.innerHTML, removedElements, removedAttributes, } } |