Skip to content

Commit

Permalink
Markup: also insert snapshot warning in multipage (tc39#3348)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot authored and ljharb committed Jun 26, 2024
1 parent 773782e commit b509787
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
35 changes: 22 additions & 13 deletions scripts/insert_snapshot_warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs');
const path = require('path');
const { JSDOM } = require('jsdom');
const { JSDOM, VirtualConsole } = require('jsdom');
const { execSync } = require('child_process');

const COMMIT = String(execSync('git rev-parse --verify HEAD')).trim();
Expand All @@ -13,21 +13,30 @@ const WARNING_CSS = fs.readFileSync(path.join(__dirname, 'snapshot_warning.css')

console.log('Inserting snapshot reference warning...');

JSDOM.fromFile('./out/index.html', { contentType: 'text/html; charset=utf-8' }).then((dom) => {
const { document } = dom.window;
const virtualConsole = new VirtualConsole();
virtualConsole.on('error', () => {
// Suppress warnings from e.g. CSS features not supported by JSDOM
});

const style = document.createElement('style');
style.textContent = WARNING_CSS;
document.head.append(style);
(async () => {
let files = ['out/index.html', ...fs.readdirSync('out/multipage').filter(f => f.endsWith('.html')).map(f => 'out/multipage/' + f)];
for (let file of files) {
console.log(file);
let dom = await JSDOM.fromFile(file, { contentType: 'text/html; charset=utf-8', virtualConsole });
const { document } = dom.window;

// insert WARNING_HTML in beginning of body so it renders
// first even on slower devices and browsers
document.body.insertAdjacentHTML('afterbegin', WARNING_HTML);
const style = document.createElement('style');
style.textContent = WARNING_CSS;
document.head.append(style);

fs.writeFileSync('./out/index.html', dom.serialize(), 'utf8');
// insert WARNING_HTML in beginning of body so it renders
// first even on slower devices and browsers
document.body.insertAdjacentHTML('afterbegin', WARNING_HTML);

fs.writeFileSync(file, dom.serialize(), 'utf8');
}
console.log('Done!');
}).catch((reason) => {
console.error(reason);
process.exitCode = 1;
})().catch(e => {
console.error(e);
process.exit(1);
});
10 changes: 10 additions & 0 deletions scripts/snapshot_warning.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@
living specification.
</p>
</details>
<script>
let base = "https://ci.tc39.es/preview/tc39/ecma262/sha/";
if (
location.href.startsWith(base) &&
document.referrer.startsWith(base) &&
location.href.substring(base.length, base.length + 40) === document.referrer.substring(base.length, base.length + 40)
) {
document.querySelector(".annoying-warning").open = false;
}
</script>

0 comments on commit b509787

Please sign in to comment.