bug(mock-doc): `Element.toggleAttribute()` is missing, so components that use it fail during SSR
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 78/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- typescript
- Ambito
- web-dev
Direzione di ricerca
Il metodo appartiene a MockElement in packages/mock-doc/src/node.ts, accanto a hasAttribute, setAttribute e removeAttribute. Leggi prima questi per seguirne le convenzioni, poi riproduci il metodo mancante chiamando toggleAttribute su un elemento di MockDocument. È completato quando toggleAttribute copre i quattro casi della tabella dell'issue (con e senza force) e restituisce il booleano corretto, e i test esistenti di mock-doc continuano a passare.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already report this problem, without success.
Stencil Version
4.45.3 and 5.0.0-rc.1
Current Behavior
mock-doc's MockElement doesn't implement Element.toggleAttribute(). Server rendering runs components on mock-doc, so a component that calls it works in the browser but throws during renderToString(). The render still completes, but the attribute is missing from the output and the error appears only in diagnostics, which makes the bug easy to miss.
toggleAttribute() is the standard way to set a boolean attribute from state, and every browser Stencil 5 supports has it, so component authors reach for it naturally.
Expected Behavior
Workaround
Authors replace it with an explicit branch:
active ? this.el.setAttribute('data-active', '') : this.el.removeAttribute('data-active');
It works, but every component that may be server-rendered has to know to avoid a standard DOM method.
Suggested fix
Add toggleAttribute() to MockElement in packages/mock-doc/src/node.ts, next to hasAttribute, setAttribute and removeAttribute, with the DOM's semantics for the optional force argument:
toggleAttribute(attrName: string, force?: boolean): boolean {
const has = this.hasAttribute(attrName);
if (force === undefined ? !has : force) {
if (!has) this.setAttribute(attrName, '');
return true;
}
if (has) this.removeAttribute(attrName);
return false;
}
┌─────────────────────────────┬──────────────────┬───────────┬─────────┐
│ Call │ Attribute before │ After │ Returns │
├─────────────────────────────┼──────────────────┼───────────┼─────────┤
│ toggleAttribute('a') │ absent │ a="" │ true │
├─────────────────────────────┼──────────────────┼───────────┼─────────┤
│ toggleAttribute('a') │ present │ absent │ false │
├─────────────────────────────┼──────────────────┼───────────┼─────────┤
│ toggleAttribute('a', true) │ present │ unchanged │ true │
├─────────────────────────────┼──────────────────┼───────────┼─────────┤
│ toggleAttribute('a', false) │ absent │ unchanged │ false │
└─────────────────────────────┴──────────────────┴───────────┴─────────┘
Going through setAttribute and removeAttribute keeps mock-doc's existing attame lowercasing, and the attributeChangedCallback behavior.
System Info
Steps to Reproduce
Reproduction
import { Component, Element, Prop, h } from '@stencil/core';
@Component({ tag: 'my-flag', encapsulation: { type: 'shadow' } })
export class MyFlag {
@Element() el!: HTMLElement;
@Prop() active = false;
componentWillLoad() {
this.el.toggleAttribute('data-active', this.active);
}
render() {
return <slot />;
}
}
import { renderToString } from './hydrate/index.js';
const r = await renderToString('<my-flag active>Hi</my-flag>', { fullDocument: false });
console.log(r.html); // <my-flag active="" class="hydrated" s-id="1">…: no data-active
console.log(r.diagnostics); // TypeError: this.el.toggleAttribute is not a function
The same happens with new MockDocument() directly: typeof doc.createElement('div').toggleAttribute is 'undefined', on 4.45.3 and 5.0.0-rc.1.
### Code Reproduction URL
https://github.com
### Additional Information
_No response_
- Lingua principale
- TypeScript
- Stelle
- 13.2k
- Fork
- 865
- Merge medio
- 2g 16h
- PR unite (30g)
- 29
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di stenciljs/core
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Stencil FOUC styles should use @layer to avoid cascade conflicts with React 19Forse già presa @johnjenkins l’ha presa 230 giorni fa. ApertaStencil v5
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
stenciljs/core#7016 · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 22/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di stenciljs/core
Issue simili
-
[BUG]: Duplicate preposition in heading and unformatted feedback text on Strengthen Argument pageApertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
AOSSIE-Org/DebateAI#801 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
ci: repo has no GitHub Actions workflow, so pushes and pull requests are never built or testedApertaagents area: runtime automation chore cli consistency dependencies developer-experience difficulty: easy documentation enhancement good first issue help wanted hygiene json-output priority: P3 starters tests
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Add: BOM CineApertaapproved check:passed streams:add
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
area:claude-code-web bug has repro platform:linux platform:web
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 66/100
anthropics/claude-code#110168 ·