Browse Source

Export applyFragDir() for browser console use.

tags/v0.1.0-src
Gerben 4 years ago
parent
commit
acdf88153c
2 changed files with 25 additions and 1 deletions
  1. +7
    -0
      demo.html
  2. +18
    -1
      src/polyfill.ts

+ 7
- 0
demo.html View File

@@ -4,6 +4,13 @@
<meta charset="utf-8"/> <meta charset="utf-8"/>
<title>Text Fragments playground</title> <title>Text Fragments playground</title>
<script src="./lib/polyfill.js" type="module"></script> <script src="./lib/polyfill.js" type="module"></script>
<script>
(async function () {
const { applyFragDir } = await import('./lib/polyfill.js');
globalThis.applyFragDir = applyFragDir;
console.log(`You can try out fragment directives using applyFragDir(…); e.g. applyFragDir(':~:text=bla')`);
})();
</script>
<style> <style>
body { body {
margin: 0; margin: 0;


+ 18
- 1
src/polyfill.ts View File

@@ -13,12 +13,18 @@ import {
} from './index.js'; } from './index.js';


function run(): void { function run(): void {
const { documentUrl, documentFragmentDirective } = initializeDocumentFragmentDirective(window.document) ?? {};
const { documentUrl, documentFragmentDirective } = initializeDocumentFragmentDirective(document) ?? {};
if (documentUrl !== document.URL) { if (documentUrl !== document.URL) {
// We could change the location to hide the fragment directive from the fragment, as the spec prescribes; however this would also hide it from the user (and could trigger other event listeners). // We could change the location to hide the fragment directive from the fragment, as the spec prescribes; however this would also hide it from the user (and could trigger other event listeners).
// document.location.replace(documentUrl); // document.location.replace(documentUrl);
} }
applyFragmentDirective({ document, documentFragmentDirective });
}


function applyFragmentDirective({ document, documentFragmentDirective } : {
document: Document,
documentFragmentDirective: string | null,
}): void {
if (documentFragmentDirective !== null) { if (documentFragmentDirective !== null) {
const { documentIndicatedPart, ranges } = indicatedPartOfTheDocument_beginning({ const { documentIndicatedPart, ranges } = indicatedPartOfTheDocument_beginning({
document, document,
@@ -80,3 +86,14 @@ function install(): void {
} }


install(); install();

// A small tool to use from e.g. the browser console.
export function applyFragDir(fragmentDirective: string) {
if (typeof fragmentDirective !== 'string' || !fragmentDirective.includes(':~:'))
throw new TypeError('Expected a fragment directive string, e.g. ":~:text=bla&text=blub"');
fragmentDirective = fragmentDirective.substring(fragmentDirective.indexOf(':~:') + 3);
applyFragmentDirective({
document,
documentFragmentDirective: fragmentDirective,
});
}

Loading…
Cancel
Save