Closed
Description
Describe the bug
When manipulating a document object with processing instructions, it should be possible to add a doctype using insertBefore
. This used to work up until 0.9.3, and was broken in 0.9.4, most probably by #760
To Reproduce
I was not able to reproduce this behavior on stackblitz, but see expected behavior below with a unit test that does reproduce it
Expected behavior
Add the following test to document.test.js
, under "insertBefore":
test('should insert doctype when processing instruction is present', () => {
const doc = new DOMImplementation().createDocument(null, '');
expect(doc.childNodes).toHaveLength(0);
expect(doc.documentElement).toBeNull();
const instruction = doc.createProcessingInstruction('target', 'data');
doc.insertBefore(instruction);
const root = doc.createElement('root');
doc.appendChild(root);
expect(doc.childNodes).toHaveLength(2);
expect(doc.childNodes.item(0)).toBe(instruction);
expect(doc.childNodes.item(1)).toBe(root);
const doctype = doc.implementation.createDocumentType('qualifiedName', '', '');
doc.insertBefore(doctype, root);
expect(doc.childNodes).toHaveLength(3);
expect(doc.childNodes.item(0)).toBe(instruction);
expect(doc.childNodes.item(1)).toBe(doctype); // This assertion fails in 0.9.4, but passes in 0.9.3
expect(doc.childNodes.item(2)).toBe(root);
});
This test passes when applied on the 0.9.3 tag and fails on the 0.9.4 tag
Runtime & Version:
xmldom version: 0.9.4
runtime version: Tested with Node.js 18.0.0, 20.15.1 and 22.3.0
other related software and version: n/a
Additional context
Add any other context about the problem here.