Skip to content

Commit 3a4e4fe

Browse files
committed
RAP-1695 - Added customer type id, renamed value functions, render htmlspecialchars
1 parent ad4d8c4 commit 3a4e4fe

File tree

7 files changed

+29
-36
lines changed

7 files changed

+29
-36
lines changed

resources/js/filters.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ window.url = function (path = '') {
2222

2323
return (window.config.base_url || window.origin) + path
2424
}
25+
26+
window.stripHtmlTags = function (html, safeTags = ['mark']) {
27+
safeTags = safeTags.map((tag) => tag.replace(/[^a-zA-Z0-9-]/g, '')).filter(Boolean);
28+
return html.replace(new RegExp('<(?!/?(?:' + safeTags.join('|') + ')>)(?:.|\n)*?>', 'gm'), '') // Safe tags are only allowed if they have NO attributes
29+
}
30+
31+
window.htmlDecode = function (input) {
32+
return new DOMParser().parseFromString(input, "text/html")?.documentElement?.textContent ?? input;
33+
}

resources/js/instantsearch.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { defineAsyncComponent } from 'vue'
22
import { addQuery } from './stores/useSearchHistory'
3-
import highlight from 'vue-instantsearch/vue3/es/src/components/Highlight.vue.js'
43

54
document.addEventListener('vue:loaded', function (event) {
65
const vue = event.detail.vue
@@ -17,7 +16,6 @@ document.addEventListener('vue:loaded', function (event) {
1716
'ais-configure',
1817
defineAsyncComponent(() => import('vue-instantsearch/vue3/es/src/components/Configure.js')),
1918
)
20-
vue.component('ais-highlight', highlight)
2119
vue.component(
2220
'ais-autocomplete',
2321
defineAsyncComponent(() => import('vue-instantsearch/vue3/es/src/components/Autocomplete.vue.js')),
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
@props(['attribute', 'item' => 'item', 'highlightTag' => 'mark'])
1+
@props(['attribute', 'item' => 'item'])
22

3-
@if (in_array($attribute, config('rapidez.searchkit.highlight_attributes')))
4-
<ais-highlight {{ $attributes->merge([
5-
'v-bind:hit' => $item,
6-
'attribute' => $attribute,
7-
'highlighted-tag-name' => $highlightTag,
8-
]) }}></ais-highlight>
9-
{{--
10-
TODO: This doesn't render the HTML correctly
11-
See: /gear/fitness-equipment.html
12-
--}}
13-
@else
14-
<span {{ $attributes }} v-text="{{ $item }}.{{ $attribute }}"></span>
15-
@endif
3+
<span
4+
{{ $attributes }}
5+
v-html="window.stripHtmlTags(window.htmlDecode({{ $item }}?._highlightResult?.{{ $attribute }}?.value || {{ $item }}.{{ $attribute }}))"
6+
></span>

resources/views/listing/partials/filter/select.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class="relative -mx-1 -mt-1"
2020
class="items-baseline flex text-base/5"
2121
:class="item.isRefined ? 'text' : 'text-muted hover:text'"
2222
>
23-
@{{ item.label }}
23+
<span v-html="window.stripHtmlTags(item.label)"></span>
2424
<span class="block ml-0.5 text-xs" data-testid="listing-filter-count">
2525
(@{{ item.count }})
2626
</span>

resources/views/listing/partials/filter/selected.blade.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ class="flex items-center gap-1 py-2 px-3 text-sm rounded-full bg hover:bg-emphas
4040
<template v-if="false"></template>
4141
@include('rapidez::listing.partials.filter.selected.boolean')
4242
@include('rapidez::listing.partials.filter.selected.swatch')
43-
<template v-else>
44-
@{{ refinement.label }}
45-
</template>
43+
<span v-html="window.stripHtmlTags(refinement.label)"></span>
4644
</a>
4745
</li>
4846
</template>

src/Models/AbstractAttribute.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ public function getTable()
3232
return $this->table;
3333
}
3434

35-
protected function value(): Attribute
35+
protected function rawValue(): Attribute
3636
{
3737
return Attribute::get(function ($value) {
38+
$value ??= $this->getAttribute('value');
3839
$class = config('rapidez.attribute-models')[$this->backend_model] ?? null;
3940

4041
if ($class) {
@@ -47,38 +48,36 @@ protected function value(): Attribute
4748
});
4849
}
4950

50-
// TODO: Maybe rename this? As this value is mostly used.
51-
// And maybe add the option value keys in the array?
52-
protected function transformedValue(): Attribute
51+
protected function value(): Attribute
5352
{
54-
return Attribute::get(function () {
53+
return Attribute::get(function ($value) {
5554
if ($this->frontend_input === 'select' || $this->frontend_input === 'multiselect') {
56-
if (is_iterable($this->value)) {
55+
if (is_iterable($value)) {
5756
return Arr::map(
58-
iterator_to_array($this->value),
57+
iterator_to_array($value),
5958
fn ($value) => $this->options[$value]?->value ?? $value,
6059
);
6160
}
6261

63-
return $this->options[$this->value]?->value ?? $this->value;
62+
return $this->options[$value]?->value ?? $value;
6463
}
6564

66-
return $this->value;
65+
return $value;
6766
});
6867
}
6968

7069
protected function label(): Attribute
7170
{
7271
return Attribute::get(function () {
73-
return is_iterable($this->transformedValue)
74-
? implode(', ', iterator_to_array($this->transformedValue))
75-
: $this->transformedValue;
72+
return is_iterable($this->value)
73+
? implode(', ', iterator_to_array($this->value))
74+
: $this->value;
7675
});
7776
}
7877

7978
protected function sortOrder(): Attribute
8079
{
81-
return Attribute::get(fn () => $this->options[$this->value]->sort_order ?? null);
80+
return Attribute::get(fn () => $this->options[$this->rawValue]->sort_order ?? null);
8281
}
8382

8483
public function __toString(): string

src/Models/EavAttribute.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ public static function getCachedCustomer()
3232
return Cache::memo()->rememberForever('customer_eav_attributes', function () {
3333
return EavAttribute::query()
3434
->leftJoin('customer_eav_attribute', 'customer_eav_attribute.attribute_id', '=', 'eav_attribute.attribute_id')
35-
// TODO: This also needs an entity type check or
36-
// it should just join instead of left join
37-
// but not sure if it's used currently?
35+
->where('entity_type_id', self::ENTITY_TYPE_CUSTOMER)
3836
->get();
3937
});
4038
}

0 commit comments

Comments
 (0)