My understanding of the route attribute of a diff object is that it can be used to retrieve the original element that was modified. For removals, this element lives in the first dom, and for additions/modifications, it lives in the second. So given:
const { DOMParser, XMLSerializer } = require('xmldom')
, p = new DOMParser()
, domBefore = p.parseFromString(xmlBefore)
, domAfter = p.parseFromString(xmlAfter)
, diff = dd.diff(domBefore, domAfter);
However sometimes the last element of route is incorrect, breaking this invariant. Given xmlBefore
of:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE assessment>
<assessment>
<title>Test</title>
<short_title>Test</short_title>
<page>
<title>Page 1</title>
<question>
<multiple_choice shuffle="true" select="multiple" labels="false">
<choice value="A">Placeholder</choice>
<choice value="B">Placeholder</choice>
</multiple_choice>
<part id="be03031c79464d08a5bce122fee68ffc">
<response match="A" score="1">
<feedback>
<p id="aa3ab72153c834c8395a0099dff39d434"/>
</feedback>
</response>
<response match="A,B" score="0" name="AUTOGEN_{A,B}">
<feedback>
<p id="ab8e552e886ee486baf47d45c778deac4"/>
</feedback>
</response>
<response match="B" score="0" name="AUTOGEN_{B}">
<feedback>
<p id="aed5ccfa249794825a9fd6e05233ca5c1"/>
</feedback>
</response>
</part>
</question>
</page>
</assessment>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE assessment>
<assessment>
<title>Test</title>
<short_title>Test</short_title>
<page>
<title>Page 1</title>
<question>
<multiple_choice shuffle="true" select="multiple" labels="false">
<choice value="A">Placeholder</choice>
<choice value="B">Placeholder</choice>
</multiple_choice>
<part id="be03031c79464d08a5bce122fee68ffc">
<response match="A" score="1">
<feedback>
<p id="aa3ab72153c834c8395a0099dff39d434"/>
</feedback>
</response>
</part>
</question>
</page>
</assessment>
[ { action: 'removeTextElement',
route: [ 4, 5, 3, 3, 0 ],
value: '\r\n ' },
{ action: 'removeElement',
route: [ 4, 5, 3, 3, 0 ],
element:
{ nodeName: 'response',
attributes: [Object],
childNodes: [Array] } },
{ action: 'removeTextElement',
route: [ 4, 5, 3, 3, 0 ],
value: '\r\n ' },
{ action: 'removeElement',
route: [ 4, 5, 3, 3, 0 ],
element:
{ nodeName: 'response',
attributes: [Object],
childNodes: [Array] } },
{ action: 'modifyAttribute',
route: [ 4, 5, 3, 3, 1 ],
name: 'match',
oldValue: 'B',
newValue: 'A' },
{ action: 'removeAttribute',
route: [ 4, 5, 3, 3, 1 ],
name: 'name',
value: 'AUTOGEN_{B}' },
{ action: 'modifyAttribute',
route: [ 4, 5, 3, 3, 1 ],
name: 'score',
oldValue: '0',
newValue: '1' },
{ action: 'removeElement',
route: [ 4, 5, 3, 3, 1, 1, 1 ],
element: { nodeName: 'p', attributes: [Object] } },
{ action: 'addElement',
route: [ 4, 5, 3, 3, 1, 1, 1 ],
element: { nodeName: 'p', attributes: [Object] } } ]
diffs.push(new Diff()
.setValue(t._const.action, t._const.removeTextElement)
.setValue(t._const.route, route.concat(index2))
.setValue(t._const.value, node.data)
);