diff --git a/components/image.js b/components/image.js new file mode 100644 index 0000000..82109f3 --- /dev/null +++ b/components/image.js @@ -0,0 +1,62 @@ +class Image extends HTMLElement { + constructor() { + super(); + + this.validateAttributes(); + this.render(); + } + + validateAttributes() { + if (!this.hasAttribute("alt")) { + throw new Error('The "alt" attribute is required.'); + } + + const allowedAttributes = ["src", "alt", "width", "height"]; + for (const attr of this.attributes) { + if (!allowedAttributes.includes(attr.name)) { + throw new Error(`The attribute "${attr.name}" is not allowed.`); + } + } + } + + render() { + this.attachShadow({ mode: "open" }); + this.shadowRoot.innerHTML = this.createCss() + this.createHtml(); + } + + createCss() { + return ` + + `; + } + + createHtml() { + const src = this.getAttribute("src") ?? ""; + const alt = this.getAttribute("alt") ?? ""; + + let imgHtml = ``; + + return ` + ${imgHtml} + `; + } +} + +customElements.define("ds-image", Image); diff --git a/index.html b/index.html index 1cee870..002d6c9 100644 --- a/index.html +++ b/index.html @@ -248,13 +248,13 @@
지역에 관계없이 다양한 언어로 참여할 수 있어요
@@ -279,7 +279,7 @@ >