Socket
Socket
Sign inDemoInstall

html-converter-js

Package Overview
Dependencies
120
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.3 to 1.0.4

6

dist/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.htmlConverter = void 0;
const isomorphic_dompurify_1 = require("isomorphic-dompurify");
const htmlConverter = (str, element, dompurifyConfig) => {
var isomorphic_dompurify_1 = require("isomorphic-dompurify");
var htmlConverter = function (str, element, dompurifyConfig) {
if (typeof document === undefined)
throw new Error('This htmlConverter only works on client. Use htmlConverterReact for an isomorphic version of this function (if you are using react)');
let cleanData = isomorphic_dompurify_1.sanitize(str, dompurifyConfig || {});
var cleanData = isomorphic_dompurify_1.sanitize(str, dompurifyConfig || {});
if (typeof cleanData !== 'string')

@@ -10,0 +10,0 @@ throw new Error("The sanitized data isn't string. Don't provide any of the properties that change the return type of dompurify. Example DON'T PROVIDE: RETURN_DOM_IMPORT?: boolean; RETURN_TRUSTED_TYPE?: boolean; WHOLE_DOCUMENT?: boolean");

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _1 = require(".");
const dangerousTags = /(script)|(alert)/;
const htmlUseCases = ['<p>Test</p>', '<p>Test</p><b>Test-Bold</b>', '<div>test</div>'];
const htmlUseCasesDangerous = [
var _1 = require(".");
var dangerousTags = /(script)|(alert)/;
var htmlUseCases = ['<p>Test</p>', '<p>Test</p><b>Test-Bold</b>', '<div>test</div>'];
var htmlUseCasesDangerous = [
'<p onClick={alert("Im dangerous")}>Test</p>',

@@ -11,33 +11,33 @@ '<p><script></script></p>',

];
const stringUseCases = ['Test', 'TestTest-Bold', 'test'];
const stringUseCasesDangerous = ['Test<script>test</script>'];
const jsdom_1 = require("jsdom");
describe('htmlConverter', () => {
const dom = new jsdom_1.JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
const element = dom.window.document.querySelector('p');
beforeEach(() => {
var stringUseCases = ['Test', 'TestTest-Bold', 'test'];
var stringUseCasesDangerous = ['Test<script>test</script>'];
var jsdom_1 = require("jsdom");
describe('htmlConverter', function () {
var dom = new jsdom_1.JSDOM("<!DOCTYPE html><p>Hello world</p>");
var element = dom.window.document.querySelector('p');
beforeEach(function () {
element.innerHTML = '';
});
test('should return error if invalid props to dompurify passed', () => {
const convertedDataCallback = () => _1.htmlConverter('test', 'p', { RETURN_DOM: true });
test('should return error if invalid props to dompurify passed', function () {
var convertedDataCallback = function () { return _1.htmlConverter('test', 'p', { RETURN_DOM: true }); };
expect(convertedDataCallback).toThrow();
});
test('should return the same string', () => {
htmlUseCases.forEach((useCase) => {
const [, cleanData] = _1.htmlConverter(useCase, 'div');
test('should return the same string', function () {
htmlUseCases.forEach(function (useCase) {
var _a = _1.htmlConverter(useCase, 'div'), cleanData = _a[1];
expect(cleanData).toMatch(useCase);
const [, cleanData2] = _1.htmlConverter(useCase, element);
var _b = _1.htmlConverter(useCase, element), cleanData2 = _b[1];
expect(cleanData2).toMatch(useCase);
});
stringUseCases.forEach((useCase) => {
const [, cleanData] = _1.htmlConverter(useCase, 'div');
stringUseCases.forEach(function (useCase) {
var _a = _1.htmlConverter(useCase, 'div'), cleanData = _a[1];
expect(cleanData).toMatch(useCase);
const [, cleanData2] = _1.htmlConverter(useCase, element);
var _b = _1.htmlConverter(useCase, element), cleanData2 = _b[1];
expect(cleanData2).toMatch(useCase);
});
});
test('should sanitize data and create new element', () => {
htmlUseCasesDangerous.forEach((useCase) => {
test('should sanitize data and create new element', function () {
htmlUseCasesDangerous.forEach(function (useCase) {
element.innerHTML = '';
const [_, cleanData] = _1.htmlConverter(useCase, 'div');
var _a = _1.htmlConverter(useCase, 'div'), _ = _a[0], cleanData = _a[1];
expect(cleanData).toEqual(expect.not.stringMatching(dangerousTags));

@@ -47,6 +47,5 @@ element === null || element === void 0 ? void 0 : element.insertAdjacentHTML('beforeend', cleanData);

});
stringUseCasesDangerous.forEach((useCase) => {
stringUseCasesDangerous.forEach(function (useCase) {
element.innerHTML = '';
const [_, cleanData] = _1.htmlConverter(useCase, 'div');
console.log('The clean data', cleanData);
var _a = _1.htmlConverter(useCase, 'div'), _ = _a[0], cleanData = _a[1];
expect(cleanData).toEqual(expect.not.stringMatching(dangerousTags));

@@ -57,12 +56,12 @@ element === null || element === void 0 ? void 0 : element.insertAdjacentHTML('beforeend', cleanData);

});
test('should sanitize data and append to existing element', () => {
htmlUseCasesDangerous.forEach((useCase) => {
test('should sanitize data and append to existing element', function () {
htmlUseCasesDangerous.forEach(function (useCase) {
element.innerHTML = '';
const [, cleanData] = _1.htmlConverter(useCase, element);
var _a = _1.htmlConverter(useCase, element), cleanData = _a[1];
expect(cleanData).toEqual(expect.not.stringMatching(dangerousTags));
expect(element === null || element === void 0 ? void 0 : element.children).toHaveLength(1);
});
stringUseCasesDangerous.forEach((useCase) => {
stringUseCasesDangerous.forEach(function (useCase) {
element.innerHTML = '';
const [, cleanData] = _1.htmlConverter(useCase, element);
var _a = _1.htmlConverter(useCase, element), cleanData = _a[1];
expect(cleanData).toEqual(expect.not.stringMatching(dangerousTags));

@@ -69,0 +68,0 @@ expect(element === null || element === void 0 ? void 0 : element.childNodes).toHaveLength(1);

{
"name": "html-converter-js",
"version": "1.0.3",
"version": "1.0.4",
"description": "",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc