Dom\HTMLDocument: Impossible to add text node on document level
Personne n'a encore pris cette issue.
- Langage dominant
- C
- Étoiles
- 40.4k
- Forks
- 8.1k
- Merge moyen
- 2 j 13 h
- PR mergées (30 j)
- 96
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par reproduire le snippet PHP avec Dom\HTMLDocument::createFromString, LIBXML_HTML_NOIMPLIED et CharacterData::replaceWith(). Suivez la manière dont replaceWith(), insertAdjacentHTML() et appendChild() gèrent les nœuds de texte au niveau du document, puis comparez les points d’entrée Lexbor conformes et non conformes à la spécification mentionnés dans l’issue. Le travail est terminé lorsqu’une décision claire a été prise et qu’un comportement cohérent pour les documents NOIMPLIED a été implémenté ou documenté, avec des tests pour le cas présenté.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- c, php
- Domaine
- api, backend
- Type d'issue
- Fonctionnalité
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- Active
- Clarté
- Plutôt claire
- Accessibilité débutants
- 48/100