Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/project/util/xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ export function parseXmlString(contents: string) {
return new xmldom.DOMParser().parseFromString(contents);
}

const FRAGMENT_ROOT = 'trampoline-fragment';

/**
* Parses a fragment that may hold more than one top-level node and returns
* those nodes. A document has a single root, so parsing a multi-element
* fragment on its own keeps the first element and discards the rest; wrapping
* it first is what makes every element survive.
*/
export function parseXmlFragment(fragment: string): any[] {
// A fragment is not a document, so a declaration it was pasted in with would
// land inside the wrapper, where it is neither legal nor printable.
const body = fragment.trimStart().startsWith('<?xml')
? fragment.slice(fragment.indexOf('?>') + 2)
: fragment;

const doc = parseXmlString(`<${FRAGMENT_ROOT}>${body}</${FRAGMENT_ROOT}>`);

return Array.from(doc.documentElement?.childNodes ?? []);
}

export function serializeXml(doc: any) {
return new XMLSerializer().serializeToString(doc);
}
Expand Down
16 changes: 8 additions & 8 deletions src/project/xml.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatXml, parseXml, parseXmlString, serializeXml, writeXml } from './util/xml';
import { formatXml, parseXml, parseXmlFragment, parseXmlString, serializeXml, writeXml } from './util/xml';
import xpath, { XPathSelect } from 'xpath';
import { xml2js, js2xml } from 'xml-js';
import { VFS, VFSFile, VFSStorable } from './vfs';
Expand Down Expand Up @@ -103,12 +103,9 @@ export class XmlFile extends VFSStorable {
Logger.v('xml', 'injectFragment', `at ${target}`);

nodes.forEach(n => {
const parsed = parseXmlString(fragment);
const children = Array.prototype.slice.call(parsed.childNodes ?? []) as any[];
const firstElement = children.findIndex(d => d.nodeType === 1);
children
.filter((d, i) => firstElement < 0 || i <= firstElement || d.nodeType !== 3)
.forEach(d => n.appendChild(d));
// Parsed per match: appendChild moves a node rather than copying it, so a
// single parse would empty itself into the first match.
parseXmlFragment(fragment).forEach(d => n.appendChild(d));
});

this.vfs.set(this.path, this);
Expand Down Expand Up @@ -159,7 +156,10 @@ export class XmlFile extends VFSStorable {
// Recursively merge nodes with some heuristics based on
// likely merge expectations
_mergeJson(target: any, fragment: any) {
for (const e of fragment.elements) {
// An element written with no children has no `elements` array at all.
target.elements ??= [];

for (const e of fragment.elements ?? []) {
let child: Element | null = null;

for (const t of target.elements) {
Expand Down
Loading