Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

feat(rendering): add setTextOnly duck #17

Merged
merged 6 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ The rendering duck controls how the server renders the HTML page sent to the cli
- [State Shape](#state-shape-5)
- [Action creators](#action-creators-5)
- [`setRenderPartialOnly`](#setrenderpartialonly)
- [`setRenderTextOnly`](#setrendertextOnly)
- [`setDangerouslyDisableScripts`](#setdangerouslydisablescripts)
- [`setDangerouslyDisableScriptsAndStyles`](#setdangerouslydisablescriptsandstyles)

Expand All @@ -521,6 +522,11 @@ const state = new Map({
disableStyles: Boolean, // Indicates if style tags should be omitted from HTML response
renderPartialOnly: Boolean, // Indicates if the response should return just the rendered HTML
// from the matched module rather than a complete HTML page
renderTextOnly: new Map({
setTextOnly: Boolean, // Indicates if HTML tags should be removed from the HTML response
htmlTagReplacement: String, // Replace html tags with the character passed to this option.
allowedHtmlTags: List, // List of HTML tags that should not be removed from the HTML response
}),
}),
// ...
});
Expand All @@ -544,6 +550,24 @@ import { setRenderPartialOnly } from '@americanexpress/one-app-ducks';
dispatch(setRenderPartialOnly(true));
```

##### `setRenderTextOnly`

Use this action creator to render text only from a holocron module, rather than a HTML.

| Argument | Type | Description |
|---|---|---|
| `setTextOnly` | `Boolean` | (required) set whether to return text instead of HTML |
10xLaCroixDrinker marked this conversation as resolved.
Show resolved Hide resolved
| `htmlTagReplacement` | `String` | Replace html tags with the character passed to this option i.e. '/n'. Defaults to empty string. |
| `allowedHtmlTags` | `Array` | Comma separated list of HTML tags that are allowed to remain in the text response i.e. `['a','<strong>']`. Defaults to empty array. |
10xLaCroixDrinker marked this conversation as resolved.
Show resolved Hide resolved

```js
import { setRenderTextOnly } from '@americanexpress/one-app-ducks';

// ...

dispatch(setRenderTextOnly(true, '\n', ['a']));
```

##### `setDangerouslyDisableScripts`

Use this action creator to disable scripts for being added to the rendered page.
Expand Down
11 changes: 11 additions & 0 deletions __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ Immutable.Map {
"disableStyles": false,
"disableScripts": false,
"renderPartialOnly": false,
"renderTextOnly": Immutable.Map {
"setTextOnly": false,
"htmlTagReplacement": "",
"allowedHtmlTags": Immutable.List [],
},
},
}
`;
Expand Down Expand Up @@ -64,6 +69,11 @@ Immutable.Map {
"disableStyles": false,
"disableScripts": false,
"renderPartialOnly": false,
"renderTextOnly": Immutable.Map {
"setTextOnly": false,
"htmlTagReplacement": "",
"allowedHtmlTags": Immutable.List [],
},
},
}
`;
Expand All @@ -85,6 +95,7 @@ Array [
"setDangerouslyDisableScripts",
"setDangerouslyDisableScriptsAndStyles",
"setRenderPartialOnly",
"setRenderTextOnly",
"default",
]
`;
61 changes: 60 additions & 1 deletion __tests__/rendering.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import reducer, {
SET_DANGEROUSLY_DISABLE_SCRIPTS,
SET_DANGEROUSLY_DISABLE_SCRIPTS_AND_STYLES,
SET_RENDER_PARTIAL_ONLY,
SET_RENDER_TEXT_ONLY,
initialState,
setDangerouslyDisableScripts,
setDangerouslyDisableScriptsAndStyles,
setRenderPartialOnly,
setRenderPartialOnly, setRenderTextOnly,
infoxicator marked this conversation as resolved.
Show resolved Hide resolved
} from '../src/rendering';

describe('rendering', () => {
Expand All @@ -34,6 +35,7 @@ describe('rendering', () => {
disableScripts: true,
disableStyles: false,
renderPartialOnly: false,
renderTextOnly: { setTextOnly: false, htmlTagReplacement: '', allowedHtmlTags: [] },
});
});

Expand All @@ -47,6 +49,7 @@ describe('rendering', () => {
disableScripts: true,
disableStyles: true,
renderPartialOnly: false,
renderTextOnly: { setTextOnly: false, htmlTagReplacement: '', allowedHtmlTags: [] },
});
});

Expand All @@ -60,6 +63,41 @@ describe('rendering', () => {
disableScripts: false,
disableStyles: false,
renderPartialOnly: true,
renderTextOnly: { setTextOnly: false, htmlTagReplacement: '', allowedHtmlTags: [] },
});
});

