Skip to content

Commit

Permalink
Do not fetch disabled links; copy over all attributes to new link
Browse files Browse the repository at this point in the history
  • Loading branch information
jgerigmeyer committed Oct 14, 2024
1 parent 8bacf91 commit e6c1dd4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ async function fetchLinkedStylesheets(
if (!data.url) {
return data as StyleData;
}
// TODO: Add MutationObserver to watch for disabled links being enabled

Check warning on line 28 in src/fetch.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: Add MutationObserver to watch for...'

Check warning on line 28 in src/fetch.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: Add MutationObserver to watch for...'
// https://github.com/oddbird/css-anchor-positioning/issues/246
if ((data.el as HTMLLinkElement | undefined)?.disabled) {
// Do not fetch or parse disabled stylesheets
return null;
}
// fetch css and add to array
try {
const response = await fetch(data.url.toString());
Expand Down
12 changes: 7 additions & 5 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ export async function transformCSS(
const blob = new Blob([css], { type: 'text/css' });
const url = URL.createObjectURL(blob);
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = url;
link.id = el.id;
link.media = el.media;
link.title = el.title;
for (const name of el.getAttributeNames()) {
const attr = el.getAttribute(name);
if (attr !== null && name !== 'href') {
link.setAttribute(name, attr);
}
}
link.setAttribute('href', url);
const promise = new Promise((res) => {
link.onload = res;
});
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('fetch stylesheet', () => {
beforeAll(() => {
// Set up our document head
document.head.innerHTML = `
<link type="text/css" href="/sample.css"/>
<link type="text/css" href="/sample.css" />
<link rel="stylesheet" />
<link />
<style>
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('transformCSS', () => {

it('parses and removes new anchor positioning CSS after transformation to JS', async () => {
document.head.innerHTML = `
<link type="text/css" href="/sample.css"/>
<link type="text/css" href="/sample.css" data-link="true" />
<style>
p { color: red; }
</style>
Expand Down Expand Up @@ -42,6 +42,7 @@ describe('transformCSS', () => {
await promise;

expect(link.href).toContain('/updated.css');
expect(link.getAttribute('data-link')).toBe('true');
expect(style.innerHTML).toBe('html { padding: 0; }');
expect(div.getAttribute('style')).toBe('--foo: var(--bar); color:blue;');
expect(div2.getAttribute('style')).toBe('color: red;');
Expand Down

0 comments on commit e6c1dd4

Please sign in to comment.