AddIfDirective.java mistakes processing instructions for elements
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 35/100
Research direction
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.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- Java
- Stars
- 248
- Forks
- 33
- Avg merge
- 17h 59m
- Merged PRs (30d)
- 46
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from yegor256/xembly
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
let's use Xnav instead of `XPathFactory`May be free again @volodya-lombrozo claimed this 634 days ago, and no pull request is open. Openenhancement
yegor256/xembly#246 · 3 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
Dependency DashboardPossibly taken @yegor256 claimed this 1 day ago. Openwontfix
Difficulty 5/5 Over a week Newbie friendliness 10/100
yegor256/xembly#111 · 1 assignee ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
Maintainers usually reply within 1 day
-
RIS export splits an organization name containing “and” into multiple authorsPossibly taken @Gautam-aman claimed this today. Openstatus: needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
JabRef/jabref#17821 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 86/100
Maintainers usually reply within 1 day