@prairielearn/browser-utils 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @prairielearn/browser-utils
2
2
 
3
+ ## 2.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - d28a15d: Support custom return type for parseHTMLElement
8
+
9
+ ## 2.0.2
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [0f7c90f]
14
+ - @prairielearn/html@4.0.2
15
+
3
16
  ## 2.0.1
4
17
 
5
18
  ### Patch Changes
package/README.md CHANGED
@@ -32,7 +32,7 @@ const elements = parseHTML(
32
32
  <div>Goodbye, world</div>
33
33
  `,
34
34
  );
35
- const div = parseHTMLElement(document, html`<div>Hello, world</div>`);
35
+ const div = parseHTMLElement<HTMLDivElement>(document, html`<div>Hello, world</div>`);
36
36
  ```
37
37
 
38
38
  ### `EncodedData` and `decodeData`
@@ -5,4 +5,4 @@ export declare function parseHTML(document: Document, html: string | HtmlSafeStr
5
5
  * {@link DocumentFragment}. If the HTML being parsed does not contain
6
6
  * exactly one element, an error is thrown.
7
7
  */
8
- export declare function parseHTMLElement(document: Document, html: string | HtmlSafeString): Element;
8
+ export declare function parseHTMLElement<T extends Element = Element>(document: Document, html: string | HtmlSafeString): T;
@@ -1 +1 @@
1
- {"version":3,"file":"parse-html.js","sourceRoot":"","sources":["../src/parse-html.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,SAAS,CAAC,QAAkB,EAAE,IAA6B;IACzE,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,IAAI,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;IACrD,MAAM,QAAQ,GAAG,QAAQ,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;IACpD,QAAQ,CAAC,SAAS,GAAG,IAAI,CAAC;IAC1B,OAAO,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AACrD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,QAAkB,EAAE,IAA6B;IAChF,MAAM,gBAAgB,GAAG,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IACnD,IAAI,gBAAgB,CAAC,iBAAiB,KAAK,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;IAClE,CAAC;IACD,OAAO,gBAAgB,CAAC,iBAA4B,CAAC;AACvD,CAAC","sourcesContent":["import type { HtmlSafeString } from '@prairielearn/html';\n\nexport function parseHTML(document: Document, html: string | HtmlSafeString): DocumentFragment {\n if (typeof html !== 'string') html = html.toString();\n const template = document.createElement('template');\n template.innerHTML = html;\n return document.importNode(template.content, true);\n}\n\n/**\n * Like {@link parseHTML}, but returns an {@link Element} instead of a\n * {@link DocumentFragment}. If the HTML being parsed does not contain\n * exactly one element, an error is thrown.\n */\nexport function parseHTMLElement(document: Document, html: string | HtmlSafeString): Element {\n const documentFragment = parseHTML(document, html);\n if (documentFragment.childElementCount !== 1) {\n throw new Error('Expected HTML to contain exactly one element');\n }\n return documentFragment.firstElementChild as Element;\n}\n"]}
1
+ {"version":3,"file":"parse-html.js","sourceRoot":"","sources":["../src/parse-html.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,SAAS,CAAC,QAAkB,EAAE,IAA6B;IACzE,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,IAAI,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;IACrD,MAAM,QAAQ,GAAG,QAAQ,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;IACpD,QAAQ,CAAC,SAAS,GAAG,IAAI,CAAC;IAC1B,OAAO,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AACrD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAC9B,QAAkB,EAClB,IAA6B;IAE7B,MAAM,gBAAgB,GAAG,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IACnD,IAAI,gBAAgB,CAAC,iBAAiB,KAAK,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;IAClE,CAAC;IACD,OAAO,gBAAgB,CAAC,iBAAsB,CAAC;AACjD,CAAC","sourcesContent":["import type { HtmlSafeString } from '@prairielearn/html';\n\nexport function parseHTML(document: Document, html: string | HtmlSafeString): DocumentFragment {\n if (typeof html !== 'string') html = html.toString();\n const template = document.createElement('template');\n template.innerHTML = html;\n return document.importNode(template.content, true);\n}\n\n/**\n * Like {@link parseHTML}, but returns an {@link Element} instead of a\n * {@link DocumentFragment}. If the HTML being parsed does not contain\n * exactly one element, an error is thrown.\n */\nexport function parseHTMLElement<T extends Element = Element>(\n document: Document,\n html: string | HtmlSafeString,\n): T {\n const documentFragment = parseHTML(document, html);\n if (documentFragment.childElementCount !== 1) {\n throw new Error('Expected HTML to contain exactly one element');\n }\n return documentFragment.firstElementChild as T;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prairielearn/browser-utils",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "sideEffects": false,
@@ -14,7 +14,7 @@
14
14
  "dev": "tsc --watch --preserveWatchOutput"
15
15
  },
16
16
  "dependencies": {
17
- "@prairielearn/html": "^4.0.1",
17
+ "@prairielearn/html": "^4.0.2",
18
18
  "js-base64": "^3.7.7"
19
19
  },
20
20
  "devDependencies": {
package/src/parse-html.ts CHANGED
@@ -12,10 +12,13 @@ export function parseHTML(document: Document, html: string | HtmlSafeString): Do
12
12
  * {@link DocumentFragment}. If the HTML being parsed does not contain
13
13
  * exactly one element, an error is thrown.
14
14
  */
15
- export function parseHTMLElement(document: Document, html: string | HtmlSafeString): Element {
15
+ export function parseHTMLElement<T extends Element = Element>(
16
+ document: Document,
17
+ html: string | HtmlSafeString,
18
+ ): T {
16
19
  const documentFragment = parseHTML(document, html);
17
20
  if (documentFragment.childElementCount !== 1) {
18
21
  throw new Error('Expected HTML to contain exactly one element');
19
22
  }
20
- return documentFragment.firstElementChild as Element;
23
+ return documentFragment.firstElementChild as T;
21
24
  }