diff --git a/lib/__fixtures__/curriculum-helpers-html.ts b/lib/__fixtures__/curriculum-helpers-html.ts index 8981cf4..30f91bb 100644 --- a/lib/__fixtures__/curriculum-helpers-html.ts +++ b/lib/__fixtures__/curriculum-helpers-html.ts @@ -22,9 +22,25 @@ not a comment not a comment `; +const htmlExampleHead = ` + + + + Piano + +`; + +const htmlInnerExample = ` + + Piano + +`; + const testValues = { htmlFullExample, htmlCodeWithCommentsRemoved, + htmlExampleHead, + htmlInnerExample, }; export default testValues; diff --git a/lib/__tests__/curriculum-helper.test.ts b/lib/__tests__/curriculum-helper.test.ts index 84cb101..89a12c4 100644 --- a/lib/__tests__/curriculum-helper.test.ts +++ b/lib/__tests__/curriculum-helper.test.ts @@ -9,7 +9,12 @@ const { stringWithWhiteSpaceChars, stringWithWhiteSpaceCharsRemoved } = const { cssFullExample, cssCodeWithCommentsRemoved } = cssTestValues; -const { htmlFullExample, htmlCodeWithCommentsRemoved } = htmlTestValues; +const { + htmlFullExample, + htmlCodeWithCommentsRemoved, + htmlExampleHead, + htmlInnerExample, +} = htmlTestValues; const { jsCodeWithSingleAndMultLineComments, @@ -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", () => { diff --git a/lib/index.ts b/lib/index.ts index 81b2a34..b85ca2b 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -11,6 +11,19 @@ export function removeHtmlComments(str: string): string { return str.replace(/|$)/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*>)" + ); + return code.match(expression)?.toString() ?? ""; +} + /** * Removes every CSS-comment from the string that is provided * @param {String} str a CSS-string where the comments need to be removed of