commit | 6104d15a73471adadce191009de7af90003cbc4f | [log] [tgz] |
---|---|---|
author | Cheick Abba Cisse <cheickcisse@google.com> | Thu Feb 04 11:20:05 2021 -0500 |
committer | GitHub <noreply@github.com> | Thu Feb 04 11:20:05 2021 -0500 |
tree | c338320ee737b27f5fce6e5888045c7ab6a0040d | |
parent | 6f126785cb095e89639c1fa34ef443b09ea25623 [diff] |
Update getTargetTextStyle to add target-text to the stylesheet (#53) Method now processes page CSS and adds any ::target-text styling to a new CSS class
This is a polyfill for the Text Fragments feature for browsers that don't support it directly.
The implementation broadly follows the text-finding algorithms, with a few small deviations and reorganizations, and should perform similarly to renderers which have implemented this natively. It is used in Chromium for iOS as well as the Link to Text Fragment Browser Extension.
The src
directory contains two files: text-fragments.js
, which is the polyfilling mechanism, and text-fragment-utils.js
, a module of util functions related to parsing, finding, and highlighting text fragments within the DOM.
The tools
directory contains a util script used for generating a regex used in the utils module.
The test
directory contains unit tests, based on Karma and Jasmine, as well as HTML files which can be loaded during those tests.
From npm:
npm install text-fragments-polyfill
From unpkg:
<script type="module> if (!('fragmentDirective' in Location.prototype) && !('fragmentDirective' in document)) { import('https://unpkg.com/text-fragments-polyfill'); } </script>
For simple usage as a polyfill, it is sufficient to import the text-fragments.js
file:
// Only load the polyfill in browsers that need it. if ( !('fragmentDirective' in Location.prototype) && !('fragmentDirective' in document) ) { import('text-fragments.js'); }
Users who wish to take a more hands-on approach can reuse chunks of the logic by importing the text-fragment-utils.js
module; support is provided for inclusion either as an ES6 module or using the Closure compiler.
Try the demo on a browser that does not support Text Fragments.
/src
.npm run start
and open http://localhost:8080/demo/ in a browser that does not support Text Fragments URLs directly, for example, Safari./demo/index.html
(look for location.hash
).Apache 2.0. This is not an official Google product.