Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore (toolbox-input): add sanitize method for input #95

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 18 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { type OlCounterType, OlCounterTypesMap } from './types/OlCounterType';
*/
import './styles/list.pcss';
import './styles/input.pcss';
import stripNumbers from './utils/stripNumbers';

/**
* Constructor Params for Nested List Tool, use to pass initial data and settings
Expand Down Expand Up @@ -74,6 +75,7 @@ export default class NestedList {

/**
* On paste sanitzation config. Allow only tags that are allowed in the Tool.
*
* @returns - paste config object used in editor
*/
public static get pasteConfig(): PasteConfig {
Expand All @@ -88,18 +90,20 @@ export default class NestedList {
public static get conversionConfig(): {
/**
* Method that is responsible for conversion from data to string
*
* @param data - current list data
* @returns - contents string formed from list data
*/
export: (data: ListData) => string;

/**
* Method that is responsible for conversion from string to data
*
* @param content - contents string
* @returns - list data formed from contents string
*/
import: (content: string, config: ToolConfig<NestedListConfig>) => ListData;
} {
} {
return {
export: (data) => {
return NestedList.joinRecursive(data);
Expand Down Expand Up @@ -128,6 +132,7 @@ export default class NestedList {

/**
* Set list style
*
* @param style - new style to set
*/
private set listStyle(style: ListDataStyle) {
Expand Down Expand Up @@ -187,6 +192,7 @@ export default class NestedList {

/**
* Render plugin`s main Element and fill it with saved data
*
* @param params - tool constructor options
* @param params.data - previously saved data
* @param params.config - user config for Tool
Expand Down Expand Up @@ -223,6 +229,7 @@ export default class NestedList {

/**
* Convert from list to text for conversionConfig
*
* @param data - current data of the list
* @returns - string of the recursively merged contents of the items of the list
*/
Expand All @@ -234,6 +241,7 @@ export default class NestedList {

/**
* Function that is responsible for content rendering
*
* @returns rendered list wrapper with all contents
*/
public render(): HTMLElement {
Expand All @@ -244,6 +252,7 @@ export default class NestedList {

/**
* Function that is responsible for content saving
*
* @returns formatted content used in editor
*/
public save(): ListData {
Expand All @@ -254,6 +263,7 @@ export default class NestedList {

/**
* Function that is responsible for mergind two lists into one
*
* @param data - data of the next standing list, that should be merged with current
*/
public merge(data: ListData): void {
Expand All @@ -262,6 +272,7 @@ export default class NestedList {

/**
* Creates Block Tune allowing to change the list style
*
* @returns array of tune configs
*/
public renderSettings(): MenuConfigItem[] {
Expand Down Expand Up @@ -297,15 +308,14 @@ export default class NestedList {

if (this.listStyle === 'ordered') {
const startWithElement = renderToolboxInput(
(index: number) => this.changeStartWith(index),
(index: string) => this.changeStartWith(Number(index)),
{
value: String(this.data.start ?? 1),
placeholder: '',
attributes: {
type: 'number',
step: '1',
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
required: 'true',
},
sanitize: input => stripNumbers(input),
});

const orderedListTunes: MenuConfigItem[] = [
Expand Down Expand Up @@ -334,7 +344,8 @@ export default class NestedList {
* For each counter type in OlCounterType create toolbox item
*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
OlCounterTypesMap.keys().forEach((counterType: string) => {

OlCounterTypesMap.forEach((_, counterType: string) => {
orderedListCountersTunes.children.items!.push({
title: this.api.i18n.t(counterType),
isActive: this.data.counterType === OlCounterTypesMap.get(counterType),
Expand All @@ -353,6 +364,7 @@ export default class NestedList {

/**
* Changes ordered list counterType property value
*
* @param counterType - new value of the counterType value
*/
private changeCounters(counterType: OlCounterType): void {
Expand All @@ -363,6 +375,7 @@ export default class NestedList {

/**
* Changes ordered list start property value
*
* @param index - new value of the start property
*/
private changeStartWith(index: number): void {
Expand Down
2 changes: 1 addition & 1 deletion src/styles/list.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

&::before {
counter-increment: item;
margin-right: 5px;

white-space: nowrap;
}
}
Expand Down
17 changes: 14 additions & 3 deletions src/utils/renderToolboxInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface InputOptions {
attributes?: {
[key: string]: string;
};
/**
* Flag that represents special behavior that prevents you from entering anything other than numbers
*/
sanitize?: (value: string) => string;
}

const css = {
Expand All @@ -36,8 +40,8 @@ const css = {
* @param inputOptions.attributes - html attributes, that would be added to the input element
* @returns - rendered html element
*/
export function renderToolboxInput(inputCallback: (index: number) => void,
{ value, placeholder, attributes }: InputOptions): HTMLElement {
export function renderToolboxInput(inputCallback: (index: string) => void,
{ value, placeholder, attributes, sanitize }: InputOptions): HTMLElement {
const startWithElementWrapper = Dom.make('div', css.wrapper);

const input = Dom.make('input', css.input, {
Expand All @@ -64,6 +68,13 @@ export function renderToolboxInput(inputCallback: (index: number) => void,
startWithElementWrapper.appendChild(input);

input.addEventListener('input', () => {
/**
* If input sanitizer specified, then sanitize input value
*/
if (sanitize !== undefined) {
input.value = sanitize(input.value);
}

const validInput = input.checkValidity();

/**
Expand All @@ -87,7 +98,7 @@ export function renderToolboxInput(inputCallback: (index: number) => void,
return;
}

inputCallback(Number(input.value));
inputCallback(input.value);
});

return startWithElementWrapper;
Expand Down
7 changes: 7 additions & 0 deletions src/utils/stripNumbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Removes everything except numbers in passed string
* @param input - string to be striped
*/
export default function stripNumbers(input: string): string {
return input.replace(/\D+/g, '');
}
Loading