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

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

@janko

Description

@janko

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions