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.

(jsx) multiple self-closing tags with attributes highlighting broken

See original GitHub issue

Given jsx code:

class App extends Component {
  render() {
    return (
      <BrowserRouter>
        <div>
          <Route path="/about" component={About} />
          <Route path="/contact" component={Contact} />
        </div>
      </BrowserRouter>
    );
  }
}

2018-11-28_114855

It seems that the XML code is not highlighted because of the second Route tag with attributes. When I tried to modify src/language/javascript.js

          { // E4X / JSX
            begin: /</, end: /(\/\w+|\w+\/)>/,
            subLanguage: 'xml',
            contains: [
              {begin: /<\w+\s*\/>/, skip: true},
              {
-               begin: /<\w+/, end: /(\/\w+|\w+\/)>/, skip: true,
+               begin: /<\w+/, end: /(\/\w+[^>]*|\w+\S*\s*\/)>/, skip: true,

                contains: [
                  {begin: /<\w+\s*\/>/, skip: true},
                  'self'
                ]
              }
            ]
          }

The highlighting works as expected.

2018-11-28_115530

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
joshgoebelcommented, Oct 7, 2019

https://github.com/highlightjs/highlight.js/issues/1625 reminded me that it’s not JUST HTML inside these blocks, but you can have JS style comments, template variables, etc…

The more I read the more it seems a separate JSX grammar might be the right choice for dealing with all of this.

0reactions
leafOfTreecommented, Dec 16, 2019

Thanks for tracking these jsx issues. @yyyc514

@leafOfTree

+      {
+        className: 'jsx-attr',
+        begin: /={/,
+        end: /}/,
+        subLanguage: 'xml',
+      },

Actually technically isn’t the sublanguage we should use javascript? The {} just allowed you to include javascript (event handlers, etc) which could also be more JSX code… correct?

And can’t {} truly be used anywhere, not just for attributes?

You are right about the {}. I know a little about highlight.js and I used xml as sublanguage beacuse src/language/javascript.js uses sublanguage: xml for jsx and it seems to work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React component closing tag - Stack Overflow
I've created a basic tab scroller as a JSFiddle using the self-closing <MyComponent /> and subsequent props, and I'm wondering if there's a ......
Read more >
JSX fragment closing tag with a space breaks syntax ...
JSX multielement tag's ( <> ) closing counterpart ( </> ) is wrongly identified as a syntax error if there is a space...
Read more >
Introducing JSX - React
This funny tag syntax is neither a string nor HTML. ... We split JSX over multiple lines for readability. ... Specifying Attributes with...
Read more >
How do I get matchit to work with '/>' (self-closed tags)?
I really like to jump from one <el> to the end of </el> with matchit. I am using it for jsx, but it...
Read more >
Troubleshooting MDX
The reason for this error is that only open tags can be closed. You probably forgot an opening tag somewhere. Unexpected attribute in...
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