Skip to content

Commit 1dfe831

Browse files
committed
Merge branch 'feat/xpath-tester' into chore/all-my-stuffs
# Conflicts: # components.d.ts # package.json # pnpm-lock.yaml # src/tools/index.ts
2 parents 17a49c1 + 2ca3a1f commit 1dfe831

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"@vueuse/router": "^10.0.0",
103103
"@zxing/library": "^0.21.0",
104104
"arr-diff": "^4.0.0",
105+
"@xmldom/xmldom": "^0.8.10",
105106
"bcryptjs": "^2.4.3",
106107
"big.js": "^6.2.2",
107108
"change-case": "^4.1.2",
@@ -261,6 +262,7 @@
261262
"xml-formatter": "^3.3.2",
262263
"xml-js": "^1.6.11",
263264
"xpath-to-css": "^1.2.0",
265+
"xpath": "^0.0.34",
264266
"yaml": "^2.2.1"
265267
},
266268
"devDependencies": {

pnpm-lock.yaml

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { tool as vatValidator } from './vat-validator';
5353
import { tool as websocketTester } from './websocket-tester';
5454
import { tool as weekNumberConverter } from './week-number-converter';
5555
import { tool as wpaPskGenerator } from './wpa-psk-generator';
56+
import { tool as xpathTester } from './xpath-tester';
5657

5758
import { tool as cssXpathConverter } from './css-xpath-converter';
5859
import { tool as cssSelectorsMemo } from './css-selectors-memo';
@@ -358,6 +359,7 @@ export const toolsByCategory: ToolCategory[] = [
358359
regexTester,
359360
regexMemo,
360361
stacktracePrettier,
362+
xpathTester,
361363
],
362364
},
363365
{

src/tools/xpath-tester/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Brackets } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'XPath Tester',
6+
path: '/xpath-tester',
7+
description: 'Test XPath expression against XML content',
8+
keywords: ['xpath', 'xml', 'tester'],
9+
component: () => import('./xpath-tester.vue'),
10+
icon: Brackets,
11+
createdAt: new Date('2024-08-15'),
12+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<script setup lang="ts">
2+
import XPathEngine from 'xpath';
3+
import { DOMParser } from '@xmldom/xmldom';
4+
import { useValidation } from '@/composable/validation';
5+
6+
const xpath = ref('//title');
7+
const xml = ref('<book><title>Harry Potter</title></book>');
8+
9+
const selectedNodes = computed(() => {
10+
try {
11+
const doc = new DOMParser().parseFromString(xml.value, 'text/xml');
12+
const result = XPathEngine.select(xpath.value, doc);
13+
return Array.isArray(result) ? result : [result];
14+
}
15+
catch (e: any) {
16+
return [e.toString()];
17+
}
18+
});
19+
20+
const xmlValidation = useValidation({
21+
source: xml,
22+
rules: [
23+
{
24+
validator: (v) => {
25+
new DOMParser().parseFromString(v, 'text/xml');
26+
return true;
27+
},
28+
message: 'Provided XML is not valid.',
29+
},
30+
],
31+
});
32+
</script>
33+
34+
<template>
35+
<div style="max-width: 600px;">
36+
<c-card title="Input" mb-2>
37+
<c-input-text
38+
v-model:value="xpath"
39+
label="XPath Expression"
40+
placeholder="Put your XPath expression here..."
41+
mb-2
42+
/>
43+
44+
<c-input-text
45+
v-model:value="xml"
46+
label="XML"
47+
multiline
48+
placeholder="Put your XML here..."
49+
rows="5"
50+
:validation="xmlValidation"
51+
mb-2
52+
/>
53+
</c-card>
54+
55+
<c-card title="Result(s)">
56+
<ul v-if="selectedNodes?.length > 0">
57+
<li v-for="(node, index) in selectedNodes" :key="index">
58+
{{ node }}
59+
</li>
60+
</ul>
61+
<c-alert v-if="!selectedNodes?.length">
62+
XPath expression selected nothing
63+
</c-alert>
64+
</c-card>
65+
</div>
66+
</template>

0 commit comments

Comments
 (0)