commit | f374a4cba7aeaa9af6768b9296927a5d5de17ec4 | [log] [tgz] |
---|---|---|
author | Ernesto Izquierdo Clua <eic@google.com> | Mon Dec 20 20:56:00 2021 |
committer | Ernesto Izquierdo Clua <eic@google.com> | Mon Dec 20 21:02:28 2021 |
tree | 78d83d63aa303eeaecce9095070b6066c9760cdb | |
parent | 04c058c92fc5be41f91db654b9e903ade4fca27e [diff] |
Fix invisible text in fragment context terms Added logic to skip invisible text nodes when traversing the DOM. Fixed traversal functions to be able to detect block boundaries. Added helper class to keep track of text inside the same block boundaries. Added tests.
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 three files:
text-fragments.js
, containing the polyfilling mechanism.
text-fragment-utils.js
, a module of util functions related to parsing, finding, and highlighting text fragments within the DOM. Most of the logic used by the polyfill for finding fragments in a page lives here.
fragment-generation-utils.js
, a module of util functions for generating URLs with a text fragment. These utils are not used by the polyfill itself, but they are likely to be useful for related projects.
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
and fragment-generation-utils.js
modules; 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.