Skip to content

Commit 59e5658

Browse files
committed
Pass block attributes as a data-attribute instead of through inline javascript
1 parent c6cb44a commit 59e5658

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

frontend/block/view.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import { BlockProps, renderBlock } from "../app";
22

3-
declare global {
4-
interface Window {
5-
ChatrixBlockConfig: {
6-
attributes: object,
7-
};
8-
}
9-
}
10-
113
window.addEventListener('DOMContentLoaded', () => {
124
renderAllBlocks().catch(error => {
135
console.error(error);
@@ -18,24 +10,42 @@ async function renderAllBlocks() {
1810
// See https://github.com/Automattic/chatrix/issues/161 for why we introduce a delay here.
1911
await introduceDelayInMilliseconds(1);
2012

21-
const config = window.ChatrixBlockConfig;
22-
if (!config) {
23-
throw "ChatrixBlockConfig is not defined";
24-
}
25-
26-
const props: BlockProps = {
27-
attributes: config.attributes,
28-
};
29-
3013
const containers = <HTMLCollectionOf<HTMLElement>>document.getElementsByClassName('wp-block-automattic-chatrix');
3114
for (const container of containers) {
15+
const config = getConfigFromDataElement(container);
16+
const props: BlockProps = {
17+
attributes: config.attributes,
18+
};
19+
3220
renderBlock(container, props);
3321

3422
// TODO: Support multiple blocks on the same page.
3523
break;
3624
}
3725
}
3826

27+
/**
28+
* The container element has a single <span> child that contains the config as a data-attribute.
29+
* This function parses that data-attribute into an object.
30+
*/
31+
function getConfigFromDataElement(container: HTMLElement): BlockConfig {
32+
const dataElement = container.getElementsByTagName('span').item(0);
33+
if (!dataElement) {
34+
throw "Data element for chatrix block was not found";
35+
}
36+
37+
const dataString = decodeURIComponent(dataElement.dataset?.chatrixBlockConfig ?? '');
38+
if (dataString === '') {
39+
throw "Data attribute for chatrix block was not found, or is empty";
40+
}
41+
42+
return JSON.parse(dataString);
43+
}
44+
45+
interface BlockConfig {
46+
attributes: object,
47+
}
48+
3949
async function introduceDelayInMilliseconds(delay: number) {
4050
return new Promise(resolve => setTimeout(resolve, delay));
4151
}

src/Block/block.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,22 @@ function () use ( $block_json_path ) {
2828
}
2929

3030
function render( array $attributes ): string {
31-
$handle = 'chatrix-block-config';
3231
$json_data = wp_json_encode(
3332
array(
3433
'attributes' => $attributes,
3534
)
3635
);
3736

38-
wp_register_script( $handle, '', array(), automattic_chatrix_version(), true );
39-
wp_add_inline_script( $handle, "window.ChatrixBlockConfig = $json_data;", 'before' );
40-
wp_enqueue_script( $handle );
41-
4237
ob_start();
4338
?>
4439
<div <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
45-
<?php // Iframe will be rendered here. ?>
40+
<?php // The following element will be replaced by the <Block> component. ?>
41+
<span style="display: none;" data-chatrix-block-config="<?php echo urlencode( $json_data ) ?>"></span>
4642
</div>
4743
<?php
4844
return ob_get_clean();
4945
}
5046

51-
5247
function parse_block_json( string $block_json_path ): array {
5348
// phpcs discourages file_get_contents for remote URLs, and recommends using wp_remote_get().
5449
// However, here we're dealing with a path to a file on disk, so we ignore phpcs's warning.

0 commit comments

Comments
 (0)