Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight.js strips whitespace in pre tag #3593

Closed
Martmists-GH opened this issue Aug 6, 2022 · 7 comments
Closed

Highlight.js strips whitespace in pre tag #3593

Martmists-GH opened this issue Aug 6, 2022 · 7 comments
Labels
bug help welcome Could use help from community needs reproduction Needs a reproducible example or sample parser

Comments

@Martmists-GH
Copy link

Martmists-GH commented Aug 6, 2022

Describe the issue/behavior that seems buggy

When calling hljs.highlightElement, it strips all whitespace.

Sample Code or Instructions to Reproduce

fun `editor$transformer`() {  // called on page load
    document.querySelector<HTMLDivElement>(".md-editor")?.let { editor ->
        val input = editor.querySelector<HTMLTextAreaElement>(".md-editor-input")!!
        val code = editor.querySelector<HTMLElement>(".md-editor-code")!!

        input.addEventListener<InputEvent>("input") {
            var text = input.value
            if (text.isNotEmpty() && text.last() == '\n') {
                text += " "
            }
            println(text)
            code.innerText = text
            hljs.highlightElement(code)  // Removing this keeps whitespace as intended
            Unit
        }
    }
}
            <div class="row md-editor">
                <div class="col-7">
                    <div class="content md-editor-out">
                    </div>
                </div>
                <div class="col-5">
                    <div class="content md-editor-wrapper">
                        <textarea class="md-editor-input" spellcheck="false"></textarea>
                        <pre class="md-editor-highlight" aria-hidden="true"><code class="md-editor-code language-markdown"></code></pre>
                    </div>
                </div>
            </div>

Expected behavior
Highlight.js correctly keeps spaces.

Additional context
Disabling highlighting preserves spaces.

@Martmists-GH Martmists-GH added bug help welcome Could use help from community parser labels Aug 6, 2022
@joshgoebel
Copy link
Member

It's the APIs you are using. Use textContent instead of innerText.

@Martmists-GH
Copy link
Author

Martmists-GH commented Aug 6, 2022

using textContent preserves spaces but still strips newlines.
I also tried using innerHTML and replacing spaces with &nbsp; and newlines with <br />, both of which are stripped by highlight.js

@joshgoebel
Copy link
Member

joshgoebel commented Aug 6, 2022

using textContent preserves spaces but still strips newlines.

Please provide a jsfiddle demonstrating this and I will review. If your taking HTML out of another element you'll be responsible for doing any work necessary for preserving breaks, etc - such as possibly converting BR tags into new lines.

Highlight.js merely requests the textContent property, which does not render HTML to white space. It silently drops HTML.

@Martmists-GH
Copy link
Author

Unfortunately JSFiddle does not support Kotlin/JS, so that's impossible...

@joshgoebel
Copy link
Member

joshgoebel commented Sep 12, 2022

using textContent preserves spaces but still strips newlines.

It does not - IF they are textual (\n), or no one would be able to use the library... as I stated earlier <br> is not supported or will need to be handled by you manually if you need to support <br>, first replace them in the content with \n before doing the highlighting, etc...

@joshgoebel
Copy link
Member

JSFiddle does not support Kotlin/JS, so that's impossible...

We are a pure JS library, if there is a bug with the library itself then it should be possible to reproduce it with just JS (and JSFiddle) - no need for Kotlin. But as mentioned, I think you're seeing the expected behavior here...

@joshgoebel joshgoebel added the needs reproduction Needs a reproducible example or sample label Sep 14, 2022
@joshgoebel
Copy link
Member

joshgoebel commented Sep 14, 2022

Closing until a test JSFiddle case can be provided. Right now this sounds like expected behavior that can be resolved by using clean text (no HTML) inside the block and using the textContent API exclusively.

If you truly need BR, consider the use of a plugin or manually replacing the <br> with real line breaks BEFORE highlighting. Ref: #2559

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help welcome Could use help from community needs reproduction Needs a reproducible example or sample parser
Projects
None yet
Development

No branches or pull requests

2 participants