Socket
Socket
Sign inDemoInstall

html-to-json-parser

Package Overview
Dependencies
1
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 2.0.0

7

CHANGELOGS.md

@@ -18,2 +18,9 @@ # CHANGELOG

## v2.0.0
#### Enhancements
- Rename the component `HTMLParser` to `HTMLToJSON`
- Move `HTMLToJSON` from default export to normal export
## v1.1.0

@@ -20,0 +27,0 @@

7

dist/index.d.ts

@@ -8,10 +8,9 @@ /**

*/
import { HTMLParser } from './html';
import { HTMLParser as HTMLToJSON } from './html';
import { JSONToHTML } from './json';
import { JSONContent } from './types';
/**
* The default export is the `HTMLParser` function.
* This function converts an HTML element or HTML string into a JSON object or string.
* The `HTMLToJSON` function converts an HTML element or HTML string into a JSON object or string.
*/
export default HTMLParser;
export { HTMLToJSON };
/**

@@ -18,0 +17,0 @@ * The `JSONToHTML` function converts a JSON object or string into an HTML string or document element.

@@ -1,2 +0,2 @@

import{DOMParser as t}from"xmldom";async function e(e,n=!1){return await new Promise((r,o)=>{try{const o={};let s;if("string"==typeof e){const n=(new t).parseFromString(e,"text/xml");n.firstChild&&(s=n.firstChild)}else s=e;const i=(t,e=o)=>{e.type=t.nodeName;const n=t.childNodes;if(null!==n&&n.length){e.content=[];for(let t=0;t<n.length;t++)3===n[t].nodeType?n[t].nodeValue&&e.content.push(n[t].nodeValue):(e.content.push({}),i(n[t],e.content[e.content.length-1]))}if(null!==t.attributes&&t.attributes.length){e.attributes={};for(let n=0;n<t.attributes.length;n++)e.attributes[t.attributes[n].nodeName]=t.attributes[n].nodeValue}};i(s),r(n?JSON.stringify(o):o)}catch(t){o(t)}})}async function n(e,n=!0){return await new Promise((r,o)=>{try{let o=e;"string"==typeof e&&(o=JSON.parse(e));const s=t=>{let e=`<${t.type}`;return t.attributes&&Object.entries(t.attributes).forEach(([t,n])=>{e+=` ${t}="${n}"`}),e+=">",t.content&&t.content.forEach(t=>{e+="string"==typeof t?t:s(t)}),e+=`</${t.type}>`,e},i=s(o);r(n?i:(new t).parseFromString(i,"text/xml"))}catch(t){o(t)}})}export{n as JSONToHTML,e as default};
import{DOMParser as t}from"xmldom";async function e(e,n=!1){return await new Promise((r,o)=>{try{const o={};let s;if("string"==typeof e){const n=(new t).parseFromString(e,"text/xml");n.firstChild&&(s=n.firstChild)}else s=e;const i=(t,e=o)=>{e.type=t.nodeName;const n=t.childNodes;if(null!==n&&n.length){e.content=[];for(let t=0;t<n.length;t++)3===n[t].nodeType?n[t].nodeValue&&e.content.push(n[t].nodeValue):(e.content.push({}),i(n[t],e.content[e.content.length-1]))}if(null!==t.attributes&&t.attributes.length){e.attributes={};for(let n=0;n<t.attributes.length;n++)e.attributes[t.attributes[n].nodeName]=t.attributes[n].nodeValue}};i(s),r(n?JSON.stringify(o):o)}catch(t){o(t)}})}async function n(e,n=!0){return await new Promise((r,o)=>{try{let o=e;"string"==typeof e&&(o=JSON.parse(e));const s=t=>{let e=`<${t.type}`;return t.attributes&&Object.entries(t.attributes).forEach(([t,n])=>{e+=` ${t}="${n}"`}),e+=">",t.content&&t.content.forEach(t=>{e+="string"==typeof t?t:s(t)}),e+=`</${t.type}>`,e},i=s(o);r(n?i:(new t).parseFromString(i,"text/xml"))}catch(t){o(t)}})}export{e as HTMLToJSON,n as JSONToHTML};
//# sourceMappingURL=index.modern.js.map

@@ -1,2 +0,2 @@

import{DOMParser as t}from"xmldom";var e=function(e,r){void 0===r&&(r=!1);try{return Promise.resolve(new Promise(function(n,o){try{var i,a={};if("string"==typeof e){var s=(new t).parseFromString(e,"text/xml");s.firstChild&&(i=s.firstChild)}else i=e;!function t(e,r){void 0===r&&(r=a),r.type=e.nodeName;var n=e.childNodes;if(null!==n&&n.length){r.content=[];for(var o=0;o<n.length;o++)3===n[o].nodeType?n[o].nodeValue&&r.content.push(n[o].nodeValue):(r.content.push({}),t(n[o],r.content[r.content.length-1]))}if(null!==e.attributes&&e.attributes.length){r.attributes={};for(var i=0;i<e.attributes.length;i++)r.attributes[e.attributes[i].nodeName]=e.attributes[i].nodeValue}}(i),n(r?JSON.stringify(a):a)}catch(t){o(t)}}))}catch(t){return Promise.reject(t)}},r=function(e,r){void 0===r&&(r=!0);try{return Promise.resolve(new Promise(function(n,o){try{var i=e;"string"==typeof e&&(i=JSON.parse(e));var a=function t(e){var r="<"+e.type;return e.attributes&&Object.entries(e.attributes).forEach(function(t){r+=" "+t[0]+'="'+t[1]+'"'}),r+=">",e.content&&e.content.forEach(function(e){r+="string"==typeof e?e:t(e)}),r+="</"+e.type+">"}(i);n(r?a:(new t).parseFromString(a,"text/xml"))}catch(t){o(t)}}))}catch(t){return Promise.reject(t)}};export{r as JSONToHTML,e as default};
import{DOMParser as t}from"xmldom";var e=function(e,r){void 0===r&&(r=!1);try{return Promise.resolve(new Promise(function(n,o){try{var i,a={};if("string"==typeof e){var s=(new t).parseFromString(e,"text/xml");s.firstChild&&(i=s.firstChild)}else i=e;!function t(e,r){void 0===r&&(r=a),r.type=e.nodeName;var n=e.childNodes;if(null!==n&&n.length){r.content=[];for(var o=0;o<n.length;o++)3===n[o].nodeType?n[o].nodeValue&&r.content.push(n[o].nodeValue):(r.content.push({}),t(n[o],r.content[r.content.length-1]))}if(null!==e.attributes&&e.attributes.length){r.attributes={};for(var i=0;i<e.attributes.length;i++)r.attributes[e.attributes[i].nodeName]=e.attributes[i].nodeValue}}(i),n(r?JSON.stringify(a):a)}catch(t){o(t)}}))}catch(t){return Promise.reject(t)}},r=function(e,r){void 0===r&&(r=!0);try{return Promise.resolve(new Promise(function(n,o){try{var i=e;"string"==typeof e&&(i=JSON.parse(e));var a=function t(e){var r="<"+e.type;return e.attributes&&Object.entries(e.attributes).forEach(function(t){r+=" "+t[0]+'="'+t[1]+'"'}),r+=">",e.content&&e.content.forEach(function(e){r+="string"==typeof e?e:t(e)}),r+="</"+e.type+">"}(i);n(r?a:(new t).parseFromString(a,"text/xml"))}catch(t){o(t)}}))}catch(t){return Promise.reject(t)}};export{e as HTMLToJSON,r as JSONToHTML};
//# sourceMappingURL=index.module.js.map

@@ -1,2 +0,2 @@

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("xmldom")):"function"==typeof define&&define.amd?define(["exports","xmldom"],e):e((t||self).htmlToJsonParser={},t.xmldom)}(this,function(t,e){t.JSONToHTML=function(t,n){void 0===n&&(n=!0);try{return Promise.resolve(new Promise(function(r,o){try{var i=t;"string"==typeof t&&(i=JSON.parse(t));var s=function t(e){var n="<"+e.type;return e.attributes&&Object.entries(e.attributes).forEach(function(t){n+=" "+t[0]+'="'+t[1]+'"'}),n+=">",e.content&&e.content.forEach(function(e){n+="string"==typeof e?e:t(e)}),n+="</"+e.type+">"}(i);r(n?s:(new e.DOMParser).parseFromString(s,"text/xml"))}catch(t){o(t)}}))}catch(t){return Promise.reject(t)}},t.default=function(t,n){void 0===n&&(n=!1);try{return Promise.resolve(new Promise(function(r,o){try{var i,s={};if("string"==typeof t){var a=(new e.DOMParser).parseFromString(t,"text/xml");a.firstChild&&(i=a.firstChild)}else i=t;!function t(e,n){void 0===n&&(n=s),n.type=e.nodeName;var r=e.childNodes;if(null!==r&&r.length){n.content=[];for(var o=0;o<r.length;o++)3===r[o].nodeType?r[o].nodeValue&&n.content.push(r[o].nodeValue):(n.content.push({}),t(r[o],n.content[n.content.length-1]))}if(null!==e.attributes&&e.attributes.length){n.attributes={};for(var i=0;i<e.attributes.length;i++)n.attributes[e.attributes[i].nodeName]=e.attributes[i].nodeValue}}(i),r(n?JSON.stringify(s):s)}catch(t){o(t)}}))}catch(t){return Promise.reject(t)}}});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("xmldom")):"function"==typeof define&&define.amd?define(["exports","xmldom"],e):e((t||self).htmlToJsonParser={},t.xmldom)}(this,function(t,e){t.HTMLToJSON=function(t,n){void 0===n&&(n=!1);try{return Promise.resolve(new Promise(function(r,o){try{var i,s={};if("string"==typeof t){var a=(new e.DOMParser).parseFromString(t,"text/xml");a.firstChild&&(i=a.firstChild)}else i=t;!function t(e,n){void 0===n&&(n=s),n.type=e.nodeName;var r=e.childNodes;if(null!==r&&r.length){n.content=[];for(var o=0;o<r.length;o++)3===r[o].nodeType?r[o].nodeValue&&n.content.push(r[o].nodeValue):(n.content.push({}),t(r[o],n.content[n.content.length-1]))}if(null!==e.attributes&&e.attributes.length){n.attributes={};for(var i=0;i<e.attributes.length;i++)n.attributes[e.attributes[i].nodeName]=e.attributes[i].nodeValue}}(i),r(n?JSON.stringify(s):s)}catch(t){o(t)}}))}catch(t){return Promise.reject(t)}},t.JSONToHTML=function(t,n){void 0===n&&(n=!0);try{return Promise.resolve(new Promise(function(r,o){try{var i=t;"string"==typeof t&&(i=JSON.parse(t));var s=function t(e){var n="<"+e.type;return e.attributes&&Object.entries(e.attributes).forEach(function(t){n+=" "+t[0]+'="'+t[1]+'"'}),n+=">",e.content&&e.content.forEach(function(e){n+="string"==typeof e?e:t(e)}),n+="</"+e.type+">"}(i);r(n?s:(new e.DOMParser).parseFromString(s,"text/xml"))}catch(t){o(t)}}))}catch(t){return Promise.reject(t)}}});
//# sourceMappingURL=index.umd.js.map
{
"name": "html-to-json-parser",
"version": "1.1.0",
"version": "2.0.0",
"description": "This library converts HTML to JSON or JSON to HTML tree",

@@ -5,0 +5,0 @@ "type": "module",

@@ -64,4 +64,4 @@ # HTML to JSON

// Imports
import HTMLParser from 'html-to-json-parser'; // ES6
const HTMLParser = require('html-to-json-parser'); // CommonJS
import { HTMLToJSON } from 'html-to-json-parser'; // ES6
const { HTMLToJSON } = require('html-to-json-parser'); // CommonJS

@@ -73,3 +73,3 @@ // Data

// Conversion
let result = await HTMLParser(element, true); // Default: false - true: return JSON, false: return JS Object
let result = await HTMLToJSON(element, true); // Default: false - true: return JSON, false: return JS Object
```

@@ -76,0 +76,0 @@

import { expect, test } from '@jest/globals';
import HTMLParser, { JSONToHTML } from '../src';
import { HTMLToJSON, JSONToHTML } from '../src';
test('Test the parser by converting HTML to JSON', async () => {
const element = '<div><ul><li>Hello <strong>World</strong></li></ul></div>';
const result = await HTMLParser(element, true);
const result = await HTMLToJSON(element, true);

@@ -8,0 +8,0 @@ const expected =

Sorry, the diff of this file is not supported yet

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