Skip to content

Commit

Permalink
Add getElementsByClassName function to legacy.ts (#1551)
Browse files Browse the repository at this point in the history
Co-authored-by: Felix <[email protected]>
  • Loading branch information
censujiang and fb55 authored Dec 23, 2024
1 parent 4bd7786 commit 2804ff3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/__fixtures__/fixture.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parseDocument } from "htmlparser2";

const markup = Array(21).join(
"<?xml><tag1 id='asdf'> <script>text</script> <!-- comment --> <tag2> text </tag1>",
"<?xml><tag1 id='asdf' class='class1'> <script>text</script> <!-- comment --> <tag2> text </tag1>",
);

export default parseDocument(markup).children;
12 changes: 12 additions & 0 deletions src/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ElementType } from "domelementtype";
import {
getElements,
getElementById,
getElementsByClassName,
getElementsByTagName,
getElementsByTagType,
} from "./legacy";
Expand Down Expand Up @@ -95,6 +96,17 @@ describe("legacy", () => {
expect(getElementById("asdfs", fixture, true)).toBeNull());
});

describe("getElementsByClassName", () => {
it("returns the specified nodes", () =>
expect(
getElementsByClassName("class1", fixture, true),
).toHaveLength(20));
it("returns empty array for unknown class names", () =>
expect(
getElementsByClassName("class23", fixture, true),
).toHaveLength(0));
});

describe("getElementsByTagName", () => {
it("returns the specified nodes", () =>
expect(getElementsByTagName("tag2", fixture, true)).toEqual(
Expand Down
24 changes: 24 additions & 0 deletions src/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,30 @@ export function getElementsByTagName(
) as Element[];
}

/**
* Returns all nodes with the supplied `className`.
*
* @category Legacy Query Functions
* @param className Class name to search for.
* @param nodes Nodes to search through.
* @param recurse Also consider child nodes.
* @param limit Maximum number of nodes to return.
* @returns All nodes with the supplied `className`.
*/
export function getElementsByClassName(
className: string | ((name: string) => boolean),
nodes: AnyNode | AnyNode[],
recurse = true,
limit: number = Infinity,
): Element[] {
return filter(
getAttribCheck("class", className),
nodes,
recurse,
limit,
) as Element[];
}

/**
* Returns all nodes with the supplied `type`.
*
Expand Down
2 changes: 1 addition & 1 deletion src/stringify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("stringify", () => {
describe("getOuterHTML", () => {
it("Correctly renders the outer HTML", () => {
expect(getOuterHTML(fixture[1])).toBe(
'<tag1 id="asdf"> <script>text</script> <!-- comment --> <tag2> text </tag2></tag1>',
'<tag1 id="asdf" class="class1"> <script>text</script> <!-- comment --> <tag2> text </tag2></tag1>',
);
});
});
Expand Down

0 comments on commit 2804ff3

Please sign in to comment.