Dom\HTMLDocument: Impossible to add text node on document level
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 48/100
Direzione di ricerca
Inizia riproducendo lo snippet PHP con Dom\HTMLDocument::createFromString, LIBXML_HTML_NOIMPLIED e CharacterData::replaceWith(). Traccia il modo in cui replaceWith(), insertAdjacentHTML() e appendChild() gestiscono i nodi di testo a livello di documento, quindi confronta gli entry point Lexbor conformi e non conformi alla specifica menzionati nell’issue. Il lavoro è completo quando si è arrivati a una decisione chiara e si è implementato o documentato un comportamento coerente per i documenti NOIMPLIED, con test per il caso mostrato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Description
The following code...
$doc = \Dom\HTMLDocument::createFromString('Hello <b>world</b>!',LIBXML_HTML_NOIMPLIED);
$greeting = $doc->firstChild;
echo get_class($greeting)."<br />";
$new_greeting = $doc->createTextNode('Welcome');
$greeting->replaceWith($new_greeting);
echo htmlentities($doc->saveHtml());
results in this error:
DOMException: Cannot insert text as a child of a document in Dom\CharacterData->replaceWith()
This is different behavior from the old DOM classes which allowed this. I understand, this spec-compliance is supposed to be a feature. If I understand correctly, the new Lexbor-based parser offers validity checks for some methods and not for others:
https://lexbor.com/modules/dom/
E.g. lxb_dom_node_insert_before_spec vs lxb_dom_node_insert_before etc.
I'm unaware of the current PHP implementation but I assume it is using the _spec versions throughout without any option to turn it off.
I think this is problematic. The NOIMPLIED option is very useful indeed if you want to handle document fragments etc. without unnecessary wrapper tags.
The above code leads to the peculiar situation that you can indeed create a Document with textNodes on the document level, but if you just want to replace that textNode with a different textNode it won't work. This complicates things a lot unnecessarily. Other methods like insertAdjacentHTML, appendChild etc are equally stubborn.
There are different solutions for this: IMHO the easiest one would be to use the non-spec versions of these functions if NOIMPLIED was specified. NOIMPLIED explicitly allows a non-spec document to be imported. Any manipulation should also be allowed then.
Another alternative would be to create a new general flag LIBXML_HTML_RELAX_SPEC.
A third alternative would be to specifically add option flags to the individual methods to allow this for a single call.
I'm unsure what's really the best solution but at the moment, porting over existing code is unnecessarily difficult due to this restriction and I really think this feature would make a lot of sense anyway. Thanks for considering.
- Lingua principale
- C
- Stelle
- 40.4k
- Fork
- 8.2k
- Merge medio
- 2g 12h
- PR unite (30g)
- 112
Guida per i contributori
Apri 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 php/php-src
-
Bug Status: Needs Triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
Bug Status: Needs Triage
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
-
Bug Status: Needs Triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Flaky hrtime.phpt test ApertaBug Category: Tests Status: Verified
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
-
Bug SAPI: fpm Status: Needs Triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
Issue simili
-
Broadcast Documentation Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
kovidgoyal/kitty#10516 ·
-
libc
Difficoltà 2/5 1-2 giorni Idoneità per principianti 76/100
llvm/llvm-project#225506 · 1 commento ·
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
-
Status: Waiting triage Type: Bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
nanoframework/Home#1857 ·
-
bug
Difficoltà 2/5 Mezza giornata Idoneità per principianti 82/100