Skip to content

Commit

Permalink
fix: forwardBlockAttributes vs blockAttributes (#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio authored Aug 9, 2024
1 parent 0c735b7 commit fe250b7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-bikes-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@headstartwp/core": patch
---

Fix: only run parseBlockAttribute when forwardBlockAttribute is set for nodes that represent wp blocks
19 changes: 15 additions & 4 deletions packages/core/src/dom/parseBlockAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ export type ParsedBlock<T extends IDataWPBlock = IDataWPBlock> = {
className: string;
};

/**
* Checks whether the node is a WordPress block
*
* @param node DomNode
*
* @returns
*/
export function isWordPressBlock(node: Element) {
return (
typeof node.attribs['data-wp-block-name'] !== 'undefined' &&
typeof node.attribs['data-wp-block'] !== 'undefined'
);
}

/**
* Returns the block name and attributes
*
Expand All @@ -64,10 +78,7 @@ export function parseBlockAttributes(node?: Element): ParsedBlock {
throw new FrameworkError('You are calling `parseBlockAttributes` on a undefined node');
}

if (
typeof node.attribs['data-wp-block-name'] === 'undefined' &&
typeof node.attribs['data-wp-block'] === 'undefined'
) {
if (!isWordPressBlock(node)) {
warn(
'[parseBlockAttributes] You are using the `parseBlockAttributes` hook in a node that is not a block.',
);
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/react/components/BaseBlocksRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { HeadlessConfig } from '../../types';
import { warn } from '../../utils';
import { IBlockAttributes } from '../blocks/types';
import { getInlineStyles } from '../blocks/utils';
import { IDataWPBlock, parseBlockAttributes, ParsedBlock } from '../../dom/parseBlockAttributes';
import {
IDataWPBlock,
isWordPressBlock,
parseBlockAttributes,
ParsedBlock,
} from '../../dom/parseBlockAttributes';

const { default: parse, domToReact } = HtmlReactParser;

Expand Down Expand Up @@ -243,7 +248,7 @@ export function BaseBlocksRenderer({
style: style || undefined,
};

if (forwardBlockAttributes) {
if (forwardBlockAttributes && isWordPressBlock(domNode as Element)) {
const { attributes, name, className } = parseBlockAttributes(
domNode as Element,
);
Expand Down
17 changes: 16 additions & 1 deletion packages/core/src/react/components/__tests__/BlocksRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import { DOMNode, domToReact, Element } from 'html-react-parser';
import React, { ReactElement } from 'react';
import { isAnchorTag, isBlockByName } from '../../../dom';
Expand Down Expand Up @@ -242,6 +242,21 @@ describe('BlocksRenderer', () => {
expect(container).toMatchSnapshot();
});

it('does not forward blockProps to the component for nodes that are not wp blocks', () => {
const DivToP = ({ block }: BlockProps<{ blockAttribute: string }>) => {
return <p data-testid="block-no-props">{JSON.stringify(block)}</p>;
};

render(
<BlocksRenderer html={`<div class="my-class""></div>`} forwardBlockAttributes>
<DivToP tagName="div" classList="my-class" />
</BlocksRenderer>,
);

const node = screen.getByTestId('block-no-props');
expect(node.textContent).toBe('');
});

it('forward context to the component', () => {
const DivToP = ({
block,
Expand Down

0 comments on commit fe250b7

Please sign in to comment.