-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenlighted-link.html
48 lines (47 loc) · 1.46 KB
/
enlighted-link.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!-- enlighted-link @version: 0.1.0 @license: MIT -->
<script>
customElements.define('enlighted-link', class extends HTMLElement{
static get observedAttributes(){
return [
// content attrs
'href',
'rel',
'media',
'hreflang',
'type',
'sizes',
'title'
]
}
connectedCallback(){
const forwardEvent = (oldEvent)=>{
this.dispatchEvent(new oldEvent.constructor(oldEvent.type, oldEvent));
}
let link;
if(this.link){
link = this.link;
} else {
link = this.link = document.createElement('link');
link.addEventListener('load', forwardEvent);
link.addEventListener('error', forwardEvent);
}
for(let attrNo = 0, len = this.attributes.length; attrNo < len; attrNo++){
link.setAttribute(this.attributes[attrNo].name, this.attributes[attrNo].value);
}
document.head.appendChild(link);
// IDEA(tomalec): attach mutation observer on links parent, to detach this in case link is detached.
}
disconnectedCallback(){
this.link.remove();
}
attributeChangedCallback(name, oldVal, newVal){
if(this.link){
if(newVal !== null){
this.link.setAttribute(name, newVal);
} else {
this.link.removeAttribute(name);
}
}
}
});
</script>