AddIfDirective.java mistakes processing instructions for elements

Aperta
#440 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@Inf1mum ci sta già lavorando.

Dal 14/11/2026.

  • #441 di @Inf1mum — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java
Ambito
tooling

Direzione di ricerca

Start in src/main/java/org/xembly/AddIfDirective.java, lines 53-57, where the child scan compares getNodeName() without checking getNodeType(). Fix the match to require Node.ELEMENT_NODE and confirm the existing creation path then adds the missing element, as shown by AttrDirective.exec() casting the node to Element. Done means the snippet in src/test/java/org/xembly (add a regression case whose PI target equals the requested element name) yields <root><?item data?><item id="1"/></root>. Note pull request #441 is already open on this, so coordinate before starting.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

ADDIF 'item' reuses <?item data?> instead of creating an item element. A following ATTR then throws ClassCastException because its cursor contains a processing instruction.

For example:

new Xembler(
    new Directives()
        .add("root")
        .pi("item", "data")
        .addIf("item")
        .attr("id", "1")
).dom();

This should produce <root><?item data?><item id="1"/></root>. Instead, ADDIF leaves the element absent and Element.class.cast(node) in AttrDirective.exec() receives the processing instruction.

In src/main/java/org/xembly/AddIfDirective.java, lines 53–57, the lookup compares names without checking the node type:

if (kids.item(idx).getNodeName().equals(this.name.raw())) {
    target = kids.item(idx);
    break;
}

A processing instruction's node name is its target, so it can match any requested element name. Restrict the match to element nodes:

if (kids.item(idx).getNodeType() == Node.ELEMENT_NODE
    && kids.item(idx).getNodeName().equals(this.name.raw())) {
    target = kids.item(idx);
    break;
}

This lets the existing creation path add the missing element while preserving the processing instruction. Add a regression case with a PI whose target equals the requested element name; the existing PI coverage uses a different target.

Lingua principale
Java
Stelle
248
Fork
33
Merge medio
17h 59m
PR unite (30g)
46

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di yegor256/xembly

Tutte le issue di yegor256/xembly

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.