Skip to content

Leading whitespace when selecting / copying code in Chrome #346

@campersau

Description

@campersau

Step 0: Describe your environment

  • OS: Windows 10
  • diff2html version: 3.1.14
  • Using diff2html directly or using diff2html-ui helper: the one the website is using
  • Extra flags: default settings used by website

Step 1: Describe the problem:

When selecting and copying code from diffs there is always an additional leading whitespace.
I have tracked down the issue to the template files which contain whitespace between the different HTML tags.

For example when I remove all the spaces and linebreaks from generic-line.mustache then it works correctly.

<tr><td class="{{lineClass}} {{type}}">{{{lineNumber}}}</td><td class="{{type}}"><div class="{{contentClass}} {{type}}">{{#prefix}}<span class="d2h-code-line-prefix">{{{prefix}}}</span>{{/prefix}}{{^prefix}}<span class="d2h-code-line-prefix">&nbsp;</span>{{/prefix}}{{#content}}<span class="d2h-code-line-ctn">{{{content}}}</span>{{/content}}{{^content}}<span class="d2h-code-line-ctn"><br></span>{{/content}}</div></td></tr>

(Maybe the d2h-code-line-prefix needs some more padding then e.g. padding-right: 7px;)

Handlebar.js has a special syntax specifically for that: https://handlebarsjs.com/guide/expressions.html#whitespace-control
But it does not work with hogan.js...

The issue came up in FredrikNoren/ungit#1430 by @simonwiles which contains a CSS only solution for this problem.

Another way to fix this is using :before and content: attr(data-prefix) CSS styles for the prefix instead of a separate HTML element like GitHub does.

Steps to reproduce:

  1. Go to https://diff2html.xyz/demo.html?matching=none&matchWordsThreshold=0.25&maxLineLengthHighlight=10000&diffStyle=word&renderNothingWhenEmpty=0&matchingMaxComparisons=2500&maxLineSizeInBlockForComparison=200&outputFormat=line-by-line&drawFileList=1&synchronisedScroll=1&highlight=1&fileListToggle=1&fileListStartVisible=0&smartSelection=1&diff=https://github.com/rtfpessoa/diff2html/pull/106
  2. Select and copy (multiple) lines (e.g. line 105 from the README.md file)
  3. Paste them in a text editor

diff example:

see URL above

Observed Results:

  • What happened? This could be a description, log output, etc.

Additional leading whitespace:

 ## Diff2HtmlUI Helper

Expected Results:

  • What did you expect to happen?

No, leading whitespace:

## Diff2HtmlUI Helper

Relevant Code:

Code used by the website (see URL above).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions