Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pr-body.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Summary

When `highlightElement` is called on already-highlighted code (e.g., consecutive `hljs.highlightAll()` calls), the existing `<span class="hljs-*">` tags from the previous highlighting are incorrectly flagged as "unescaped HTML" - a false positive security warning.

## Fix

This fix checks for the `data-highlighted` attribute early and silently skips re-highlighting, matching the proposed solution in issue #3761.

## Test

The fix prevents the false warning when calling `hljs.highlightAll()` multiple times on the same code blocks.

Fixes #3761
3 changes: 2 additions & 1 deletion src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@ const HLJS = function(hljs) {
{ el: element, language });

if (element.dataset.highlighted) {
console.log("Element previously highlighted. To highlight again, first unset `dataset.highlighted`.", element);
// Already highlighted - skip to avoid false "unescaped HTML" warnings
// caused by hljs's own <span> tags from previous highlighting
return;
}

Expand Down
9 changes: 8 additions & 1 deletion src/languages/bash.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ export default function(hljs) {
"yes"
];

// Match command-line options like --project, --deployment-package, -f, etc.
const OPTIONS = {
className: 'attribute',
begin: /--[\w-]+|-\w(?!\s*=)/,
};

return {
name: 'Bash',
aliases: [
Expand Down Expand Up @@ -401,7 +407,8 @@ export default function(hljs) {
ESCAPED_QUOTE,
APOS_STRING,
ESCAPED_APOS,
VAR
VAR,
OPTIONS
]
};
}