Skip to content

Commit

Permalink
Merge pull request #262 from oddbird/link-attrs
Browse files Browse the repository at this point in the history
Do not fetch disabled links, but copy over all attributes to new link.
  • Loading branch information
jgerigmeyer authored Oct 15, 2024
2 parents e9f6ce6 + 2895255 commit 49081d8
Show file tree
Hide file tree
Showing 4 changed files with 26 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...'
// 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
21 changes: 16 additions & 5 deletions src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { type StyleData } from './utils.js';

const excludeAttributes = [
'crossorigin',
'href',
'integrity',
'referrerpolicy',
];

export async function transformCSS(
styleData: StyleData[],
inlineStyles?: Map<HTMLElement, Record<string, string>>,
Expand All @@ -17,11 +24,15 @@ 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()) {
if (!name.startsWith('on') && !excludeAttributes.includes(name)) {
const attr = el.getAttribute(name);
if (attr !== null) {
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
4 changes: 3 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" crossorigin="anonymous" />
<style>
p { color: red; }
</style>
Expand Down Expand Up @@ -42,6 +42,8 @@ describe('transformCSS', () => {
await promise;

expect(link.href).toContain('/updated.css');
expect(link.getAttribute('data-link')).toBe('true');
expect(link.getAttribute('crossorigin')).toBeNull();
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 49081d8

Please sign in to comment.