fix(external-script): preserve HTML attributes (#2819)

This commit is contained in:
hhang
2026-10-21 14:33:24 +08:00
committed by GitHub
parent b6ba9b6702
commit 3d82b261fa
2 changed files with 42 additions and 1 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ function handleExternalScript() {
const newScript = document.createElement('script');
Array.from(script.attributes).forEach(attribute => {
newScript[attribute.name] = attribute.value;
newScript.setAttribute(attribute.name, attribute.value);
});
script.before(newScript);
+41
View File
@@ -3,6 +3,47 @@ import { waitForFunction } from '../helpers/wait-for.js';
import { test, expect } from './fixtures/docsify-init-fixture.js';
test.describe('Plugins', () => {
test('external scripts retain their HTML attributes when executed', async ({
page,
}) => {
const pageErrors = [];
page.on('pageerror', error => pageErrors.push(error.message));
await docsifyInit({
markdown: {
homepage: `
# External script
<script src="/widget.js" data-message="hello" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
`,
},
routes: {
'/widget.js': `
const script = document.currentScript;
window.widgetAttributes = {
message: script.dataset.message,
crossOrigin: script.crossOrigin,
referrerPolicy: script.referrerPolicy,
};
`,
},
scriptURLs: ['/dist/plugins/external-script.js'],
});
await expect
.poll(async () => ({
attributes: await page.evaluate(() => window.widgetAttributes),
errors: pageErrors,
}))
.toEqual({
attributes: {
message: 'hello',
crossOrigin: 'anonymous',
referrerPolicy: 'no-referrer',
},
errors: [],
});
});
test('Hook order', async ({ page }) => {
const consoleMsgs = [];
const expectedMsgs = [