mirror of
https://github.com/docsifyjs/docsify.git
synced 2026-10-29 03:47:04 +08:00
fix(external-script): preserve HTML attributes (#2819)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
Reference in New Issue
Block a user