Skip to content

Commit

Permalink
feat(hiccup): replace entity handling w/ thi.ng/strings
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 20, 2021
1 parent 57c246d commit 04154b8
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 20 deletions.
3 changes: 2 additions & 1 deletion packages/hiccup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"dependencies": {
"@thi.ng/api": "^8.0.3",
"@thi.ng/checks": "^3.0.3",
"@thi.ng/errors": "^2.0.3"
"@thi.ng/errors": "^2.0.3",
"@thi.ng/strings": "^3.0.3"
},
"devDependencies": {
"@thi.ng/atom": "^5.0.3",
Expand Down
11 changes: 0 additions & 11 deletions packages/hiccup/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,8 @@ export const PROC_TAGS: { [id: string]: string } = {
"!ATTLIST": ">\n",
};

/** @internal */
export const ENTITIES: { [id: string]: string } = {
"&": "&",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&apos;",
};

/** @internal */
export const RE_TAG = /^([^\s\.#]+)(?:#([^\s\.#]+))?(?:\.([^\s#]+))?$/;
/** @internal */
export const RE_ENTITY = new RegExp(`[${Object.keys(ENTITIES).join("")}]`, "g");

/** @internal */
export const COMMENT = "__COMMENT__";
Expand Down
3 changes: 0 additions & 3 deletions packages/hiccup/src/escape.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/hiccup/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export * from "./api.js";
export * from "./attribs.js";
export * from "./css.js";
export * from "./deref.js";
export * from "./escape.js";
export * from "./normalize.js";
export * from "./prefix.js";
export * from "./serialize.js";
8 changes: 4 additions & 4 deletions packages/hiccup/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isNotStringAndIterable } from "@thi.ng/checks/is-not-string-iterable";
import { isPlainObject } from "@thi.ng/checks/is-plain-object";
import { isString } from "@thi.ng/checks/is-string";
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
import { escapeEntities } from "@thi.ng/strings/entities";
import {
ATTRIB_JOIN_DELIMS,
CDATA,
Expand All @@ -16,7 +17,6 @@ import {
VOID_TAGS,
} from "./api.js";
import { css } from "./css.js";
import { escape } from "./escape.js";
import { normalize } from "./normalize.js";
import { formatPrefixes } from "./prefix.js";

Expand Down Expand Up @@ -179,7 +179,7 @@ const _serialize = (
? _serialize(tree.deref(), ctx, esc, span, keys, path)
: isNotStringAndIterable(tree)
? serializeIter(tree, ctx, esc, span, keys, path)
: ((tree = esc ? escape(String(tree)) : String(tree)), span)
: ((tree = esc ? escapeEntities(String(tree)) : String(tree)), span)
? `<span${keys ? ` key="${path.join("-")}"` : ""}>${tree}</span>`
: tree;

Expand Down Expand Up @@ -277,15 +277,15 @@ const attribPair = (a: string, v: any, esc: boolean) => {
: isArray(v)
? v.join(ATTRIB_JOIN_DELIMS[a] || " ")
: v.toString();
return v.length ? ` ${a}="${esc ? escape(v) : v}"` : null;
return v.length ? ` ${a}="${esc ? escapeEntities(v) : v}"` : null;
};

const serializeDataAttribs = (data: any, esc: boolean) => {
let res = "";
for (let id in data) {
let v = deref(data[id]);
isFunction(v) && (v = v(data));
v != null && (res += ` data-${id}="${esc ? escape(v) : v}"`);
v != null && (res += ` data-${id}="${esc ? escapeEntities(v) : v}"`);
}
return res;
};
Expand Down

0 comments on commit 04154b8

Please sign in to comment.