|
'use strict'; |
|
const { INVALID_CONTENT, INVALID_SRC_ATTR, INVALID_WORKER_ATTR } = require('../errors.js'); |
|
|
|
const { dedent, unescape } = require('../utils.js'); |
|
|
|
const hasCommentsOnly = text => !text |
|
.replace(/\/\*[\s\S]*?\*\//g, '') |
|
.replace(/^\s*(?:\/\/|#).*/gm, '') |
|
.trim() |
|
; |
|
|
|
|
|
module.exports = element => { |
|
const { src, worker } = element.attributes; |
|
if (worker) { |
|
let { value } = worker; |
|
|
|
|
|
if (value) throw new SyntaxError(INVALID_WORKER_ATTR); |
|
value = src?.value; |
|
if (!value) { |
|
|
|
if (src) throw new SyntaxError(INVALID_SRC_ATTR); |
|
if (!element.childElementCount) |
|
value = element.textContent; |
|
else { |
|
const { innerHTML, localName, type } = element; |
|
const name = type || localName.replace(/-script$/, ''); |
|
value = unescape(innerHTML); |
|
console.warn( |
|
`Deprecated: use <script type="${name}"> for an always safe content parsing:\n`, |
|
value, |
|
); |
|
} |
|
|
|
const url = URL.createObjectURL(new Blob([dedent(value)], { type: 'text/plain' })); |
|
|
|
|
|
|
|
return url; |
|
} |
|
return value; |
|
} |
|
|
|
if (src && !hasCommentsOnly(element.textContent)) |
|
throw new SyntaxError(INVALID_CONTENT); |
|
}; |
|
|
|
|