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.

HTML character entities throw "Expected valid tag name" error

See original GitHub issue

Description

When using HTML character entities like < in a MDsveX page with SvelteKit, Vite throws an Expected valid tag name error

(Note, before Vite 2.5.4 and SvelteKit 1.0.0-next.165 the error was a cryptic offset is longer than source length!. Thanks to https://github.com/vitejs/vite/pull/4782 this is now fixed.)

Reproduction

https://github.com/vwkd/sveltekit-bugs/tree/mdsvex-character-references

  1. Run npm run dev
  2. Observe error Expected valid tag name

System Info

  Binaries:
    Node: 16.6.0 - ~/.nvm/versions/node/v16.6.0/bin/node
    Yarn: 1.22.11 - ~/.nvm/versions/node/v16.6.0/bin/yarn
    npm: 7.19.1 - ~/.nvm/versions/node/v16.6.0/bin/npm
  npmPackages:
    @sveltejs/adapter-static: ^1.0.0-next.11 => 1.0.0-next.18
    @sveltejs/kit: next => 1.0.0-next.165
    mdsvex: ^0.9.8 => 0.9.8 
    svelte: ^3.34.0 => 3.42.5

Additional context

Maybe related to https://github.com/sveltejs/svelte/issues/6440 ?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
milahucommented, Aug 29, 2021

quickfix

// cjs js line 8716

      if (terminated) {
        end++;

        namedEntity = type === name$1 ? decodeEntity_1(characters) : false;

        // QUICKFIX
        if (namedEntity == '<') namedEntity = '&lt;';
        if (namedEntity == '>') namedEntity = '&gt;';

        if (namedEntity) {
          entityCharacters = characters;
          entity = namedEntity;
        }

much faster than fixing all the dependencies : ) how to apply with patch-package

git clone --depth 1 https://github.com/milahu/sveltekit-bugs.git --branch quickfix-with-patch-package
cd sveltekit-bugs
pnpm install
npm run dev
1reaction
milahucommented, Aug 29, 2021

digging deeper

node_modules/mdsvex/dist/main.cjs.js
// line 8722

        if (namedEntity) {
          entityCharacters = characters;
          entity = namedEntity;
        }

        // DEBUG
        console.log(`\n\nmdsvex cjs 8727: found entity in ${characters}. named ${namedEntity}. type ${type}`);
        //console.dir(new Error());

// line 8815

      // Found it!
      // First eat the queued characters as normal text, then eat an entity.
      if (reference) {
        flush();

        prev = now();
        index = end - 1;
        column += end - start + 1;
        result.push(reference);

        // DEBUG
        console.log(`mdsvex cjs 8827: push reference ${reference}`);

output

mdsvex cjs 8727: found entity in lt. named <. type named
mdsvex cjs 8827: push reference <

where does this code come from? lets ask github - looks like parse-entities some traces later, this is called by remark-parse.markdown

so the problem is …

	const toMDAST = unified()
		.use(markdown) // HERE

simple but wrong: escaping &lt; with &amp;lt; and &gt; with &amp;gt; would give false positives for example

&lt;
<div title="&lt;"/>

would become

&lt;
<div title="&amp;lt;"/>

so either set options via .use(markdown, options) or patch the markdown parser

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Expected valid tag name" when using special characters like
I am having an issue when using the < character inside a Markdown file. Even when replacing it with < . It seems...
Read more >
Error Explanations for The W3C Markup Validation Service
An entity reference was found in the document, but there is no reference by that name defined. Often this is caused by misspelling...
Read more >
13.2 Parsing HTML documents - HTML Standard - whatwg
This error occurs if the parser encounters the end of the input stream where a tag name is expected. In this case the...
Read more >
What are invalid characters in XML - Stack Overflow
I tried looking for a list of characters that cannot be put in XML nodes without being in a CDATA. Can someone point...
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
This string has to be valid JSON and will throw this error if incorrect syntax was encountered. Examples. JSON.parse() does not allow trailing...
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