{"id":13725266,"url":"https://github.com/GoogleChromeLabs/text-fragments-polyfill","last_synced_at":"2025-05-07T19:33:29.679Z","repository":{"id":41984661,"uuid":"283178791","full_name":"GoogleChromeLabs/text-fragments-polyfill","owner":"GoogleChromeLabs","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-09T10:02:03.000Z","size":719,"stargazers_count":113,"open_issues_count":11,"forks_count":27,"subscribers_count":15,"default_branch":"main","last_synced_at":"2024-10-09T12:01:52.721Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GoogleChromeLabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-07-28T10:27:09.000Z","updated_at":"2024-10-09T09:57:43.000Z","dependencies_parsed_at":"2024-01-09T16:08:24.263Z","dependency_job_id":null,"html_url":"https://github.com/GoogleChromeLabs/text-fragments-polyfill","commit_stats":{"total_commits":172,"total_committers":19,"mean_commits":9.052631578947368,"dds":0.6395348837209303,"last_synced_commit":"2635dffc84fd8d666f27d580cd2af0fd3193434c"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleChromeLabs%2Ftext-fragments-polyfill","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleChromeLabs%2Ftext-fragments-polyfill/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleChromeLabs%2Ftext-fragments-polyfill/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleChromeLabs%2Ftext-fragments-polyfill/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GoogleChromeLabs","download_url":"https://codeload.github.com/GoogleChromeLabs/text-fragments-polyfill/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224645284,"owners_count":17346111,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T01:02:17.712Z","updated_at":"2024-11-14T15:31:00.289Z","avatar_url":"https://github.com/GoogleChromeLabs.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Text Fragments Polyfill\n\nThis is a polyfill for the\n[Text Fragments](https://wicg.github.io/scroll-to-text-fragment/) feature for\nbrowsers that don't support it directly.\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg width=\"400\" src=\"https://user-images.githubusercontent.com/145676/79250513-02bb5800-7e7f-11ea-8e56-bd63edd31f5b.jpeg\"\u003e\n  \u003cp\u003e\n    \u003csup\u003e\n      The text fragment link\n      \u003ca href=\"https://en.wikipedia.org/wiki/Cat#Size:~:text=This%20article%20is,kept%20as%20a%20pet\"\u003e\n        #:~:text=This%20article%20is,kept%20as%20a%20pet\n      \u003c/a\u003e\n      rendered in Safari on an iOS device, with\n      \u003ca href=\"https://github.com/GoogleChromeLabs/text-fragments-polyfill/blob/main/src/text-fragments.js\"\u003e\n        \u003ccode\u003etext-fragments.js\u003c/code\u003e\n      \u003c/a\u003e\n      injected via Web Inspector.\n    \u003c/sup\u003e\n  \u003c/p\u003e\n\u003c/div\u003e\n\nThe implementation broadly follows the text-finding algorithms, with a few small deviations\nand reorganizations, and should perform similarly to renderers which have implemented this\nnatively. It is used in Chromium for iOS as well as the\n[Link to Text Fragment Browser Extension](https://github.com/GoogleChromeLabs/link-to-text-fragment).\n\nThe `src` directory contains three files:\n\n* `text-fragments.js`, containing the polyfilling mechanism.\n\n* `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.\n\n* `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.\n\nThe `tools` directory contains a util script used for generating a regex used in the utils\nmodule.\n\nThe `test` directory contains unit and data-driven tests, based on Karma and Jasmine, as well as HTML files\nwhich can be loaded during those tests. Unit tests go in the `unit` subfolder and data-driven tests go in\nthe `data-driven` subfolder.\n\n## Installation\n\nFrom npm:\n\n```bash\nnpm install text-fragments-polyfill\n```\n\nFrom unpkg:\n\n```html\n\u003cscript type=\"module\"\u003e\n  if (!('fragmentDirective' in document)) {\n    import('https://unpkg.com/text-fragments-polyfill');\n  }\n\u003c/script\u003e\n```\n\n## Usage\n\nFor simple usage as a polyfill, it is sufficient to import the `text-fragments.js` file:\n\n```js\n// Only load the polyfill in browsers that need it.\nif (\n  !('fragmentDirective' in document)\n) {\n  import('text-fragments.js');\n}\n```\n\nUsers 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.\n\n## Demo\n\nTry the [demo](https://text-fragments-polyfill.glitch.me/) on a browser that\ndoes not support Text Fragments.\n\n## Development\n\n1. Hack in `/src`.\n1. Run `npm run start` and open\n   [http://localhost:8080/demo/](http://localhost:8080/demo/`) in a browser that\n   does not support Text Fragments URLs directly, for example, Safari.\n1. Hack, reload, hack,…\n1. You can modify the Text Fragments URLs directly in\n   [`/demo/index.html`](https://github.com/GoogleChromeLabs/text-fragments-polyfill/blob/main/demo/index.html)\n   (look for `location.hash`).\n\n## Data-Driven Tests\nData-Driven tests are round trip tests checking both fragment generation and highlighting.\nEach test case is defined by an html document and a selection range inside the document.\nThe tests try to generate a text fragment targeting the selection range in the document\nand use it for highlighting, comparing the highlighted text against the expected highlighted text.\n\nTo add more data-driven tests, put your files in the following subfolders of `test/data-driven`:\n  - `input/test-params`: Json file with test case definition. See `TextFragmentDataDrivenTestInput` in `text-fragments-data-driven-test.js` for a detailed schema description.\n  - `input/html`: Html file referenced in the test case's input file.\n  - `output`: Plain text file with the test case's expected results. Must have extension `.out` and the same file name\n  as the test case's input file.\n  See the documentation of `TextFragmentsDataDrivenTest` in `text-fragments-data-driven-test.js` for more details.\n\nThe example test case `basic-test` can be used for reference. Files: `input/test-params/basic-test.json`,\n`input/html/basic-test.html` and `output/basic-test.out`.\n\n## Debugging tests\n1. Run `npm run debug \u003cdebug browser\u003e` with `debug browser = Firefox_with_debugging | Chrome_with_debugging`\n2. Go to the browser window and click on \"Debug\" to run the tests\n3. Add breakpoints in the browser's developer tool debugger\n4. Refresh the page so the tests run again and your breakpoints are hit\n\n## License\n\nApache 2.0. This is not an official Google product.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGoogleChromeLabs%2Ftext-fragments-polyfill","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGoogleChromeLabs%2Ftext-fragments-polyfill","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGoogleChromeLabs%2Ftext-fragments-polyfill/lists"}