Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom scripts #903

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import ColorScheme from 'docc-render/constants/ColorScheme';
import Footer from 'docc-render/components/Footer.vue';
import InitialLoadingPlaceholder from 'docc-render/components/InitialLoadingPlaceholder.vue';
import { baseNavStickyAnchorId } from 'docc-render/constants/nav';
import { runCustomPageLoadScripts, runCustomNavigateScripts } from 'docc-render/utils/custom-scripts';
import { fetchThemeSettings, themeSettingsState, getSetting } from 'docc-render/utils/theme-settings';
import { objectToCustomProperties } from 'docc-render/utils/themes';
import { AppTopID } from 'docc-render/constants/AppTopID';
Expand All @@ -70,6 +71,7 @@ export default {
return {
AppTopID,
appState: AppStore.state,
initialRoutingEventHasOccurred: false,
fromKeyboard: false,
isTargetIDE: process.env.VUE_APP_TARGET === 'ide',
themeSettings: themeSettingsState,
Expand Down Expand Up @@ -107,6 +109,30 @@ export default {
},
},
watch: {
async $route() {
// A routing event has just occurred, which is either the initial page load or a subsequent
// navigation. So load any custom scripts that should be run, based on their `run` property,
// after this routing event.
//
// This hook, and (as a result) any appropriate custom scripts for the current routing event,
// are called *after* the HTML for the current route has been dynamically added to the DOM.
// This means that custom scripts have access to the documentation HTML for the current
// topic (or tutorial, etc).

if (this.initialRoutingEventHasOccurred) {
// The initial page load counts as a routing event, so we only want to run "on-navigate"
// scripts from the second routing event onward.
await runCustomNavigateScripts();
} else {
// The "on-load" scripts are run here (on the routing hook), not on `created` or `mounted`,
// so that the scripts have access to the dynamically-added documentation HTML for the
// current topic.
await runCustomPageLoadScripts();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm doing a quick test with a very simple script that looks like this:

[
  {
    "code": "window.alert(\"hello world\");",
    "run": "on-load-and-navigate"
  }
]

My expectation is that the popup happens once on the initial page load (1) and also after the page loads for every link I click (2).

What actually happens for scenario 2 is that I get the popup immediately after clicking a link when the URL has changed to the new page, but the HTML is still showing the previous page I was navigating away from.

Question: Is my expectation wrong or is this just possibly an issue with the implementation?

If it's an issue with the implementation, we would probably want to look into using the vue-router hooks and guards for calling these functions instead of the $route watcher that you have in the root component here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The visual issue you’re describing in scenario 2 is a quirk of alerts: despite what the browser may be displaying, the alert in scenario 2 is fired strictly after the link is clicked and the subpage is loaded. To prove this, try the following script instead:

window.alert(document.querySelector('h1').innerText);

and notice that the alert text matches the H1 of the subpage to which you’re navigating, not of the subpage you’re in, regardless of what the browser is displaying. This means that, in scenario 2, the script is running strictly after navigation.

As an additional test, also try:

document.querySelector('h1').style.fontStyle = 'italic';

and notice, in scenario 2, that the H1 (dynamically inserted into the subpage to which you're navigating) is successfully styled, which means that the script did run strictly after navigation.

As for using vue-router hooks instead of the $route watcher, I agree that that would be the more correct approach. I’ll work on it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The visual issue you’re describing in scenario 2 is a quirk of alerts: despite what the browser may be displaying, the alert in scenario 2 is fired strictly after the link is clicked and the subpage is loaded. To prove this, try the following script instead:

window.alert(document.querySelector('h1').innerText);

and notice that the alert text matches the H1 of the subpage to which you’re navigating, not of the subpage you’re in, regardless of what the browser is displaying. This means that, in scenario 2, the script is running strictly after navigation.

As an additional test, also try:

document.querySelector('h1').style.fontStyle = 'italic';

and notice, in scenario 2, that the H1 (dynamically inserted into the subpage to which you're navigating) is successfully styled, which means that the script did run strictly after navigation.

Whoops. Got it, that makes sense.

As for using vue-router hooks instead of the $route watcher, I agree that that would be the more correct approach. I’ll work on it.

Sounds good. I think this approach is fine after realizing my problem was with my example, although it still might be good to use those hooks if possible to be safe from any potential issues related to the navigation cycle.


// The next time we enter the routing hook, run the navigation scripts.
this.initialRoutingEventHasOccurred = true;
}
},
CSSCustomProperties: {
immediate: true,
handler(CSSCustomProperties) {
Expand Down
192 changes: 192 additions & 0 deletions src/utils/custom-scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2021 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import fetchText from 'docc-render/utils/fetch-text';
import {
copyPresentProperties,
copyPropertyIfPresent,
has,
mustNotHave,
} from 'docc-render/utils/object-properties';
import { resolveAbsoluteUrl } from 'docc-render/utils/url-helper';

/**
* Returns whether the custom script should be run when the reader navigates to a subpage.
* @param {object} customScript
* @returns {boolean} Returns whether the custom script has a `run` property with a value of
* "on-load" or "on-load-and-navigate". Also returns true if the `run` property is absent.
*/
function shouldRunOnPageLoad(customScript) {
return !has(customScript, 'run')
|| customScript.run === 'on-load' || customScript.run === 'on-load-and-navigate';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor thing, but it would be great to have some constants or an enum-like Object for some of these strings so that we don't have to rely on them always being typed exactly the same in all the places in the code. (Wish we were using typescript so this kind of thing was a little more straightforward)

Something like:

const Run = {
  onLoad: 'on-load',
  onLoadAndNavigate: 'on-load-and-navigate',
};

or

const RunOnLoad = 'on-load';
const RunOnLoadAndNavigate = 'on-load-and-navigate';

etc

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!

}

/**
* Returns whether the custom script should be run when the reader navigates to a topic.
* @param {object} customScript
* @returns {boolean} Returns whether the custom script has a `run` property with a value of
* "on-navigate" or "on-load-and-navigate".
*/
function shouldRunOnNavigate(customScript) {
return has(customScript, 'run')
&& (customScript.run === 'on-navigate' || customScript.run === 'on-load-and-navigate');
}

/**
* Gets the URL for a local custom script given its name.
* @param {string} customScriptName The name of the custom script as spelled in
* custom-scripts.json. While the actual filename (in the custom-scripts directory) is always
* expected to end in ".js", the name in custom-scripts.json may or may not include the ".js"
* extension.
* @returns {string} The absolute URL where the script is, accounting for baseURL.
* @example
* // if baseURL if '/foo'
* urlGivenScriptName('hello-world') // http://localhost:8080/foo/hello-world.js
* urlGivenScriptName('hello-world.js') // http://localhost:8080/foo/hello-world.js
*/
function urlGivenScriptName(customScriptName) {
let scriptNameWithExtension = customScriptName;

// If the provided name does not already include the ".js" extension, add it.
if (customScriptName.slice(-3) !== '.js') {
scriptNameWithExtension = `${customScriptName}.js`;
}

return resolveAbsoluteUrl(['', 'custom-scripts', scriptNameWithExtension]);
}

/**
* Add an HTMLScriptElement containing the custom script to the document's head, which runs the
* script on page load.
* @param {object} customScript The custom script, assuming it should be run on page load.
*/
function addScriptElement(customScript) {
const scriptElement = document.createElement('script');

copyPropertyIfPresent('type', customScript, scriptElement);

if (has(customScript, 'url')) {
mustNotHave(customScript, 'name', 'Custom script cannot have both `url` and `name`.');
mustNotHave(customScript, 'code', 'Custom script cannot have both `url` and `code`.');

scriptElement.src = customScript.url;

copyPresentProperties(['async', 'defer', 'integrity'], customScript, scriptElement);

// If `integrity` is set on an external script, then CORS must be enabled as well.
if (has(customScript, 'integrity')) {
scriptElement.crossOrigin = 'anonymous';
}
} else if (has(customScript, 'name')) {
mustNotHave(customScript, 'code', 'Custom script cannot have both `name` and `code`.');

scriptElement.src = urlGivenScriptName(customScript.name);

copyPresentProperties(['async', 'defer', 'integrity'], customScript, scriptElement);
} else if (has(customScript, 'code')) {
mustNotHave(customScript, 'async', 'Inline script cannot be `async`.');
mustNotHave(customScript, 'defer', 'Inline script cannot have `defer`.');
mustNotHave(customScript, 'integrity', 'Inline script cannot have `integrity`.');

scriptElement.innerHTML = customScript.code;
} else {
throw new Error('Custom script does not have `url`, `name`, or `code` properties.');
}

document.head.appendChild(scriptElement);
}

/**
* Run the custom script using `eval`. Useful for running a custom script anytime after page load,
* namely when the reader navigates to a subpage.
* @param {object} customScript The custom script, assuming it should be run on navigate.
*/
async function evalScript(customScript) {
let codeToEval;

if (has(customScript, 'url')) {
mustNotHave(customScript, 'name', 'Custom script cannot have both `url` and `name`.');
mustNotHave(customScript, 'code', 'Custom script cannot have both `url` and `code`.');

if (has(customScript, 'integrity')) {
// External script with integrity. Must also use CORS.
codeToEval = await fetchText(customScript.url, {
integrity: customScript.integrity,
crossOrigin: 'anonymous',
});
} else {
// External script without integrity.
codeToEval = await fetchText(customScript.url);
}
} else if (has(customScript, 'name')) {
mustNotHave(customScript, 'code', 'Custom script cannot have both `name` and `code`.');

const url = urlGivenScriptName(customScript.name);

if (has(customScript, 'integrity')) {
// Local script with integrity. Do not use CORS.
codeToEval = await fetchText(url, { integrity: customScript.integrity });
} else {
// Local script without integrity.
codeToEval = await fetchText(url);
}
} else if (has(customScript, 'code')) {
mustNotHave(customScript, 'async', 'Inline script cannot be `async`.');
mustNotHave(customScript, 'defer', 'Inline script cannot have `defer`.');
mustNotHave(customScript, 'integrity', 'Inline script cannot have `integrity`.');

codeToEval = customScript.code;
} else {
throw new Error('Custom script does not have `url`, `name`, or `code` properties.');
}

// eslint-disable-next-line no-eval
eval(codeToEval);
}

/**
* Run all custom scripts that pass the `predicate` using the `executor`.
* @param {(customScript: object) => boolean} predicate
* @param {(customScript: object) => void} executor
* @returns {Promise<void>}
*/
async function runCustomScripts(predicate, executor) {
const customScriptsFileName = 'custom-scripts.json';
const url = resolveAbsoluteUrl(`/${customScriptsFileName}`);

const response = await fetch(url);
if (!response.ok) {
// If the file is absent, fail silently.
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One existing problem we have with the same pattern from theme-settings.json is that we always try to fetch the file, and the 404 is always reported in the console in the case where one wasn't provided.

I don't think this needs to be solved with this PR, but it would be nice to fix this for both files so that the renderer is informed in some way of the fact that the catalog even has one of these files before it tries to fetch them, so this "always 404" problem goes away in the normal case.

(This would need to be addressed in a coordinated way between DocC and DocC-Render in the future)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's an unfortunate behavior of fetch... something we can look into in a later PR.

Sidenote: I should probably point out that the way I’m error-handling custom-scripts.json intentionally does not match how theme-settings.json is error-handled. fetchThemeSettings has a blanket catch statement, which means that documentation authors aren’t notified if the file is malformed or if it violates the theme-settings schema. Messing with this choice for theme-settings.json is outside the scope of the proposal, so I didn’t; but for custom-scripts.json, if the file is malformed or violates the custom-scripts schema then we throw an error and don’t catch it. Please let me know if schema violations should be caught for custom-scripts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good.

I don't think it's necessarily a problem with the fetch behavior—it's just that we shouldn't even be trying to fetch any of these JSON files if they haven't been provided in the documentation catalog in the first place—we would just need some added infrastructure so that DocC could inform the renderer that it will never even need to make an http request for these files if they weren't used.

Again, not any issue with your implementation—just noting an existing problem that will happen here as well.

}

const customScripts = await response.json();
if (!Array.isArray(customScripts)) {
throw new Error(`Content of ${customScriptsFileName} should be an array.`);
}

customScripts.filter(predicate).forEach(executor);
}

/**
* Runs all "on-load" and "on-load-and-navigate" scripts.
* @returns {Promise<void>}
*/
export async function runCustomPageLoadScripts() {
await runCustomScripts(shouldRunOnPageLoad, addScriptElement);
}

/**
* Runs all "on-navigate" and "on-load-and-navigate" scripts.
* @returns {Promise<void>}
*/
export async function runCustomNavigateScripts() {
await runCustomScripts(shouldRunOnNavigate, evalScript);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the reason why you're using eval instead of import just for the inline code use case?

Unless there's a strong reason for it, I would suggest maybe eliminating the inline code option and using import instead, especially since all inline code could easily be extracted into a simple local script.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m using eval instead of import just to support inline scripts; though, for sandboxing and performance, I should’ve used new Function instead. Please let me know whether I should:

  • Replace eval with new Function and keep inline scripts.
  • Replace eval with dynamic imports and remove inline scripts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would personally prefer the latter since all inline scripts could be expressed as simple local scripts, and I would much prefer using dynamic imports over manually evaluating the code. I could be convinced otherwise if there's a strong need to explicitly support inline code, but I think the dynamic import approach would be simpler code-wise and also less prone to possible issues.

}
23 changes: 23 additions & 0 deletions src/utils/fetch-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2021 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import { resolveAbsoluteUrl } from 'docc-render/utils/url-helper';

/**
* Fetch the contents of a file as text.
* @param {string} filepath The file path.
* @param {RequestInit?} options Optional request settings.
* @returns {Promise<string>} The text contents of the file.
*/
export default async function fetchText(filepath, options) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this maybe make sense to live in the src/utils/data.js file where we have similar functions for fetching data instead of creating a new file just for this?

(Not a major issue, and there's a possibility this function might not even be needed based on my other comment about avoiding eval if possible.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I’ll move fetchText to src/utils/data.js — unless we switch to dynamic imports, in which case I’ll remove it.

const url = resolveAbsoluteUrl(filepath);
return fetch(url, options)
.then(r => r.text());
}
50 changes: 50 additions & 0 deletions src/utils/object-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2021 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

/* eslint-disable */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why the linter needed to be disabled for this entire file or was it just for the one line where you disabled it inline? Just curious.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s a mistake! A relic from when I littered the file with console.log statements for debugging. Thanks for catching it. 😮‍💨


/** Convenient shorthand for `Object.hasOwn`. */
export const has = Object.hasOwn;
/**
* Copies source.property, if it exists, to destination.property.
* @param {string} property
* @param {object} source
* @param {object} destination
*/
export function copyPropertyIfPresent(property, source, destination) {
if (has(source, property)) {
// eslint-disable-next-line no-param-reassign
destination[property] = source[property];
}
}

/**
* Copies all specified properties present in the source to the destination.
* @param {string[]} properties
* @param {object} source
* @param {object} destination
*/
export function copyPresentProperties(properties, source, destination) {
properties.forEach((property) => {
copyPropertyIfPresent(property, source, destination);
});
}

/**
* Throws an error if `object` has the property `property`.
* @param {object} object
* @param {string} property
* @param {string} errorMessage
*/
export function mustNotHave(object, property, errorMessage) {
if (has(object, property)) {
throw new Error(errorMessage);
}
}
2 changes: 1 addition & 1 deletion src/utils/theme-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const themeSettingsState = {
export const { baseUrl } = window;

/**
* Method to fetch the theme settings and store in local module state.
* Fetches the theme settings and store in local module state.
* Method is called before Vue boots in `main.js`.
* @return {Promise<{}>}
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/App.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ jest.mock('docc-render/utils/theme-settings', () => ({
getSetting: jest.fn(() => {}),
}));

jest.mock('docc-render/utils/custom-scripts', () => ({
runCustomPageLoadScripts: jest.fn(),
}));

let App;

let fetchThemeSettings = jest.fn();
let getSetting = jest.fn(() => {});

let runCustomPageLoadScripts = jest.fn();

const matchMedia = {
matches: false,
addListener: jest.fn(),
Expand Down Expand Up @@ -92,6 +99,7 @@ describe('App', () => {
/* eslint-disable global-require */
App = require('docc-render/App.vue').default;
({ fetchThemeSettings } = require('docc-render/utils/theme-settings'));
({ runCustomPageLoadScripts } = require('docc-render/utils/custom-scripts'));

setThemeSetting({});
window.matchMedia = jest.fn().mockReturnValue(matchMedia);
Expand Down Expand Up @@ -244,6 +252,12 @@ describe('App', () => {
expect(wrapper.find(`#${AppTopID}`).exists()).toBe(true);
});

it('does not load "on-load" scripts immediately', () => {
// If "on-load" scripts are run immediately after creating or mounting the app, they will not
// have access to the dynamic documentation HTML for the initial route.
expect(runCustomPageLoadScripts).toHaveBeenCalledTimes(0);
});

describe('Custom CSS Properties', () => {
beforeEach(() => {
setThemeSetting(LightDarkModeCSSSettings);
Expand Down
Loading