这是indexloc提供的服务,不要输入任何密码
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
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 @@
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
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
Loading