AddIfDirective.java mistakes processing instructions for elements

オープン
#440 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@Inf1mum がすでに取り組んでいます。

2026年11月14日 から。

  • #441 @Inf1mum による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
java
領域
tooling

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Java
スター
248
フォーク
33
平均マージ
17時間 59分
マージ済み PR(30日)
46

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

yegor256/xembly のほかの issue

yegor256/xembly の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。