9148c358d0
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를 Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류. - 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로 자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거, 동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거. - 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming, Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business, Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로, 나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는 title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백). 원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지. - 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서. - 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는 지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지. - Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경. - 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
172 lines
5.1 KiB
Markdown
172 lines
5.1 KiB
Markdown
---
|
|
id: wiki-2026-0508-dom
|
|
title: DOM (Document Object Model)
|
|
category: 10_Wiki/Topics
|
|
status: verified
|
|
canonical_id: self
|
|
aliases: [DOM, Document Object Model, DOM Tree]
|
|
duplicate_of: none
|
|
source_trust_level: A
|
|
confidence_score: 0.95
|
|
verification_status: applied
|
|
tags: [dom, web, browser, html, javascript]
|
|
raw_sources: []
|
|
last_reinforced: 2026-05-10
|
|
github_commit: pending
|
|
tech_stack:
|
|
language: javascript
|
|
framework: browser
|
|
---
|
|
|
|
# DOM (Document Object Model)
|
|
|
|
## 매 한 줄
|
|
> **"매 HTML/XML document 의 tree 표현, language-agnostic API"**. W3C 표준 (1998), 현재 WHATWG DOM Living Standard. 매 browser 매 internal representation; 2026 현재 React/Vue/Solid 매 abstraction layer 위에 있지만, performance/edge cases 매 직접 DOM 이해 매 essential.
|
|
|
|
## 매 핵심
|
|
|
|
### 매 구조
|
|
- **Document** root.
|
|
- **Element node** (`<div>`, `<p>`).
|
|
- **Text node** (literal text).
|
|
- **Attribute** (element 의 property; not separate child node since DOM4).
|
|
- **Comment node**.
|
|
- **DocumentFragment** — lightweight sub-tree, not connected.
|
|
- **ShadowRoot** — encapsulated sub-tree (Web Components).
|
|
|
|
### 매 traversal
|
|
- `parentNode`, `childNodes`, `firstChild`, `lastChild`, `nextSibling`, `previousSibling`.
|
|
- `children` (Element only), `firstElementChild`.
|
|
- `querySelector` / `querySelectorAll` — CSS selector.
|
|
- `closest(selector)` — ancestor matching.
|
|
|
|
### 매 mutation
|
|
- `appendChild`, `insertBefore`, `removeChild`, `replaceChild` (legacy).
|
|
- `append`, `prepend`, `before`, `after`, `replaceWith`, `remove` (modern).
|
|
- `cloneNode(deep)`.
|
|
|
|
### 매 응용
|
|
1. Vanilla JS DOM 조작.
|
|
2. React reconciler 매 virtual DOM diff → real DOM mutation.
|
|
3. Web Components Shadow DOM encapsulation.
|
|
4. Server-side render (jsdom, happy-dom) 매 SSR.
|
|
5. Browser automation (Playwright, Puppeteer).
|
|
|
|
## 💻 패턴
|
|
|
|
### Modern element creation (no innerHTML)
|
|
```javascript
|
|
const card = Object.assign(document.createElement('article'), {
|
|
className: 'card',
|
|
});
|
|
card.append(
|
|
Object.assign(document.createElement('h2'), { textContent: title }),
|
|
Object.assign(document.createElement('p'), { textContent: body }),
|
|
);
|
|
container.append(card);
|
|
```
|
|
|
|
### Event delegation
|
|
```javascript
|
|
list.addEventListener('click', (e) => {
|
|
const item = e.target.closest('.item');
|
|
if (!item || !list.contains(item)) return;
|
|
handleClick(item.dataset.id);
|
|
});
|
|
```
|
|
|
|
### DocumentFragment — batch insert
|
|
```javascript
|
|
const frag = document.createDocumentFragment();
|
|
for (const item of items) {
|
|
const li = document.createElement('li');
|
|
li.textContent = item.name;
|
|
frag.append(li);
|
|
}
|
|
list.append(frag); // single reflow
|
|
```
|
|
|
|
### MutationObserver
|
|
```javascript
|
|
const observer = new MutationObserver((mutations) => {
|
|
for (const m of mutations) {
|
|
if (m.type === 'childList') console.log('children changed');
|
|
}
|
|
});
|
|
observer.observe(target, { childList: true, subtree: true, attributes: true });
|
|
```
|
|
|
|
### Shadow DOM (Web Component)
|
|
```javascript
|
|
class CounterButton extends HTMLElement {
|
|
#count = 0;
|
|
connectedCallback() {
|
|
const root = this.attachShadow({ mode: 'open' });
|
|
root.innerHTML = `<style>button{padding:8px}</style><button>0</button>`;
|
|
root.querySelector('button').onclick = () => {
|
|
this.#count++;
|
|
root.querySelector('button').textContent = this.#count;
|
|
};
|
|
}
|
|
}
|
|
customElements.define('counter-button', CounterButton);
|
|
```
|
|
|
|
### Range & Selection
|
|
```javascript
|
|
const range = document.createRange();
|
|
range.selectNodeContents(el);
|
|
const text = range.toString();
|
|
```
|
|
|
|
### IntersectionObserver — lazy load
|
|
```javascript
|
|
const io = new IntersectionObserver((entries) => {
|
|
for (const e of entries) {
|
|
if (e.isIntersecting) {
|
|
e.target.src = e.target.dataset.src;
|
|
io.unobserve(e.target);
|
|
}
|
|
}
|
|
});
|
|
document.querySelectorAll('img[data-src]').forEach((img) => io.observe(img));
|
|
```
|
|
|
|
## 매 결정 기준
|
|
| 상황 | Approach |
|
|
|---|---|
|
|
| App-level UI | React / Vue / Solid (don't touch DOM) |
|
|
| Library / web component | Shadow DOM + custom element |
|
|
| One-off page | Vanilla JS (`querySelector`, `append`) |
|
|
| Test / SSR | jsdom / happy-dom |
|
|
| Watch external mutations | MutationObserver |
|
|
|
|
**기본값**: framework abstraction; vanilla DOM API for libraries / leaf optimization.
|
|
|
|
## 🔗 Graph
|
|
- 부모: [[Web Standards]]
|
|
- 변형: [[Virtual DOM과 Reconciliation|Virtual DOM]] · [[Shadow DOM]]
|
|
- 응용: [[React]] · [[Web Components]] · [[Playwright]]
|
|
- Adjacent: [[CSSOM]] · [[Reflow_and_Repaint|Reflow & Repaint]]
|
|
|
|
## 🤖 LLM 활용
|
|
**언제**: DOM snippet 생성, accessibility audit, querySelector 추천.
|
|
**언제 X**: real-time interaction (LLM 매 round-trip 너무 느림).
|
|
|
|
## ❌ 안티패턴
|
|
- **innerHTML with user input**: XSS.
|
|
- **Layout thrashing**: read-write-read-write 매 force reflow 매 loop.
|
|
- **No event delegation**: 매 list item 매 listener → memory leak.
|
|
- **Detached node leak**: removed but reference 보유 → GC 실패.
|
|
- **Manipulate DOM in framework-controlled subtree**: React reconciler 와 충돌.
|
|
|
|
## 🧪 검증 / 중복
|
|
- Verified (WHATWG DOM Living Standard 2026, MDN).
|
|
- 신뢰도 A.
|
|
|
|
## 🕓 Changelog
|
|
| 날짜 | 변경 |
|
|
|---|---|
|
|
| 2026-05-08 | Phase 1 |
|
|
| 2026-05-10 | Manual cleanup — full canonical content with modern DOM APIs |
|