FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(external-script): preserve HTML attributes by xu91102 · Pull Request #2819 · docsifyjs/docsify · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion src/plugins/external-script.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/plugins.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Loading

Back | FazBrowse Home | New Git URL