it('sets renderTextOnly flag on SET_RENDER_TEXT_ONLY type', () => {
const result = reducer(undefined, {
type: SET_RENDER_TEXT_ONLY,
setTextOnly: true,
htmlTagReplacement: '',
allowedHtmlTags: [],
});

expect(result.toJS()).toEqual({
disableScripts: false,
disableStyles: false,
renderPartialOnly: false,
renderTextOnly: { setTextOnly: true, htmlTagReplacement: '', allowedHtmlTags: [] },
});
});

it('sets renderTextOnly flag on SET_RENDER_TEXT_ONLY type and adds additional options', () => {
const htmlTagReplacement = '\n';
const allowedHtmlTags = ['<a>', '<p>'];
const result = reducer(undefined, {
type: SET_RENDER_TEXT_ONLY,
setTextOnly: true,
htmlTagReplacement,
allowedHtmlTags,
});

expect(result.toJS()).toEqual({
disableScripts: false,
disableStyles: false,
renderPartialOnly: false,
renderTextOnly: { setTextOnly: true, htmlTagReplacement, allowedHtmlTags },
});
});

Expand Down Expand Up @@ -105,5 +143,26 @@ describe('rendering', () => {
});
});
});

describe('setRenderTextOnly', () => {
it('returns action payload', () => {
const result = setRenderTextOnly(true, '\n', ['<a>', '<p>']);
expect(result).toEqual({
type: SET_RENDER_TEXT_ONLY,
setTextOnly: true,
htmlTagReplacement: '\n',
allowedHtmlTags: ['<a>', '<p>'],
});
});
it('returns action payload with default options for htmlTagReplacement and allowedHtmlTags', () => {
const result = setRenderTextOnly(true);
expect(result).toEqual({
type: SET_RENDER_TEXT_ONLY,
setTextOnly: true,
htmlTagReplacement: '',
allowedHtmlTags: [],
});
});
});
});
});
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import rendering, {
setDangerouslyDisableScripts,
setDangerouslyDisableScriptsAndStyles,
setRenderPartialOnly,
setRenderTextOnly,
} from './rendering';

export default {
Expand Down Expand Up @@ -64,4 +65,5 @@ export {
setDangerouslyDisableScripts,
setDangerouslyDisableScriptsAndStyles,
setRenderPartialOnly,
setRenderTextOnly,
};
18 changes: 16 additions & 2 deletions src/rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
* under the License.
*/

import { Map as iMap } from 'immutable';
import { fromJS } from 'immutable';
import typeScope from './utils/typeScope';

export const SET_DANGEROUSLY_DISABLE_SCRIPTS = `${typeScope}/render/SET_DANGEROUSLY_DISABLE_SCRIPTS`;
export const SET_DANGEROUSLY_DISABLE_SCRIPTS_AND_STYLES = `${typeScope}/render/SET_DANGEROUSLY_DISABLE_SCRIPTS_AND_STYLES`;
export const SET_RENDER_PARTIAL_ONLY = `${typeScope}/render/SET_RENDER_PARTIAL_ONLY`;
export const SET_RENDER_TEXT_ONLY = `${typeScope}/render/SET_RENDER_TEXT_ONLY`;

export const initialState = iMap({
export const initialState = fromJS({
disableStyles: false,
disableScripts: false,
renderPartialOnly: false,
renderTextOnly: { setTextOnly: false, htmlTagReplacement: '', allowedHtmlTags: [] },
});

export default function reducer(state = initialState, action) {
Expand All @@ -35,6 +37,11 @@ export default function reducer(state = initialState, action) {
.set('disableScripts', action.disableScriptsAndStyles);
case SET_RENDER_PARTIAL_ONLY:
return state.set('renderPartialOnly', action.renderPartialOnly);
case SET_RENDER_TEXT_ONLY:
return state.withMutations((newState) => newState
.setIn(['renderTextOnly', 'setTextOnly'], action.setTextOnly)
.setIn(['renderTextOnly', 'htmlTagReplacement'], action.htmlTagReplacement)
.setIn(['renderTextOnly', 'allowedHtmlTags'], action.allowedHtmlTags));
default:
return state;
}
Expand All @@ -54,3 +61,10 @@ export const setRenderPartialOnly = (renderPartialOnly) => ({
type: SET_RENDER_PARTIAL_ONLY,
renderPartialOnly,
});

export const setRenderTextOnly = (setTextOnly, htmlTagReplacement = '', allowedHtmlTags = []) => ({
type: SET_RENDER_TEXT_ONLY,
setTextOnly,
htmlTagReplacement,
allowedHtmlTags,
});
10xLaCroixDrinker marked this conversation as resolved.
Show resolved Hide resolved