Repository navigation
refactor: 하이라이트 JS 리소스 분리 + 메시지 버스 단일화 (step 1·2) - #105
Conversation
step1: ArticleHighlightJS의 JS 757줄을 highlight-core.js/highlight-selection.js 리소스로 분리. Bundle.module 로더로 축소해 이스케이프 4중첩 제거. step2: WKWebView messageHandler 5개를 단일 highlightEvent 봉투로 통합하고 HighlightEvent enum으로 디코딩. 죽은 getSelectedText 핸들러 제거. 콜백 3개와 selectedText 바인딩을 ViewModel의 handle(_:) 단일 진입점으로 통합. 동작 보존(최종 주입 JS·이벤트 의미 동일). 포트(HighlightRenderable)와 HighlightLocator는 후속 PR.
There was a problem hiding this comment.
Code Review
This pull request refactors the Article Highlight feature in the Detail module by extracting inline JavaScript code from ArticleHighlightJS.swift into dedicated .js resource files (highlight-core.js and highlight-selection.js). It also simplifies the communication bridge between the WKWebView and Swift by introducing a unified HighlightEvent enum and a single message handler, cleaning up view and view model bindings. Additionally, an architecture review document has been added. The review feedback suggests adding defensive checks in the JavaScript files, specifically verifying the existence of the webkit message handler to prevent runtime errors in non-iOS environments, and adding a null guard for el.parentNode when deleting highlights to avoid potential crashes.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| function postHighlightEvent(type, payload) { | ||
| window.webkit.messageHandlers.highlightEvent.postMessage({ type: type, payload: payload }); | ||
| } |
There was a problem hiding this comment.
window.webkit.messageHandlers.highlightEvent가 정의되어 있지 않은 환경(예: 일반 브라우저에서 페이지를 열거나 테스트 환경 등)에서 호출될 경우 TypeError가 발생하여 JavaScript 실행이 중단될 수 있습니다.
안전한 실행을 위해 해당 객체의 존재 여부를 먼저 확인하는 방어적 코드를 추가하는 것을 권장합니다.
function postHighlightEvent(type, payload) {
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.highlightEvent) {
window.webkit.messageHandlers.highlightEvent.postMessage({ type: type, payload: payload });
}
}| groupElements.forEach(function(el) { | ||
| const parent = el.parentNode; | ||
| while (el.firstChild) { | ||
| parent.insertBefore(el.firstChild, el); | ||
| } | ||
| parent.removeChild(el); | ||
| parent.normalize(); | ||
| }); |
There was a problem hiding this comment.
el.parentNode가 어떤 이유로든 null인 경우(예: DOM에서 이미 분리된 요소 등), parent.insertBefore 호출 시 에러가 발생할 수 있습니다.
parent가 유효한지 확인하는 방어적 코드(Null Guard)를 추가하는 것이 안전합니다.
| groupElements.forEach(function(el) { | |
| const parent = el.parentNode; | |
| while (el.firstChild) { | |
| parent.insertBefore(el.firstChild, el); | |
| } | |
| parent.removeChild(el); | |
| parent.normalize(); | |
| }); | |
| groupElements.forEach(function(el) { | |
| const parent = el.parentNode; | |
| if (!parent) return; | |
| while (el.firstChild) { | |
| parent.insertBefore(el.firstChild, el); | |
| } | |
| parent.removeChild(el); | |
| parent.normalize(); | |
| }); |
Article Highlight 리팩토링 설계(
docs/architecture-review.md)의 step 1·2. 모두 동작 보존.step 1 — JS 리소스 분리
ArticleHighlightJS의 JS 757줄 →Resources/highlight-core.js(221) +highlight-selection.js(410)ArticleHighlightJS는Bundle.module로더로 축소/[.*+?^${}()|[\\]\\\\]/g등) 제거 → JS 원본 그대로step 2 — 메시지 버스 단일화
highlightEvent봉투({type, payload})HighlightEventenum으로 디코딩, rawValue→HighlightStyle변환을 디코드 지점으로 이동getSelectedText핸들러 제거selectedText바인딩 → ViewModelhandle(_:)단일 진입점범위 밖 (후속 PR)
HighlightRenderable/WebViewHighlightRenderer) — SwiftUI WebView 소유권 재배치라 테스트 가능 단계와 함께HighlightLocator검증
Detail빌드 통과 (BUILD SUCCEEDED)