2024-06-04 15:57:52 +02:00
<!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1" > < meta name = "description" content = "Basic concepts of elements modifications and creations." > < title > `deka-dom-el` — Elements< / title > <!-- <dde:mark type="component" name="metaAuthor" host="this" ssr/> --> < meta name = "author" content = "Jan Andrle" > < link type = "text/plain" rel = "author" href = "https://jaandrle.github.io/humans.txt" > < meta name = "generator" content = "deka-dom-el" > <!-- <dde:mark type="component" name="metaTwitter" host="this" ssr/> --> < meta name = "twitter:card" content = "summary_large_image" > < meta name = "twitter:url" content = "https://github.com/jaandrle/deka-dom-el" > < meta name = "twitter:title" content = "deka-dom-el" > < meta name = "twitter:description" content = "A low-code library that simplifies the creation of native DOM elements/components using small wrappers and tweaks." > < meta name = "twitter:creator" content = "@jaandrle" > <!-- <dde:mark type="component" name="metaFacebook" host="this" ssr/> --> < meta name = "og:url" content = "https://github.com/jaandrle/deka-dom-el" > < meta name = "og:title" content = "deka-dom-el" > < meta name = "og:description" content = "A low-code library that simplifies the creation of native DOM elements/components using small wrappers and tweaks." > < meta name = "og:creator" content = "@jaandrle" > < script src = "https://cdn.jsdelivr.net/npm/shiki@0.9" defer = "" > < / script > < script type = "module" src = "code.js.js" > < / script > < script src = "https://flems.io/flems.html" type = "text/javascript" charset = "utf-8" > < / script > < link rel = "stylesheet" href = "global.css" > < / head > < body > <!-- <dde:mark type="component" name="page" host="this" ssr/> --> <!-- <dde:mark type="component" name="simplePage" host="this" ssr/> --> <!-- <dde:mark type="component" name="header" host="this" ssr/> --> < header > < h1 > `deka-dom-el` — Elements< / h1 > < p > Basic concepts of elements modifications and creations.< / p > < / header > < nav > < a href = "https://github.com/jaandrle/deka-dom-el" > < svg class = "icon" viewBox = "0 0 32 32" > <!-- <dde:mark type="component" name="iconGitHub" host="parentElement" ssr/> --> < path d = "M 16,0.395c -8.836,0 -16,7.163 -16,16c 0,7.069 4.585,13.067 10.942,15.182c 0.8,0.148 1.094,-0.347 1.094,-0.77c 0,-0.381 -0.015,-1.642 -0.022,-2.979c -4.452,0.968 -5.391,-1.888 -5.391,-1.888c -0.728,-1.849 -1.776,-2.341 -1.776,-2.341c -1.452,-0.993 0.11,-0.973 0.11,-0.973c 1.606,0.113 2.452,1.649 2.452,1.649c 1.427,2.446 3.743,1.739 4.656,1.33c 0.143,-1.034 0.558,-1.74 1.016,-2.14c -3.554,-0.404 -7.29,-1.777 -7.29,-7.907c 0,-1.747 0.625,-3.174 1.649,-4.295c -0.166,-0.403 -0.714,-2.03 0.155,-4.234c 0,0 1.344,-0.43 4.401,1.64c 1.276,-0.355 2.645,-0.532 4.005,-0.539c 1.359,0.006 2.729,0.184 4.008,0.539c 3.054,-2.07 4.395,-1.64 4.395,-1.64c 0.871,2.204 0.323,3.831 0.157,4.234c 1.026,1.12 1.647,2.548 1.647,4.295c 0,6.145 -3.743,7.498 -7.306,7.895c 0.574,0.497 1.085,1.47 1.085,2.963c 0,2.141 -0.019,3.864 -0.019,4.391c 0,0.426 0.288,0.925 1.099,0.768c 6.354,-2.118 10.933,-8.113 10.933,-15.18c 0,-8.837 -7.164,-16 -16,-16Z" > < / path > < / svg > GitHub< / a > < a href = "./" title = "Introducing a library." > 1. Introduction< / a > < a href = "p02-elements" title = "Basic concepts of elements modifications and creations." class = "current" > 2. Elements< / a > < a href = "p03-events" title = "Using not only events in UI declaratively." > 3. Events and Addons< / a > < a href = "p04-signals" title = "Handling reactivity in UI via signals." > 4. Signals and reactivity< / a > < a href = "p05-scopes" title = "Organizing UI into components" > 5. Scopes and components< / a > < a href = "p06-customElement" title = "Using custom elements in combinantion with DDE" > 6. Web Components< / a > < / nav > < main > < h2 > Native JavaScript DOM elements creations< / h2 > < p > Let’ s go through all patterns we would like to use and what needs to be improved for better experience. < / p > < div class = "code" data-js = "todo" > <!-- <dde:mark type="component" name="code" host="parentElement" ssr/> --> < code class = "language-js" > // use NPM or for example https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm.js
2023-11-29 18:25:21 +01:00
import {
assign,
el, createElement,
elNS, createElementNS
} from "deka-dom-el";
el===createElement
elNS===createElementNS
2023-11-24 17:02:16 +01:00
// “internal” utils
import {
assignAttribute,
classListDeclarative,
chainableAppend
} from "deka-dom-el";
2024-05-31 14:14:48 +02:00
< / code > < / div > < h3 id = "h-creating-elements-with-custom-attributes" > <!-- <dde:mark type="component" name="h3" host="parentElement" ssr/> --> < a href = "#h-creating-elements-with-custom-attributes" tabindex = "-1" > #< / a > Creating element(s) (with custom attributes)< / h3 > < p > You can create a native DOM element by using the < a title = "MDN documentation page for document.createElement()" href = "https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement" > < code > document.createElement()< / code > < / a > . It is also possible to provide a some attribute(s) declaratively using < code > Object.assign()< / code > . More precisely, this way you can sets some < a title = "MDN page about Interface Description Language" href = "https://developer.mozilla.org/en-US/docs/Glossary/IDL" > < abbr title = "Interface Description Language" > IDL< / abbr > < / a > also known as a JavaScript property.< / p > <!-- <dde:mark type="component" name="example" host="this" ssr/> --> < div id = "code-example-1-nkz9lcdhykg" class = "example" > <!-- <dde:mark type="component" name="code" host="parentElement" ssr/> --> < code class = "language-js" > document.body.append(
2023-11-23 16:30:20 +01:00
document.createElement("div")
);
console.log(
"Emty div is generated inside < body> :",
document.body.innerHTML.includes("< div> < /div> ")
);
document.body.append(
Object.assign(
document.createElement("p"),
{ textContent: "Element’ s text content.", style: "color: coral;" }
)
);
2024-11-07 10:17:54 +01:00
< / code > < / div > < script > F l e m s ( d o c u m e n t . g e t E l e m e n t B y I d ( " c o d e - e x a m p l e - 1 - n k z 9 l c d h y k g " ) , J S O N . p a r s e ( " { \ " f i l e s \ " : [ { \ " n a m e \ " : \ " . j s \ " , \ " c o n t e n t \ " : \ " d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t d o c u m e n t . c r e a t e E l e m e n t ( \ \ \ " d i v \ \ \ " ) \ \ n ) ; \ \ n c o n s o l e . l o g ( \ \ n \ \ t \ \ \ " E m t y d i v i s g e n e r a t e d i n s i d e < b o d y > : \ \ \ " , \ \ n \ \ t d o c u m e n t . b o d y . i n n e r H T M L . i n c l u d e s ( \ \ \ " < d i v > < / d i v > \ \ \ " ) \ \ n ) ; \ \ n \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t O b j e c t . a s s i g n ( \ \ n \ \ t \ \ t d o c u m e n t . c r e a t e E l e m e n t ( \ \ \ " p \ \ \ " ) , \ \ n \ \ t \ \ t { t e x t C o n t e n t : \ \ \ " E l e m e n t ’ s t e x t c o n t e n t . \ \ \ " , s t y l e : \ \ \ " c o l o r : c o r a l ; \ \ \ " } \ \ n \ \ t ) \ \ n ) ; \ \ n \ " } , { \ " n a m e \ " : \ " e s m - w i t h - s i g n a l s . j s \ " , \ " c o n t e n t \ " : \ " / / s r c / s i g n a l s - c o m m o n . j s \ \ n v a r s i g n a l s _ g l o b a l = { \ \ n \ \ t i s S i g n a l ( a t t r i b u t e s ) { \ \ n \ \ t \ \ t r e t u r n f a l s e ; \ \ n \ \ t } , \ \ n \ \ t p r o c e s s R e a c t i v e A t t r i b u t e ( o b j , k e y , a t t r , s e t ) { \ \ n \ \ t \ \ t r e t u r n a t t r ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n r e g i s t e r R e a c t i v i t y ( d e f , g l o b a l = t r u e ) { \ \ n \ \ t i f ( g l o b a l ) r e t u r n O b j e c t . a s s i g n ( s i g n a l s _ g l o b a l , d e f ) ; \ \ n \ \ t O b j e c t . s e t P r o t o t y p e O f ( d e f , s i g n a l s _ g l o b a l ) ; \ \ n \ \ t r e t u r n d e f ; \ \ n } \ \ n f u n c t i o n s i g n a l s ( _ t h i s ) { \ \ n \ \ t r e t u r n s i g n a l s _ g l o b a l . i s P r o t o t y p e O f ( _ t h i s ) & & _ t h i s ! = = s i g n a l s _ g l o b a l ? _ t h i s : s i g n a l s _ g l o b a l ; \ \ n } \ \ n \ \ n / / s r c / h e l p e r s . j s \ \ n v a r h a s O w n = ( . . . a ) = > O b j e c t . p r o t o t y p e . h a s O w n P r o p e r t y . c a l l ( . . . a ) ; \ \ n f u n c t i o n i s U n d e f ( v a l u e ) { \ \ n \ \ t r e t u r n t y p e o f v a l u e = = = \ \ \ " u n d e f i n e d \ \ \ " ; \ \ n } \ \ n f u n c t i o n t y p e O f ( v ) { \ \ n \ \ t c o n s t t = t y p e o f v ; \ \ n \ \ t i f ( t ! = = \ \ \ " o b j e c t \ \ \ " ) r e t u r n t ; \ \ n \ \ t i f ( v = = = n u l l ) r e t u r n \ \ \ " n u l l \ \ \ " ; \ \ n \ \ t r e t u r n O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v ) ; \ \ n } \ \ n f u n c t i o n o n A b o r t ( s i g n a l 2 , l i s t e n e r ) { \ \ n \ \ t i f ( ! s i g n a l 2 | | ! ( s i g n a l 2 i n s t a n c e o f A b o r t S i g n a l ) ) \ \ n \ \ t \ \ t r e t u r n t r u e ; \ \ n \ \ t i f ( s i g n a l 2 . a b o r t e d ) \ \ n \ \ t \ \ t r e t u r n ; \ \ n \ \ t s i g n a l 2 . a d d E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t r e t u r n f u n c t i o n c l e a n U p ( ) { \ \ n \ \ t \ \ t s i g n a l 2 . r e m o v e E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t } ; \ \ n } \ \ n f u n c t i o n o b s e r v e d A t t r i b u t e s ( i n s t a n c e , o b s e r v e d A t t r i b u t e 2 ) { \ \ n \ \ t c o n s t { o b s e r v e d A t t r i b u t e s : o b s e r v e d A t t r i b u t e s 3 = [ ] } = i n s t a n c e . c o n s t r u c t o r ; \ \ n \ \ t r e t u r n o b s e r v e d A t t r i b u t e s 3 . r e d u c e ( f u n c t i o n ( o u t , n a m e ) { \ \ n \ \ t \ \ t o u t [ k e b a b T o C a m e l ( n a m e ) ] = o b s e r v e d A t t r i b u t e 2 ( i n s t a n c e , n a m e ) ; \ \ n \ \ t \ \ t r e t u r n o u t ; \ \ n \ \ t } , { } ) ; \ \ n } \ \ n f u n c t i o n k e b a b T o C a m e l ( n a m e ) { \ \ n \ \ t r e t u r n n a m e . r e p l a c e ( / - . / g , ( x ) = > x [ 1 ] . t o U p p e r C a s e ( ) ) ; \ \ n } \ \ n \ \ n / / s r c / d o m - c o m m o n . j s \ \ n v a r e n v i r o m e n t = { \ \ n \ \ t s e t D e l e t e A t t r , \ \ n \ \ t s s r : \ \ \ " \ \ \ " , \ \ n \ \ t D : g l o b a l T h i s . d o c u m e n t , \ \ n \ \ t F : g l o b a l T h i s . D o c u m e n t F r a g m e n t , \ \ n \ \ t H : g l o b a l T h i s . H T M L E l e m e n t , \ \ n \ \ t S : g l o b a l T h i s . S V G E l e m e n t , \ \ n \ \ t M : g l o b a l T h i s . M u t a t i o n O b s e r v e r \ \ n } ; \ \ n f u n c t i o n s e t D e l e t e A t t r ( o b j , p r o p , v a l ) { \ \ n \ \ t R e f l e c t . s e t ( o b j , p r o p , v a l ) ; \ \ n \ \ t i f ( ! i s U n d e f ( v a l ) ) r e t u r n ; \ \ n \ \ t R e f l e c t . d e l e t e P r o p e r t y ( o b j , p r o p ) ; \ \ n \ \ t i f ( o b j i n s t a n c e o f e n v i r o m e n t . H & & o b j . g e t A t t r i b u t e ( p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n o b j . r e m o v e A t t r i b u t e ( p r o p ) ; \ \ n \ \ t i f ( R e f l e c t . g e t ( o b j , p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n R e f l e c t . s e t ( o b j , p r o p , \ \ \ " \ \ \ " ) ; \ \ n } \ \ n v a r k e y L T E = \ \ \ " _ _ d d e _ l i f e c y c l e s T o E v e n t s \ \ \ " ; \ \ n v a r e v c = \ \ \ " d d e : c o n n e c t e d \ \ \ " ; \ \ n v a r e v d = \ \ \ " d d e : d i s c o n n e c t e d \ \ \ " ; \ \ n v a r e v a = \ \ \ " d d e : a t t r i b u t e C h a n g e d \ \ \ " ; \ \ n \ \ n / / s r c / d o m . j s \ \ n v a r s c o p e s = [ { \ \ n \ \ t g e t s c o p e ( ) { \ \ n \ \ t \ \ t r e t u r n e n v i r o m e n t . D . b o d y ; \ \ n \ \ t } , \ \ n \ \ t h o s t : ( c ) = > c ? c ( e n v i r o m e n t . D . b o d y ) : e n v i r o m e n t . D . b o d y , \ \ n \ \ t p r e v e n t : t r u e \ \ n } ] ; \ \ n v a r s c o p e = { \ \ n \ \ t g e t c u r r e n t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s [ s c o p e s . l e n g t h - 1 ] ; \ \ n \ \ t } , \ \ n \ \ t g e t h o s t ( ) { \ \ n \ \ t \ \ t r e t u r n t h i s . c u r r e n t . h o s t ; \ \ n \ \ t } , \ \ n \ \ t p r e v e n t D e f a u l t ( ) { \ \ n \ \ t \ \ t c o n s t { c u r r e n t } = t h i s ; \ \ n \ \ t \ \ t c u r r e n t . p r e v e n t = t r u e ; \ \ n \ \ t \ \ t r e t u r n c u r r e n t ; \ \ n \ \ t } , \ \ n \ \ t g e t s t a t e ( ) { \ \ n \ \ t \ \ t r e t u r n [ . . . s c o p e s ] ; \ \ n \ \ t } , \ \ n \ \ t p u s h ( s = { } ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s . p u s h ( O b j e c t . a s s i g n ( { } , t h i s . c u r r e n t , { p r e v e n t : f a l s e } , s ) ) ; \ \ n \ \ t } , \ \ n \ \ t p u s h R o o t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s . p u s h ( s c o p e s [ 0 ] ) ; \ \ n \ \ t } , \ \ n \ \ t p o p ( ) { \ \ n \ \ t \ \ t i f ( s c o p e s . l e n g t h = = = 1 ) r e t u r n ; \ \ n \ \ t \ \ t r e t u r n s c o p e s . p o p ( ) ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n a p p e n d ( . . . e l s ) { \ \ n \ \ t t h i s . a p p e n d O r i g i n a l ( . . . e l s ) ; \ \ n \ \ t r e t u r n t h i s ; \ \ n } \ \ n f u n c t i o n c h a i n a b l e A p p e n d ( e l ) { \ \ n \ \ t i f ( e l . a p p e n d = = = a p p e n d ) r e t u r n e l ; \ \ n \ \ t e l . a p p e n d O r i g i n a l = e l . a p p e n d ; \ \ n \ \ t e l . a p p e n d = a p p e n d ; \ \ n \ \ t r e t u r n e l ; \ \ n } \ \ n v a r n a m e s p a c e ; \ \ n f u n c t i o n c r e a t e E l e m e n t ( t a g , a t t r i b u t e s , . . . a d d o n s ) { \ \ n \ \ t c o n s t s = s i
2023-11-23 16:30:20 +01:00
const color= "lightcoral";
document.body.append(
el("p", { textContent: "Hello (first time)", style: { color } })
);
document.body.append(
assign(
document.createElement("p"),
{ textContent: "Hello (second time)", style: { color } }
)
);
2024-11-07 10:17:54 +01:00
< / code > < / div > < script > F l e m s ( d o c u m e n t . g e t E l e m e n t B y I d ( " c o d e - e x a m p l e - 1 - 1 3 l y j u k v r 0 y k " ) , J S O N . p a r s e ( " { \ " f i l e s \ " : [ { \ " n a m e \ " : \ " . j s \ " , \ " c o n t e n t \ " : \ " i m p o r t { e l , a s s i g n } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n c o n s t c o l o r = \ \ \ " l i g h t c o r a l \ \ \ " ; \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t e l ( \ \ \ " p \ \ \ " , { t e x t C o n t e n t : \ \ \ " H e l l o ( f i r s t t i m e ) \ \ \ " , s t y l e : { c o l o r } } ) \ \ n ) ; \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t a s s i g n ( \ \ n \ \ t \ \ t d o c u m e n t . c r e a t e E l e m e n t ( \ \ \ " p \ \ \ " ) , \ \ n \ \ t \ \ t { t e x t C o n t e n t : \ \ \ " H e l l o ( s e c o n d t i m e ) \ \ \ " , s t y l e : { c o l o r } } \ \ n \ \ t ) \ \ n ) ; \ \ n \ " } , { \ " n a m e \ " : \ " e s m - w i t h - s i g n a l s . j s \ " , \ " c o n t e n t \ " : \ " / / s r c / s i g n a l s - c o m m o n . j s \ \ n v a r s i g n a l s _ g l o b a l = { \ \ n \ \ t i s S i g n a l ( a t t r i b u t e s ) { \ \ n \ \ t \ \ t r e t u r n f a l s e ; \ \ n \ \ t } , \ \ n \ \ t p r o c e s s R e a c t i v e A t t r i b u t e ( o b j , k e y , a t t r , s e t ) { \ \ n \ \ t \ \ t r e t u r n a t t r ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n r e g i s t e r R e a c t i v i t y ( d e f , g l o b a l = t r u e ) { \ \ n \ \ t i f ( g l o b a l ) r e t u r n O b j e c t . a s s i g n ( s i g n a l s _ g l o b a l , d e f ) ; \ \ n \ \ t O b j e c t . s e t P r o t o t y p e O f ( d e f , s i g n a l s _ g l o b a l ) ; \ \ n \ \ t r e t u r n d e f ; \ \ n } \ \ n f u n c t i o n s i g n a l s ( _ t h i s ) { \ \ n \ \ t r e t u r n s i g n a l s _ g l o b a l . i s P r o t o t y p e O f ( _ t h i s ) & & _ t h i s ! = = s i g n a l s _ g l o b a l ? _ t h i s : s i g n a l s _ g l o b a l ; \ \ n } \ \ n \ \ n / / s r c / h e l p e r s . j s \ \ n v a r h a s O w n = ( . . . a ) = > O b j e c t . p r o t o t y p e . h a s O w n P r o p e r t y . c a l l ( . . . a ) ; \ \ n f u n c t i o n i s U n d e f ( v a l u e ) { \ \ n \ \ t r e t u r n t y p e o f v a l u e = = = \ \ \ " u n d e f i n e d \ \ \ " ; \ \ n } \ \ n f u n c t i o n t y p e O f ( v ) { \ \ n \ \ t c o n s t t = t y p e o f v ; \ \ n \ \ t i f ( t ! = = \ \ \ " o b j e c t \ \ \ " ) r e t u r n t ; \ \ n \ \ t i f ( v = = = n u l l ) r e t u r n \ \ \ " n u l l \ \ \ " ; \ \ n \ \ t r e t u r n O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v ) ; \ \ n } \ \ n f u n c t i o n o n A b o r t ( s i g n a l 2 , l i s t e n e r ) { \ \ n \ \ t i f ( ! s i g n a l 2 | | ! ( s i g n a l 2 i n s t a n c e o f A b o r t S i g n a l ) ) \ \ n \ \ t \ \ t r e t u r n t r u e ; \ \ n \ \ t i f ( s i g n a l 2 . a b o r t e d ) \ \ n \ \ t \ \ t r e t u r n ; \ \ n \ \ t s i g n a l 2 . a d d E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t r e t u r n f u n c t i o n c l e a n U p ( ) { \ \ n \ \ t \ \ t s i g n a l 2 . r e m o v e E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t } ; \ \ n } \ \ n f u n c t i o n o b s e r v e d A t t r i b u t e s ( i n s t a n c e , o b s e r v e d A t t r i b u t e 2 ) { \ \ n \ \ t c o n s t { o b s e r v e d A t t r i b u t e s : o b s e r v e d A t t r i b u t e s 3 = [ ] } = i n s t a n c e . c o n s t r u c t o r ; \ \ n \ \ t r e t u r n o b s e r v e d A t t r i b u t e s 3 . r e d u c e ( f u n c t i o n ( o u t , n a m e ) { \ \ n \ \ t \ \ t o u t [ k e b a b T o C a m e l ( n a m e ) ] = o b s e r v e d A t t r i b u t e 2 ( i n s t a n c e , n a m e ) ; \ \ n \ \ t \ \ t r e t u r n o u t ; \ \ n \ \ t } , { } ) ; \ \ n } \ \ n f u n c t i o n k e b a b T o C a m e l ( n a m e ) { \ \ n \ \ t r e t u r n n a m e . r e p l a c e ( / - . / g , ( x ) = > x [ 1 ] . t o U p p e r C a s e ( ) ) ; \ \ n } \ \ n \ \ n / / s r c / d o m - c o m m o n . j s \ \ n v a r e n v i r o m e n t = { \ \ n \ \ t s e t D e l e t e A t t r , \ \ n \ \ t s s r : \ \ \ " \ \ \ " , \ \ n \ \ t D : g l o b a l T h i s . d o c u m e n t , \ \ n \ \ t F : g l o b a l T h i s . D o c u m e n t F r a g m e n t , \ \ n \ \ t H : g l o b a l T h i s . H T M L E l e m e n t , \ \ n \ \ t S : g l o b a l T h i s . S V G E l e m e n t , \ \ n \ \ t M : g l o b a l T h i s . M u t a t i o n O b s e r v e r \ \ n } ; \ \ n f u n c t i o n s e t D e l e t e A t t r ( o b j , p r o p , v a l ) { \ \ n \ \ t R e f l e c t . s e t ( o b j , p r o p , v a l ) ; \ \ n \ \ t i f ( ! i s U n d e f ( v a l ) ) r e t u r n ; \ \ n \ \ t R e f l e c t . d e l e t e P r o p e r t y ( o b j , p r o p ) ; \ \ n \ \ t i f ( o b j i n s t a n c e o f e n v i r o m e n t . H & & o b j . g e t A t t r i b u t e ( p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n o b j . r e m o v e A t t r i b u t e ( p r o p ) ; \ \ n \ \ t i f ( R e f l e c t . g e t ( o b j , p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n R e f l e c t . s e t ( o b j , p r o p , \ \ \ " \ \ \ " ) ; \ \ n } \ \ n v a r k e y L T E = \ \ \ " _ _ d d e _ l i f e c y c l e s T o E v e n t s \ \ \ " ; \ \ n v a r e v c = \ \ \ " d d e : c o n n e c t e d \ \ \ " ; \ \ n v a r e v d = \ \ \ " d d e : d i s c o n n e c t e d \ \ \ " ; \ \ n v a r e v a = \ \ \ " d d e : a t t r i b u t e C h a n g e d \ \ \ " ; \ \ n \ \ n / / s r c / d o m . j s \ \ n v a r s c o p e s = [ { \ \ n \ \ t g e t s c o p e ( ) { \ \ n \ \ t \ \ t r e t u r n e n v i r o m e n t . D . b o d y ; \ \ n \ \ t } , \ \ n \ \ t h o s t : ( c ) = > c ? c ( e n v i r o m e n t . D . b o d y ) : e n v i r o m e n t . D . b o d y , \ \ n \ \ t p r e v e n t : t r u e \ \ n } ] ; \ \ n v a r s c o p e = { \ \ n \ \ t g e t c u r r e n t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s [ s c o p e s . l e n g t h - 1 ] ; \ \ n \ \ t } , \ \ n \ \ t g e t h o s t ( ) { \ \ n \ \ t \ \ t r e t u r n t h i s . c u r r e n t . h o s t ; \ \ n \ \ t } , \ \ n \ \ t p r e v e n t D e f a u l t ( ) { \ \ n \ \ t \ \ t c o n s t { c u r r e n t } = t h i s ; \ \ n \ \ t \ \ t c u r r e n t . p r e v e n t = t r u e ; \ \ n \ \ t \ \ t r e t u r n c u r r e n t ; \ \ n \ \ t } , \ \ n \ \ t g e t s t a t e ( ) { \ \ n \ \ t \ \ t r e t u r n [ . . . s c o p e s ] ; \ \ n \ \ t } , \ \ n \ \ t p u s h ( s = { } ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s . p u s h ( O b j e c t . a s s i g n ( { } , t h i s . c u r r e n t , { p r e v e n t : f a l s e } , s ) ) ; \ \ n \ \ t } , \ \ n \ \ t p u s h R o o t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s . p u s h ( s c o p e s [ 0 ] ) ; \ \ n \ \ t } , \ \ n \ \ t p o p ( ) { \ \ n \ \ t \ \ t i f ( s c o p e s . l e n g t h = = = 1 ) r e t u r n ; \ \ n \ \ t \ \ t r e t u r n s c o p e s . p o p ( ) ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n a p p e n d ( . . . e l s ) { \ \ n \ \ t t h i s . a p p e n d O r i g i n a l ( . . . e l s ) ; \ \ n \ \ t r e t u r n t h i s ; \ \ n } \ \ n f u n c t i o n c h a i n a b l e A p p e n d ( e l ) { \ \ n \ \ t i f ( e l . a p p e n d = = = a p p e n d ) r e t u r n e l ; \ \ n \ \ t e l . a p p e n d O r i g i n a l = e l . a p p e n d ; \ \ n \ \ t e l . a p p e n d = a p p e n d ; \ \ n \ \ t r e t u r n e l ; \ \ n } \ \ n v a r n a m e s p a c e ; \ \ n f u n c t i o n c r e a t e E l e m e n t ( t a g , a t t r i b u t e s , . . . a d d o n s ) { \ \ n \ \ t c o n s t s = s i g n a l s ( t h i s ) ; \ \ n \ \ t l e t
2023-11-23 16:30:20 +01:00
const paragraph= document.createElement("p");
assignAttribute(paragraph, "textContent", "Hello, world!");
assignAttribute(paragraph, "style", "color: red; font-weight: bold;");
assignAttribute(paragraph, "style", { color: "navy" });
assignAttribute(paragraph, "dataTest1", "v1");
assignAttribute(paragraph, "dataset", { test2: "v2" });
assign(paragraph, { //textContent and style see above
ariaLabel: "v1", //data* see above
ariaset: { role: "none" }, // dataset see above
"=onclick": "console.log(event)",
onmouseout: console.info,
".something": "something",
classList: {} //see below
});
classListDeclarative(paragraph, {
classAdd: true,
classRemove: false,
classAdd1: 1,
classRemove1: 0,
classToggle: -1
});
console.log(paragraph.outerHTML);
console.log("paragraph.something=", paragraph.something);
document.body.append(
paragraph
);
2024-11-07 10:17:54 +01:00
< / code > < / div > < script > F l e m s ( d o c u m e n t . g e t E l e m e n t B y I d ( " c o d e - e x a m p l e - 1 - i r o 9 n c x a 4 b c " ) , J S O N . p a r s e ( " { \ " f i l e s \ " : [ { \ " n a m e \ " : \ " . j s \ " , \ " c o n t e n t \ " : \ " i m p o r t { a s s i g n , a s s i g n A t t r i b u t e , c l a s s L i s t D e c l a r a t i v e } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n c o n s t p a r a g r a p h = d o c u m e n t . c r e a t e E l e m e n t ( \ \ \ " p \ \ \ " ) ; \ \ n \ \ n a s s i g n A t t r i b u t e ( p a r a g r a p h , \ \ \ " t e x t C o n t e n t \ \ \ " , \ \ \ " H e l l o , w o r l d ! \ \ \ " ) ; \ \ n \ \ n a s s i g n A t t r i b u t e ( p a r a g r a p h , \ \ \ " s t y l e \ \ \ " , \ \ \ " c o l o r : r e d ; f o n t - w e i g h t : b o l d ; \ \ \ " ) ; \ \ n a s s i g n A t t r i b u t e ( p a r a g r a p h , \ \ \ " s t y l e \ \ \ " , { c o l o r : \ \ \ " n a v y \ \ \ " } ) ; \ \ n \ \ n a s s i g n A t t r i b u t e ( p a r a g r a p h , \ \ \ " d a t a T e s t 1 \ \ \ " , \ \ \ " v 1 \ \ \ " ) ; \ \ n a s s i g n A t t r i b u t e ( p a r a g r a p h , \ \ \ " d a t a s e t \ \ \ " , { t e s t 2 : \ \ \ " v 2 \ \ \ " } ) ; \ \ n \ \ n a s s i g n ( p a r a g r a p h , { / / t e x t C o n t e n t a n d s t y l e s e e a b o v e \ \ n \ \ t a r i a L a b e l : \ \ \ " v 1 \ \ \ " , / / d a t a * s e e a b o v e \ \ n \ \ t a r i a s e t : { r o l e : \ \ \ " n o n e \ \ \ " } , / / d a t a s e t s e e a b o v e \ \ n \ \ t \ \ \ " = o n c l i c k \ \ \ " : \ \ \ " c o n s o l e . l o g ( e v e n t ) \ \ \ " , \ \ n \ \ t o n m o u s e o u t : c o n s o l e . i n f o , \ \ n \ \ t \ \ \ " . s o m e t h i n g \ \ \ " : \ \ \ " s o m e t h i n g \ \ \ " , \ \ n \ \ t c l a s s L i s t : { } / / s e e b e l o w \ \ n } ) ; \ \ n \ \ n c l a s s L i s t D e c l a r a t i v e ( p a r a g r a p h , { \ \ n \ \ t c l a s s A d d : t r u e , \ \ n \ \ t c l a s s R e m o v e : f a l s e , \ \ n \ \ t c l a s s A d d 1 : 1 , \ \ n \ \ t c l a s s R e m o v e 1 : 0 , \ \ n \ \ t c l a s s T o g g l e : - 1 \ \ n } ) ; \ \ n \ \ n c o n s o l e . l o g ( p a r a g r a p h . o u t e r H T M L ) ; \ \ n c o n s o l e . l o g ( \ \ \ " p a r a g r a p h . s o m e t h i n g = \ \ \ " , p a r a g r a p h . s o m e t h i n g ) ; \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t p a r a g r a p h \ \ n ) ; \ \ n \ " } , { \ " n a m e \ " : \ " e s m - w i t h - s i g n a l s . j s \ " , \ " c o n t e n t \ " : \ " / / s r c / s i g n a l s - c o m m o n . j s \ \ n v a r s i g n a l s _ g l o b a l = { \ \ n \ \ t i s S i g n a l ( a t t r i b u t e s ) { \ \ n \ \ t \ \ t r e t u r n f a l s e ; \ \ n \ \ t } , \ \ n \ \ t p r o c e s s R e a c t i v e A t t r i b u t e ( o b j , k e y , a t t r , s e t ) { \ \ n \ \ t \ \ t r e t u r n a t t r ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n r e g i s t e r R e a c t i v i t y ( d e f , g l o b a l = t r u e ) { \ \ n \ \ t i f ( g l o b a l ) r e t u r n O b j e c t . a s s i g n ( s i g n a l s _ g l o b a l , d e f ) ; \ \ n \ \ t O b j e c t . s e t P r o t o t y p e O f ( d e f , s i g n a l s _ g l o b a l ) ; \ \ n \ \ t r e t u r n d e f ; \ \ n } \ \ n f u n c t i o n s i g n a l s ( _ t h i s ) { \ \ n \ \ t r e t u r n s i g n a l s _ g l o b a l . i s P r o t o t y p e O f ( _ t h i s ) & & _ t h i s ! = = s i g n a l s _ g l o b a l ? _ t h i s : s i g n a l s _ g l o b a l ; \ \ n } \ \ n \ \ n / / s r c / h e l p e r s . j s \ \ n v a r h a s O w n = ( . . . a ) = > O b j e c t . p r o t o t y p e . h a s O w n P r o p e r t y . c a l l ( . . . a ) ; \ \ n f u n c t i o n i s U n d e f ( v a l u e ) { \ \ n \ \ t r e t u r n t y p e o f v a l u e = = = \ \ \ " u n d e f i n e d \ \ \ " ; \ \ n } \ \ n f u n c t i o n t y p e O f ( v ) { \ \ n \ \ t c o n s t t = t y p e o f v ; \ \ n \ \ t i f ( t ! = = \ \ \ " o b j e c t \ \ \ " ) r e t u r n t ; \ \ n \ \ t i f ( v = = = n u l l ) r e t u r n \ \ \ " n u l l \ \ \ " ; \ \ n \ \ t r e t u r n O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v ) ; \ \ n } \ \ n f u n c t i o n o n A b o r t ( s i g n a l 2 , l i s t e n e r ) { \ \ n \ \ t i f ( ! s i g n a l 2 | | ! ( s i g n a l 2 i n s t a n c e o f A b o r t S i g n a l ) ) \ \ n \ \ t \ \ t r e t u r n t r u e ; \ \ n \ \ t i f ( s i g n a l 2 . a b o r t e d ) \ \ n \ \ t \ \ t r e t u r n ; \ \ n \ \ t s i g n a l 2 . a d d E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t r e t u r n f u n c t i o n c l e a n U p ( ) { \ \ n \ \ t \ \ t s i g n a l 2 . r e m o v e E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t } ; \ \ n } \ \ n f u n c t i o n o b s e r v e d A t t r i b u t e s ( i n s t a n c e , o b s e r v e d A t t r i b u t e 2 ) { \ \ n \ \ t c o n s t { o b s e r v e d A t t r i b u t e s : o b s e r v e d A t t r i b u t e s 3 = [ ] } = i n s t a n c e . c o n s t r u c t o r ; \ \ n \ \ t r e t u r n o b s e r v e d A t t r i b u t e s 3 . r e d u c e ( f u n c t i o n ( o u t , n a m e ) { \ \ n \ \ t \ \ t o u t [ k e b a b T o C a m e l ( n a m e ) ] = o b s e r v e d A t t r i b u t e 2 ( i n s t a n c e , n a m e ) ; \ \ n \ \ t \ \ t r e t u r n o u t ; \ \ n \ \ t } , { } ) ; \ \ n } \ \ n f u n c t i o n k e b a b T o C a m e l ( n a m e ) { \ \ n \ \ t r e t u r n n a m e . r e p l a c e ( / - . / g , ( x ) = > x [ 1 ] . t o U p p e r C a s e ( ) ) ; \ \ n } \ \ n \ \ n / / s r c / d o m - c o m m o n . j s \ \ n v a r e n v i r o m e n t = { \ \ n \ \ t s e t D e l e t e A t t r , \ \ n \ \ t s s r : \ \ \ " \ \ \ " , \ \ n \ \ t D : g l o b a l T h i s . d o c u m e n t , \ \ n \ \ t F : g l o b a l T h i s . D o c u m e n t F r a g m e n t , \ \ n \ \ t H : g l o b a l T h i s . H T M L E l e m e n t , \ \ n \ \ t S : g l o b a l T h i s . S V G E l e m e n t , \ \ n \ \ t M : g l o b a l T h i s . M u t a t i o n O b s e r v e r \ \ n } ; \ \ n f u n c t i o n s e t D e l e t e A t t r ( o b j , p r o p , v a l ) { \ \ n \ \ t R e f l e c t . s e t ( o b j , p r o p , v a l ) ; \ \ n \ \ t i f ( ! i s U n d e f ( v a l ) ) r e t u r n ; \ \ n \ \ t R e f l e c t . d e l e t e P r o p e r t y ( o b j , p r o p ) ; \ \ n \ \ t i f ( o b j i n s t a n c e o f e n v i r o m e n t . H & & o b j . g e t A t t r i b u t e ( p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n o b j . r e m o v e A t t r i b u t e ( p r o p ) ; \ \ n \ \ t i f ( R e f l e c t . g e t ( o b j , p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n R e f l e c t . s e t ( o b j , p r o p , \ \ \ " \ \ \ " ) ; \ \ n } \ \ n v a r k e y L T E = \ \ \ " _ _ d d e _ l i f e c y c l e s T o E v e n t s \ \ \ " ; \ \ n v a r e v c = \ \ \ " d d e : c o n n e c t e d \ \ \ " ; \ \ n v a r e v d = \ \ \ " d d e : d i s c o n n e c t e d \ \ \ " ; \ \ n v a r e v a = \ \ \ " d d e : a t t r i b u t e C h a n g e d \ \ \ " ; \ \ n \ \ n / / s r c / d o m . j s \ \ n v a r s c o p e s = [ { \ \ n \ \ t g e t s c o p e ( ) { \ \ n \ \ t \ \ t r e t u r n e n v i r o m e n t . D . b o d y ; \ \ n \ \ t } , \ \ n \ \ t h o s t : ( c ) = > c ? c ( e n v i r o m e n t . D . b o d y ) : e n v i r o m e n t . D . b o d y , \ \ n \ \ t p r e v e n t : t r u e \ \ n } ] ; \ \ n v a r s c o p e = { \ \ n \ \ t g e t c u r r e n t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s [ s c o p e s . l e n g t h - 1 ] ; \ \ n \ \ t } , \ \ n \ \ t g e t h o s t ( ) { \ \ n \ \ t \ \ t r e t u r n t h i s . c u r r e n t . h o s t ; \ \ n \ \ t } , \ \ n \ \ t p r e v e n t D e f a u l t ( )
2023-11-23 16:30:20 +01:00
document.createElement("div"),
document.createElement("span"),
document.createElement("main")
);
console.log(document.body.innerHTML.includes("< div> < /div> < span> < /span> < main> < /main> "));
const template= document.createElement("main").append(
document.createElement("div"),
document.createElement("span"),
);
console.log(typeof template==="undefined");
2024-11-07 10:17:54 +01:00
< / code > < / div > < script > F l e m s ( d o c u m e n t . g e t E l e m e n t B y I d ( " c o d e - e x a m p l e - 1 - b 6 a 1 h b r x h 7 s " ) , J S O N . p a r s e ( " { \ " f i l e s \ " : [ { \ " n a m e \ " : \ " . j s \ " , \ " c o n t e n t \ " : \ " d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t d o c u m e n t . c r e a t e E l e m e n t ( \ \ \ " d i v \ \ \ " ) , \ \ n \ \ t d o c u m e n t . c r e a t e E l e m e n t ( \ \ \ " s p a n \ \ \ " ) , \ \ n \ \ t d o c u m e n t . c r e a t e E l e m e n t ( \ \ \ " m a i n \ \ \ " ) \ \ n ) ; \ \ n c o n s o l e . l o g ( d o c u m e n t . b o d y . i n n e r H T M L . i n c l u d e s ( \ \ \ " < d i v > < / d i v > < s p a n > < / s p a n > < m a i n > < / m a i n > \ \ \ " ) ) ; \ \ n c o n s t t e m p l a t e = d o c u m e n t . c r e a t e E l e m e n t ( \ \ \ " m a i n \ \ \ " ) . a p p e n d ( \ \ n \ \ t d o c u m e n t . c r e a t e E l e m e n t ( \ \ \ " d i v \ \ \ " ) , \ \ n \ \ t d o c u m e n t . c r e a t e E l e m e n t ( \ \ \ " s p a n \ \ \ " ) , \ \ n ) ; \ \ n c o n s o l e . l o g ( t y p e o f t e m p l a t e = = = \ \ \ " u n d e f i n e d \ \ \ " ) ; \ \ n \ " } , { \ " n a m e \ " : \ " e s m - w i t h - s i g n a l s . j s \ " , \ " c o n t e n t \ " : \ " / / s r c / s i g n a l s - c o m m o n . j s \ \ n v a r s i g n a l s _ g l o b a l = { \ \ n \ \ t i s S i g n a l ( a t t r i b u t e s ) { \ \ n \ \ t \ \ t r e t u r n f a l s e ; \ \ n \ \ t } , \ \ n \ \ t p r o c e s s R e a c t i v e A t t r i b u t e ( o b j , k e y , a t t r , s e t ) { \ \ n \ \ t \ \ t r e t u r n a t t r ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n r e g i s t e r R e a c t i v i t y ( d e f , g l o b a l = t r u e ) { \ \ n \ \ t i f ( g l o b a l ) r e t u r n O b j e c t . a s s i g n ( s i g n a l s _ g l o b a l , d e f ) ; \ \ n \ \ t O b j e c t . s e t P r o t o t y p e O f ( d e f , s i g n a l s _ g l o b a l ) ; \ \ n \ \ t r e t u r n d e f ; \ \ n } \ \ n f u n c t i o n s i g n a l s ( _ t h i s ) { \ \ n \ \ t r e t u r n s i g n a l s _ g l o b a l . i s P r o t o t y p e O f ( _ t h i s ) & & _ t h i s ! = = s i g n a l s _ g l o b a l ? _ t h i s : s i g n a l s _ g l o b a l ; \ \ n } \ \ n \ \ n / / s r c / h e l p e r s . j s \ \ n v a r h a s O w n = ( . . . a ) = > O b j e c t . p r o t o t y p e . h a s O w n P r o p e r t y . c a l l ( . . . a ) ; \ \ n f u n c t i o n i s U n d e f ( v a l u e ) { \ \ n \ \ t r e t u r n t y p e o f v a l u e = = = \ \ \ " u n d e f i n e d \ \ \ " ; \ \ n } \ \ n f u n c t i o n t y p e O f ( v ) { \ \ n \ \ t c o n s t t = t y p e o f v ; \ \ n \ \ t i f ( t ! = = \ \ \ " o b j e c t \ \ \ " ) r e t u r n t ; \ \ n \ \ t i f ( v = = = n u l l ) r e t u r n \ \ \ " n u l l \ \ \ " ; \ \ n \ \ t r e t u r n O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v ) ; \ \ n } \ \ n f u n c t i o n o n A b o r t ( s i g n a l 2 , l i s t e n e r ) { \ \ n \ \ t i f ( ! s i g n a l 2 | | ! ( s i g n a l 2 i n s t a n c e o f A b o r t S i g n a l ) ) \ \ n \ \ t \ \ t r e t u r n t r u e ; \ \ n \ \ t i f ( s i g n a l 2 . a b o r t e d ) \ \ n \ \ t \ \ t r e t u r n ; \ \ n \ \ t s i g n a l 2 . a d d E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t r e t u r n f u n c t i o n c l e a n U p ( ) { \ \ n \ \ t \ \ t s i g n a l 2 . r e m o v e E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t } ; \ \ n } \ \ n f u n c t i o n o b s e r v e d A t t r i b u t e s ( i n s t a n c e , o b s e r v e d A t t r i b u t e 2 ) { \ \ n \ \ t c o n s t { o b s e r v e d A t t r i b u t e s : o b s e r v e d A t t r i b u t e s 3 = [ ] } = i n s t a n c e . c o n s t r u c t o r ; \ \ n \ \ t r e t u r n o b s e r v e d A t t r i b u t e s 3 . r e d u c e ( f u n c t i o n ( o u t , n a m e ) { \ \ n \ \ t \ \ t o u t [ k e b a b T o C a m e l ( n a m e ) ] = o b s e r v e d A t t r i b u t e 2 ( i n s t a n c e , n a m e ) ; \ \ n \ \ t \ \ t r e t u r n o u t ; \ \ n \ \ t } , { } ) ; \ \ n } \ \ n f u n c t i o n k e b a b T o C a m e l ( n a m e ) { \ \ n \ \ t r e t u r n n a m e . r e p l a c e ( / - . / g , ( x ) = > x [ 1 ] . t o U p p e r C a s e ( ) ) ; \ \ n } \ \ n \ \ n / / s r c / d o m - c o m m o n . j s \ \ n v a r e n v i r o m e n t = { \ \ n \ \ t s e t D e l e t e A t t r , \ \ n \ \ t s s r : \ \ \ " \ \ \ " , \ \ n \ \ t D : g l o b a l T h i s . d o c u m e n t , \ \ n \ \ t F : g l o b a l T h i s . D o c u m e n t F r a g m e n t , \ \ n \ \ t H : g l o b a l T h i s . H T M L E l e m e n t , \ \ n \ \ t S : g l o b a l T h i s . S V G E l e m e n t , \ \ n \ \ t M : g l o b a l T h i s . M u t a t i o n O b s e r v e r \ \ n } ; \ \ n f u n c t i o n s e t D e l e t e A t t r ( o b j , p r o p , v a l ) { \ \ n \ \ t R e f l e c t . s e t ( o b j , p r o p , v a l ) ; \ \ n \ \ t i f ( ! i s U n d e f ( v a l ) ) r e t u r n ; \ \ n \ \ t R e f l e c t . d e l e t e P r o p e r t y ( o b j , p r o p ) ; \ \ n \ \ t i f ( o b j i n s t a n c e o f e n v i r o m e n t . H & & o b j . g e t A t t r i b u t e ( p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n o b j . r e m o v e A t t r i b u t e ( p r o p ) ; \ \ n \ \ t i f ( R e f l e c t . g e t ( o b j , p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n R e f l e c t . s e t ( o b j , p r o p , \ \ \ " \ \ \ " ) ; \ \ n } \ \ n v a r k e y L T E = \ \ \ " _ _ d d e _ l i f e c y c l e s T o E v e n t s \ \ \ " ; \ \ n v a r e v c = \ \ \ " d d e : c o n n e c t e d \ \ \ " ; \ \ n v a r e v d = \ \ \ " d d e : d i s c o n n e c t e d \ \ \ " ; \ \ n v a r e v a = \ \ \ " d d e : a t t r i b u t e C h a n g e d \ \ \ " ; \ \ n \ \ n / / s r c / d o m . j s \ \ n v a r s c o p e s = [ { \ \ n \ \ t g e t s c o p e ( ) { \ \ n \ \ t \ \ t r e t u r n e n v i r o m e n t . D . b o d y ; \ \ n \ \ t } , \ \ n \ \ t h o s t : ( c ) = > c ? c ( e n v i r o m e n t . D . b o d y ) : e n v i r o m e n t . D . b o d y , \ \ n \ \ t p r e v e n t : t r u e \ \ n } ] ; \ \ n v a r s c o p e = { \ \ n \ \ t g e t c u r r e n t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s [ s c o p e s . l e n g t h - 1 ] ; \ \ n \ \ t } , \ \ n \ \ t g e t h o s t ( ) { \ \ n \ \ t \ \ t r e t u r n t h i s . c u r r e n t . h o s t ; \ \ n \ \ t } , \ \ n \ \ t p r e v e n t D e f a u l t ( ) { \ \ n \ \ t \ \ t c o n s t { c u r r e n t } = t h i s ; \ \ n \ \ t \ \ t c u r r e n t . p r e v e n t = t r u e ; \ \ n \ \ t \ \ t r e t u r n c u r r e n t ; \ \ n \ \ t } , \ \ n \ \ t g e t s t a t e ( ) { \ \ n \ \ t \ \ t r e t u r n [ . . . s c o p e s ] ; \ \ n \ \ t } , \ \ n \ \ t p u s h ( s = { } ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s . p u s h ( O b j e c t . a s s i g n ( { } , t h i s . c u r r e n t , { p r e v e n t : f a l s e } , s ) ) ; \ \ n \ \ t } , \ \ n \ \ t p u s h R o o t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s . p u s h ( s c o p e s [ 0 ] ) ; \ \ n \ \ t } , \ \ n \ \ t p o p ( ) { \ \ n \ \ t \ \ t i f ( s c o p e s . l e n g t h = = = 1 ) r e t u r n ; \ \ n \ \ t \ \ t r e t u r n s c o p e s . p o p ( ) ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n a p p e n d ( . . . e l s ) { \ \ n \ \ t t h i s . a p p e n d O r i g i n a l ( . . . e l s ) ; \ \ n \ \ t r e t u r n t h i s ; \ \ n } \ \ n f u n c t i o n c h a i n a b l e A p p e n d ( e l ) { \ \ n \ \ t i f ( e l . a p p e n d = = = a p p e n d ) r e t u r n e l ; \ \ n \ \ t e l . a p p e n d O r i g i n a l = e l . a p p e n d ; \ \ n \ \ t e l . a p p e n d = a p p e n d ; \ \ n \ \ t r e t u r n e l ; \ \ n } \ \ n v a r n a m e s p a c e ; \ \ n f u n c
2023-11-23 16:30:20 +01:00
document.head.append(
el("style").append(
"tr, td{ border: 1px solid red; padding: 1em; }",
"table{ border-collapse: collapse; }"
)
);
document.body.append(
el("p", "Example of a complex template. Using for example nesting lists:"),
el("ul").append(
el("li", "List item 1"),
el("li").append(
el("ul").append(
el("li", "Nested list item 1"),
)
)
),
el("table").append(
el("tr").append(
el("td", "Row 1 – Col 1"),
el("td", "Row 1 – Col 2")
)
)
);
2024-05-22 21:43:49 +02:00
import { chainableAppend } from "./esm-with-signals.js";
2023-11-23 16:30:20 +01:00
/** @param {keyof HTMLElementTagNameMap} tag */
const createElement= tag=> chainableAppend(document.createElement(tag));
document.body.append(
createElement("p").append(
createElement("em").append(
"You can also use `chainableAppend`!"
)
)
);
2024-11-07 10:17:54 +01:00
< / code > < / div > < script > F l e m s ( d o c u m e n t . g e t E l e m e n t B y I d ( " c o d e - e x a m p l e - 2 - i r o 9 n c x a 4 b c " ) , J S O N . p a r s e ( " { \ " f i l e s \ " : [ { \ " n a m e \ " : \ " . j s \ " , \ " c o n t e n t \ " : \ " i m p o r t { e l } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n d o c u m e n t . h e a d . a p p e n d ( \ \ n \ \ t e l ( \ \ \ " s t y l e \ \ \ " ) . a p p e n d ( \ \ n \ \ t \ \ t \ \ \ " t r , t d { b o r d e r : 1 p x s o l i d r e d ; p a d d i n g : 1 e m ; } \ \ \ " , \ \ n \ \ t \ \ t \ \ \ " t a b l e { b o r d e r - c o l l a p s e : c o l l a p s e ; } \ \ \ " \ \ n \ \ t ) \ \ n ) ; \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t e l ( \ \ \ " p \ \ \ " , \ \ \ " E x a m p l e o f a c o m p l e x t e m p l a t e . U s i n g f o r e x a m p l e n e s t i n g l i s t s : \ \ \ " ) , \ \ n \ \ t e l ( \ \ \ " u l \ \ \ " ) . a p p e n d ( \ \ n \ \ t \ \ t e l ( \ \ \ " l i \ \ \ " , \ \ \ " L i s t i t e m 1 \ \ \ " ) , \ \ n \ \ t \ \ t e l ( \ \ \ " l i \ \ \ " ) . a p p e n d ( \ \ n \ \ t \ \ t \ \ t e l ( \ \ \ " u l \ \ \ " ) . a p p e n d ( \ \ n \ \ t \ \ t \ \ t \ \ t e l ( \ \ \ " l i \ \ \ " , \ \ \ " N e s t e d l i s t i t e m 1 \ \ \ " ) , \ \ n \ \ t \ \ t \ \ t ) \ \ n \ \ t \ \ t ) \ \ n \ \ t ) , \ \ n \ \ t e l ( \ \ \ " t a b l e \ \ \ " ) . a p p e n d ( \ \ n \ \ t \ \ t e l ( \ \ \ " t r \ \ \ " ) . a p p e n d ( \ \ n \ \ t \ \ t \ \ t e l ( \ \ \ " t d \ \ \ " , \ \ \ " R o w 1 – C o l 1 \ \ \ " ) , \ \ n \ \ t \ \ t \ \ t e l ( \ \ \ " t d \ \ \ " , \ \ \ " R o w 1 – C o l 2 \ \ \ " ) \ \ n \ \ t \ \ t ) \ \ n \ \ t ) \ \ n ) ; \ \ n \ \ n i m p o r t { c h a i n a b l e A p p e n d } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n / * * @ p a r a m { k e y o f H T M L E l e m e n t T a g N a m e M a p } t a g * / \ \ n c o n s t c r e a t e E l e m e n t = t a g = > c h a i n a b l e A p p e n d ( d o c u m e n t . c r e a t e E l e m e n t ( t a g ) ) ; \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t c r e a t e E l e m e n t ( \ \ \ " p \ \ \ " ) . a p p e n d ( \ \ n \ \ t \ \ t c r e a t e E l e m e n t ( \ \ \ " e m \ \ \ " ) . a p p e n d ( \ \ n \ \ t \ \ t \ \ t \ \ \ " Y o u c a n a l s o u s e ` c h a i n a b l e A p p e n d ` ! \ \ \ " \ \ n \ \ t \ \ t ) \ \ n \ \ t ) \ \ n ) ; \ \ n \ " } , { \ " n a m e \ " : \ " e s m - w i t h - s i g n a l s . j s \ " , \ " c o n t e n t \ " : \ " / / s r c / s i g n a l s - c o m m o n . j s \ \ n v a r s i g n a l s _ g l o b a l = { \ \ n \ \ t i s S i g n a l ( a t t r i b u t e s ) { \ \ n \ \ t \ \ t r e t u r n f a l s e ; \ \ n \ \ t } , \ \ n \ \ t p r o c e s s R e a c t i v e A t t r i b u t e ( o b j , k e y , a t t r , s e t ) { \ \ n \ \ t \ \ t r e t u r n a t t r ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n r e g i s t e r R e a c t i v i t y ( d e f , g l o b a l = t r u e ) { \ \ n \ \ t i f ( g l o b a l ) r e t u r n O b j e c t . a s s i g n ( s i g n a l s _ g l o b a l , d e f ) ; \ \ n \ \ t O b j e c t . s e t P r o t o t y p e O f ( d e f , s i g n a l s _ g l o b a l ) ; \ \ n \ \ t r e t u r n d e f ; \ \ n } \ \ n f u n c t i o n s i g n a l s ( _ t h i s ) { \ \ n \ \ t r e t u r n s i g n a l s _ g l o b a l . i s P r o t o t y p e O f ( _ t h i s ) & & _ t h i s ! = = s i g n a l s _ g l o b a l ? _ t h i s : s i g n a l s _ g l o b a l ; \ \ n } \ \ n \ \ n / / s r c / h e l p e r s . j s \ \ n v a r h a s O w n = ( . . . a ) = > O b j e c t . p r o t o t y p e . h a s O w n P r o p e r t y . c a l l ( . . . a ) ; \ \ n f u n c t i o n i s U n d e f ( v a l u e ) { \ \ n \ \ t r e t u r n t y p e o f v a l u e = = = \ \ \ " u n d e f i n e d \ \ \ " ; \ \ n } \ \ n f u n c t i o n t y p e O f ( v ) { \ \ n \ \ t c o n s t t = t y p e o f v ; \ \ n \ \ t i f ( t ! = = \ \ \ " o b j e c t \ \ \ " ) r e t u r n t ; \ \ n \ \ t i f ( v = = = n u l l ) r e t u r n \ \ \ " n u l l \ \ \ " ; \ \ n \ \ t r e t u r n O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v ) ; \ \ n } \ \ n f u n c t i o n o n A b o r t ( s i g n a l 2 , l i s t e n e r ) { \ \ n \ \ t i f ( ! s i g n a l 2 | | ! ( s i g n a l 2 i n s t a n c e o f A b o r t S i g n a l ) ) \ \ n \ \ t \ \ t r e t u r n t r u e ; \ \ n \ \ t i f ( s i g n a l 2 . a b o r t e d ) \ \ n \ \ t \ \ t r e t u r n ; \ \ n \ \ t s i g n a l 2 . a d d E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t r e t u r n f u n c t i o n c l e a n U p ( ) { \ \ n \ \ t \ \ t s i g n a l 2 . r e m o v e E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t } ; \ \ n } \ \ n f u n c t i o n o b s e r v e d A t t r i b u t e s ( i n s t a n c e , o b s e r v e d A t t r i b u t e 2 ) { \ \ n \ \ t c o n s t { o b s e r v e d A t t r i b u t e s : o b s e r v e d A t t r i b u t e s 3 = [ ] } = i n s t a n c e . c o n s t r u c t o r ; \ \ n \ \ t r e t u r n o b s e r v e d A t t r i b u t e s 3 . r e d u c e ( f u n c t i o n ( o u t , n a m e ) { \ \ n \ \ t \ \ t o u t [ k e b a b T o C a m e l ( n a m e ) ] = o b s e r v e d A t t r i b u t e 2 ( i n s t a n c e , n a m e ) ; \ \ n \ \ t \ \ t r e t u r n o u t ; \ \ n \ \ t } , { } ) ; \ \ n } \ \ n f u n c t i o n k e b a b T o C a m e l ( n a m e ) { \ \ n \ \ t r e t u r n n a m e . r e p l a c e ( / - . / g , ( x ) = > x [ 1 ] . t o U p p e r C a s e ( ) ) ; \ \ n } \ \ n \ \ n / / s r c / d o m - c o m m o n . j s \ \ n v a r e n v i r o m e n t = { \ \ n \ \ t s e t D e l e t e A t t r , \ \ n \ \ t s s r : \ \ \ " \ \ \ " , \ \ n \ \ t D : g l o b a l T h i s . d o c u m e n t , \ \ n \ \ t F : g l o b a l T h i s . D o c u m e n t F r a g m e n t , \ \ n \ \ t H : g l o b a l T h i s . H T M L E l e m e n t , \ \ n \ \ t S : g l o b a l T h i s . S V G E l e m e n t , \ \ n \ \ t M : g l o b a l T h i s . M u t a t i o n O b s e r v e r \ \ n } ; \ \ n f u n c t i o n s e t D e l e t e A t t r ( o b j , p r o p , v a l ) { \ \ n \ \ t R e f l e c t . s e t ( o b j , p r o p , v a l ) ; \ \ n \ \ t i f ( ! i s U n d e f ( v a l ) ) r e t u r n ; \ \ n \ \ t R e f l e c t . d e l e t e P r o p e r t y ( o b j , p r o p ) ; \ \ n \ \ t i f ( o b j i n s t a n c e o f e n v i r o m e n t . H & & o b j . g e t A t t r i b u t e ( p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n o b j . r e m o v e A t t r i b u t e ( p r o p ) ; \ \ n \ \ t i f ( R e f l e c t . g e t ( o b j , p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n R e f l e c t . s e t ( o b j , p r o p , \ \ \ " \ \ \ " ) ; \ \ n } \ \ n v a r k e y L T E = \ \ \ " _ _ d d e _ l i f e c y c l e s T o E v e n t s \ \ \ " ; \ \ n v a r e v c = \ \ \ " d d e : c o n n e c t e d \ \ \ " ; \ \ n v a r e v d = \ \ \ " d d e : d i s c o n n e c t e d \ \ \ " ; \ \ n v a r e v a = \ \ \ " d d e : a t t r i b u t e C h a n g e d \ \ \ " ; \ \ n \ \ n / / s r c / d o m . j s \ \ n v a r s c o p e s = [ { \ \ n \ \ t g e t s c o p e ( ) { \ \ n \ \ t \ \ t r e t u r n e n v i r o m e n t . D . b o d y ; \ \ n \ \ t } , \ \ n \ \ t h o s t : ( c ) = > c ? c ( e n v i r o m e n t . D . b o d y ) : e n v i r o m e n t . D . b o d y , \ \ n \ \ t p r e v e n t : t r u e \ \ n } ] ; \ \ n v a r s c o p e = { \ \ n \ \ t g e t c u r r e n t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s [ s c o p e s . l e n g t h - 1 ] ; \ \ n \ \ t } , \ \ n \ \ t g e t h o s t ( ) { \ \ n \ \ t \ \ t r e t u r n t h i s . c u r r e n t . h o s t ; \ \ n \ \ t } , \ \ n \ \ t p r e v e n t D e f a u l t ( ) { \ \ n \ \
2023-11-23 16:30:20 +01:00
document.head.append(
el("style").append(
".class1{ font-weight: bold; }",
".class2{ color: purple; }"
)
);
document.body.append(
el(component, { className: "class2", textContent: "Hello World!" }),
component({ className: "class2", textContent: "Hello World!" })
);
function component({ className, textContent }){
return el("div", { className: [ "class1", className ] }).append(
el("p", textContent)
);
}
2024-11-07 10:17:54 +01:00
< / code > < / div > < script > F l e m s ( d o c u m e n t . g e t E l e m e n t B y I d ( " c o d e - e x a m p l e - 1 - 3 2 0 z z 4 y 0 7 2 o " ) , J S O N . p a r s e ( " { \ " f i l e s \ " : [ { \ " n a m e \ " : \ " . j s \ " , \ " c o n t e n t \ " : \ " i m p o r t { e l } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n d o c u m e n t . h e a d . a p p e n d ( \ \ n \ \ t e l ( \ \ \ " s t y l e \ \ \ " ) . a p p e n d ( \ \ n \ \ t \ \ t \ \ \ " . c l a s s 1 { f o n t - w e i g h t : b o l d ; } \ \ \ " , \ \ n \ \ t \ \ t \ \ \ " . c l a s s 2 { c o l o r : p u r p l e ; } \ \ \ " \ \ n \ \ t ) \ \ n ) ; \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t e l ( c o m p o n e n t , { c l a s s N a m e : \ \ \ " c l a s s 2 \ \ \ " , t e x t C o n t e n t : \ \ \ " H e l l o W o r l d ! \ \ \ " } ) , \ \ n \ \ t c o m p o n e n t ( { c l a s s N a m e : \ \ \ " c l a s s 2 \ \ \ " , t e x t C o n t e n t : \ \ \ " H e l l o W o r l d ! \ \ \ " } ) \ \ n ) ; \ \ n \ \ n f u n c t i o n c o m p o n e n t ( { c l a s s N a m e , t e x t C o n t e n t } ) { \ \ n \ \ t r e t u r n e l ( \ \ \ " d i v \ \ \ " , { c l a s s N a m e : [ \ \ \ " c l a s s 1 \ \ \ " , c l a s s N a m e ] } ) . a p p e n d ( \ \ n \ \ t \ \ t e l ( \ \ \ " p \ \ \ " , t e x t C o n t e n t ) \ \ n \ \ t ) ; \ \ n } \ \ n \ " } , { \ " n a m e \ " : \ " e s m - w i t h - s i g n a l s . j s \ " , \ " c o n t e n t \ " : \ " / / s r c / s i g n a l s - c o m m o n . j s \ \ n v a r s i g n a l s _ g l o b a l = { \ \ n \ \ t i s S i g n a l ( a t t r i b u t e s ) { \ \ n \ \ t \ \ t r e t u r n f a l s e ; \ \ n \ \ t } , \ \ n \ \ t p r o c e s s R e a c t i v e A t t r i b u t e ( o b j , k e y , a t t r , s e t ) { \ \ n \ \ t \ \ t r e t u r n a t t r ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n r e g i s t e r R e a c t i v i t y ( d e f , g l o b a l = t r u e ) { \ \ n \ \ t i f ( g l o b a l ) r e t u r n O b j e c t . a s s i g n ( s i g n a l s _ g l o b a l , d e f ) ; \ \ n \ \ t O b j e c t . s e t P r o t o t y p e O f ( d e f , s i g n a l s _ g l o b a l ) ; \ \ n \ \ t r e t u r n d e f ; \ \ n } \ \ n f u n c t i o n s i g n a l s ( _ t h i s ) { \ \ n \ \ t r e t u r n s i g n a l s _ g l o b a l . i s P r o t o t y p e O f ( _ t h i s ) & & _ t h i s ! = = s i g n a l s _ g l o b a l ? _ t h i s : s i g n a l s _ g l o b a l ; \ \ n } \ \ n \ \ n / / s r c / h e l p e r s . j s \ \ n v a r h a s O w n = ( . . . a ) = > O b j e c t . p r o t o t y p e . h a s O w n P r o p e r t y . c a l l ( . . . a ) ; \ \ n f u n c t i o n i s U n d e f ( v a l u e ) { \ \ n \ \ t r e t u r n t y p e o f v a l u e = = = \ \ \ " u n d e f i n e d \ \ \ " ; \ \ n } \ \ n f u n c t i o n t y p e O f ( v ) { \ \ n \ \ t c o n s t t = t y p e o f v ; \ \ n \ \ t i f ( t ! = = \ \ \ " o b j e c t \ \ \ " ) r e t u r n t ; \ \ n \ \ t i f ( v = = = n u l l ) r e t u r n \ \ \ " n u l l \ \ \ " ; \ \ n \ \ t r e t u r n O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v ) ; \ \ n } \ \ n f u n c t i o n o n A b o r t ( s i g n a l 2 , l i s t e n e r ) { \ \ n \ \ t i f ( ! s i g n a l 2 | | ! ( s i g n a l 2 i n s t a n c e o f A b o r t S i g n a l ) ) \ \ n \ \ t \ \ t r e t u r n t r u e ; \ \ n \ \ t i f ( s i g n a l 2 . a b o r t e d ) \ \ n \ \ t \ \ t r e t u r n ; \ \ n \ \ t s i g n a l 2 . a d d E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t r e t u r n f u n c t i o n c l e a n U p ( ) { \ \ n \ \ t \ \ t s i g n a l 2 . r e m o v e E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t } ; \ \ n } \ \ n f u n c t i o n o b s e r v e d A t t r i b u t e s ( i n s t a n c e , o b s e r v e d A t t r i b u t e 2 ) { \ \ n \ \ t c o n s t { o b s e r v e d A t t r i b u t e s : o b s e r v e d A t t r i b u t e s 3 = [ ] } = i n s t a n c e . c o n s t r u c t o r ; \ \ n \ \ t r e t u r n o b s e r v e d A t t r i b u t e s 3 . r e d u c e ( f u n c t i o n ( o u t , n a m e ) { \ \ n \ \ t \ \ t o u t [ k e b a b T o C a m e l ( n a m e ) ] = o b s e r v e d A t t r i b u t e 2 ( i n s t a n c e , n a m e ) ; \ \ n \ \ t \ \ t r e t u r n o u t ; \ \ n \ \ t } , { } ) ; \ \ n } \ \ n f u n c t i o n k e b a b T o C a m e l ( n a m e ) { \ \ n \ \ t r e t u r n n a m e . r e p l a c e ( / - . / g , ( x ) = > x [ 1 ] . t o U p p e r C a s e ( ) ) ; \ \ n } \ \ n \ \ n / / s r c / d o m - c o m m o n . j s \ \ n v a r e n v i r o m e n t = { \ \ n \ \ t s e t D e l e t e A t t r , \ \ n \ \ t s s r : \ \ \ " \ \ \ " , \ \ n \ \ t D : g l o b a l T h i s . d o c u m e n t , \ \ n \ \ t F : g l o b a l T h i s . D o c u m e n t F r a g m e n t , \ \ n \ \ t H : g l o b a l T h i s . H T M L E l e m e n t , \ \ n \ \ t S : g l o b a l T h i s . S V G E l e m e n t , \ \ n \ \ t M : g l o b a l T h i s . M u t a t i o n O b s e r v e r \ \ n } ; \ \ n f u n c t i o n s e t D e l e t e A t t r ( o b j , p r o p , v a l ) { \ \ n \ \ t R e f l e c t . s e t ( o b j , p r o p , v a l ) ; \ \ n \ \ t i f ( ! i s U n d e f ( v a l ) ) r e t u r n ; \ \ n \ \ t R e f l e c t . d e l e t e P r o p e r t y ( o b j , p r o p ) ; \ \ n \ \ t i f ( o b j i n s t a n c e o f e n v i r o m e n t . H & & o b j . g e t A t t r i b u t e ( p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n o b j . r e m o v e A t t r i b u t e ( p r o p ) ; \ \ n \ \ t i f ( R e f l e c t . g e t ( o b j , p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n R e f l e c t . s e t ( o b j , p r o p , \ \ \ " \ \ \ " ) ; \ \ n } \ \ n v a r k e y L T E = \ \ \ " _ _ d d e _ l i f e c y c l e s T o E v e n t s \ \ \ " ; \ \ n v a r e v c = \ \ \ " d d e : c o n n e c t e d \ \ \ " ; \ \ n v a r e v d = \ \ \ " d d e : d i s c o n n e c t e d \ \ \ " ; \ \ n v a r e v a = \ \ \ " d d e : a t t r i b u t e C h a n g e d \ \ \ " ; \ \ n \ \ n / / s r c / d o m . j s \ \ n v a r s c o p e s = [ { \ \ n \ \ t g e t s c o p e ( ) { \ \ n \ \ t \ \ t r e t u r n e n v i r o m e n t . D . b o d y ; \ \ n \ \ t } , \ \ n \ \ t h o s t : ( c ) = > c ? c ( e n v i r o m e n t . D . b o d y ) : e n v i r o m e n t . D . b o d y , \ \ n \ \ t p r e v e n t : t r u e \ \ n } ] ; \ \ n v a r s c o p e = { \ \ n \ \ t g e t c u r r e n t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s [ s c o p e s . l e n g t h - 1 ] ; \ \ n \ \ t } , \ \ n \ \ t g e t h o s t ( ) { \ \ n \ \ t \ \ t r e t u r n t h i s . c u r r e n t . h o s t ; \ \ n \ \ t } , \ \ n \ \ t p r e v e n t D e f a u l t ( ) { \ \ n \ \ t \ \ t c o n s t { c u r r e n t } = t h i s ; \ \ n \ \ t \ \ t c u r r e n t . p r e v e n t = t r u e ; \ \ n \ \ t \ \ t r e t u r n c u r r e n t ; \ \ n \ \ t } , \ \ n \ \ t g e t s t a t e ( ) { \ \ n \ \ t \ \ t r e t u r n [ . . . s c o p e s ] ; \ \ n \ \ t } , \ \ n \ \ t p u s h ( s = { } ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s . p u s h ( O b j e c t . a s s i g n ( { } , t h i s . c u r r e n t , { p r e v e n t : f a l s e } , s ) ) ; \ \ n \ \ t } , \ \ n \ \ t p u s h R o o t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s . p u s h ( s c o p e s [ 0 ] ) ; \ \ n \ \ t } , \ \ n \ \ t p o p ( ) { \ \ n \ \ t \ \ t i f ( s c o p e s . l e n g t h = = = 1 ) r e t u r n ; \ \ n \ \ t \ \ t r e t u r n s c o p e s . p o p ( ) ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n a p p e n d ( . . . e l s ) { \ \ n \ \ t t h i s . a p p e n d O r i g i n a l ( . . . e l s ) ; \ \ n \ \ t r e t u r n t h i s ; \ \ n } \ \ n f u n c t i o n c h a i n a b l e A p p e n d ( e l ) { \ \ n \ \ t i f ( e l . a p p e n
2023-11-23 16:30:20 +01:00
const elSVG= elNS("http://www.w3.org/2000/svg");
const elMath= elNS("http://www.w3.org/1998/Math/MathML");
document.body.append(
elSVG("svg"), // see https://developer.mozilla.org/en-US/docs/Web/SVG and https://developer.mozilla.org/en-US/docs/Web/API/SVGElement
elMath("math") // see https://developer.mozilla.org/en-US/docs/Web/MathML and https://developer.mozilla.org/en-US/docs/Web/MathML/Global_attributes
);
console.log(
document.body.innerHTML.includes("< svg> < /svg> < math> < /math> ")
)
2024-11-07 10:17:54 +01:00
< / code > < / div > < script > F l e m s ( d o c u m e n t . g e t E l e m e n t B y I d ( " c o d e - e x a m p l e - 1 - b c j y d b 5 0 g d c " ) , J S O N . p a r s e ( " { \ " f i l e s \ " : [ { \ " n a m e \ " : \ " . j s \ " , \ " c o n t e n t \ " : \ " i m p o r t { e l N S , a s s i g n } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n c o n s t e l S V G = e l N S ( \ \ \ " h t t p : / / w w w . w 3 . o r g / 2 0 0 0 / s v g \ \ \ " ) ; \ \ n c o n s t e l M a t h = e l N S ( \ \ \ " h t t p : / / w w w . w 3 . o r g / 1 9 9 8 / M a t h / M a t h M L \ \ \ " ) ; \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t e l S V G ( \ \ \ " s v g \ \ \ " ) , / / s e e h t t p s : / / d e v e l o p e r . m o z i l l a . o r g / e n - U S / d o c s / W e b / S V G a n d h t t p s : / / d e v e l o p e r . m o z i l l a . o r g / e n - U S / d o c s / W e b / A P I / S V G E l e m e n t \ \ n \ \ t e l M a t h ( \ \ \ " m a t h \ \ \ " ) / / s e e h t t p s : / / d e v e l o p e r . m o z i l l a . o r g / e n - U S / d o c s / W e b / M a t h M L a n d h t t p s : / / d e v e l o p e r . m o z i l l a . o r g / e n - U S / d o c s / W e b / M a t h M L / G l o b a l _ a t t r i b u t e s \ \ n ) ; \ \ n \ \ n c o n s o l e . l o g ( \ \ n \ \ t d o c u m e n t . b o d y . i n n e r H T M L . i n c l u d e s ( \ \ \ " < s v g > < / s v g > < m a t h > < / m a t h > \ \ \ " ) \ \ n ) \ \ n \ " } , { \ " n a m e \ " : \ " e s m - w i t h - s i g n a l s . j s \ " , \ " c o n t e n t \ " : \ " / / s r c / s i g n a l s - c o m m o n . j s \ \ n v a r s i g n a l s _ g l o b a l = { \ \ n \ \ t i s S i g n a l ( a t t r i b u t e s ) { \ \ n \ \ t \ \ t r e t u r n f a l s e ; \ \ n \ \ t } , \ \ n \ \ t p r o c e s s R e a c t i v e A t t r i b u t e ( o b j , k e y , a t t r , s e t ) { \ \ n \ \ t \ \ t r e t u r n a t t r ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n r e g i s t e r R e a c t i v i t y ( d e f , g l o b a l = t r u e ) { \ \ n \ \ t i f ( g l o b a l ) r e t u r n O b j e c t . a s s i g n ( s i g n a l s _ g l o b a l , d e f ) ; \ \ n \ \ t O b j e c t . s e t P r o t o t y p e O f ( d e f , s i g n a l s _ g l o b a l ) ; \ \ n \ \ t r e t u r n d e f ; \ \ n } \ \ n f u n c t i o n s i g n a l s ( _ t h i s ) { \ \ n \ \ t r e t u r n s i g n a l s _ g l o b a l . i s P r o t o t y p e O f ( _ t h i s ) & & _ t h i s ! = = s i g n a l s _ g l o b a l ? _ t h i s : s i g n a l s _ g l o b a l ; \ \ n } \ \ n \ \ n / / s r c / h e l p e r s . j s \ \ n v a r h a s O w n = ( . . . a ) = > O b j e c t . p r o t o t y p e . h a s O w n P r o p e r t y . c a l l ( . . . a ) ; \ \ n f u n c t i o n i s U n d e f ( v a l u e ) { \ \ n \ \ t r e t u r n t y p e o f v a l u e = = = \ \ \ " u n d e f i n e d \ \ \ " ; \ \ n } \ \ n f u n c t i o n t y p e O f ( v ) { \ \ n \ \ t c o n s t t = t y p e o f v ; \ \ n \ \ t i f ( t ! = = \ \ \ " o b j e c t \ \ \ " ) r e t u r n t ; \ \ n \ \ t i f ( v = = = n u l l ) r e t u r n \ \ \ " n u l l \ \ \ " ; \ \ n \ \ t r e t u r n O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v ) ; \ \ n } \ \ n f u n c t i o n o n A b o r t ( s i g n a l 2 , l i s t e n e r ) { \ \ n \ \ t i f ( ! s i g n a l 2 | | ! ( s i g n a l 2 i n s t a n c e o f A b o r t S i g n a l ) ) \ \ n \ \ t \ \ t r e t u r n t r u e ; \ \ n \ \ t i f ( s i g n a l 2 . a b o r t e d ) \ \ n \ \ t \ \ t r e t u r n ; \ \ n \ \ t s i g n a l 2 . a d d E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t r e t u r n f u n c t i o n c l e a n U p ( ) { \ \ n \ \ t \ \ t s i g n a l 2 . r e m o v e E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t } ; \ \ n } \ \ n f u n c t i o n o b s e r v e d A t t r i b u t e s ( i n s t a n c e , o b s e r v e d A t t r i b u t e 2 ) { \ \ n \ \ t c o n s t { o b s e r v e d A t t r i b u t e s : o b s e r v e d A t t r i b u t e s 3 = [ ] } = i n s t a n c e . c o n s t r u c t o r ; \ \ n \ \ t r e t u r n o b s e r v e d A t t r i b u t e s 3 . r e d u c e ( f u n c t i o n ( o u t , n a m e ) { \ \ n \ \ t \ \ t o u t [ k e b a b T o C a m e l ( n a m e ) ] = o b s e r v e d A t t r i b u t e 2 ( i n s t a n c e , n a m e ) ; \ \ n \ \ t \ \ t r e t u r n o u t ; \ \ n \ \ t } , { } ) ; \ \ n } \ \ n f u n c t i o n k e b a b T o C a m e l ( n a m e ) { \ \ n \ \ t r e t u r n n a m e . r e p l a c e ( / - . / g , ( x ) = > x [ 1 ] . t o U p p e r C a s e ( ) ) ; \ \ n } \ \ n \ \ n / / s r c / d o m - c o m m o n . j s \ \ n v a r e n v i r o m e n t = { \ \ n \ \ t s e t D e l e t e A t t r , \ \ n \ \ t s s r : \ \ \ " \ \ \ " , \ \ n \ \ t D : g l o b a l T h i s . d o c u m e n t , \ \ n \ \ t F : g l o b a l T h i s . D o c u m e n t F r a g m e n t , \ \ n \ \ t H : g l o b a l T h i s . H T M L E l e m e n t , \ \ n \ \ t S : g l o b a l T h i s . S V G E l e m e n t , \ \ n \ \ t M : g l o b a l T h i s . M u t a t i o n O b s e r v e r \ \ n } ; \ \ n f u n c t i o n s e t D e l e t e A t t r ( o b j , p r o p , v a l ) { \ \ n \ \ t R e f l e c t . s e t ( o b j , p r o p , v a l ) ; \ \ n \ \ t i f ( ! i s U n d e f ( v a l ) ) r e t u r n ; \ \ n \ \ t R e f l e c t . d e l e t e P r o p e r t y ( o b j , p r o p ) ; \ \ n \ \ t i f ( o b j i n s t a n c e o f e n v i r o m e n t . H & & o b j . g e t A t t r i b u t e ( p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n o b j . r e m o v e A t t r i b u t e ( p r o p ) ; \ \ n \ \ t i f ( R e f l e c t . g e t ( o b j , p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n R e f l e c t . s e t ( o b j , p r o p , \ \ \ " \ \ \ " ) ; \ \ n } \ \ n v a r k e y L T E = \ \ \ " _ _ d d e _ l i f e c y c l e s T o E v e n t s \ \ \ " ; \ \ n v a r e v c = \ \ \ " d d e : c o n n e c t e d \ \ \ " ; \ \ n v a r e v d = \ \ \ " d d e : d i s c o n n e c t e d \ \ \ " ; \ \ n v a r e v a = \ \ \ " d d e : a t t r i b u t e C h a n g e d \ \ \ " ; \ \ n \ \ n / / s r c / d o m . j s \ \ n v a r s c o p e s = [ { \ \ n \ \ t g e t s c o p e ( ) { \ \ n \ \ t \ \ t r e t u r n e n v i r o m e n t . D . b o d y ; \ \ n \ \ t } , \ \ n \ \ t h o s t : ( c ) = > c ? c ( e n v i r o m e n t . D . b o d y ) : e n v i r o m e n t . D . b o d y , \ \ n \ \ t p r e v e n t : t r u e \ \ n } ] ; \ \ n v a r s c o p e = { \ \ n \ \ t g e t c u r r e n t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s [ s c o p e s . l e n g t h - 1 ] ; \ \ n \ \ t } , \ \ n \ \ t g e t h o s t ( ) { \ \ n \ \ t \ \ t r e t u r n t h i s . c u r r e n t . h o s t ; \ \ n \ \ t } , \ \ n \ \ t p r e v e n t D e f a u l t ( ) { \ \ n \ \ t \ \ t c o n s t { c u r r e n t } = t h i s ; \ \ n \ \ t \ \ t c u r r e n t . p r e v e n t = t r u e ; \ \ n \ \ t \ \ t r e t u r n c u r r e n t ; \ \ n \ \ t } , \ \ n \ \ t g e t s t a t e ( ) { \ \ n \ \ t \ \ t r e t u r n [ . . . s c o p e s ] ; \ \ n \ \ t } , \ \ n \ \ t p u s h ( s = { } ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s . p u s h ( O b j e c t . a s s i g n ( { } , t h i s . c u r r e n t , { p r e v e n t : f a l s e } , s ) ) ; \ \ n \ \ t } , \ \ n \ \ t p u s h R o o t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s . p u s h ( s c o p e s [ 0 ] ) ; \ \ n \ \ t } , \ \ n \ \ t p o p ( ) { \ \ n \ \ t \ \ t i f ( s c o p e s . l e n g t h = = = 1 ) r e t u r n ; \ \ n \ \ t \ \ t r e t u r n s c o p e s . p o p ( ) ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n a p p e n d ( . . . e l s ) { \ \ n \ \ t t h i s . a p p e n d O r i g i n a l ( . . . e l s ) ; \ \ n \ \ t r e t u r n t h i s ; \ \ n } \ \ n f u n c t i o n c h a i n a b l e A p p e