Skip to content

Commit

Permalink
enh(parser) Warn if unescaped HTML is present (#3057)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Mar 22, 2021
1 parent fbdbc7b commit 8791320
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/highlight.js
Expand Up @@ -42,6 +42,7 @@ const HLJS = function(hljs) {
// calling the `hljs.configure` function.
/** @type HLJSOptions */
let options = {
ignoreUnescapedHTML: false,
noHighlightRe: /^(no-?highlight)$/i,
languageDetectRe: /\blang(?:uage)?-([\w-]+)\b/i,
classPrefix: 'hljs-',
Expand Down Expand Up @@ -668,15 +669,20 @@ const HLJS = function(hljs) {

if (shouldNotHighlight(language)) return;

// support for v10 API
fire("before:highlightElement",
{ el: element, language: language });

// we should be all text, no child nodes
if (!options.ignoreUnescapedHTML && element.children.length > 0) {
console.warn("One of your code blocks includes unescaped HTML. This is a potentially serious security risk.");
console.warn("https://github.com/highlightjs/highlight.js/issues/2886");
console.warn(element);
}

node = element;
const text = node.textContent;
const result = language ? highlight(text, { language, ignoreIllegals: true }) : highlightAuto(text);

// support for v10 API
fire("after:highlightElement", { el: element, result, text });

element.innerHTML = result.value;
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Expand Up @@ -123,6 +123,7 @@ interface HLJSOptions {
classPrefix: string
languages?: string[]
__emitter: EmitterConstructor
ignoreUnescapedHTML?: boolean
}

interface CallbackResponse {
Expand Down

0 comments on commit 8791320

Please sign in to comment.