question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unclean work with class names (adding unneeded spaces during DOM manipulations)

See original GitHub issue

Information:

  • Prism version: latest (from the master branch)
  • Plugins: none
  • Environment: Browser

Description

The way how PrismJS works with class attributes for languages (See: https://github.com/PrismJS/prism/blob/master/components/prism-core.js#L260) is unclean since it introduces unnecessary leading white space, e.g. if the original HTML was:

<pre class="highlight">
  <code class="language-css">
     some code
  </code>
</pre>

After PrismJS.highlightAll() it would become (there are two spaces in front of language-XXX instead of one:

<pre class="highlight  language-css">
  <code class="  language-css">
     some code
  </code>
</pre>

It is a minor, mostly cosmetic issue, but it would be nice if PrismJS did the right thing and introduced the leading white space only if there is something in the class line already (and definitely use one space in that case 😃 )

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
RunDevelopmentcommented, Oct 25, 2021

So it actually does cause problems. I’ll make a fix shortly.

1reaction
xjAreacommented, Sep 2, 2020

@RunDevelopment

Oh, thank you very much for your quick reply. In fact, I was already writing, but I made a mistake in a parameter, so it did not take effect. See your code, I corrected the first parameter of the function, and it was able to run. On this basis, I tried to add the class on the <pre>(if it exists), and done.

👏👏👏 😀

Prism.hooks.add('complete', function(env){
	
    function addClass(node){ node.classList.add('prism-highlighted') };
    var parentNode = env.element.parentNode;
	
    if(parentNode && parentNode.nodeName.toLowerCase() === 'pre'){ addClass(parentNode) }
    else{ addClass(env.element) };
	
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add/remove a class in JavaScript? - Stack Overflow
Here is solution for addClass, removeClass, hasClass in pure javascript solution. ... classList which has remove(name) , add(name) , toggle(name) ...
Read more >
Why React classnames Is A Lifesaver And How You Could ...
In this tutorial, I cover why you should use classnames and how to use it ... just for the sake of changing up...
Read more >
How whitespace is handled by HTML, CSS, and in the DOM
Whitespace is any string of text composed only of spaces, tabs or line breaks (to be precise, CRLF sequences, carriage returns or line...
Read more >
Fighting the Space Between Inline Block Elements | CSS-Tricks
A series of inline-block elements with "normal" HTML formatting result in spaces between them when set on the same line.
Read more >
Google JavaScript Style Guide
Default import names are derived from the imported file name and follow the rules in 6.2 Rules by identifier type. import MyClass from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found