fix(embeds): address Phase 38 review findings across registry, Blog Editor, and docs

This commit is contained in:
Jakub Zych
2026-11-28 13:13:09 +01:00
parent 3fc656da0f
commit a047d4284f
9 changed files with 69 additions and 37 deletions

File diff suppressed because one or more lines are too long

View File

@@ -798,12 +798,12 @@ export class BlogEditorElement extends HTMLElement {
}
const aspect = this.#inputOf(this.#embedForm, 'aspect')
if (aspect !== '' && !isValidAspect(aspect)) {
this.#embedForm.error.textContent = this.#labels.embedIdInvalid
this.#embedForm.error.textContent = this.#labels.embedAspectInvalid
return
}
const height = this.#inputOf(this.#embedForm, 'height')
if (height !== '' && !isValidHeight(height)) {
this.#embedForm.error.textContent = this.#labels.embedIdInvalid
this.#embedForm.error.textContent = this.#labels.embedHeightInvalid
return
}
const fallbackAlt = this.#inputOf(this.#embedForm, 'fallbackAlt')

View File

@@ -43,6 +43,7 @@ const YOUTUBE_HOSTS = new Set([
])
const EMBED_KEY_RE = /^[a-z][a-z0-9_-]{0,31}$/
const RESERVED_EMBED_KEYS = new Set(['constructor', 'prototype'])
const EMBED_ASPECT_RE = /^[1-9][0-9]{0,3}(?:\s*\/\s*[1-9][0-9]{0,3}|\.[0-9]{1,4})?$/
const EMBED_HEIGHT_RE = /^[1-9][0-9]{0,3}(?:px)?$/
@@ -92,7 +93,7 @@ export function resolveEmbedProviders(raw: unknown): ResolvedEmbedProvider[] {
if (!item || typeof item !== 'object' || Array.isArray(item)) continue
const cfg = item as Record<string, unknown>
const key = typeof cfg.key === 'string' ? cfg.key.trim() : ''
if (!EMBED_KEY_RE.test(key) || seen.has(key)) continue
if (!EMBED_KEY_RE.test(key) || RESERVED_EMBED_KEYS.has(key) || seen.has(key)) continue
const patternSrc = typeof cfg.idPattern === 'string' ? cfg.idPattern.trim() : ''
if (!patternSrc) continue
let idPattern: RegExp
@@ -103,9 +104,11 @@ export function resolveEmbedProviders(raw: unknown): ResolvedEmbedProvider[] {
continue
}
const name = typeof cfg.name === 'string' && cfg.name.trim() !== '' ? cfg.name.trim() : key
const idFromUrl = key === 'youtube' ? EMBED_PROVIDERS.youtube.idFromUrl : undefined
const rawMode = typeof cfg.mode === 'string' ? cfg.mode.trim() : ''
// Only providers with a URL parser can operate in URL mode (WR-03).
const mode: EmbedInputMode =
rawMode === 'url' || rawMode === 'id' ? rawMode : key === 'youtube' ? 'url' : 'id'
rawMode === 'url' && idFromUrl ? 'url' : rawMode === 'id' ? 'id' : idFromUrl ? 'url' : 'id'
const placeholder = typeof cfg.placeholder === 'string' && cfg.placeholder.trim() !== '' ? cfg.placeholder.trim() : undefined
const resolved: ResolvedEmbedProvider = {
key,
@@ -113,7 +116,7 @@ export function resolveEmbedProviders(raw: unknown): ResolvedEmbedProvider[] {
idPattern,
mode,
...(placeholder ? { placeholder } : {}),
...(key === 'youtube' ? { idFromUrl: EMBED_PROVIDERS.youtube.idFromUrl } : {}),
...(idFromUrl ? { idFromUrl } : {}),
}
seen.add(key)
out.push(resolved)

View File

@@ -48,7 +48,9 @@ export interface Labels {
embedFallbackAlt: string
embedCaption: string
embedAspect: string
embedAspectInvalid: string
embedHeight: string
embedHeightInvalid: string
embedGeneric: string
cancel: string
uploading: string
@@ -133,7 +135,9 @@ const en: Labels = {
embedFallbackAlt: 'Fallback alt text (optional)',
embedCaption: 'Caption (optional)',
embedAspect: 'Aspect ratio (optional)',
embedAspectInvalid: 'Enter a valid aspect ratio (e.g. 16/9 or 1.5).',
embedHeight: 'Height (optional)',
embedHeightInvalid: 'Enter a valid height in pixels (e.g. 420 or 420px).',
embedGeneric: 'Embed',
cancel: 'Cancel',
uploading: 'Uploading {name}…',
@@ -215,7 +219,9 @@ const pl: Labels = {
embedFallbackAlt: 'Tekst alternatywny obrazu (opcjonalny)',
embedCaption: 'Podpis (opcjonalny)',
embedAspect: 'Proporcje (opcjonalne)',
embedAspectInvalid: 'Wpisz poprawne proporcje (np. 16/9 lub 1.5).',
embedHeight: 'Wysokość (opcjonalna)',
embedHeightInvalid: 'Wpisz poprawną wysokość w pikselach (np. 420 lub 420px).',
embedGeneric: 'Osadź',
cancel: 'Anuluj',
uploading: 'Wysyłanie {name}…',

View File

@@ -161,7 +161,7 @@ export function previewBody(
if (provider === 'youtube' && figure.childNodes.length === 0) {
const card = doc.createElement('figure')
card.className = 'embed-card'
card.className = 'embed-card embed-card--youtube'
const img = doc.createElement('img')
img.setAttribute('src', youtubeThumbnail(id))
img.setAttribute('alt', '')
@@ -220,7 +220,8 @@ function previewStyles(c: PreviewColors, font: string): string {
`th{background:${c.subtle};font-weight:600;text-align:left}`,
`figcaption{font-size:13px;color:${c.muted};margin-top:4px}`,
`.embed-card{max-width:480px;border:1px solid ${c.border};border-radius:8px;overflow:hidden;background:${c.subtle}}`,
'.embed-card img{display:block;width:100%;height:auto;aspect-ratio:16/9;object-fit:cover}',
'.embed-card img{display:block;width:100%;height:auto}',
'.embed-card--youtube img{aspect-ratio:16/9;object-fit:cover}',
`.embed-card__placeholder{padding:24px 16px;text-align:center;font-size:14px;color:${c.muted}}`,
'.embed-card figcaption{padding:8px 12px;margin:0}',
].join('\n')