Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
feat(highlight): use helper from is.js instead of custom implementati…
Browse files Browse the repository at this point in the history
…on (#888)
  • Loading branch information
dhayab authored Jan 26, 2022
1 parent 9e0952c commit 33b7be3
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 151 deletions.
3 changes: 2 additions & 1 deletion ng-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"algoliasearch/lite": "algoliasearch",
"instantsearch.js/es": "instantsearch",
"instantsearch.js/es/connectors": "instantsearch.connectors",
"instantsearch.js/es/widgets/index/index": "instantsearch.widgets.index"
"instantsearch.js/es/helpers": "instantsearch.helpers",
"instantsearch.js/es/widgets/index/index": "instantsearch.widgets.index",
"nouislider": "noUiSlider",
"querystring-es3/encode": "qs.encode"
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
},
"dependencies": {
"instantsearch.css": "^7.3.1",
"instantsearch.js": "^4.31.1",
"instantsearch.js": "^4.37.3",
"nouislider": "^10.0.0",
"querystring-es3": "^0.2.1"
},
Expand Down
45 changes: 5 additions & 40 deletions src/highlight/__tests__/__snapshots__/highlight.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ exports[`highlight should highlight nested objects 1`] = `
<span
class="ais-Highlight"
>
<em
<mark
class="ais-Highlight-highlighted"
>
foo
</em>
</mark>
bar
</span>
</ais-highlight>
Expand All @@ -47,39 +47,18 @@ exports[`highlight should highlight strings 1`] = `
<span
class="ais-Highlight"
>
<em
class="ais-Highlight-highlighted"
>
foo
</em>
bar
</span>
</ais-highlight>
</test-component>
`;

exports[`highlight should highlight values in array 1`] = `
<test-component
attribute={[Function String]}
hit={[Function Object]}
tagName={[Function String]}
>
<ais-highlight>
<span
class="ais-Highlight"
>
<em
<mark
class="ais-Highlight-highlighted"
>
foo
</em>
</mark>
bar
</span>
</ais-highlight>
</test-component>
`;

exports[`highlight should use \`hit.highlighted\` if it exists 1`] = `
exports[`highlight should highlight with a custom tagName 1`] = `
<test-component
attribute={[Function String]}
hit={[Function Object]}
Expand All @@ -99,17 +78,3 @@ exports[`highlight should use \`hit.highlighted\` if it exists 1`] = `
</ais-highlight>
</test-component>
`;

exports[`highlight should warn when attribute is not found 1`] = `
<test-component
attribute={[Function String]}
hit={[Function Object]}
tagName={[Function String]}
>
<ais-highlight>
<span
class="ais-Highlight"
/>
</ais-highlight>
</test-component>
`;
46 changes: 8 additions & 38 deletions src/highlight/__tests__/highlight.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { TestBed } from '@angular/core/testing';

import { NgAisHighlightModule } from '../highlight.module';

const render = ({ hit, attribute, tagName = 'em' }) => {
const render = ({ hit, attribute, tagName = 'mark' }) => {
@Component({
selector: 'test-component',
template: `
<ais-highlight [attribute]="attribute" [hit]="hit">
<ais-highlight [attribute]="attribute" [hit]="hit" [tagName]="tagName">
</ais-highlight>
`,
})
Expand All @@ -34,7 +34,7 @@ describe('highlight', () => {
attribute: 'name',
hit: {
_highlightResult: {
name: { value: '<em>foo</em> bar' },
name: { value: '<mark>foo</mark> bar' },
},
},
});
Expand All @@ -47,47 +47,25 @@ describe('highlight', () => {
hit: {
_highlightResult: {
parent: {
name: { value: '<em>foo</em> bar' },
name: { value: '<mark>foo</mark> bar' },
},
},
},
});
expect(fixture).toMatchSnapshot();
});

