Skip to content

Commit 9d78132

Browse files
committed
Allow disabling EL / show sanitization
1 parent a473d9f commit 9d78132

9 files changed

Lines changed: 29 additions & 6 deletions

File tree

resources/js/entity-list/components/EntityList.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@
783783
<template v-if="field.html && typeof item[field.key] === 'string'">
784784
<Content class="break-words [&_a]:relative [&_a]:z-10"
785785
:class="{ '[&_a]:pointer-events-none': selecting || reordering }"
786-
:html="sanitize(item[field.key])"
786+
:html="field.sanitize ? sanitize(item[field.key]) : item[field.key]"
787787
/>
788788
</template>
789789
<template v-else>

resources/js/show/components/fields/text/Text.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import { useParentShow } from "@/show/useParentShow";
1111
import { ContentUploadManager } from "@/content/ContentUploadManager";
1212
import { Button } from "@/components/ui/button";
13+
import { sanitize } from "@/utils/sanitize";
1314
1415
const props = defineProps<ShowFieldProps<ShowTextFieldData>>();
1516
@@ -74,7 +75,7 @@
7475
<template v-if="currentContent && field.html">
7576
<TextRenderer
7677
class="content content-sm text-sm [:where(&)_:where(h1,h2,h3)]:text-foreground/75"
77-
:content="currentContent"
78+
:content="field.sanitize ? sanitize(currentContent) : currentContent"
7879
:field="field"
7980
/>
8081
</template>

resources/js/show/components/fields/text/TextRenderer.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import { ShowTextFieldData } from "@/types";
66
import Embed from "@/show/components/fields/text/nodes/Embed.vue";
77
import { components } from '@/components/TemplateRenderer.vue';
8-
import { sanitize } from "@/utils/sanitize";
98
109
const props = defineProps<{
1110
field: ShowTextFieldData,
@@ -14,7 +13,7 @@
1413
1514
const formattedContent = computed(() => {
1615
const dom = document.createElement('template');
17-
dom.innerHTML = sanitize(props.content);
16+
dom.innerHTML = props.content;
1817
dom.content.querySelectorAll('[data-html-content]').forEach(htmlNode => {
1918
const component = document.createElement('html-content');
2019
component.setAttribute('content', htmlNode.innerHTML.trim());

resources/js/types/generated.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export type EntityListFieldData = {
184184
width: string | null;
185185
hideOnXS: boolean;
186186
html: boolean | null;
187+
sanitize: boolean | null;
187188
tooltip: string | null;
188189
};
189190
export type EntityListItemMeta = {
@@ -881,6 +882,7 @@ export type ShowTextFieldData = {
881882
type: "text";
882883
emptyVisible: boolean;
883884
html: boolean;
885+
sanitize: boolean;
884886
localized: boolean | null;
885887
collapseToWordCount: number | null;
886888
embeds: { [key: string]: EmbedData } | null;

src/Data/EntityList/EntityListFieldData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function __construct(
1919
public ?string $width,
2020
public bool $hideOnXS,
2121
public ?bool $html = null,
22+
public ?bool $sanitize = null,
2223
public ?string $tooltip = null,
2324
) {}
2425
}

src/Data/Show/Fields/ShowTextFieldData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function __construct(
2424
public ShowFieldType $type,
2525
public bool $emptyVisible,
2626
public bool $html,
27+
public bool $sanitize,
2728
public ?bool $localized = null,
2829
public ?int $collapseToWordCount = null,
2930
/** @var array<string, EmbedData> */

src/EntityList/Fields/EntityListField.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22

33
namespace Code16\Sharp\EntityList\Fields;
44

5+
use Code16\Sharp\Utils\Sanitization\IsSharpFieldWithHtmlSanitization;
6+
use Code16\Sharp\Utils\Sanitization\SharpFieldWithHtmlSanitization;
57
use Illuminate\Contracts\Support\Arrayable;
68

7-
class EntityListField implements Arrayable, IsEntityListField
9+
class EntityListField implements Arrayable, IsEntityListField, IsSharpFieldWithHtmlSanitization
810
{
911
use HasCommonEntityListFieldAttributes;
12+
use SharpFieldWithHtmlSanitization;
1013

1114
protected bool $html = true;
1215

1316
private function __construct(string $key)
1417
{
1518
$this->key = $key;
19+
$this->sanitize = true;
1620
}
1721

1822
public static function make(string $key): self
@@ -51,6 +55,7 @@ public function toArray(): array
5155
'label' => $this->label,
5256
'sortable' => $this->sortable,
5357
'html' => $this->html,
58+
'sanitize' => $this->sanitize,
5459
'width' => $this->width,
5560
'hideOnXS' => $this->hideOnXs,
5661
];

src/Show/Fields/SharpShowTextField.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
namespace Code16\Sharp\Show\Fields;
44

5+
use Code16\Sharp\Show\Fields\Formatters\SharpShowFieldFormatter;
56
use Code16\Sharp\Show\Fields\Formatters\TextFormatter;
67
use Code16\Sharp\Utils\Fields\IsSharpFieldWithEmbeds;
78
use Code16\Sharp\Utils\Fields\IsSharpFieldWithLocalization;
89
use Code16\Sharp\Utils\Fields\SharpFieldWithEmbeds;
910
use Code16\Sharp\Utils\Fields\SharpFieldWithLocalization;
11+
use Code16\Sharp\Utils\Sanitization\IsSharpFieldWithHtmlSanitization;
12+
use Code16\Sharp\Utils\Sanitization\SharpFieldWithHtmlSanitization;
1013

11-
class SharpShowTextField extends SharpShowField implements IsSharpFieldWithEmbeds, IsSharpFieldWithLocalization
14+
class SharpShowTextField extends SharpShowField implements IsSharpFieldWithEmbeds, IsSharpFieldWithHtmlSanitization, IsSharpFieldWithLocalization
1215
{
1316
use SharpFieldWithEmbeds;
17+
use SharpFieldWithHtmlSanitization;
1418
use SharpFieldWithLocalization;
1519

1620
const FIELD_TYPE = 'text';
@@ -19,6 +23,12 @@ class SharpShowTextField extends SharpShowField implements IsSharpFieldWithEmbed
1923
protected ?int $collapseToWordCount = null;
2024
protected bool $html = true;
2125

26+
protected function __construct(string $key, string $type, ?SharpShowFieldFormatter $formatter = null)
27+
{
28+
parent::__construct($key, $type, $formatter);
29+
$this->sanitize = true;
30+
}
31+
2232
public static function make(string $key): SharpShowTextField
2333
{
2434
return new static($key, static::FIELD_TYPE, new TextFormatter());
@@ -57,6 +67,7 @@ public function toArray(): array
5767
return parent::buildArray([
5868
'label' => $this->label,
5969
'html' => $this->html,
70+
'sanitize' => $this->sanitize,
6071
'collapseToWordCount' => $this->collapseToWordCount,
6172
'localized' => $this->localized,
6273
'embeds' => $this->innerComponentEmbedsConfiguration(false) ?: null,

src/Utils/Sanitization/SharpFieldWithHtmlSanitization.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public function shouldSanitizeHtml(bool $sanitize = true): self
1313
return $this;
1414
}
1515

16+
/**
17+
* @internal
18+
*/
1619
public function isSanitizingHtml(): bool
1720
{
1821
return $this->sanitize;

0 commit comments

Comments
 (0)