npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

text-fragments-polyfill

v6.1.0

Published

This is a polyfill for the [Text Fragments](https://wicg.github.io/scroll-to-text-fragment/) feature for browsers that don't support it natively.

Downloads

9,434

Readme

Text Fragments Polyfill

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 and data-driven tests, based on Karma and Jasmine, as well as HTML files which can be loaded during those tests. Unit tests go in the unit subfolder and data-driven tests go in the data-driven subfolder.

Installation

From npm:

npm install text-fragments-polyfill

From unpkg:

<script type="module">
  if (!('fragmentDirective' in document)) {
    import('https://unpkg.com/text-fragments-polyfill');
  }
</script>

Usage

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 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.

Demo

Try the demo on a browser that does not support Text Fragments.

Development

  1. Hack in /src.
  2. Run npm run start and open http://localhost:8080/demo/ in a browser that does not support Text Fragments URLs directly, for example, Safari.
  3. Hack, reload, hack,…
  4. You can modify the Text Fragments URLs directly in /demo/index.html (look for location.hash).

Data-Driven Tests

Data-Driven tests are round trip tests checking both fragment generation and highlighting. Each test case is defined by an html document and a selection range inside the document. The tests try to generate a text fragment targeting the selection range in the document and use it for highlighting, comparing the highlighted text against the expected highlighted text.

To add more data-driven tests, put your files in the following subfolders of test/data-driven:

  • input/test-params: Json file with test case definition. See TextFragmentDataDrivenTestInput in text-fragments-data-driven-test.js for a detailed schema description.
  • input/html: Html file referenced in the test case's input file.
  • output: Plain text file with the test case's expected results. Must have extension .out and the same file name as the test case's input file. See the documentation of TextFragmentsDataDrivenTest in text-fragments-data-driven-test.js for more details.

The example test case basic-test can be used for reference. Files: input/test-params/basic-test.json, input/html/basic-test.html and output/basic-test.out.

Debugging tests

  1. Run npm run debug <debug browser> with debug browser = Firefox_with_debugging | Chrome_with_debugging
  2. Go to the browser window and click on "Debug" to run the tests
  3. Add breakpoints in the browser's developer tool debugger
  4. Refresh the page so the tests run again and your breakpoints are hit

License

Apache 2.0. This is not an official Google product.