Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(parser) Warn if unescaped HTML is present #3057

Merged
merged 4 commits into from Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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