Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

### Changes

- Utils: The tailwind/trash class checks are now named predicates (`isTailwindClass`, `isTrashClass`) shared
by the HTML snapshots and the diff's container-class filter, replacing three copies of the same inline
lambda.
- Config: `dynamicPageRegex` now extends the built-in dynamic-segment heuristics (numeric, UUID,
ULID, hex) instead of replacing them. Previously, setting a custom pattern silently disabled
every built-in match on any segment the custom pattern didn't also cover.
Expand Down
8 changes: 3 additions & 5 deletions src/utils/html-diff.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parse, serialize } from 'parse5';
import type * as parse5TreeAdapter from 'parse5/lib/tree-adapters/default';
import type { HtmlConfig } from '../config.ts';
import { TAILWIND_CLASS_PATTERNS, TRASH_HTML_CLASSES, minifyHtml } from './html.ts';
import { isTailwindClass, isTrashClass, minifyHtml } from './html.ts';
import { isDynamicId, isGenericClass } from './xpath.ts';

export interface HtmlDiffPart {
Expand Down Expand Up @@ -467,15 +467,13 @@ const directTextDiffer = (current: ElementNode, previous: ElementNode): boolean
return false;
};

const trashHtmlClasses = TRASH_HTML_CLASSES;

function filterContainerClasses(classes: string[]): string[] {
return classes
.filter((cls) => !/\d/.test(cls))
.filter((cls) => !isGenericClass(cls))
.filter((cls) => !trashHtmlClasses.test(cls))
.filter((cls) => !isTrashClass(cls))
.filter((cls) => !/(:|__)/.test(cls))
.filter((cls) => !TAILWIND_CLASS_PATTERNS.some((pattern) => pattern.test(cls)));
.filter((cls) => !isTailwindClass(cls));
}

function buildContainerSelector(element: ElementNode, allElements: NodeMap): string | null {
Expand Down
10 changes: 6 additions & 4 deletions src/utils/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ export const TAILWIND_CLASS_PATTERNS: RegExp[] = [
/^prose$/i,
];

export const isTailwindClass = (cls: string): boolean => TAILWIND_CLASS_PATTERNS.some((pattern) => pattern.test(cls));
export const isTrashClass = (cls: string): boolean => TRASH_HTML_CLASSES.test(cls);

const NON_SEMANTIC_TAGS = new Set([
'style',
'script',
Expand Down Expand Up @@ -758,7 +761,6 @@ export function sanitizeHtmlString(html: string, htmlConfig?: HtmlConfig): strin
*/
export function htmlMinimalUISnapshot(html: string, htmlConfig?: HtmlConfig['minimal']) {
const document = parse(html);
const trashHtmlClasses = TRASH_HTML_CLASSES;
const removeElements = ['path', 'script'];

function isFilteredOut(node) {
Expand Down Expand Up @@ -867,9 +869,9 @@ export function htmlMinimalUISnapshot(html: string, htmlConfig?: HtmlConfig['min
attr.value = value
.split(' ')
.filter((className) => !/\d/.test(className))
.filter((className) => !className.match(trashHtmlClasses))
.filter((className) => !isTrashClass(className))
.filter((className) => !className.match(/(:|__)/))
.filter((className) => !TAILWIND_CLASS_PATTERNS.some((pattern) => pattern.test(className)))
.filter((className) => !isTailwindClass(className))
.join(' ');
if (attr.value === '') return false;
}
Expand Down Expand Up @@ -1451,7 +1453,7 @@ function cleanElement(element: parse5TreeAdapter.Element): void {
.filter(Boolean)
.filter((className) => !/\d/.test(className))
.filter((className) => !className.includes(':'))
.filter((className) => !TAILWIND_CLASS_PATTERNS.some((pattern) => pattern.test(className)))
.filter((className) => !isTailwindClass(className))
.join(' ');

if (!attr.value) {
Expand Down
Loading