it('should highlight values in array', () => {
it('should highlight with a custom tagName', () => {
const fixture = render({
attribute: 'children[0].name',
hit: {
_highlightResult: {
children: [
{
name: { value: '<em>foo</em> bar' },
},
],
},
},
});
expect(fixture).toMatchSnapshot();
});

it('should warn when attribute is not found', () => {
const spy = jest.spyOn(global.console, 'warn');
spy.mockImplementation(() => {});

const fixture = render({
attribute: 'invalid',
attribute: 'name',
hit: {
_highlightResult: {
name: { value: '<em>foo</em> bar' },
name: { value: '<mark>foo</mark> bar' },
},
},
tagName: 'em',
});
expect(spy).toHaveBeenCalled();
expect(fixture).toMatchSnapshot();

spy.mockReset();
spy.mockRestore();
});

it('should fallback to non highlighted when no match', () => {
Expand All @@ -97,12 +75,4 @@ describe('highlight', () => {
});
expect(fixture).toMatchSnapshot();
});

it('should use `hit.highlighted` if it exists', () => {
const fixture = render({
attribute: 'highlighted',
hit: { highlighted: '<em>foo</em> bar' },
});
expect(fixture).toMatchSnapshot();
});
});
67 changes: 22 additions & 45 deletions src/highlight/highlight.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,36 @@
import { Component, Input } from '@angular/core';
import { bem, getPropertyByPath } from '../utils';
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';

import { Hit } from 'instantsearch.js';

import { highlight } from 'instantsearch.js/es/helpers';
import { getPropertyByPath } from 'instantsearch.js/es/lib/utils';

@Component({
selector: 'ais-highlight',
template: `<span [class]="cx()" [innerHtml]="content"></span>`,
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<span class="ais-Highlight" [innerHtml]="content"></span>`,
})
export class NgAisHighlight {
@Input() attribute: string;
@Input() hit: { _highlightResult?: {}; label?: string; highlighted?: string };
@Input() tagName: string = 'em';

cx = bem('Highlight');
@Input() hit: Partial<Hit>;
@Input() tagName: string = 'mark';

get content() {
if (this.attribute === 'highlighted') {
return this.hit.highlighted
? this.replaceWithTagName(this.hit.highlighted)
: this.hit.label;
}

if (this.hit.hasOwnProperty('_highlightResult')) {
const attributeHighlighted = getPropertyByPath(
this.hit._highlightResult,
this.attribute
);

// check that the attributeHighlighted is a string
if (
attributeHighlighted !== undefined &&
typeof attributeHighlighted.value === 'string'
) {
return this.replaceWithTagName(attributeHighlighted.value);
}
}

const highlightAttributeResult = getPropertyByPath(
this.hit._highlightResult,
this.attribute
);
const fallback = getPropertyByPath(this.hit, this.attribute);
if (!fallback) {
console.warn(
`Could not find attribute [${
this.attribute
}] into hit object, will display an empty string.`
);

return '';
// @MAJOR drop this custom fallback once it is implemented directly in instantsearch.js v5
if (!highlightAttributeResult && fallback) {
return fallback;
}

return fallback;
}

replaceWithTagName(value: string) {
return value
.replace(
new RegExp('<em>', 'g'),
`<${this.tagName} class="${this.cx('highlighted')}">`
)
.replace(new RegExp('</em>', 'g'), `</${this.tagName}>`);
return highlight({
attribute: this.attribute,
highlightedTagName: this.tagName,
hit: this.hit,
});
}
}
32 changes: 28 additions & 4 deletions src/refinement-list/__tests__/refinement-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,34 @@ const defaultState = {
createURL: jest.fn(),
isShowingMore: false,
items: [
{ label: 'foo', count: 100, value: 'foo', isRefined: false },
{ label: 'bar', count: 100, value: 'bar', isRefined: false },
{ label: 'foobar', count: 100, value: 'foobar', isRefined: false },
{ label: 'barfoo', count: 100, value: 'barfoo', isRefined: false },
{
label: 'foo',
highlighted: 'foo',
count: 100,
value: 'foo',
isRefined: false,
},
{
label: 'bar',
highlighted: 'bar',
count: 100,
value: 'bar',
isRefined: false,
},
{
label: 'foobar',
highlighted: 'foobar',
count: 100,
value: 'foobar',
isRefined: false,
},
{
label: 'barfoo',
highlighted: 'barfoo',
count: 100,
value: 'barfoo',
isRefined: false,
},
],
refine: jest.fn(),
toggleShowMore: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"allowJs": true,
"outDir": "../out-tsc/spec",
"module": "CommonJS",
"types": ["jest"],
"types": ["node", "jest"],
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
},
Expand Down
Loading

0 comments on commit 33b7be3

Please sign in to comment.