Skip to content

Commit

Permalink
Merge pull request #78 from ecosia/nuxt-support
Browse files Browse the repository at this point in the history
Add Nuxt SSR support
  • Loading branch information
LostCrew authored Jun 22, 2022
2 parents cefccd0 + bdeff33 commit bba83ea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-safe-html",
"version": "2.0.0",
"version": "2.1.0",
"description": "A Vue directive which renders sanitised HTML dynamically",
"main": "dist/main.js",
"repository": "[email protected]:ecosia/vue-safe-html.git",
Expand Down
13 changes: 9 additions & 4 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ export { defaultTags as allowedTags };
export default (tags) => {
const initialTags = areTagsValid(tags) ? tags : defaultTags;
return (el, binding) => {
const directiveTags = Object.keys(binding.modifiers);
const finalTags = directiveTags.length > 0 && areTagsValid(directiveTags) ?
directiveTags :
initialTags;
let finalTags = initialTags;

if (binding.modifiers) {
const directiveTags = Object.keys(binding.modifiers);
if (directiveTags.length > 0 && areTagsValid(directiveTags)) {
finalTags = directiveTags;
}
}

const sanitized = sanitizeHTML(binding.value, finalTags);

if (typeof el.innerHTML === 'string') {
Expand Down
15 changes: 11 additions & 4 deletions src/directive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@ describe('Directive', () => {
});

describe('Sanitizes', () => {
const binding = {
modifiers: {},
value: '<p><strong>Safe</strong> HTML<script></script></p>',
};
const expected = '<strong>Safe</strong> HTML';
const directive = createDirective();

it('Client-side', () => {
const binding = {
modifiers: {},
value: '<p><strong>Safe</strong> HTML<script></script></p>',
};
const el = document.createElement('div');
directive(el, binding);
expect(el.innerHTML).toBe(expected);
});

it('Server-side', () => {
// example bindings from Nuxt 2
const binding = {
name: 'safe-html',
rawName: 'v-safe-html',
value: '<p><strong>Safe</strong> HTML<script></script></p>',
expression: 'paragraph',
};
const el = { data: {} };
directive(el, binding);
expect(el.data.domProps).toEqual({ innerHTML: expected });
Expand Down

0 comments on commit bba83ea

Please sign in to comment.