Skip to content

Commit

Permalink
feat(recommend): support escapeHTML in recommend widgets (#6199)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhayab committed May 21, 2024
1 parent d24870b commit 720e700
Show file tree
Hide file tree
Showing 21 changed files with 339 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('connectFrequentlyBoughtTogether', () => {
maxRecommendations: 10,
threshold: 95,
queryParameters: { userToken: 'token' },
escapeHTML: false,
});

// @ts-expect-error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
createDocumentationMessageGenerator,
checkRendering,
noop,
escapeHits,
TAG_PLACEHOLDER,
} from '../../lib/utils';

import type { Connector, TransformItems, Hit, BaseHit } from '../../types';
Expand Down Expand Up @@ -50,6 +52,13 @@ export type FrequentlyBoughtTogetherConnectorParams<
'page' | 'hitsPerPage' | 'offset' | 'length'
>;

/**
* Whether to escape HTML tags from items string values.
*
* @default true
*/
escapeHTML?: boolean;

/**
* Function to transform the items passed to the templates.
*/
Expand All @@ -75,6 +84,8 @@ const connectFrequentlyBoughtTogether: FrequentlyBoughtTogetherConnector =

return (widgetParams) => {
const {
// @MAJOR: this can default to false
escapeHTML = true,
transformItems = ((items) => items) as NonNullable<
FrequentlyBoughtTogetherConnectorParams['transformItems']
>,
Expand Down Expand Up @@ -123,6 +134,10 @@ const connectFrequentlyBoughtTogether: FrequentlyBoughtTogetherConnector =
return { items: [], widgetParams };
}

if (escapeHTML && results.hits.length > 0) {
results.hits = escapeHits(results.hits);
}

const transformedItems = transformItems(results.hits, {
results: results as RecommendResultItem,
});
Expand All @@ -142,7 +157,10 @@ const connectFrequentlyBoughtTogether: FrequentlyBoughtTogetherConnector =
objectID,
threshold,
maxRecommendations,
queryParameters,
queryParameters: {
...queryParameters,
...(escapeHTML ? TAG_PLACEHOLDER : {}),
},
$$id: this.$$id!,
}),
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
createDocumentationMessageGenerator,
checkRendering,
noop,
escapeHits,
TAG_PLACEHOLDER,
} from '../../lib/utils';

