Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Additional non-breaking space added at the end of lines #69

Open
1 task done
janko opened this issue Oct 18, 2019 · 3 comments
Open
1 task done

Additional non-breaking space added at the end of lines #69

janko opened this issue Oct 18, 2019 · 3 comments

Comments

@janko
Copy link

janko commented Oct 18, 2019

Prerequisites

Description

I'm using highlights as a library outside of Atom. It works great, except that I noticed that in certain cases it adds trailing <span>&nbsp;</span> at the end of lines.

This is not a problem visually, but when developers are copy-pasting code into their editors, the trailing whitespace will get pasted as well.

Steps to Reproduce

Run the following script:

const Highlights = require('highlights')

const highlighter = new Highlights()

const rubyCode = `Sequel.migration do
  change do
    add_column :photos, :image_data, :text # or :jsonb
  end
end`

let html = highlighter.highlightSync({
  fileContents: rubyCode,
  scopeName: `source.ruby`,
})

console.log(html)

Expected behavior:

I expect only whitespace to be translated into &nbsp;.

Actual behavior:

Additional trailing &nbsp; character is added after the # or :jsonb comment:

<pre class="editor editor-colors">
  <div class="line"><span class="source ruby"><span class="support class ruby"><span>Sequel</span></span><span class="punctuation separator method ruby"><span>.</span></span><span>migration&nbsp;</span><span class="keyword control start-block ruby"><span>do</span></span></span></div>
  <div class="line"><span class="source ruby"><span>&nbsp;&nbsp;change&nbsp;</span><span class="keyword control start-block ruby"><span>do</span></span></span></div>
  <div class="line"><span class="source ruby"><span>&nbsp;&nbsp;&nbsp;&nbsp;add_column&nbsp;</span><span class="constant other symbol ruby"><span class="punctuation definition constant ruby"><span>:</span></span><span>photos</span></span><span class="punctuation separator object ruby"><span>,</span></span><span>&nbsp;</span><span class="constant other symbol ruby"><span class="punctuation definition constant ruby"><span>:</span></span><span>image_data</span></span><span class="punctuation separator object ruby"><span>,</span></span><span>&nbsp;</span><span class="constant other symbol ruby"><span class="punctuation definition constant ruby"><span>:</span></span><span>text</span></span><span>&nbsp;</span><span class="comment line number-sign ruby"><span class="punctuation definition comment ruby"><span>#</span></span><span>&nbsp;or&nbsp;:jsonb</span><span>&nbsp;</span></span></span></div>
  <div class="line"><span class="source ruby"><span>&nbsp;&nbsp;</span><span class="keyword control ruby"><span>end</span></span></span></div>
  <div class="line"><span class="source ruby"><span class="keyword control ruby"><span>end</span></span></span></div>
</pre>

I believe this is because highlights converts empty strings into &nbsp;, we can see there is one extra empty string in the list of tokens:

[ [ { value: 'Sequel', scopes: [Array] },
    { value: '.', scopes: [Array] },
    { value: 'migration ', scopes: [Array] },
    { value: 'do', scopes: [Array] } ],
  [ { value: '  change ', scopes: [Array] },
    { value: 'do', scopes: [Array] } ],
  [ { value: '    add_column ', scopes: [Array] },
    { value: ':', scopes: [Array] },
    { value: 'photos', scopes: [Array] },
    { value: ',', scopes: [Array] },
    { value: ' ', scopes: [Array] },
    { value: ':', scopes: [Array] },
    { value: 'image_data', scopes: [Array] },
    { value: ',', scopes: [Array] },
    { value: ' ', scopes: [Array] },
    { value: ':', scopes: [Array] },
    { value: 'text', scopes: [Array] },
    { value: ' ', scopes: [Array] },
    { value: '#', scopes: [Array] },
    { value: ' or :jsonb', scopes: [Array] },
    { value: '', scopes: [Array] } ],
  [ { value: '  ', scopes: [Array] },
    { value: 'end', scopes: [Array] } ],
  [ { value: 'end', scopes: [Array] } ] ]

Reproduces how often:

Every time.

Versions

Highlights 3.1.4

@janko
Copy link
Author

janko commented Oct 18, 2019

Indeed, this is where an empty string value gets converted into a space (because an empty string is falsy), which then gets converted into &nbsp;:

value = ' ' unless value

@rsese
Copy link

rsese commented Oct 23, 2019

Thanks for the report!

I'm using highlights as a library outside of Atom. It works great, except that I noticed that in certain cases it adds trailing <span>&nbsp;</span> at the end of lines.

Just to mention up front, with our current resources we're specifically not prioritizing issues that aren't causing a problem in Atom. To clarify, is this behavior causing a particular issue in Atom?

@janko
Copy link
Author

janko commented Oct 23, 2019

Not that I know of, I don't use Atom, I've only experienced it when using the library standalone.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants