Skip to content

Commit

Permalink
feat: support attributes parsing
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <[email protected]>
  • Loading branch information
skjnldsv committed Dec 9, 2024
1 parent 817d85e commit 1ad6d32
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
11 changes: 11 additions & 0 deletions source/tools/dav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,22 @@ enum PropertyType {

function getParser(): XMLParser {
return new XMLParser({
allowBooleanAttributes: true,
attributeNamePrefix: "",
textNodeName: "text",
ignoreAttributes: false,
removeNSPrefix: true,
numberParseOptions: {
hex: true,
leadingZeros: false
},
attributeValueProcessor(attrName, attrValue, jPath) {
// handle boolean attributes
if (attrValue === "true" || attrValue === "false") {
return attrValue === "true";
}
return attrValue;
},
tagValueProcessor(tagName, tagValue, jPath) {
if (jPath.endsWith("propstat.prop.displayname")) {
// Do not parse the display name, because this causes e.g. '2024.10' to result in number 2024.1
Expand Down
32 changes: 32 additions & 0 deletions test/node/operations/stat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,38 @@ describe("stat", function () {
});
});

it("correctly parses the attributes property", async function () {
returnFakeResponse(
readFileSync(
new URL("../../responses/propfind-attributes.xml", import.meta.url)
).toString()
);

await this.client.stat("/foo/", { details: true }).then(function (result) {
expect(result.data).to.have.property("props").that.is.an("object");
expect(result.data.props).to.have.property("system-tags").that.is.an("object");
expect(result.data.props["system-tags"]["system-tag"])
.to.be.an("array")
.and.have.lengthOf(2);
// <z:system-tag z:can-assign="true" z:id="321" z:checked="">Tag1</z:system-tag>
// <z:system-tag z:can-assign="false" z:id="654">Tag2</z:system-tag>
expect(result.data.props["system-tags"]["system-tag"]).to.deep.equal([
{
"can-assign": true,
id: "321",
checked: true,
text: "Tag1"
},
{
"can-assign": false,
id: "654",
prop: "",
text: "Tag2"
}
]);
});
});

describe("with details: true", function () {
it("returns data property", function () {
return this.client.stat("/", { details: true }).then(function (result) {
Expand Down
27 changes: 27 additions & 0 deletions test/responses/propfind-attributes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<d:multistatus
xmlns:d="DAV:"
xmlns:z="http://ns.example.com/standards/z1.2.3.4">
<d:response>
<d:href>/remote.php/dav/files/admin/foo/</d:href>
<d:propstat>
<d:prop>
<d:getetag>&quot;66a15a0171527&quot;</d:getetag>
<d:getlastmodified>Wed, 24 Jul 2024 19:46:09 GMT</d:getlastmodified>
<d:creationdate>1970-01-01T00:00:00+00:00</d:creationdate>
<z:system-tags>
<z:system-tag z:can-assign="true" z:id="321" z:checked>Tag1</z:system-tag>
<z:system-tag z:can-assign="false" z:id="654" z:prop="">Tag2</z:system-tag>
</z:system-tags>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
<d:propstat>
<d:prop>
<d:getcontentlength/>
<d:getcontenttype/>
</d:prop>
<d:status>HTTP/1.1 404 Not Found</d:status>
</d:propstat>
</d:response>
</d:multistatus>

0 comments on commit 1ad6d32

Please sign in to comment.