How can i check whether an element can host Shadow DOM?

swelet

Elements that allow Shadow DOM are specified here: https://docs.w3cub.com/dom/element/attachshadow/

How can i validate if an element supports Shadow DOM or not ?

if (myElement.attachShadow) {
    ...
}

does not seem to work.

Kaiido

You could try and catch if unsupported.

try {
   let shadowRoot = elem.attachShadow( { mode: "open" } );
   shadowRoot.innerHTML = ...;
   return true;
} catch( err ) {
   return false;
}

You could also turn this into a helper, but if you need to use it several times, it might then be a good idea to store the results after checking each element.

function canAttachShadow( elem ) {
  try {
    elem.cloneNode().attachShadow( { mode: "open" } );
    return true;
  }
  catch( err ) {
    return false;
  }
}
  
console.log( "a", canAttachShadow( document.createElement( "a" ) ) );
console.log( "article", canAttachShadow( document.createElement( "article" ) ) );

If you really wish, you could also use the specs algorithm, which is a combination of valid-custom-element-name and the white list in your article (i.e today, "article", "aside", "blockquote", "body", "div", "footer", "h1", "h2", "h3", "h4", "h5", "h6", "header", "main", "nav", "p", "section", or "span"), however this white list may change in the future, so any code using it would need to get updated along with the specs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I check whether a jQuery element is in the DOM or not?

From Dev

how can I check whether an element does exist or not?

From Java

How can I check if an element exists in the visible DOM?

From Dev

How can I check the variable whether it exists or not

From Dev

How can I use the :first-child selector w/ shadow DOM's <content> element?

From Dev

How can I use the :first-child selector w/ shadow DOM's <content> element?

From Dev

How can I access the host of a custom element

From Dev

How can I access the host of a custom element

From Dev

Can I access the shadow DOM using jQuery?

From Dev

How i can do to animate shadow of the element with jquery?

From Java

How can I add a box-shadow on one side of an element?

From Dev

Shadow DOM: how to apply CSS style only if the host element is <body>?

From Java

How can I check whether dark mode is enabled in iOS/iPadOS?

From Java

How can I check whether an optional parameter was provided?

From Dev

How can I check whether two branches are "even"?

From Dev

How can I check whether a word is reserved by PHP?

From Dev

In R, how can I check whether a list contains a particular key?

From Dev

How can I check whether a member function has const overload?

From Dev

How can I check whether the data already exists in combobox list?

From Dev

How can I check whether the quiz answer was correct?

From Dev

How can I check whether my RDD or dataframe is cached or not?

From Dev

How can I check whether array of objects is empty or not?

From Dev

How can I generate my code to check whether the word is palindrome or not?

From Dev

How can I check whether user is currently using the audio seekbar?

From Dev

How can I check the whether a Facebook Page is verified with Graph API?

From Dev

How can I check whether the build is triiggered by timer or by the user?

From Dev

How can I check, whether a file name exists in a directory?

From Dev

How can i check whether the browser is active or not in crossrider?

From Dev

How can I check whether the numpy array exists already or not?

Related Related

  1. 1

    How can I check whether a jQuery element is in the DOM or not?

  2. 2

    how can I check whether an element does exist or not?

  3. 3

    How can I check if an element exists in the visible DOM?

  4. 4

    How can I check the variable whether it exists or not

  5. 5

    How can I use the :first-child selector w/ shadow DOM's <content> element?

  6. 6

    How can I use the :first-child selector w/ shadow DOM's <content> element?

  7. 7

    How can I access the host of a custom element

  8. 8

    How can I access the host of a custom element

  9. 9

    Can I access the shadow DOM using jQuery?

  10. 10

    How i can do to animate shadow of the element with jquery?

  11. 11

    How can I add a box-shadow on one side of an element?

  12. 12

    Shadow DOM: how to apply CSS style only if the host element is <body>?

  13. 13

    How can I check whether dark mode is enabled in iOS/iPadOS?

  14. 14

    How can I check whether an optional parameter was provided?

  15. 15

    How can I check whether two branches are "even"?

  16. 16

    How can I check whether a word is reserved by PHP?

  17. 17

    In R, how can I check whether a list contains a particular key?

  18. 18

    How can I check whether a member function has const overload?

  19. 19

    How can I check whether the data already exists in combobox list?

  20. 20

    How can I check whether the quiz answer was correct?

  21. 21

    How can I check whether my RDD or dataframe is cached or not?

  22. 22

    How can I check whether array of objects is empty or not?

  23. 23

    How can I generate my code to check whether the word is palindrome or not?

  24. 24

    How can I check whether user is currently using the audio seekbar?

  25. 25

    How can I check the whether a Facebook Page is verified with Graph API?

  26. 26

    How can I check whether the build is triiggered by timer or by the user?

  27. 27

    How can I check, whether a file name exists in a directory?

  28. 28

    How can i check whether the browser is active or not in crossrider?

  29. 29

    How can I check whether the numpy array exists already or not?

HotTag

Archive