Skip to content

Commit

Permalink
fix(hiccup): update/rename regexes & tag maps
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 20, 2019
1 parent fa56951 commit 6dba80d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
35 changes: 18 additions & 17 deletions packages/hiccup/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ export const SVG_NS = "http://www.w3.org/2000/svg";
export const XLINK_NS = "http://www.w3.org/1999/xlink";
export const XHTML_NS = "http://www.w3.org/1999/xhtml";

export const TAG_REGEXP = /^([^\s\.#]+)(?:#([^\s\.#]+))?(?:\.([^\s#]+))?$/;

export const PROC_TAGS: { [id: string]: string } = {
"?xml": "?>\n",
"!DOCTYPE": ">\n",
Expand All @@ -12,20 +10,6 @@ export const PROC_TAGS: { [id: string]: string } = {
"!ATTLIST": ">\n"
};

// tslint:disable-next-line
export const SVG_TAGS: {
[id: string]: number;
} = "animate animateColor animateMotion animateTransform circle clipPath color-profile defs desc discard ellipse feBlend feColorMatrix feComponentTransfer feComposite feConvolveMatrix feDiffuseLighting feDisplacementMap feDistantLight feDropShadow feFlood feFuncA feFuncB feFuncG feFuncR feGaussianBlur feImage feMerge feMergeNode feMorphology feOffset fePointLight feSpecularLighting feSpotLight feTile feTurbulence filter font foreignObject g image line linearGradient marker mask metadata mpath path pattern polygon polyline radialGradient rect set stop style svg switch symbol text textPath title tref tspan use view"
.split(" ")
.reduce((acc: any, x) => ((acc[x] = 1), acc), {});

// tslint:disable-next-line
export const VOID_TAGS: {
[id: string]: number;
} = "area base br circle col command ellipse embed hr img input keygen line link meta param path polygon polyline rect source stop track use wbr ?xml"
.split(" ")
.reduce((acc: any, x) => ((acc[x] = 1), acc), {});

export const ENTITIES: { [id: string]: string } = {
"&": "&",
"<": "&lt;",
Expand All @@ -34,6 +18,9 @@ export const ENTITIES: { [id: string]: string } = {
"'": "&apos;"
};

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

export const COMMENT = "__COMMENT__";

export const NO_SPANS: {
Expand All @@ -45,4 +32,18 @@ export const NO_SPANS: {
textarea: 1
};

export const ENTITY_RE = new RegExp(`[${Object.keys(ENTITIES)}]`, "g");
const tagMap = (
tags: string
): {
[id: string]: boolean;
} => tags.split(" ").reduce((acc: any, x) => ((acc[x] = true), acc), {});

// tslint:disable-next-line
export const SVG_TAGS = tagMap(
"animate animateColor animateMotion animateTransform circle clipPath color-profile defs desc discard ellipse feBlend feColorMatrix feComponentTransfer feComposite feConvolveMatrix feDiffuseLighting feDisplacementMap feDistantLight feDropShadow feFlood feFuncA feFuncB feFuncG feFuncR feGaussianBlur feImage feMerge feMergeNode feMorphology feOffset fePointLight feSpecularLighting feSpotLight feTile feTurbulence filter font foreignObject g image line linearGradient marker mask metadata mpath path pattern polygon polyline radialGradient rect set stop style svg switch symbol text textPath title tref tspan use view"
);

// tslint:disable-next-line
export const VOID_TAGS = tagMap(
"area base br circle col command ellipse embed hr img input keygen line link meta param path polygon polyline rect source stop track use wbr ?xml"
);
4 changes: 2 additions & 2 deletions packages/hiccup/src/escape.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { ENTITIES, ENTITY_RE } from "./api";
import { ENTITIES, RE_ENTITY } from "./api";

export const escape = (x: string) => x.replace(ENTITY_RE, (y) => ENTITIES[y]);
export const escape = (x: string) => x.replace(RE_ENTITY, (y) => ENTITIES[y]);
4 changes: 2 additions & 2 deletions packages/hiccup/src/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isPlainObject, isString } from "@thi.ng/checks";
import { illegalArgs } from "@thi.ng/errors";
import { TAG_REGEXP } from "./api";
import { RE_TAG } from "./api";
import { css } from "./css";

export const normalize = (tag: any[]) => {
Expand All @@ -10,7 +10,7 @@ export const normalize = (tag: any[]) => {
let clazz: string;
const hasAttribs = isPlainObject(tag[1]);
const attribs: any = hasAttribs ? { ...tag[1] } : {};
if (!isString(el) || !(match = TAG_REGEXP.exec(el))) {
if (!isString(el) || !(match = RE_TAG.exec(el))) {
illegalArgs(`"${el}" is not a valid tag name`);
}
el = match![1];
Expand Down

0 comments on commit 6dba80d

Please sign in to comment.