Skip to content

Commit

Permalink
feat: fix trimify and configs, and export trimify
Browse files Browse the repository at this point in the history
  • Loading branch information
j4w8n committed Nov 29, 2024
1 parent 76b53db commit b35a531
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* @type {import('types').DefaultConfig}
*/
export const CONFIG = {
ignore: {},
ignore: [],
strict: false,
tab_size: 2,
trim: {}
trim: []
}
1 change: 1 addition & 0 deletions src/exports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { closify } from '../closify.js'
export { entify } from '../entify.js'
export { minify } from '../minify.js'
export { prettify } from '../prettify.js'
export { trimify } from '../utils.js'
8 changes: 4 additions & 4 deletions src/prettify.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CONFIG } from './constants.js'
let strict

/**
* @type {Record<string, string>}
* @type {string[]}
*/
let trim

Expand Down Expand Up @@ -53,8 +53,8 @@ const enqueue = (html) => {
const preprocess = (html) => {
html = closify(html, false)

if (trim)
trimify(html, trim)
if (trim.length > 0)
html = trimify(html, trim)

html = minify(html, false)
html = enqueue(html)
Expand Down Expand Up @@ -156,7 +156,7 @@ export const prettify = (html, config) => {
const validated_config = config ? validateConfig(config) : CONFIG
strict = validated_config.strict

const ignore = Object.keys(validated_config.ignore).length > 0
const ignore = validated_config.ignore.length > 0
trim = validated_config.trim

/* Protect ignored elements. */
Expand Down
4 changes: 2 additions & 2 deletions src/types/internal.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export interface DefaultConfig {
ignore: Record<string, string>;
ignore: string[];
strict: boolean;
tab_size: number;
trim: Record<string, string>;
trim: string[];
}

type RecursiveRequired<T> = {
Expand Down
34 changes: 20 additions & 14 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@ export const isHtml = (content) => {
const mergeObjects = (current, updates) => {
if (!current || !updates)
throw new Error("Both 'current' and 'updates' must be passed-in to merge()")
if (typeof current !== 'object' || typeof updates !== 'object')
throw new Error("Both 'current' and 'updates' must be passed-in as objects to merge()")

let merged = { ...current }

for (let key of Object.keys(updates)) {
if (typeof updates[key] !== 'object') {
merged[key] = updates[key]
} else {
/* key is an object, run mergeObjects again. */
merged[key] = mergeObjects(merged[key] || {}, updates[key])
/**
* @type {any}
*/
let merged

if (Array.isArray(current)) {
merged = structuredClone(current).concat(updates)
} else if (typeof current === 'object') {
merged = { ...current }
for (let key of Object.keys(updates)) {
if (typeof updates[key] !== 'object') {
merged[key] = updates[key]
} else {
/* key is an object, run mergeObjects again. */
merged[key] = mergeObjects(merged[key] || {}, updates[key])
}
}
}

Expand All @@ -57,12 +63,12 @@ export const mergeConfig = (dconfig, config) => {
* Ignores elements by protecting or unprotecting their entities.
*
* @param {string} html
* @param {Record<string, string>} ignore
* @param {string[]} ignore
* @param {string} [mode]
* @returns {string}
*/
export const ignoreElement = (html, ignore, mode = 'protect') => {
for (let e = 0; e < Object.keys(ignore).length; e++) {
for (let e = 0; e < ignore.length; e++) {
const regex = new RegExp(`<${ignore[e]}[^>]*>((.|\n)*?)<\/${ignore[e]}>`, "g")
html = html.replace(regex, mode === 'protect' ? protectElement : unprotectElement)
}
Expand Down Expand Up @@ -92,11 +98,11 @@ const protectElement = (match, capture) => {
* Trim leading and trailing whitespace characters.
*
* @param {string} html
* @param {Record<string, string>} trim
* @param {string[]} trim
* @returns {string}
*/
export const trimify = (html, trim) => {
for (let e = 0; e < Object.keys(trim).length; e++) {
for (let e = 0; e < trim.length; e++) {
/* Whitespace character must be escaped with '\' or RegExp() won't include it. */
const leading_whitespace = new RegExp(`(<${trim[e]}[^>]*>)\\s+`, "g")
const trailing_whitespace = new RegExp(`\\s+(</${trim[e]}>)`, "g")
Expand Down
8 changes: 6 additions & 2 deletions tests/all.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ const testConfig = async (config) => {
}

test('Trimify', () => {
expect(trimify(trim_leading_whitespace, { '0': 'div' })).toBe('<div>Hello</div>')
expect(trimify(trim_trailing_whitespace, { '0': 'div' })).toBe('<div>Hello</div>')
expect(trimify(trim_leading_whitespace, ['div'])).toBe('<div>Hello</div>')
expect(trimify(trim_trailing_whitespace, ['div'])).toBe('<div>Hello</div>')
expect(prettify(entify_html, { trim: [ 'textarea' ]})).toBe(
`<textarea>Did&nbsp;&nbsp;&nbsp;you&nbsp;know&nbsp;that&nbsp;3&nbsp;&gt;&nbsp;&nbsp;&nbsp;2?&#10;&#10;This&nbsp;is&nbsp;another&nbsp;paragraph.</textarea>
<textarea class="more stuff"></textarea>`
)
})

test('Prettify', () => {
Expand Down
16 changes: 10 additions & 6 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ declare module 'htmlfy' {
trim?: string[];
}

export interface Config {
ignore: Record<string, string>;
strict: boolean;
tab_size: number;
trim: Record<string, string>;
}
export type Config = Required<UserConfig>

/**
* Ensure void elements are "self-closing".
Expand Down Expand Up @@ -55,4 +50,13 @@ declare module 'htmlfy' {
* @returns A well-formed HTML string.
*/
export function prettify(html: string, config?: UserConfig): string

/**
* Trim leading and trailing whitespace from the defined HTML elements.
*
* @param {string} html
* @param {string[]} trim
* @returns A trimmed string.
*/
export function trimify(html: string, trim: string[]): string
}

0 comments on commit b35a531

Please sign in to comment.