Skip to content

Commit

Permalink
Merge pull request #59 from AegisJSProject/feature/url-parser
Browse files Browse the repository at this point in the history
Add URL parser & fix bad config passing
  • Loading branch information
shgysk8zer0 authored Sep 25, 2024
2 parents 52551ec + 5deefd2 commit 253649e
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 23 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v0.0.10] - 2024-09-25

### Added
- Add `url` parser
- Add new types supported in `stringify`

### Fixed
- Fix infinite recursion in config via update to `@aegisjsproject/sanitizer`

## [v0.0.9] - 2024-09-19

### Changed
Expand Down
3 changes: 1 addition & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ignoreFile } from '@shgysk8zer0/eslint-config/ignoreFile.js';
import browser from '@shgysk8zer0/eslint-config/browser.js';

export default [ignoreFile, browser()];
export default browser({ files: ['*.js', 'test/**/*.js'], ignores: ['**/*.min.js'] });
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aegisjsproject/parsers",
"version": "0.0.9",
"version": "0.0.10",
"description": "A collection of secure & minimal parsers for HTML, CSS, SVG, MathML, XML, and JSON",
"keywords": [
"aegis",
Expand Down Expand Up @@ -90,6 +90,6 @@
"rollup": "^4.13.1"
},
"dependencies": {
"@aegisjsproject/sanitizer": "^0.1.1"
"@aegisjsproject/sanitizer": "^0.1.2"
}
}
1 change: 1 addition & 0 deletions parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export { svg } from './svg.js';
export { math } from './math.js';
export { xml } from './xml.js';
export { json } from './json.js';
export { url } from './url.js';
export { createPolicy } from './trust.js';
13 changes: 4 additions & 9 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,17 @@
<meta name="color-scheme" content="light dark" />
<meta http-equiv="Content-Security-Policy"
content="default-src 'none';
script-src 'self' https://unpkg.com/@shgysk8zer0/ https://unpkg.com/@aegisjsproject/ 'sha384-qOnpoDjAcZtXfanBdq59LK71K0lxdJmnLrSCdgYcsxL4PrFIFIpw79PfBnEwlm+M';
script-src 'self' https://unpkg.com/@shgysk8zer0/ https://unpkg.com/@aegisjsproject/ 'sha384-kLwKc1h2n3rxgHAQorvtkYZGT+zfan1+nzl0xYlGL0uR6tweMM5ZrOFt3O1nwUtx';
style-src blob:;
connect-src 'none';
trusted-types empty#html empty#script aegis-sanitizer#html default;
require-trusted-types-for 'script';" />
<title>Aegis Parsers</title>
<script type="importmap" integrity="sha384-qOnpoDjAcZtXfanBdq59LK71K0lxdJmnLrSCdgYcsxL4PrFIFIpw79PfBnEwlm+M">
<script type="importmap" integrity="sha384-kLwKc1h2n3rxgHAQorvtkYZGT+zfan1+nzl0xYlGL0uR6tweMM5ZrOFt3O1nwUtx">
{
"imports": {
"@shgysk8zer0/": "https://unpkg.com/@shgysk8zer0/",
"@aegisjsproject/": "https://unpkg.com/@aegisjsproject/",
"@kernvalley/": "https://unpkg.com/@kernvalley/",
"@aegisjsproject/parsers": "../bundle.min.js",
"@aegisjsproject/parsers/": ".../",
"@aegisjsproject/sanitizer": "../node_modules/@aegisjsproject/sanitizer/sanitizer.js",
"@aegisjsproject/sanitizer/": "../node_modules/@aegisjsproject/sanitizer/"
"@aegisjsproject/": "../node_modules/@aegisjsproject/",
"@shgysk8zer0/": "../node_modules/@shgysk8zer0/"
}
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHTMLParser, css, svg, completeConfig as sanitizer } from '@aegisjsproject/parsers';
import { createHTMLParser, css, svg, completeConfig as sanitizer } from '../bundle.js';

const html = createHTMLParser(sanitizer);
const file = new File(['Thanks for downloading my file :)'], 'thanks.txt', { type: 'text/plain' });
Expand Down
15 changes: 15 additions & 0 deletions url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { stringify } from './utils.js';

export function escape(str) {
return encodeURIComponent(stringify(str).trim()).replaceAll('.', '%2E');
}

export function url(strings, base, ...values) {
if (base instanceof Blob && strings.length === 2 && strings[0] === '' && strings[1] === '') {
return URL.createObjectURL(base);
} else if (URL.canParse(base)) {
return URL.parse(String.raw(strings, '', ...values.map(escape)), base);
} else {
return URL.parse(String.raw(strings, escape(base), ...values.map(escape)));
}
}
9 changes: 9 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export function stringify(thing) {
case 'function':
throw new TypeError('Functions are not supported.');

case 'undefined':
return '';

case 'object':
if (thing === null) {
return '';
Expand All @@ -22,8 +25,14 @@ export function stringify(thing) {
return stringifyChildren(thing);
} else if (thing instanceof Element) {
return thing.outerHTML;
} else if (thing instanceof Date) {
return thing.toISOString();
} else if (Array.isArray(thing)) {
return thing.map(stringify).join('');
} else if (thing instanceof ArrayBuffer && Uint8Array.prototype.toBase64 instanceof Function) {
return new Uint8Array(thing).toBase64();
} else if (ArrayBuffer.isView(thing) && thing.toBase64 instanceof Function) {
return thing.toBase64();
} else {
return thing.toString();
}
Expand Down

0 comments on commit 253649e

Please sign in to comment.