htmldiff.net icon indicating copy to clipboard operation
htmldiff.net copied to clipboard

Script tags should be ignored

Open leonkyr opened this issue 10 years ago • 2 comments

script starts cssConstPopupClose = "nice"; var css = css || {}; css.archive = {}; css.archive.serverUrl = "https://google.com.au"; css.archive.disabled = false; css.archive.tenant = "css-lhb"; css.archive.mock = false; ends script 2) script starts cssConstPopupClose = "nice"; var css = css || {}; css.archive = {}; css.archive.serverUrl = "https://google.com.au"; css.archive.disabled = false; css.archive.tenant = "sanagate-lhb"; css.archive.mock = false; ends script

If you compare those two versions then the result is: script starts cssConstPopupClose = "nice"; var css = css || {}; css.archive = {}; css.archive.serverUrl = "https://google.com.au"; css.archive.disabled = false; css.archive.tenant = "css"sanagate-lhb"; css.archive.mock = false; ends script

This results in a javascript error and the comparison cannot be seen.

leonkyr avatar Feb 04 '15 15:02 leonkyr

I have a very rudimentary fix for this:

private static bool IsEndOfTag(string val, string currentWord)
{
  return val == ">"
    && (!currentWord.StartsWith("<script", StringComparison.OrdinalIgnoreCase)
      || (currentWord.StartsWith("<script", StringComparison.OrdinalIgnoreCase)
        && currentWord.ToLower().Contains("</script")));        
}

And then change the ConvertHtmlToListOfWords to to pass in the current word:

case DiffMode.Tag:
  if (IsEndOfTag(character, currentWord))
  {

This will at least collect the entire script tag as a word which can be diff'd. Problem is, if you are rendering the diff'd HTML, and you have the <del> and <ins> tags wrapped around your tags, the script tag is still intact, so will execute twice on the page.e.g:

<script>console.log('first');</script>

and

<script>console.log('second');</script>

Will result in

<del class='diffmod'><script>console.log('first');</script></del><ins class='diffmod'><script>console.log('second');</script></ins>

Not sure how best to handle that, but then that's probably not this libraries responsibility.

Antaris avatar May 29 '15 10:05 Antaris

why shouldn't it be this libraries responsibility @Antaris?

iamdjones avatar Feb 04 '16 03:02 iamdjones