Skip to content

Fix when one of originalContent/changedContent is empty - #28

Open
erikn69 wants to merge 1 commit into
arnab:masterfrom
erikn69:patch-5
Open

erikn69 wants to merge 1 commit into
arnab:masterfrom
erikn69:patch-5

Conversation

@erikn69

@erikn69 erikn69 commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

When one of the two values are empty, the diff is not shown in case of originalContent and changedContent are passed as parameters.

Example (pass an empty originalContent):

$("input[type=button2]").click(function () {
 $("#wrapper tr").prettyTextDiff({
   cleanup: $("#cleanup").is(":checked"),
   originalContent: "",
   changedContent: "Some more text which can be passed to this function.",
   diffContainer: ".diff2"
 });
});

Potential XSS risk with .html() on detached elements

The $('<i/>').html(settings.originalContent).text() pattern is not fully safe against XSS, even though the element is never appended to the DOM.

<script> tags inserted via .html()/innerHTML never execute — that's guaranteed by the HTML5 spec, regardless of whether the element is attached to the document or not.

However, event handlers like onerror on <img> elements can still fire even on detached elements, because the browser starts attempting to load the resource (src) as soon as the element is parsed, not when it's inserted into the document. If the load fails, onerror fires.

Example payload that still executes despite never touching the live DOM:

let settings = {
  originalContent: '<img src=x onerror="alert(document.cookie)">',
}
$('<i/>').html(settings.originalContent).text();
// onerror still fires, even though <i> was never appended to document

I've replaced html() with text().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When one of settings.originalContent && settings.changedContent are empty

1 participant