@@ -529,6 +529,34 @@ const escape_css_id = (id) => {
529529 return `#${ CSS . escape ( id . split ( "#" ) [ 1 ] ) } ` ;
530530} ;
531531
532+ /**
533+ * Get a universally unique id (uuid) for a DOM element.
534+ *
535+ * This method returns a uuid for the given element. On the first call it will
536+ * generate a uuid and store it on the element.
537+ *
538+ * @param {Node } el - The DOM node to get the uuid for.
539+ * @returns {String } - The uuid.
540+ */
541+ const element_uuid = ( el ) => {
542+ if ( ! get_data ( el , "uuid" , false ) ) {
543+ let uuid ;
544+ if ( window . crypto . randomUUID ) {
545+ // Create a real UUID
546+ // window.crypto.randomUUID does only exist in browsers with secure
547+ // context.
548+ // See: https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID
549+ uuid = window . crypto . randomUUID ( ) ;
550+ } else {
551+ // Create a sufficiently unique ID
552+ const array = new Uint32Array ( 4 ) ;
553+ uuid = window . crypto . getRandomValues ( array ) . join ( "" ) ;
554+ }
555+ set_data ( el , "uuid" , uuid ) ;
556+ }
557+ return get_data ( el , "uuid" ) ;
558+ } ;
559+
532560const dom = {
533561 toNodeArray : toNodeArray ,
534562 querySelectorAllAndMe : querySelectorAllAndMe ,
@@ -556,6 +584,7 @@ const dom = {
556584 template : template ,
557585 get_visible_ratio : get_visible_ratio ,
558586 escape_css_id : escape_css_id ,
587+ element_uuid : element_uuid ,
559588 add_event_listener : events . add_event_listener , // BBB export. TODO: Remove in an upcoming version.
560589 remove_event_listener : events . remove_event_listener , // BBB export. TODO: Remove in an upcoming version.
561590} ;
0 commit comments