Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/four-turtles-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@finsweet/attributes': patch
---

fix: re-check for scripts after DOM loaded to find unprocessed entities
43 changes: 24 additions & 19 deletions packages/attributes/src/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ const init = () => {
* Inits all Attributes that are defined in the current script
* or in the DOM if fs-attributes-auto is enabled.
*/
const initAttributes = () => {
const initAttributes = async () => {
let autoLoad = false;

const scripts = document.querySelectorAll<HTMLScriptElement>(`script[type="module"][src="${import.meta.url}"]`);
const getScripts = () =>
document.querySelectorAll<HTMLScriptElement>(`script[type="module"][src="${import.meta.url}"]`);

for (const script of scripts) {
if (window.FinsweetAttributes.scripts.includes(script)) continue;
const initScript = (script: HTMLScriptElement) => {
if (window.FinsweetAttributes.scripts.includes(script)) return;

window.FinsweetAttributes.scripts.push(script);

Expand All @@ -84,29 +85,33 @@ const initAttributes = () => {

initAttribute(key);
}
}
};

getScripts().forEach(initScript);

await waitDOMReady();

getScripts().forEach(initScript);

if (!autoLoad) return;

waitDOMReady().then(() => {
const usedAttributes = new Set<FinsweetAttributeKey>();
const allElements = document.querySelectorAll('*');
const usedAttributes = new Set<FinsweetAttributeKey>();
const allElements = document.querySelectorAll('*');

for (const element of allElements) {
for (const name of element.getAttributeNames()) {
const fsMatch = name.match(/^fs-([^-]+)/);
const key = fsMatch?.[1] as FinsweetAttributeKey | undefined;
for (const element of allElements) {
for (const name of element.getAttributeNames()) {
const fsMatch = name.match(/^fs-([^-]+)/);
const key = fsMatch?.[1] as FinsweetAttributeKey | undefined;

if (key && ATTRIBUTE_KEYS.has(key)) {
usedAttributes.add(key);
}
if (key && ATTRIBUTE_KEYS.has(key)) {
usedAttributes.add(key);
}
}
}

for (const attribute of usedAttributes) {
initAttribute(attribute);
}
});
for (const attribute of usedAttributes) {
initAttribute(attribute);
}
};

/**
Expand Down