atom-script icon indicating copy to clipboard operation
atom-script copied to clipboard

HTML output renders/causes errors.

Open hansrodtang opened this issue 12 years ago • 9 comments

I was testing outputting HTML to the terminal for debugging reasons and and something similar to the image above turned up, not really ideal and could cause errors for other output that includes HTML-like syntax or symbols.

Can be fixed by adding:

line = line.replace /</g, '&lt;'.replace />/g, '&gt;'

before ansiFilter in display, but there are probably better ways to do it.

hansrodtang avatar Mar 26 '14 00:03 hansrodtang

Thanks for reporting this.

I'll have to do some thinking about how to handle ansiFilter.

rgbkrk avatar Mar 26 '14 15:03 rgbkrk

I'd like it to be as simple as escaping brackets like you did above, but we're going to run into issues on other entities that should be escaped as well (&, etc.). Rather than escaping them ourselves, I'd like to rely on good libraries/jQuery/spacepen to do it for us.

rgbkrk avatar Mar 26 '14 16:03 rgbkrk

Underscore has a great one to handle that. _.escape

ciarand avatar Mar 26 '14 17:03 ciarand

The way underscore does it is pretty much the exact same thing I did just with added ",' and & added in. Wouldn't it be a little bloated to add a library for that when adding those characters to my hack would be an identical fix?

hansrodtang avatar Mar 26 '14 17:03 hansrodtang

The way Mustache.js does it pretty clean and trivial to port to CoffeeScript. Since HTML is the only thing that would risk being rendered in the Atom browser environment it should be enough.

var entityMap = {
    "&": "&amp;",
    "<": "&lt;",
    ">": "&gt;",
    '"': '&quot;',
    "'": '&#39;',
    "/": '&#x2F;'
  };

  function escapeHtml(string) {
    return String(string).replace(/[&<>"'\/]/g, function (s) {
      return entityMap[s];
    });
  }

hansrodtang avatar Mar 26 '14 17:03 hansrodtang

There's a huge number of packages that rely on the Atom version of underscore, but I can understand the hesitation. @rgbkrk?

ciarand avatar Mar 26 '14 17:03 ciarand

Ah, it slipped my mind that it might be included in Atom. If it is there is no reason not to use it.

hansrodtang avatar Mar 26 '14 17:03 hansrodtang

I thought for sure underscore-plus was included as part of Atom, but even vim-mode includes it as a dependency.

rgbkrk avatar Mar 26 '14 17:03 rgbkrk

I'm honestly fine with either having our own escape function or using underscore's. Either way, I've tested this out and am quite happy:

  display: (css, line) ->
    line = _.escape(line)
    line = @ansiFilter.toHtml(line)

    @output.append("<pre class='line #{css}'>#{line}</pre>")

rgbkrk avatar Mar 26 '14 17:03 rgbkrk