Skip to content

Commit fa6f4be

Browse files
authored
Merge pull request #63 from liip/feat/improve-button-block
feat(): Add linkTarget and rel to button block
2 parents 1a9e4e6 + a198da8 commit fa6f4be

33 files changed

+850
-464
lines changed

.wp-env.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"core": "WordPress/WordPress#5.5",
2+
"core": "WordPress/WordPress#5.5.3",
33
"plugins": [ "." ],
44
"mappings": {
55
"wp-content/plugins/wp-bootstrap-blocks-test-plugins": "./e2e-tests/plugins"

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Bootstrap Gutenberg Blocks for WordPress. Supports Bootstrap 4 and **Bootstrap 5
4141
#### Options
4242

4343
* Style: Choose the styling of the button.
44+
* Open in new tab: Choose if link should be opened in a new tab.
45+
* Rel: Set rel attribute of the link.
4446
* Alignment: Horizontal alignment of the button.
4547

4648
## Further Information
@@ -368,6 +370,10 @@ Modify default attributes of the button block.
368370

369371
* `$default_attributes` (`array`) Default attributes of button block.
370372
* `url` (`string`) Default url of the button (Default: `''`)
373+
* `linkTarget` (`string`) Default link target (Default: `''`). Possible values:
374+
* `''`: Target attribute empty
375+
* `_blank`: Target attribute is set to `_blank`
376+
* `rel` (`string`) Default rel attribute of the link (Default: `''`)
371377
* `text` (`string`) Default text of the button (Default: `''`)
372378
* `style` (`string`) Default style of the button (Default: `''`)
373379
* `alignment` (`string`) Default alignment of the button (Default: `''`)
@@ -379,6 +385,8 @@ add_filter( 'wp_bootstrap_blocks_button_default_attributes', 'my_button_default_
379385

380386
function my_button_default_attributes( $default_attributes ) {
381387
$default_attributes['url'] = 'https://getbootstrap.com/';
388+
$default_attributes['linkTarget'] = '_blank';
389+
$default_attributes['rel'] = 'noreferrer noopener';
382390
$default_attributes['text'] = 'Bootstrap';
383391
$default_attributes['style'] = 'secondary';
384392
$default_attributes['alignment'] = 'right';

build/index.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-polyfill'), 'version' => '0c2c0621be9f2289794f9955ae3690f9');
1+
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-polyfill'), 'version' => '67aee816442ce60ef154e852f39f18fa');

build/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e-tests/button/__snapshots__/button-block.spec.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
exports[`button block Button block should be initialized with default attributes 1`] = `"<!-- wp:wp-bootstrap-blocks/button /-->"`;
44

5+
exports[`button block Should be possible to enable and disable open in new tab 1`] = `"<!-- wp:wp-bootstrap-blocks/button {\\"linkTarget\\":\\"_blank\\",\\"rel\\":\\"noreferrer noopener\\"} /-->"`;
6+
7+
exports[`button block Should be possible to enable and disable open in new tab 2`] = `"<!-- wp:wp-bootstrap-blocks/button /-->"`;
8+
59
exports[`button block Should be possible to select style 1`] = `"<!-- wp:wp-bootstrap-blocks/button {\\"style\\":\\"secondary\\"} /-->"`;
610

711
exports[`button block Should be possible to set link url and text 1`] = `"<!-- wp:wp-bootstrap-blocks/button {\\"text\\":\\"Liip\\"} /-->"`;
812

913
exports[`button block Should be possible to set link url and text 2`] = `"<!-- wp:wp-bootstrap-blocks/button {\\"url\\":\\"https://liip.ch\\",\\"text\\":\\"Liip\\"} /-->"`;
14+
15+
exports[`button block Should keep rel value if set when open in new tab is enabled or disabled 1`] = `"<!-- wp:wp-bootstrap-blocks/button {\\"linkTarget\\":\\"_blank\\",\\"rel\\":\\"custom rel value\\"} /-->"`;
16+
17+
exports[`button block Should keep rel value if set when open in new tab is enabled or disabled 2`] = `"<!-- wp:wp-bootstrap-blocks/button {\\"rel\\":\\"custom rel value\\"} /-->"`;

e2e-tests/button/button-block.spec.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ import {
77
createNewPost,
88
getEditedPostContent,
99
} from '@wordpress/e2e-test-utils';
10-
import { ensureSidebarOpened, selectOption } from '../helper';
10+
import {
11+
clickElementByText,
12+
ensureSidebarOpened,
13+
getTextControlValueByLabel,
14+
selectOption,
15+
setTextControlValueByLabel,
16+
} from '../helper';
1117
import { insertButtonBlock, selectButtonBlock } from './button-helper';
1218

19+
const NEW_TAB_REL_DEFAULT_VALUE = 'noreferrer noopener';
20+
1321
describe( 'button block', () => {
1422
beforeEach( async () => {
1523
await createNewPost();
@@ -72,4 +80,63 @@ describe( 'button block', () => {
7280
// Editor content should match snapshot
7381
expect( await getEditedPostContent() ).toMatchSnapshot();
7482
} );
83+
84+
it( 'Should be possible to enable and disable open in new tab', async () => {
85+
await insertButtonBlock();
86+
await selectButtonBlock();
87+
await ensureSidebarOpened();
88+
89+
// Enable open in new tab
90+
await clickElementByText( 'label', 'Open in new tab' );
91+
92+
// Check if default rel value is set
93+
expect( await getTextControlValueByLabel( 'Link rel' ) ).toMatch(
94+
NEW_TAB_REL_DEFAULT_VALUE
95+
);
96+
97+
// Editor content should match snapshot
98+
expect( await getEditedPostContent() ).toMatchSnapshot();
99+
100+
// Disable open in new tab
101+
await clickElementByText( 'label', 'Open in new tab' );
102+
103+
// Check if default rel value is removed
104+
expect( await getTextControlValueByLabel( 'Link rel' ) ).toMatch( '' );
105+
106+
// Editor content should match snapshot
107+
expect( await getEditedPostContent() ).toMatchSnapshot();
108+
} );
109+
110+
it( 'Should keep rel value if set when open in new tab is enabled or disabled', async () => {
111+
await insertButtonBlock();
112+
await selectButtonBlock();
113+
await ensureSidebarOpened();
114+
115+
const customRelValue = 'custom rel value';
116+
117+
// Enable no gutters option
118+
await setTextControlValueByLabel( 'Link rel', customRelValue );
119+
120+
// Enable open in new tab
121+
await clickElementByText( 'label', 'Open in new tab' );
122+
123+
// Check if rel value hasn't changed
124+
expect( await getTextControlValueByLabel( 'Link rel' ) ).toMatch(
125+
customRelValue
126+
);
127+
128+
// Editor content should match snapshot
129+
expect( await getEditedPostContent() ).toMatchSnapshot();
130+
131+
// Disable open in new tab
132+
await clickElementByText( 'label', 'Open in new tab' );
133+
134+
// Check if rel value hasn't changed
135+
expect( await getTextControlValueByLabel( 'Link rel' ) ).toMatch(
136+
customRelValue
137+
);
138+
139+
// Editor content should match snapshot
140+
expect( await getEditedPostContent() ).toMatchSnapshot();
141+
} );
75142
} );

e2e-tests/button/button-filters.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
getInputValueByLabel,
1414
getRichTextValueByLabel,
1515
getSelectedValueBySelectLabel,
16+
getTextControlValueByLabel,
17+
getToggleValueByLabel,
1618
selectOption,
1719
selectOptionIsAvailable,
1820
toolbarOptionIsActive,
@@ -76,6 +78,14 @@ describe( 'button block filters', () => {
7678
'https://liip.ch'
7779
);
7880

81+
// Open in new tab is enabled
82+
expect( await getToggleValueByLabel( 'Open in new tab' ) ).toBe( true );
83+
84+
// Rel should be set
85+
expect( await getTextControlValueByLabel( 'Link rel' ) ).toBe(
86+
'custom rel'
87+
);
88+
7989
// Check if attributes are set correctly
8090
expect( await getEditedPostContent() ).toMatchSnapshot();
8191
} );

e2e-tests/helper.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ export const getRichTextValueByLabel = async ( label ) => {
8383
);
8484
};
8585

86+
export const getTextControlValueByLabel = async ( label ) => {
87+
const [ inputEl ] = await page.$x(
88+
`//label[@class="components-base-control__label"][contains(text(),"${ label }")]/following-sibling::input[@class="components-text-control__input"]`
89+
);
90+
return await page.evaluate( ( el ) => el.value, inputEl );
91+
};
92+
93+
export const setTextControlValueByLabel = async ( label, value ) => {
94+
const [ inputEl ] = await page.$x(
95+
`//label[@class="components-base-control__label"][contains(text(),"${ label }")]/following-sibling::input[@class="components-text-control__input"]`
96+
);
97+
await inputEl.type( value );
98+
};
99+
86100
export const inputIsDisabledByLabel = async ( label ) => {
87101
return (
88102
( await page.$( `input[aria-label="${ label }"][disabled]` ) ) !== null
@@ -110,6 +124,13 @@ export const getCheckboxValueByLabel = async ( label ) => {
110124
return await page.evaluate( ( el ) => el.checked, inputEl );
111125
};
112126

127+
export const getToggleValueByLabel = async ( label ) => {
128+
const [ inputEl ] = await page.$x(
129+
`//label[@class="components-toggle-control__label"][contains(text(),"${ label }")]/preceding-sibling::span[contains(@class, "components-form-toggle")]/input`
130+
);
131+
return await page.evaluate( ( el ) => el.checked, inputEl );
132+
};
133+
113134
export const getSelectedValueBySelectLabel = async ( label ) => {
114135
const [ selectEl ] = await page.$x(
115136
`//label[@class="components-base-control__label"][contains(text(),"${ label }")]/following-sibling::select[@class="components-select-control__input"]`

e2e-tests/plugins/button-filters.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ function enqueue_button_filters_plugin_script() {
3333
*/
3434
function button_filters_default_attributes( $default_attributes ) {
3535
$default_attributes['url'] = 'https://liip.ch';
36+
$default_attributes['linkTarget'] = '_blank';
37+
$default_attributes['rel'] = 'custom rel';
3638
$default_attributes['text'] = 'Liip';
3739
$default_attributes['style'] = 'secondary';
3840
$default_attributes['alignment'] = 'center';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"translation-revision-date":"2020-10-18 20:43+0200","generator":"WP-CLI\/2.4.0","source":"build\/index.js","domain":"messages","locale_data":{"messages":{"":{"domain":"messages","lang":"de_CH","plural-forms":"nplurals=2; plural=(n != 1);"},"Button":["Button"],"Bootstrap Button":["Bootstrap Button"],"Bootstrap":["Bootstrap"],"Primary":["Primary"],"Secondary":["Secondary"],"Add text...":["Text hinzuf\u00fcgen..."],"Style":["Stil"],"Change button alignment":["\u00c4ndere die Positionierung des Buttons"],"Apply":["\u00dcbernehmen"],"Column":["Column"],"Bootstrap Column":["Bootstrap Column"],"None":["Deaktiviert"],"Small":["Klein"],"Medium":["Mittel"],"Large":["Gross"],"Column size":["Anzahl Spalten"],"Xs Column count":["Xs Anzahl Spalten"],"Xs equal-width":["Xs gleiche Breite (equal-width)"],"Sm Column count":["Sm Anzahl Spalten"],"Sm equal-width":["Sm gleiche Breite (equal-width)"],"Md Column count":["Md Anzahl Spalten"],"Md equal-width":["Md gleiche Breite (equal-width)"],"Lg Column count":["Lg Anzahl Spalten"],"Lg equal-width":["Lg gleiche Breite (equal-width)"],"Xl Column count":["Xl Anzahl Spalten"],"Xl equal-width":["Xl gleiche Breite (equal-width)"],"Xxl Column count":["Xxl Anzahl Spalten"],"Xxl equal-width":["Xxl gleiche Breite (equal-width)"],"Background color":["Hintergrundfarbe"],"Center content vertically in row":["Zeileninhalt vertikal zentrieren"],"This setting only applies if there is no vertical alignment set on the parent row block.":["Diese Einstellung kann nur verwendet werden, wenn auf dem umschliessenden Row-Block keine vertikale Positionierung gesetzt ist."],"Padding (inside column)":["Padding (innerhalb der Spalte)"],"Size":["Gr\u00f6sse"],"Container":["Container"],"Bootstrap Container":["Bootstrap Container"],"Xl":["Xl"],"Lg":["Lg"],"Md":["Md"],"Sm":["Sm"],"Xxl":["Xxl"],"No breakpoint selected":["Kein Breakpoint ausgew\u00e4hlt"],"Fluid":["Fluid"],"Fluid Breakpoint":["Fluid Breakpoint"],"Fluid breakpoints only work with Bootstrap v4.4+. The container will be 100% wide until the specified breakpoint is reached, after which max-widths for each of the higher breakpoints will be applied.":["Fluid Breakpoints werden erst ab Bootstrap v4.4+ unterst\u00fctzt. Wenn die Option aktiviert ist, nimmt der Container 100% der Breite ein bis zum gew\u00e4hlten Breakpoint. Ab diesem Breakpoint gilt die jeweilige maximale Breite (max-width) des Containers."],"Margin":["Margin"],"Margin After":["Margin unterhalb vom Block"],"Row":["Row"],"Bootstrap Row":["Bootstrap Row"],"2 Columns (1:1)":["2 Spalten (1:1)"],"2 Columns (1:2)":["2 Spalten (1:2)"],"2 Columns (2:1)":["2 Spalten (2:1)"],"3 Columns (1:1:1)":["3 Spalten (1:1:1)"],"Custom":["Benutzerdefiniert"],"Bootstrap Default":["Bootstrap Standardwert"],"Bootstrap Default (None)":["Bootstrap Standardwert (Kein Abstand)"],"Align columns left":["Spalten links positionieren"],"Align columns center":["Spalten zentrieren"],"Align columns right":["Spalten rechts positionieren"],"Align columns top":["Spalten oben positionieren"],"Align columns bottom":["Spalten unten positionieren"],"Editor: Display columns stacked":["Editor: Spalten untereinander darstellen"],"Displays stacked columns in editor to enhance readability of block content. This option is only used in the editor and won't affect the output of the row.":["Stellt die Spalten untereinander dar um die Lesbarkeit der Block-Inhalte zu verbessern. Diese Option wird lediglich f\u00fcr den Editor verwendet und ver\u00e4ndert die Ausgabe des Blocks nicht."],"Change layout":["Layout \u00e4ndern"],"Row options":["Zeilen Optionen"],"No Gutters":["Keine Abst\u00e4nde zwischen Spalten (No Gutters)"],"Horizontal Gutters":["Horizontale Abst\u00e4nde zwischen Spalten"],"Vertical Gutters":["Vertikale Abst\u00e4nde zwischen Spalten"],"Change horizontal alignment of columns":["\u00c4ndere die horizontale Positionierung der Spalten"],"Change vertical alignment of columns":["\u00c4ndere die vertikale Positionierung der Spalten"]}}}
1+
{"translation-revision-date":"2020-10-18 22:25+0200","generator":"WP-CLI\/2.4.0","source":"build\/index.js","domain":"messages","locale_data":{"messages":{"":{"domain":"messages","lang":"de_CH","plural-forms":"nplurals=2; plural=(n != 1);"},"Button":["Button"],"Bootstrap Button":["Bootstrap Button"],"Bootstrap":["Bootstrap"],"Primary":["Primary"],"Secondary":["Secondary"],"Add text...":["Text hinzuf\u00fcgen..."],"Style":["Stil"],"Link settings":["Link-Einstellungen"],"Open in new tab":["In einem neuen Tab \u00f6ffnen"],"Link rel":["Link rel"],"Change button alignment":["\u00c4ndere die Positionierung des Buttons"],"Apply":["\u00dcbernehmen"],"Column":["Column"],"Bootstrap Column":["Bootstrap Column"],"None":["Deaktiviert"],"Small":["Klein"],"Medium":["Mittel"],"Large":["Gross"],"Column size":["Anzahl Spalten"],"Xs Column count":["Xs Anzahl Spalten"],"Xs equal-width":["Xs gleiche Breite (equal-width)"],"Sm Column count":["Sm Anzahl Spalten"],"Sm equal-width":["Sm gleiche Breite (equal-width)"],"Md Column count":["Md Anzahl Spalten"],"Md equal-width":["Md gleiche Breite (equal-width)"],"Lg Column count":["Lg Anzahl Spalten"],"Lg equal-width":["Lg gleiche Breite (equal-width)"],"Xl Column count":["Xl Anzahl Spalten"],"Xl equal-width":["Xl gleiche Breite (equal-width)"],"Xxl Column count":["Xxl Anzahl Spalten"],"Xxl equal-width":["Xxl gleiche Breite (equal-width)"],"Background color":["Hintergrundfarbe"],"Center content vertically in row":["Zeileninhalt vertikal zentrieren"],"This setting only applies if there is no vertical alignment set on the parent row block.":["Diese Einstellung kann nur verwendet werden, wenn auf dem umschliessenden Row-Block keine vertikale Positionierung gesetzt ist."],"Padding (inside column)":["Padding (innerhalb der Spalte)"],"Size":["Gr\u00f6sse"],"Container":["Container"],"Bootstrap Container":["Bootstrap Container"],"Xl":["Xl"],"Lg":["Lg"],"Md":["Md"],"Sm":["Sm"],"Xxl":["Xxl"],"No breakpoint selected":["Kein Breakpoint ausgew\u00e4hlt"],"Fluid":["Fluid"],"Fluid Breakpoint":["Fluid Breakpoint"],"Fluid breakpoints only work with Bootstrap v4.4+. The container will be 100% wide until the specified breakpoint is reached, after which max-widths for each of the higher breakpoints will be applied.":["Fluid Breakpoints werden erst ab Bootstrap v4.4+ unterst\u00fctzt. Wenn die Option aktiviert ist, nimmt der Container 100% der Breite ein bis zum gew\u00e4hlten Breakpoint. Ab diesem Breakpoint gilt die jeweilige maximale Breite (max-width) des Containers."],"Margin":["Margin"],"Margin After":["Margin unterhalb vom Block"],"Row":["Row"],"Bootstrap Row":["Bootstrap Row"],"2 Columns (1:1)":["2 Spalten (1:1)"],"2 Columns (1:2)":["2 Spalten (1:2)"],"2 Columns (2:1)":["2 Spalten (2:1)"],"3 Columns (1:1:1)":["3 Spalten (1:1:1)"],"Custom":["Benutzerdefiniert"],"Bootstrap Default":["Bootstrap Standardwert"],"Bootstrap Default (None)":["Bootstrap Standardwert (Kein Abstand)"],"Align columns left":["Spalten links positionieren"],"Align columns center":["Spalten zentrieren"],"Align columns right":["Spalten rechts positionieren"],"Align columns top":["Spalten oben positionieren"],"Align columns bottom":["Spalten unten positionieren"],"Editor: Display columns stacked":["Editor: Spalten untereinander darstellen"],"Displays stacked columns in editor to enhance readability of block content. This option is only used in the editor and won't affect the output of the row.":["Stellt die Spalten untereinander dar um die Lesbarkeit der Block-Inhalte zu verbessern. Diese Option wird lediglich f\u00fcr den Editor verwendet und ver\u00e4ndert die Ausgabe des Blocks nicht."],"Change layout":["Layout \u00e4ndern"],"Row options":["Zeilen Optionen"],"No Gutters":["Keine Abst\u00e4nde zwischen Spalten (No Gutters)"],"Horizontal Gutters":["Horizontale Abst\u00e4nde zwischen Spalten"],"Vertical Gutters":["Vertikale Abst\u00e4nde zwischen Spalten"],"Change horizontal alignment of columns":["\u00c4ndere die horizontale Positionierung der Spalten"],"Change vertical alignment of columns":["\u00c4ndere die vertikale Positionierung der Spalten"]}}}

0 commit comments

Comments
 (0)