import type { Connector, TransformItems, Hit, BaseHit } from '../../types';
Expand Down Expand Up @@ -49,6 +51,12 @@ export type LookingSimilarConnectorParams<THit extends BaseHit = BaseHit> = {
PlainSearchParameters,
'page' | 'hitsPerPage' | 'offset' | 'length'
>;
/**
* Whether to escape HTML tags from items string values.
*
* @default true
*/
escapeHTML?: boolean;
/**
* Function to transform the items passed to the templates.
*/
Expand All @@ -71,6 +79,8 @@ const connectLookingSimilar: LookingSimilarConnector =

return function LookingSimilar(widgetParams) {
const {
// @MAJOR: this can default to false
escapeHTML = true,
objectIDs,
maxRecommendations,
threshold,
Expand Down Expand Up @@ -120,6 +130,10 @@ const connectLookingSimilar: LookingSimilarConnector =
return { items: [], widgetParams };
}

if (escapeHTML && results.hits.length > 0) {
results.hits = escapeHits(results.hits);
}

return {
items: transformItems(results.hits, {
results: results as RecommendResultItem,
Expand All @@ -140,8 +154,14 @@ const connectLookingSimilar: LookingSimilarConnector =
objectID,
maxRecommendations,
threshold,
fallbackParameters,
queryParameters,
fallbackParameters: {
...fallbackParameters,
...(escapeHTML ? TAG_PLACEHOLDER : {}),
},
queryParameters: {
...queryParameters,
...(escapeHTML ? TAG_PLACEHOLDER : {}),
},
$$id: this.$$id!,
}),
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
createDocumentationMessageGenerator,
checkRendering,
noop,
escapeHits,
TAG_PLACEHOLDER,
} from '../../lib/utils';

import type { Connector, TransformItems, Hit, BaseHit } from '../../types';
Expand Down Expand Up @@ -49,6 +51,12 @@ export type RelatedProductsConnectorParams<THit extends BaseHit = BaseHit> = {
PlainSearchParameters,
'page' | 'hitsPerPage' | 'offset' | 'length'
>;
/**
* Whether to escape HTML tags from items string values.
*
* @default true
*/
escapeHTML?: boolean;
/**
* Function to transform the items passed to the templates.
*/
Expand All @@ -72,6 +80,8 @@ const connectRelatedProducts: RelatedProductsConnector =

return function relatedProducts(widgetParams) {
const {
// @MAJOR: this can default to false
escapeHTML = true,
objectIDs,
maxRecommendations,
threshold,
Expand Down Expand Up @@ -121,6 +131,10 @@ const connectRelatedProducts: RelatedProductsConnector =
return { items: [], widgetParams };
}

if (escapeHTML && results.hits.length > 0) {
results.hits = escapeHits(results.hits);
}

return {
items: transformItems(results.hits, {
results: results as RecommendResultItem,
Expand All @@ -141,8 +155,14 @@ const connectRelatedProducts: RelatedProductsConnector =
objectID,
maxRecommendations,
threshold,
fallbackParameters,
queryParameters,
fallbackParameters: {
...fallbackParameters,
...(escapeHTML ? TAG_PLACEHOLDER : {}),
},
queryParameters: {
...queryParameters,
...(escapeHTML ? TAG_PLACEHOLDER : {}),
},
$$id: this.$$id!,
}),
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
createDocumentationMessageGenerator,
checkRendering,
noop,
escapeHits,
TAG_PLACEHOLDER,
} from '../../lib/utils';

import type { Connector, TransformItems, Hit, BaseHit } from '../../types';
Expand Down Expand Up @@ -57,6 +59,12 @@ export type TrendingItemsConnectorParams<THit extends BaseHit = BaseHit> = (
PlainSearchParameters,
'page' | 'hitsPerPage' | 'offset' | 'length'
>;
/**
* Whether to escape HTML tags from items string values.
*
* @default true
*/
escapeHTML?: boolean;
/**
* Function to transform the items passed to the templates.
*/
Expand Down Expand Up @@ -85,6 +93,8 @@ const connectTrendingItems: TrendingItemsConnector =
threshold,
fallbackParameters,
queryParameters,
// @MAJOR: this can default to false
escapeHTML = true,
transformItems = ((items) => items) as NonNullable<
TrendingItemsConnectorParams['transformItems']
>,
Expand Down Expand Up @@ -125,6 +135,10 @@ const connectTrendingItems: TrendingItemsConnector =
return { items: [], widgetParams };
}

if (escapeHTML && results.hits.length > 0) {
results.hits = escapeHits(results.hits);
}

return {
items: transformItems(results.hits, {
results: results as RecommendResultItem,
Expand All @@ -144,8 +158,14 @@ const connectTrendingItems: TrendingItemsConnector =
facetValue,
maxRecommendations,
threshold,
fallbackParameters,
queryParameters,
fallbackParameters: {
...fallbackParameters,
...(escapeHTML ? TAG_PLACEHOLDER : {}),
},
queryParameters: {
...queryParameters,
...(escapeHTML ? TAG_PLACEHOLDER : {}),
},
$$id: this.$$id!,
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ const frequentlyBoughtTogether: FrequentlyBoughtTogetherWidget =
maxRecommendations,
queryParameters,
threshold,
escapeHTML,
transformItems,
templates = {},
cssClasses = {},
Expand Down Expand Up @@ -205,6 +206,7 @@ const frequentlyBoughtTogether: FrequentlyBoughtTogetherWidget =
maxRecommendations,
queryParameters,
threshold,
escapeHTML,
transformItems,
}),
$$widgetType: 'ais.frequentlyBoughtTogether',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ const lookingSimilar: LookingSimilarWidget = function lookingSimilar(
queryParameters,
fallbackParameters,
threshold,
escapeHTML,
transformItems,
templates = {},
cssClasses = {},
Expand Down Expand Up @@ -203,6 +204,7 @@ const lookingSimilar: LookingSimilarWidget = function lookingSimilar(
queryParameters,
fallbackParameters,
threshold,
escapeHTML,
transformItems,
}),
$$widgetType: 'ais.lookingSimilar',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ const relatedProducts: RelatedProductsWidget = function relatedProducts(
queryParameters,
fallbackParameters,
threshold,
escapeHTML,
transformItems,
templates = {},
cssClasses = {},
Expand Down Expand Up @@ -215,6 +216,7 @@ const relatedProducts: RelatedProductsWidget = function relatedProducts(
queryParameters,
fallbackParameters,
threshold,
escapeHTML,
transformItems,
}),
$$widgetType: 'ais.relatedProducts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ const trendingItems: TrendingItemsWidget = function trendingItems(
queryParameters,
fallbackParameters,
threshold,
escapeHTML,
transformItems,
templates = {},
cssClasses = {},
Expand Down Expand Up @@ -217,6 +218,7 @@ const trendingItems: TrendingItemsWidget = function trendingItems(
queryParameters,
fallbackParameters,
threshold,
escapeHTML,
transformItems,
}),
$$widgetType: 'ais.trendingItems',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function FrequentlyBoughtTogether<THit extends BaseHit = BaseHit>({
maxRecommendations,
threshold,
queryParameters,
escapeHTML,
transformItems,
itemComponent,
headerComponent,
Expand All @@ -56,6 +57,7 @@ export function FrequentlyBoughtTogether<THit extends BaseHit = BaseHit>({
maxRecommendations,
threshold,
queryParameters,
escapeHTML,
transformItems,
},
{ $$widgetType: 'ais.frequentlyBoughtTogether' }
Expand Down
2 changes: 2 additions & 0 deletions packages/react-instantsearch/src/widgets/LookingSimilar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function LookingSimilar<THit extends BaseHit = BaseHit>({
threshold,
queryParameters,
fallbackParameters,
escapeHTML,
transformItems,
itemComponent,
headerComponent,
Expand All @@ -54,6 +55,7 @@ export function LookingSimilar<THit extends BaseHit = BaseHit>({
threshold,
queryParameters,
fallbackParameters,
escapeHTML,
transformItems,
},
{ $$widgetType: 'ais.lookingSimilar' }
Expand Down
2 changes: 2 additions & 0 deletions packages/react-instantsearch/src/widgets/RelatedProducts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function RelatedProducts<TItem extends BaseHit = BaseHit>({
threshold,
fallbackParameters,
queryParameters,
escapeHTML,
transformItems,
itemComponent,
headerComponent,
Expand All @@ -54,6 +55,7 @@ export function RelatedProducts<TItem extends BaseHit = BaseHit>({
threshold,
fallbackParameters,
queryParameters,
escapeHTML,
transformItems,
},
{ $$widgetType: 'ais.relatedProducts' }
Expand Down
2 changes: 2 additions & 0 deletions packages/react-instantsearch/src/widgets/TrendingItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function TrendingItems<TItem extends BaseHit = BaseHit>({
threshold,
fallbackParameters,
queryParameters,
escapeHTML,
transformItems,
itemComponent,
headerComponent,
Expand All @@ -58,6 +59,7 @@ export function TrendingItems<TItem extends BaseHit = BaseHit>({
threshold,
fallbackParameters,
queryParameters,
escapeHTML,
transformItems,
},
{ $$widgetType: 'ais.trendingItems' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function createOptionsTests(
maxRecommendations: 2,
threshold: 3,
queryParameters: { analytics: true },
escapeHTML: false,
},
};

Expand Down
1 change: 1 addition & 0 deletions tests/common/connectors/looking-similar/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function createOptionsTests(
threshold: 3,
fallbackParameters: { facetFilters: ['test1'] },
queryParameters: { analytics: true },
escapeHTML: false,
},
};

Expand Down
1 change: 1 addition & 0 deletions tests/common/connectors/related-products/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function createOptionsTests(
threshold: 3,
fallbackParameters: { facetFilters: ['test1'] },
queryParameters: { analytics: true },
escapeHTML: false,
},
};

Expand Down
1 change: 1 addition & 0 deletions tests/common/connectors/trending-items/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function createOptionsTests(
threshold: 3,
fallbackParameters: { facetFilters: ['test1'] },
queryParameters: { analytics: true },
escapeHTML: false,
},
};

Expand Down

0 comments on commit 720e700

Please sign in to comment.