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.

Accept custom characters for attributes name

See original GitHub issue

Basic info:

  • Node.js v10.13.0:
  • jsdom 12.2.0:

Minimal reproduction case

const { JSDOM } = require("jsdom");

const dom = new JSDOM(`
  <lazy-image src="path-to-src"></lazy-image>
`);

let document = dom.window.document;

let image = document.createElement('img');

image.setAttribute('[src]', 'placeholder-image'); 
// throws
UnhandledPromiseRejectionWarning: InvalidCharacterError: "[src]" did not match the Name production: Unexpected syntax in top
Line 1:
[src]

I know this is not a standard naming of attributes, but can the thrown exception in /node_modules/jsdom/lib/jsdom/living/nodes/Element-impl.js line 240 become an optional

For example


  setAttribute(name, value) {
     try {
    validateNames.name(name);
   } catch(e) {
     if (! allowCustomAttributeNaming) { 
        throw e;
      }
   }
   ...
  }

The reason for this that i’m using Jsdom for compiling html files and some times some attributes are exchanged with another ones.

Also elements could be duplicated with same attributes but with extra/minimal attributes.

Mainly i use [custom-attribute] and (custom-event) as attributes for any element so it would be easier to maintain it with same syntax.

How does similar code behave in browsers?

It is not rendered in the browser as the passed html to jsdom is not being displayed directly to the browser.

The code is transformed to dom elements so the passed code will be handled only by the dom compiler.

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
Morglodcommented, Jul 25, 2019

I’m using jsdom to generate DOM tree in node without browser.
There is browser’s supported custom attribute names like ‘–smth’ for generated html code (not for setAttribute, yes).

Monkeypatch for this:

const validateNames = require('jsdom/lib/jsdom/living/helpers/validate-names.js');

const name_ = validateNames.name;
validateNames.name = function (name: string) {
    try {
        name_(name);
    } catch {}
};
1reaction
rubyjedicommented, May 26, 2020

+1 for adding this as an option - perhaps in the JSDOM constructor options?

I ran into this issue while trying to use JSDOM as a code-generator to produce PWA code (Angular, React, etc) from a collection of templates.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What characters are allowed in an HTML attribute name?
Attribute names must consist of one or more characters other than the space characters, U+0000 NULL, U+0022 QUOTATION MARK ("), U+0027 ...
Read more >
Relaxing restrictions on custom attribute names - HTML - WICG
Custom attribute names must contain a hyphen; The prefix must be at least 2 characters long; The prefix must not be in the...
Read more >
<input>: The Input (Form Input) element - HTML
A control that lets the user select a file. Use the accept attribute to define the types of files that the control can...
Read more >
Custom attributes in JavaScript | Trepachev Dmitry - code.mu
HTML allows you to add your own custom attributes to tags. Such attributes should start with data- followed by whatever attribute name you...
Read more >
Custom Object Name attribute limited to 80 char | Clarity
I agree to all the answers posted above. There is a Description field as you should be aware, where more characters can be...
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