Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add html element extractor #229

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/__fixtures__/curriculum-helpers-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,25 @@ not a comment
not a comment
`;

const htmlExampleHead = `
<link rel="stylesheet" href="styles.css">
<head>
<meta charset="UTF-8" />
<title>Piano</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>`;

const htmlInnerExample = `
<meta charset="UTF-8" />
<title>Piano</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
`;

const testValues = {
htmlFullExample,
htmlCodeWithCommentsRemoved,
htmlExampleHead,
htmlInnerExample,
};

export default testValues;
17 changes: 16 additions & 1 deletion lib/__tests__/curriculum-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ const { stringWithWhiteSpaceChars, stringWithWhiteSpaceCharsRemoved } =

const { cssFullExample, cssCodeWithCommentsRemoved } = cssTestValues;

const { htmlFullExample, htmlCodeWithCommentsRemoved } = htmlTestValues;
const {
htmlFullExample,
htmlCodeWithCommentsRemoved,
htmlExampleHead,
htmlInnerExample,
} = htmlTestValues;

const {
jsCodeWithSingleAndMultLineComments,
Expand Down Expand Up @@ -89,6 +94,16 @@ describe("removeHtmlComments", () => {
});
});

describe("extractHTMLElement", () => {
const { extractHTMLElement } = helper;
it("returns an empty string if no head is found", () => {
expect(typeof extractHTMLElement("", "head")).toBe("string");
});
it("returns inner HTML string if a head is present", () => {
expect(extractHTMLElement(htmlExampleHead, "head")).toBe(htmlInnerExample);
});
});

describe("isCalledWithNoArgs", () => {
const { isCalledWithNoArgs } = helper;
it("returns a boolean", () => {
Expand Down
13 changes: 13 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ export function removeHtmlComments(str: string): string {
return str.replace(/<!--[\s\S]*?(-->|$)/g, "");
}

/**
* Extracts the inner html of every element inside the head
* @param {String} code a HTML string of the head
* @returns {String} the inner html of every element in the head or an empty string if no head is found
*/

export function extractHTMLElement(code: string, tag: string = "head"): string {
const expression = new RegExp(
"(?<=<" + tag + "\\s*>)(?:.|\\s*)*?(?=<\\/" + tag + "\\s*>)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"(?<=<" + tag + "\\s*>)(?:.|\\s*)*?(?=<\\/" + tag + "\\s*>)"
"(?<=<" + tag + "[^>]*>)(?:.|\\s*)*?(?=<\\/" + tag + "\\s*>)"

);
return code.match(expression)?.toString() ?? "";
Comment on lines +15 to +24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description, params, and def do not match.

Is this just for head elements? By the look of things, this is for all non-void elements provided they have no attributes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was originally designed for that but I wanted it to be more useful so that I can justify adding it to the library.

}

/**
* Removes every CSS-comment from the string that is provided
* @param {String} str a CSS-string where the comments need to be removed of
Expand Down
Loading