Skip to content

Commit bba83ea

Browse files
authored
Merge pull request #78 from ecosia/nuxt-support
Add Nuxt SSR support
2 parents cefccd0 + bdeff33 commit bba83ea

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-safe-html",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "A Vue directive which renders sanitised HTML dynamically",
55
"main": "dist/main.js",
66
"repository": "[email protected]:ecosia/vue-safe-html.git",

src/directive.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ export { defaultTags as allowedTags };
2424
export default (tags) => {
2525
const initialTags = areTagsValid(tags) ? tags : defaultTags;
2626
return (el, binding) => {
27-
const directiveTags = Object.keys(binding.modifiers);
28-
const finalTags = directiveTags.length > 0 && areTagsValid(directiveTags) ?
29-
directiveTags :
30-
initialTags;
27+
let finalTags = initialTags;
28+
29+
if (binding.modifiers) {
30+
const directiveTags = Object.keys(binding.modifiers);
31+
if (directiveTags.length > 0 && areTagsValid(directiveTags)) {
32+
finalTags = directiveTags;
33+
}
34+
}
35+
3136
const sanitized = sanitizeHTML(binding.value, finalTags);
3237

3338
if (typeof el.innerHTML === 'string') {

src/directive.test.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@ describe('Directive', () => {
66
});
77

88
describe('Sanitizes', () => {
9-
const binding = {
10-
modifiers: {},
11-
value: '<p><strong>Safe</strong> HTML<script></script></p>',
12-
};
139
const expected = '<strong>Safe</strong> HTML';
1410
const directive = createDirective();
1511

1612
it('Client-side', () => {
13+
const binding = {
14+
modifiers: {},
15+
value: '<p><strong>Safe</strong> HTML<script></script></p>',
16+
};
1717
const el = document.createElement('div');
1818
directive(el, binding);
1919
expect(el.innerHTML).toBe(expected);
2020
});
2121

2222
it('Server-side', () => {
23+
// example bindings from Nuxt 2
24+
const binding = {
25+
name: 'safe-html',
26+
rawName: 'v-safe-html',
27+
value: '<p><strong>Safe</strong> HTML<script></script></p>',
28+
expression: 'paragraph',
29+
};
2330
const el = { data: {} };
2431
directive(el, binding);
2532
expect(el.data.domProps).toEqual({ innerHTML: expected });

0 commit comments

Comments
 (0)