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
15 changes: 13 additions & 2 deletions site/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ body {

header,
footer p:not(:last-child),
.options > div:first-child {
.options > div:not(:last-child) {
margin-bottom: 1rem;
}

Expand Down Expand Up @@ -103,16 +103,21 @@ button {

input[type='text'],
input[type='number'],
input[type='search'],
span.quiet {
display: block;
}

/* Set heights, as Safari draws search fields taller at narrow widths */
input[type='text'],
input[type='number'] {
input[type='number'],
input[type='search'] {
background: var(--color-field);
border: 1px solid var(--color-field-border);
border-radius: var(--radius);
box-sizing: border-box;
font-size: .875rem;
height: 1.75em;
margin: .2rem 0;
padding: .125rem .25rem;
}
Expand All @@ -133,6 +138,12 @@ input[type='checkbox'] {
vertical-align: middle;
}

/* Drops Safari’s own search field look */
input[type='search'] {
appearance: none;
width: 100%;
}

button {
background: var(--color-text);
border: 0;
Expand Down
33 changes: 33 additions & 0 deletions site/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Alpine from 'alpinejs';
import HTMLMinifier, { getPreset } from '../src/htmlminifier.js';
import { optionDefinitions } from '../src/lib/option-definitions.js';
import { paramCase } from '../src/lib/utils.js';
import { changedOptionIds, matchesFilter, optionFilterText } from './lib/filter.js';
import { getOptions } from './lib/get-options.js';
import { annotateInvisibles } from './lib/show-invisibles.js';
import { decodeState, encodeState } from './lib/url-state.js';
Expand Down Expand Up @@ -224,6 +225,8 @@ const defaultOptions = siteOptionIds.map(id => {
};
});

const optionFilterTexts = new Map(defaultOptions.map(option => [option.id, optionFilterText(option)]));

const sillyClone = (o) => JSON.parse(JSON.stringify(o));

const formatNumber = (num) => num.toLocaleString('en-US');
Expand Down Expand Up @@ -270,6 +273,9 @@ const updateUrlWithState = (input, options, showInvisibles) => {
// names because Alpine maps its shorthands to the default prefix before `prefix()` runs
const minifierData = () => ({
options: sillyClone(defaultOptions),
optionFilter: '',
// Counts the options a preset or selection changed while the filter hid them
presetNote: '',
input: '',
output: '',
showInvisibles: false,
Expand Down Expand Up @@ -332,6 +338,17 @@ const minifierData = () => ({
}
},

// Some browsers clear a search field (on Escape or with its clear button)
// without an `input` event, which the model would miss
bindOptionFilter: {
'data-x-on:keydown.escape'() {
this.optionFilter = '';
},
'data-x-on:search'(event) {
this.optionFilter = event.target.value;
}
},

bindOptionInput: {
'data-x-bind:disabled'() {
return this.option.disabled === true;
Expand Down Expand Up @@ -363,9 +380,13 @@ const minifierData = () => ({
bindPreset: {
'data-x-on:click.prevent'(event) {
const { preset } = event.currentTarget.dataset;
const before = this.options;
if (preset === 'all' || preset === 'none') this.selectAllOptions(preset === 'all');
else if (preset === 'reset') this.resetOptions();
else this.applyPreset(preset);
const hidden = changedOptionIds(before, this.options)
.filter(id => !matchesFilter(optionFilterTexts.get(id), this.optionFilter)).length;
this.presetNote = hidden ? `Also changed ${hidden} hidden option${hidden === 1 ? '' : 's'}` : '';
}
},

Expand Down Expand Up @@ -395,6 +416,17 @@ const minifierData = () => ({
return this.showInvisibles && !this.editingInput && this.input !== '';
},

// Empty without a filter, so that only a filtered list is announced
get optionFilterStatus() {
if (!this.optionFilter.trim()) return '';
const count = this.options.filter(option => this.showsOption(option)).length;
return count ? `${count} of ${this.options.length} options` : 'No options match';
},

showsOption(option) {
return matchesFilter(optionFilterTexts.get(option.id), this.optionFilter);
},

// Swap the marked input for the textarea, keeping size, scroll position, and a clicked caret
editInput(event) {
const { input: textarea, inputView: view } = this.$refs;
Expand Down Expand Up @@ -451,6 +483,7 @@ const minifierData = () => ({
}

this.$watch('options', () => this.scheduleMinify());
this.$watch('optionFilter', () => { this.presetNote = ''; });
},

// Debounced, as text options change with each keystroke
Expand Down
8 changes: 7 additions & 1 deletion site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ <h1><img src="/logo.svg" alt="" width="28" height="28"> HTML Minifier <span id="
</div>
</div>
<div class="options">
<div class="form-group">
<label for="option-filter">Filter options:</label>
<input type="search" id="option-filter" data-x-model="optionFilter" data-x-bind="bindOptionFilter">
<span class="quiet" role="status" data-x-text="optionFilterStatus"></span>
</div>
<div>
<template data-x-for="option in options">
<div>
<div data-x-show="showsOption(option)">
<template data-x-if="option.type === 'checkbox'">
<div class="form-group">
<input type="checkbox" data-x-model="option.checked" data-x-bind="bindOptionInput">
Expand Down Expand Up @@ -80,6 +85,7 @@ <h1><img src="/logo.svg" alt="" width="28" height="28"> HTML Minifier <span id="
<a href="#" data-preset="none" data-x-bind="bindPreset">none</a>,
<a href="#" data-preset="reset" data-x-bind="bindPreset">reset</a>
(<a href="https://github.com/j9t/html-minifier?tab=readme-ov-file#options-quick-reference" target="_blank">all options explained</a>)
<span class="quiet" role="status" data-x-text="presetNote"></span>
</div>
</div>
</main>
Expand Down
38 changes: 38 additions & 0 deletions site/lib/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { paramCase } from '../../src/lib/utils.js';

// Lowercased text to find an option by: its label, name, CLI flag, and description, without markup

/**
* @param {{id: string, label: string, helpText?: string}} option
* @returns {string}
*/
export const optionFilterText = ({ id, label, helpText = '' }) => [label, id, `--${paramCase(id)}`, helpText]
.join(' ')
.replace(/<[^>]*>/g, '')
Comment thread
j9t marked this conversation as resolved.
Dismissed
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&')
.toLowerCase();

// Whether text holds every word of a query, in any order (a blank query matches all)

/**
* @param {string} text
* @param {string} query
* @returns {boolean}
*/
export const matchesFilter = (text, query) => query
.toLowerCase()
.split(/\s+/)
.every(word => text.includes(word));

// IDs of the options whose choice differs between two lists of the same options, in the same order

/**
* @param {{id: string, checked?: boolean, value?: string | number}[]} before
* @param {{id: string, checked?: boolean, value?: string | number}[]} after
* @returns {string[]}
*/
export const changedOptionIds = (before, after) => after
.filter((option, i) => option.checked !== before[i].checked || String(option.value) !== String(before[i].value))
.map(option => option.id);
50 changes: 50 additions & 0 deletions site/test/page.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, test } from 'node:test';
import assert from 'node:assert';
import { existsSync, readFileSync } from 'node:fs';
import { changedOptionIds, matchesFilter, optionFilterText } from '../lib/filter.js';
import { getOptions } from '../lib/get-options.js';
import { annotateInvisibles } from '../lib/show-invisibles.js';
import { decodeState, encodeState } from '../lib/url-state.js';
Expand Down Expand Up @@ -138,6 +139,55 @@ describe('Web version', () => {
});
});

describe('`optionFilterText`/`matchesFilter`/`changedOptionIds`', () => {
const option = {
id: 'removeEmptyElements',
label: 'Remove empty elements',
helpText: 'Remove all elements with empty contents (e.g., <code>&lt;td&gt;</code>)'
};
const text = optionFilterText(option);

test('`optionFilterText` finds an option by its label, name, CLI flag, and description', () => {
for (const query of ['remove empty', 'removeEmptyElements', '--remove-empty-elements', 'contents']) {
assert.ok(matchesFilter(text, query), query);
}
});

test('`optionFilterText` reads the description as text, not markup', () => {
assert.ok(matchesFilter(text, '<td>'));
assert.ok(!matchesFilter(text, 'code'));
});

test('`matchesFilter` ignores case and requires every word, in any order', () => {
assert.ok(matchesFilter(text, 'ELEMENTS empty'));
assert.ok(!matchesFilter(text, 'empty attributes'));
});

test('`matchesFilter` matches anything with an empty or blank query', () => {
assert.ok(matchesFilter(text, ''));
assert.ok(matchesFilter(text, ' '));
});

test('`changedOptionIds` names the options whose choice changed', () => {
const before = [
{ id: 'collapseWhitespace', type: 'checkbox', checked: true },
{ id: 'removeComments', type: 'checkbox', checked: false },
{ id: 'maxLineLength', type: 'number', value: '' }
];
const after = [
{ ...before[0] },
{ ...before[1], checked: true },
{ ...before[2], value: '80' }
];
assert.deepStrictEqual(changedOptionIds(before, after), ['removeComments', 'maxLineLength']);
});

test('`changedOptionIds` takes a value of another type that reads the same as unchanged', () => {
const before = [{ id: 'maxLineLength', type: 'number', value: '80' }];
assert.deepStrictEqual(changedOptionIds(before, [{ ...before[0], value: 80 }]), []);
});
});

describe('`annotateInvisibles`', () => {
test('Escapes text that holds no invisible character, without marking it', () => {
assert.strictEqual(annotateInvisibles('<p>a & b</p>'), '&lt;p&gt;a &amp; b&lt;/p&gt;');
Expand Down
Loading