Skip to content

Commit

Permalink
refactor trustedCreateElement() a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaleleka committed Feb 12, 2024
1 parent 15cee76 commit 1086e9a
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/scriptlets/trusted-create-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,6 @@ export function trustedCreateElement(
logMessage(source, `${prefix} due to ${getErrorMessage(error)}`);
};

let attributes: ParsedAttributePair[];

try {
attributes = parseAttributePairs(attributePairs);
} catch (e) {
logError(`Cannot parse attributePairs param: '${attributePairs}'`, e);
return;
}

let element: HTMLElement;
try {
element = document.createElement(tagName);
Expand All @@ -110,16 +101,23 @@ export function trustedCreateElement(
return;
}

if (attributes) {
attributes.forEach((attr) => {
try {
element.setAttribute(attr.name, attr.value);
} catch (e) {
logError(`Cannot set attribute '${attr.name}' with value '${attr.value}'`, e);
}
});
let attributes: ParsedAttributePair[] = [];

try {
attributes = parseAttributePairs(attributePairs);
} catch (e) {
logError(`Cannot parse attributePairs param: '${attributePairs}'`, e);
return;
}

attributes.forEach((attr) => {
try {
element.setAttribute(attr.name, attr.value);
} catch (e) {
logError(`Cannot set attribute '${attr.name}' with value '${attr.value}'`, e);
}
});

let timerId: ReturnType<typeof setTimeout>;

/**
Expand Down

0 comments on commit 1086e9a

Please sign in to comment.