Data fixes, fix for <img> with 2+ src attributes

This commit is contained in:
Will Boyd
2025-02-23 13:22:32 -05:00
parent f0e8400ccd
commit db5430117e
4 changed files with 69 additions and 23 deletions
+41 -11
View File
@@ -5,20 +5,15 @@ class Data {
#expression;
constructor(obj, expression) {
// xml2js returns leaf nodes as strings, turn those into consistent objects
// I found this to be safer and more efficient than using the explicitCharkey option
this.#obj = typeof obj === 'string' ? { _: obj } : obj;
// this identifies how the object was referenced, helps a ton with debugging
this.#expression = expression;
}
get value() {
const value = this.#obj._;
if (value === undefined) {
throw new Error(`Could not get value from ${this.#expression}.`);
}
return value;
}
#buildExpression(propName, index) {
#buildExpression(propName, index = undefined) {
let expression = `${this.#expression}.${propName}`;
if (index !== undefined) {
expression += `[${index}]`;
@@ -27,11 +22,22 @@ class Data {
return expression;
}
// used by "optional" functions to return undefined instead of throwing an error
#optional(func) {
try {
return func();
} catch (ex) {
return undefined;
}
}
// will not throw an error if property doesn't exist, defaults to empty array
children(propName) {
const nodes = this.#obj[propName] ?? [];
return nodes.map((value, index) => new Data(value, this.#buildExpression(propName, index)));
}
// throws an error if property (or index on property) doesn't exist
child(propName, index = 0) {
const nodes = this.#obj[propName];
if (nodes === undefined) {
@@ -46,10 +52,22 @@ class Data {
return new Data(node, this.#buildExpression(propName, index));
}
// convenience function, since it's very common to want the value of a child
childValue(propName, index = 0) {
return this.child(propName, index).value;
return this.child(propName, index).value();
}
// throws an error if this object doesn't have a value string
value() {
const value = this.#obj._;
if (value === undefined) {
throw new Error(`Could not get value from ${this.#expression}.`);
}
return value;
}
// throws an error if attribute does not exist
attribute(attrName) {
const attribute = this.#obj.$?.[attrName];
if (attribute === undefined) {
@@ -58,6 +76,18 @@ class Data {
return attribute;
}
optionalChild(propName, index = 0) {
return this.#optional(() => this.child(propName, index));
}
optionalChildValue(propName, index = 0) {
return this.#optional(() => this.childValue(propName, index));
}
optionalValue() {
return this.#optional(() => this.value());
}
}
export async function load(content) {