Skip to content

TypeError: Cannot read properties of undefined (reading 'trim') #102

Open
@Billy-Sheppard

Description

@Billy-Sheppard

Describe the bug
JS console error from highlightjs-line-numbers.min.js.

To Reproduce
Steps to reproduce the behavior:

...
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/vs2015.min.css" integrity="sha512-mtXspRdOWHCYp+f4c7CkWGYPPRAhq9X+xCvJMUBVAb6pqA4U8pxhT3RWT3LP3bKbiolYL2CkL1bSKZZO4eeTew==" crossorigin="anonymous" referrerpolicy="no-referrer">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js" integrity="sha512-D9gUyxqja7hBtkWpPWGt9wfbfaMGVt9gnyCvYa+jojwwPHLCzUm5i8rpk7vD7wNee9bA35eYIjobYPaQuKS1MQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.8.0/highlightjs-line-numbers.min.js" integrity="sha512-axd5V66bnXpNVQzm1c7u1M614TVRXXtouyWCE+eMYl8ALK8ePJEs96Xtx7VVrPBc0UraCn63U1+ARFI3ofW+aA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
...

Have this in your HTML, use highlightjs (and line-numbers). Prints this to console.

highlightjs-line-numbers.min.js:1  
Uncaught TypeError: Cannot read properties of undefined (reading 'trim')
    at highlightjs-line-numbers.min.js:1:1620
    at f (highlightjs-line-numbers.min.js:1:1932)
    at highlightjs-line-numbers.min.js:1:1004

Expected behavior
No errors.

Desktop (please complete the following information):

  • OS: Ubuntu
  • Browser [e.g. chrome, safari]: Edge
  • Version [e.g. 22]: 125.0.2535.79

Activity

angelikatyborska

angelikatyborska commented on Nov 4, 2024

@angelikatyborska

I am also experiencing this error. To add some details, for me it happens for elements whose text content is empty. E.g.

<pre><code></code></pre>
const codeElement = document.querySelectorAll("pre code")
window.hljs.highlightElement(codeElement);
(window.hljs as HLJSApi & HLJSNumbers).lineNumbersBlock(codeElement, { singleLine: true });

The raising code is this line, it raises when lines is equal to []:
https://github.com/wcoder/highlightjs-line-numbers.js/blob/master/src/highlightjs-line-numbers.js#L213

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      `TypeError: Cannot read properties of undefined (reading 'trim')` · Issue #102 · wcoder/highlightjs-line-numbers.js