Skip to content

Commit

Permalink
Publish version 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Jul 26, 2024
1 parent 72bd5c2 commit 6611553
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ <h2>License</h2>
</div>
</body>
<script type="module">
import { highlightSearchTerm } from "https://cdn.jsdelivr.net/npm/[email protected].0/src/index.js";
import { highlightSearchTerm } from "https://cdn.jsdelivr.net/npm/[email protected].1/src/index.js";
const search = document.getElementById("search");
search.addEventListener("input", () => {
highlightSearchTerm({ search: search.value, selector: ".content" });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "highlight-search-term",
"version": "1.0.0",
"version": "1.0.1",
"description": "Highlight search term in a page. Vanilla JS, compatible with frontend frameworks (React, Vite, Angular, etc).",
"repository": "marmelab/highlight-search-term",
"homepage": "https://marmelab.com/highlight-search-term/",
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ const highlightSearchTerm = ({
};

const getTextNodesInElementContainingText = (element, text) => {
const lowerCaseText = text.toLowerCase();
const nodes = [];
const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT);
let node;
while ((node = walker.nextNode())) {
if (node.textContent?.toLowerCase().includes(text.toLowerCase())) {
if (node.textContent?.toLowerCase().includes(lowerCaseText)) {
nodes.push(node);
}
}
Expand All @@ -55,11 +56,12 @@ const getTextNodesInElementContainingText = (element, text) => {

const getRangesForSearchTermInElement = (element, search) => {
const ranges = [];
const lowerCaseSearch = search.toLowerCase();
if (!element.firstChild) return ranges;
const text = element.textContent?.toLowerCase() || "";
let start = 0;
let index;
while ((index = text.indexOf(search.toLowerCase(), start)) >= 0) {
while ((index = text.indexOf(lowerCaseSearch, start)) >= 0) {
const range = new Range();
range.setStart(element.firstChild, index);
range.setEnd(element.firstChild, index + search.length);
Expand Down

0 comments on commit 6611553

Please sign in to comment.