Files
it-tools/src/tools/xml-diff/diff-viewer/xml-diff-viewer.vue
T
e8f0677f94 feat(xml-diff): add XML diff tool
Compare two pasted XML documents and highlight the differences, mirroring
the JSON diff tool's UI and diff engine. XML is parsed with a native
DOMParser-based normalizer (attributes as @name, text-only leaves as plain
strings) instead of xml-js, and a reconciliation step coerces mismatched
single/repeated element shapes into arrays before diffing so a repeated
element compares cleanly instead of producing a garbled type-mismatch diff.

From PR #1840
Fix #1839

Co-Authored-By: @sharevb
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-22 21:06:08 +01:00

115 lines
2.9 KiB
Vue

<script lang="ts" setup>
import * as _ from 'es-toolkit/compat';
import { diff } from '../../json-diff/json-diff.models';
import { DiffRootViewer } from '../../json-diff/diff-viewer/diff-viewer.models';
import { reconcileArrayShapes } from '../xml-diff.reconcile';
import { useAppTheme } from '@/ui/theme/themes';
const props = defineProps<{ leftXml: unknown; rightXml: unknown }>();
const onlyShowDifferences = ref(false);
const { leftXml, rightXml } = toRefs(props);
const appTheme = useAppTheme();
const result = computed(() => {
const [reconciledLeftXml, reconciledRightXml] = reconcileArrayShapes(leftXml.value, rightXml.value);
return diff(reconciledLeftXml, reconciledRightXml, { onlyShowDifferences: onlyShowDifferences.value });
});
const xmlAreTheSame = computed(() => _.isEqual(leftXml.value, rightXml.value));
const showResults = computed(() => !_.isUndefined(leftXml.value) && !_.isUndefined(rightXml.value));
</script>
<template>
<div v-if="showResults">
<div flex justify-center>
<n-form-item :label="$t('tools.xml-diff-viewer.texts.only-show-differences')" label-placement="left">
<n-switch v-model:value="onlyShowDifferences" />
</n-form-item>
</div>
<c-card data-test-id="diff-result">
<div v-if="xmlAreTheSame" text-center op-70>
{{ $t('tools.xml-diff-viewer.texts.the-provided-xmls-are-the-same') }}
</div>
<DiffRootViewer v-else :diff="result" />
</c-card>
</div>
</template>
<style lang="less" scoped>
::v-deep(.diffs-viewer) {
color: v-bind('appTheme.text.mutedColor');
& > ul {
padding-left: 0 !important;
}
ul {
list-style: none;
padding-left: 20px;
margin: 0;
li {
.updated-line {
padding: 3px 0;
}
.result,
.array,
.object,
.value {
&:not(:last-child) {
margin-right: 4px;
}
&.added {
padding: 3px 5px;
border-radius: 4px;
background-color: v-bind('appTheme.success.colorFaded');
color: v-bind('appTheme.success.color');
.key {
color: inherit;
}
}
&.removed {
padding: 3px 5px;
border-radius: 4px;
background-color: v-bind('appTheme.error.colorFaded');
color: v-bind('appTheme.error.color');
.key {
color: inherit;
}
}
}
.value {
cursor: pointer;
border: 1px solid transparent;
transition: border-color 0.2s ease-in-out;
&.added:hover {
border-color: v-bind('appTheme.success.color');
}
&.removed:hover {
border-color: v-bind('appTheme.error.color');
}
}
.added .added,
.removed .removed {
background-color: transparent;
color: inherit;
}
.key {
font-weight: 500;
color: v-bind('appTheme.text.baseColor');
}
}
}
}
</style>