Skip to content

Commit

Permalink
deno fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
dbushell committed Oct 11, 2024
1 parent becbbb2 commit 04ed27e
Show file tree
Hide file tree
Showing 17 changed files with 506 additions and 499 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dbushell/hyperless",
"version": "0.23.0",
"version": "0.24.0",
"exports": {
".": "./mod.ts"
},
Expand Down
20 changes: 10 additions & 10 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export {escape, unescape} from './src/html-utils.ts';
export {inlineTags, opaqueTags, voidTags} from './src/html-tags.ts';
export {AttributeMap} from './src/attribute-map.ts';
export {parseAttributes} from './src/attribute-parser.ts';
export {Node} from './src/html-node.ts';
export { escape, unescape } from "./src/html-utils.ts";
export { inlineTags, opaqueTags, voidTags } from "./src/html-tags.ts";
export { AttributeMap } from "./src/attribute-map.ts";
export { parseAttributes } from "./src/attribute-parser.ts";
export { Node } from "./src/html-node.ts";
export {
type ParseOptions,
getParseOptions,
parseHTML
} from './src/html-parser.ts';
export {excerpt} from './src/excerpt.ts';
export {stripTags} from './src/striptags.ts';
parseHTML,
type ParseOptions,
} from "./src/html-parser.ts";
export { excerpt } from "./src/excerpt.ts";
export { stripTags } from "./src/striptags.ts";
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dbushell/hyperless",
"version": "0.23.0",
"version": "0.24.0",
"repository": {
"type": "git",
"url": "git+https://github.com/dbushell/hyperless.git"
Expand Down
12 changes: 6 additions & 6 deletions src/attribute-map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {escape, unescape} from './html-utils.ts';
import { escape, unescape } from "./html-utils.ts";

/**
* HTML attributes map
Expand All @@ -7,12 +7,12 @@ import {escape, unescape} from './html-utils.ts';
*/
export class AttributeMap extends Map<string, string> {
constructor(
iterable?: Iterable<readonly [string, string]> | null | undefined
iterable?: Iterable<readonly [string, string]> | null | undefined,
) {
super(
iterable instanceof AttributeMap
? Array.from(iterable, ([k, v]) => [k, escape(v)])
: iterable
: iterable,
);
}
override [Symbol.iterator](): MapIterator<[string, string]> {
Expand Down Expand Up @@ -49,7 +49,7 @@ export class AttributeMap extends Map<string, string> {
}
override forEach(
callbackfn: (value: string, key: string, map: Map<string, string>) => void,
thisArg?: unknown
thisArg?: unknown,
): void {
super.forEach((value, key, map) =>
callbackfn.call(thisArg, unescape(value), key, map)
Expand All @@ -58,8 +58,8 @@ export class AttributeMap extends Map<string, string> {
override toString(): string {
const entries = Array.from(super.entries());
const attr = entries.map(([k, v]) =>
v === '' ? k : v.indexOf('"') === -1 ? `${k}="${v}"` : `${k}='${v}'`
v === "" ? k : v.indexOf('"') === -1 ? `${k}="${v}"` : `${k}='${v}'`
);
return attr.join(' ');
return attr.join(" ");
}
}
80 changes: 40 additions & 40 deletions src/attribute-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
* Parse HTML Attributes
* {@link https://html.spec.whatwg.org/#before-attribute-name-state}
*/
import {AttributeMap} from './attribute-map.ts';
import { AttributeMap } from "./attribute-map.ts";
import {
ASCII_WHITESPACE,
INVALID_ATTRIBUTE_NAME,
INVALID_ATTRIBUTE_VALUE,
ASCII_WHITESPACE
} from './constants.ts';
} from "./constants.ts";

/** Attribute parser state */
type ParseState =
| 'BEFORE_NAME'
| 'NAME'
| 'AFTER_NAME'
| 'BEFORE_VALUE'
| 'DOUBLE_QUOTED'
| 'SINGLE_QUOTED'
| 'UNQUOTED';
| "BEFORE_NAME"
| "NAME"
| "AFTER_NAME"
| "BEFORE_VALUE"
| "DOUBLE_QUOTED"
| "SINGLE_QUOTED"
| "UNQUOTED";

/**
* Return a map of HTML attributes
Expand All @@ -27,88 +27,88 @@ type ParseState =
export const parseAttributes = (attributes: string): AttributeMap => {
const map: AttributeMap = new AttributeMap();

let state: ParseState = 'BEFORE_NAME';
let name = '';
let value = '';
let state: ParseState = "BEFORE_NAME";
let name = "";
let value = "";

for (let i = 0; i < attributes.length; i++) {
const char = attributes[i];
// Handle case where closing HTML tag is included to avoid error
if (state === 'BEFORE_NAME' || state === 'NAME' || state === 'UNQUOTED') {
if (char === '/' || char === '>') {
if (state === "BEFORE_NAME" || state === "NAME" || state === "UNQUOTED") {
if (char === "/" || char === ">") {
break;
}
}
switch (state) {
case 'BEFORE_NAME':
case "BEFORE_NAME":
if (ASCII_WHITESPACE.has(char)) {
continue;
} else if (INVALID_ATTRIBUTE_NAME.has(char)) {
throw new Error(`Invalid attribute name at character ${i}`);
}
name = char;
value = '';
state = 'NAME';
value = "";
state = "NAME";
continue;
case 'NAME':
if (char === '=') {
state = 'BEFORE_VALUE';
case "NAME":
if (char === "=") {
state = "BEFORE_VALUE";
} else if (ASCII_WHITESPACE.has(char)) {
state = 'AFTER_NAME';
state = "AFTER_NAME";
} else if (INVALID_ATTRIBUTE_NAME.has(char)) {
throw new Error(`invalid name at ${i}`);
} else {
name += char;
}
continue;
case 'AFTER_NAME':
if (char === '=') {
state = 'BEFORE_VALUE';
case "AFTER_NAME":
if (char === "=") {
state = "BEFORE_VALUE";
} else if (ASCII_WHITESPACE.has(char) === false) {
// End of empty attribute
map.set(name, '');
map.set(name, "");
// Rewind state to match new name
i--;
state = 'BEFORE_NAME';
state = "BEFORE_NAME";
}
continue;
case 'BEFORE_VALUE':
case "BEFORE_VALUE":
if (char === "'") {
state = 'SINGLE_QUOTED';
state = "SINGLE_QUOTED";
} else if (char === '"') {
state = 'DOUBLE_QUOTED';
state = "DOUBLE_QUOTED";
} else if (ASCII_WHITESPACE.has(char)) {
continue;
} else if (INVALID_ATTRIBUTE_VALUE.has(char)) {
throw new Error(`Invalid unquoted attribute value at character ${i}`);
} else {
value += char;
state = 'UNQUOTED';
state = "UNQUOTED";
}
continue;
case 'DOUBLE_QUOTED':
case "DOUBLE_QUOTED":
if (char === '"') {
// End of double quoted attribute
map.set(name, value);
state = 'BEFORE_NAME';
state = "BEFORE_NAME";
} else {
value += char;
}
continue;
case 'SINGLE_QUOTED':
case "SINGLE_QUOTED":
if (char === "'") {
// End of single quoted attribute
map.set(name, value);
state = 'BEFORE_NAME';
state = "BEFORE_NAME";
} else {
value += char;
}
continue;
case 'UNQUOTED':
case "UNQUOTED":
if (ASCII_WHITESPACE.has(char)) {
// End of unquoted attribute
map.set(name, value);
state = 'BEFORE_NAME';
state = "BEFORE_NAME";
} else if (INVALID_ATTRIBUTE_VALUE.has(char)) {
throw new Error(`Invalid unquoted attribute value at character ${i}`);
} else {
Expand All @@ -119,10 +119,10 @@ export const parseAttributes = (attributes: string): AttributeMap => {
}
// Handle end state
switch (state) {
case 'NAME':
map.set(name, '');
case "NAME":
map.set(name, "");
break;
case 'UNQUOTED':
case "UNQUOTED":
map.set(name, value);
break;
}
Expand Down
20 changes: 10 additions & 10 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* {@link https://infra.spec.whatwg.org/#ascii-whitespace}
*/
export const ASCII_WHITESPACE = new Set([
'\t', // U+0009 TAB
'\n', // U+000A LF (Line Feed)
'\f', // U+000C FF (Form Feed)
'\r', // U+000D CR (Carriage Return)
' ' // U+0020 SPACE
"\t", // U+0009 TAB
"\n", // U+000A LF (Line Feed)
"\f", // U+000C FF (Form Feed)
"\r", // U+000D CR (Carriage Return)
" ", // U+0020 SPACE
]);

/**
Expand Down Expand Up @@ -36,10 +36,10 @@ export const INVALID_ATTRIBUTE_NAME = new Set(CONTROLS);
[
'"', // U+0022 Quotation Mark
"'", // U+0027 Apostrophe
'=', // U+003D Equals Sign
'>', // U+003C Less-Than Sign
'<', // U+003E Greater-Than Sign
'/' // U+002F Solidus
"=", // U+003D Equals Sign
">", // U+003C Less-Than Sign
"<", // U+003E Greater-Than Sign
"/", // U+002F Solidus
].forEach((char) => INVALID_ATTRIBUTE_NAME.add(char));

/**
Expand All @@ -48,5 +48,5 @@ export const INVALID_ATTRIBUTE_NAME = new Set(CONTROLS);
*/
export const INVALID_ATTRIBUTE_VALUE = new Set(INVALID_ATTRIBUTE_NAME);
[
'`' // U+0060 Grave Accent
"`", // U+0060 Grave Accent
].forEach((char) => INVALID_ATTRIBUTE_VALUE.add(char));
12 changes: 6 additions & 6 deletions src/excerpt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {stripTags} from './striptags.ts';
import { stripTags } from "./striptags.ts";

/**
* Generate a text excerpt from HTML content.
Expand All @@ -14,8 +14,8 @@ import {stripTags} from './striptags.ts';
export const excerpt = (
html: string,
maxLength = 300,
trunateSuffix = '[…]',
endChars = ['.', '!', '?']
trunateSuffix = "[…]",
endChars = [".", "!", "?"],
): string => {
// Remove HTML
const text = stripTags(html);
Expand All @@ -31,19 +31,19 @@ export const excerpt = (
const excerpt = text.substring(0, offsets.at(-1)! + 1);
// Ensure excerpt is not too short
if (maxLength - excerpt.length < maxLength * 0.2) {
return (excerpt + ' ' + trunateSuffix).trim();
return (excerpt + " " + trunateSuffix).trim();
}
}
// Fallback to using words
let excerpt = '';
let excerpt = "";
const words = text.split(/\s+/);
while (words.length) {
// Concatenate words until maximum length is reached
const word = words.shift()!;
if (excerpt.length + word.length > maxLength) {
break;
}
excerpt += word + ' ';
excerpt += word + " ";
}
return (excerpt + trunateSuffix).trim();
};
Loading

0 comments on commit 04ed27e

Please sign in to comment.