Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: type error by xmldom #437

Merged
merged 2 commits into from
Sep 16, 2024
Merged
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
18 changes: 11 additions & 7 deletions lib/defaults-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import log from './logger';
* @param {any} value The value to be serialized
* @param {boolean} serialize [true] Whether to serialize the resulting
* XML to string or to return raw HTMLElement instance
* @returns {Node|string} Either string or raw node representation of
* @returns {xmlDomElement|string} Either string or raw node representation of
* the given value
* @throws {TypeError} If it is not known how to serialize the given value
*/
Expand All @@ -25,17 +25,17 @@ export function toXmlArg (value, serialize = true) {
const keyEl = xmlDoc.createElement('key');
const keyTextEl = xmlDoc.createTextNode(subKey);
keyEl.appendChild(keyTextEl);
xmlDoc.documentElement.appendChild(keyEl);
/** @type{xmlDomElement} */ (xmlDoc.documentElement).appendChild(keyEl);
// @ts-ignore The typecast here is fine
const subValueEl = xmlDoc.importNode(toXmlArg(subValue, false), true);
xmlDoc.documentElement.appendChild(subValueEl);
/** @type{xmlDomElement} */ (xmlDoc.documentElement).appendChild(subValueEl);
}
} else if (_.isArray(value)) {
xmlDoc = new DOMParser().parseFromString('<array></array>', 'text/xml');
for (const subValue of value) {
// @ts-ignore The typecast here is fine
const subValueEl = xmlDoc.importNode(toXmlArg(subValue, false), true);
xmlDoc.documentElement.appendChild(subValueEl);
/** @type{xmlDomElement} */ (xmlDoc.documentElement).appendChild(subValueEl);
}
} else if (_.isBoolean(value)) {
xmlDoc = new DOMParser().parseFromString(value ? '<true/>' : '<false/>', 'text/xml');
Expand All @@ -46,7 +46,7 @@ export function toXmlArg (value, serialize = true) {
} else if (_.isString(value)) {
xmlDoc = new DOMParser().parseFromString(`<string></string>`, 'text/xml');
const valueTextEl = xmlDoc.createTextNode(value);
xmlDoc.documentElement.appendChild(valueTextEl);
/** @type{xmlDomElement} */ (xmlDoc.documentElement).appendChild(valueTextEl);
}

if (!xmlDoc) {
Expand All @@ -55,8 +55,8 @@ export function toXmlArg (value, serialize = true) {
}

return serialize
? new XMLSerializer().serializeToString(xmlDoc.documentElement)
: xmlDoc.documentElement;
? new XMLSerializer().serializeToString(/** @type{xmlDomElement} */ (xmlDoc.documentElement))
: /** @type{xmlDomElement} */ (xmlDoc.documentElement);
}

/**
Expand Down Expand Up @@ -154,3 +154,7 @@ export class NSUserDefaults {
}
}
}

/**
* @typedef {import('@xmldom/xmldom').Element} xmlDomElement
*/
Loading