AddIfDirective matches existing children case-insensitively, conflating differently-cased element names

Closed Beginner friendly
#371 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java
Domain
backend, tooling

Research direction

The bug is in AddIfDirective.java, specifically in the exec method where it compares node names case-insensitively. Start by locating the file in the codebase, examine the comparison logic, and change it to a case-sensitive check. Run the provided reproduction test to verify the fix creates distinct 'Item' and 'item' elements.

Written by the indexing model from the issue text.

Description

bug

Element names in Xembly are now created and preserved with their original case (issue #31 about forced lowercasing was fixed long ago). However, AddIfDirective still looks for an already-existing child using a case-insensitive comparison, so it can silently reuse a differently-cased sibling instead of creating the element it was asked for, and subsequent directives then modify the wrong node.

Code (AddIfDirective.java, exec):

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

Steps to reproduce

Directives d = new Directives("ADD \x27root\x27; ADD \x27Item\x27; UP; ADDIF \x27item\x27; ATTR \x27marker\x27, \x27yes\x27;");
org.w3c.dom.Document dom = new Xembler(d).dom();

Actual result

<root><Item marker="yes"/></root>

ADDIF "item" found the existing Item element (capital I) via the case-insensitive comparison, reused it as the target, and the following ATTR directive set marker on Item instead of creating a new, separate item element.

Expected result

Since XML element names are case-sensitive and Xembly itself preserves the case a caller gives it when creating elements, ADDIF "item" should not treat the existing Item element as a match. It should create a new item element (so both Item and item exist as distinct siblings), the same way AddDirective and every other part of the library now treats names as case-sensitive.

Dominant language
Java
Stars
248
Forks
32
Avg merge
1d 2h
Merged PRs (30d)
37

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from yegor256/xembly

All issues in yegor256/xembly

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.