Socket
Book a DemoInstallSign in
Socket

@contentstack/json-rte-serializer

Package Overview
Dependencies
Maintainers
12
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@contentstack/json-rte-serializer - npm Package Compare versions

Comparing version

to
2.0.4

93

lib/fromRedactor.js

@@ -163,6 +163,9 @@ "use strict";

const fromRedactor = (el, options) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36;
// If node is text node
if (el.nodeType === 3) {
if (whiteCharPattern.test(el.textContent))
return null;
if (["TABLE", "THEAD", "TBODY", "TR"].includes((_b = (_a = el === null || el === void 0 ? void 0 : el.parentElement) === null || _a === void 0 ? void 0 : _a.nodeName) !== null && _b !== void 0 ? _b : "") && ((_e = (_d = (_c = el === null || el === void 0 ? void 0 : el.textContent) === null || _c === void 0 ? void 0 : _c.trim) === null || _d === void 0 ? void 0 : _d.call(_c)) === null || _e === void 0 ? void 0 : _e.length) === 0)
return null;
if (el.textContent === '\n') {

@@ -173,3 +176,3 @@ return null;

let attrs = { style: {} };
if ((_a = el.parentNode.style) === null || _a === void 0 ? void 0 : _a.color) {
if ((_f = el.parentNode.style) === null || _f === void 0 ? void 0 : _f.color) {
attrs = {

@@ -183,3 +186,3 @@ ...attrs,

}
if ((_b = el.parentNode.style) === null || _b === void 0 ? void 0 : _b['font-family']) {
if ((_g = el.parentNode.style) === null || _g === void 0 ? void 0 : _g['font-family']) {
attrs = {

@@ -193,3 +196,3 @@ ...attrs,

}
if ((_c = el.parentNode.style) === null || _c === void 0 ? void 0 : _c['font-size']) {
if ((_h = el.parentNode.style) === null || _h === void 0 ? void 0 : _h['font-size']) {
attrs = {

@@ -257,9 +260,9 @@ ...attrs,

}
const isEmbedEntry = (_d = el.attributes['data-sys-entry-uid']) === null || _d === void 0 ? void 0 : _d.value;
const type = (_e = el.attributes['type']) === null || _e === void 0 ? void 0 : _e.value;
const isEmbedEntry = (_j = el.attributes['data-sys-entry-uid']) === null || _j === void 0 ? void 0 : _j.value;
const type = (_k = el.attributes['type']) === null || _k === void 0 ? void 0 : _k.value;
if (isEmbedEntry && type === "entry") {
const entryUid = (_f = el.attributes['data-sys-entry-uid']) === null || _f === void 0 ? void 0 : _f.value;
const contentTypeUid = (_g = el.attributes['data-sys-content-type-uid']) === null || _g === void 0 ? void 0 : _g.value;
const displayType = (_h = el.attributes['sys-style-type']) === null || _h === void 0 ? void 0 : _h.value;
const locale = (_j = el.attributes['data-sys-entry-locale']) === null || _j === void 0 ? void 0 : _j.value;
const entryUid = (_l = el.attributes['data-sys-entry-uid']) === null || _l === void 0 ? void 0 : _l.value;
const contentTypeUid = (_m = el.attributes['data-sys-content-type-uid']) === null || _m === void 0 ? void 0 : _m.value;
const displayType = (_o = el.attributes['sys-style-type']) === null || _o === void 0 ? void 0 : _o.value;
const locale = (_p = el.attributes['data-sys-entry-locale']) === null || _p === void 0 ? void 0 : _p.value;
if (entryUid && contentTypeUid && displayType && locale) {

@@ -310,15 +313,15 @@ let elementAttrs = { attrs: { style: {} } };

}
const isEmbedAsset = (_k = el.attributes['data-sys-asset-uid']) === null || _k === void 0 ? void 0 : _k.value;
const isEmbedAsset = (_q = el.attributes['data-sys-asset-uid']) === null || _q === void 0 ? void 0 : _q.value;
if (isEmbedAsset && type === "asset") {
const fileLink = (_l = el.attributes['data-sys-asset-filelink']) === null || _l === void 0 ? void 0 : _l.value;
const uid = (_m = el.attributes['data-sys-asset-uid']) === null || _m === void 0 ? void 0 : _m.value;
const fileName = (_o = el.attributes['data-sys-asset-filename']) === null || _o === void 0 ? void 0 : _o.value;
const contentType = (_p = el.attributes['data-sys-asset-contenttype']) === null || _p === void 0 ? void 0 : _p.value;
const displayType = (_q = el.attributes['sys-style-type']) === null || _q === void 0 ? void 0 : _q.value;
const caption = (_r = el.attributes['data-sys-asset-caption']) === null || _r === void 0 ? void 0 : _r.value;
const alt = (_s = el.attributes['data-sys-asset-alt']) === null || _s === void 0 ? void 0 : _s.value;
const link = (_t = el.attributes['data-sys-asset-link']) === null || _t === void 0 ? void 0 : _t.value;
const position = (_u = el.attributes['data-sys-asset-position']) === null || _u === void 0 ? void 0 : _u.value;
const target = ((_v = el.attributes['data-sys-asset-isnewtab']) === null || _v === void 0 ? void 0 : _v.value) ? "_blank" : "_self";
const contentTypeUid = ((_w = el.attributes['content-type-uid']) === null || _w === void 0 ? void 0 : _w.value) || 'sys_assets';
const fileLink = (_r = el.attributes['data-sys-asset-filelink']) === null || _r === void 0 ? void 0 : _r.value;
const uid = (_s = el.attributes['data-sys-asset-uid']) === null || _s === void 0 ? void 0 : _s.value;
const fileName = (_t = el.attributes['data-sys-asset-filename']) === null || _t === void 0 ? void 0 : _t.value;
const contentType = (_u = el.attributes['data-sys-asset-contenttype']) === null || _u === void 0 ? void 0 : _u.value;
const displayType = (_v = el.attributes['sys-style-type']) === null || _v === void 0 ? void 0 : _v.value;
const caption = (_w = el.attributes['data-sys-asset-caption']) === null || _w === void 0 ? void 0 : _w.value;
const alt = (_x = el.attributes['data-sys-asset-alt']) === null || _x === void 0 ? void 0 : _x.value;
const link = (_y = el.attributes['data-sys-asset-link']) === null || _y === void 0 ? void 0 : _y.value;
const position = (_z = el.attributes['data-sys-asset-position']) === null || _z === void 0 ? void 0 : _z.value;
const target = ((_0 = el.attributes['data-sys-asset-isnewtab']) === null || _0 === void 0 ? void 0 : _0.value) ? "_blank" : "_self";
const contentTypeUid = ((_1 = el.attributes['content-type-uid']) === null || _1 === void 0 ? void 0 : _1.value) || 'sys_assets';
if (fileLink && uid && fileName && contentType) {

@@ -395,3 +398,3 @@ let elementAttrs = { attrs: { style: {} } };

if (nodeName === 'DIV') {
const dataType = (_x = el.attributes['data-type']) === null || _x === void 0 ? void 0 : _x.value;
const dataType = (_2 = el.attributes['data-type']) === null || _2 === void 0 ? void 0 : _2.value;
if (dataType === 'row') {

@@ -416,3 +419,3 @@ const attrs = {

if (dataType === 'grid-container') {
const gutter = (_z = (_y = el.attributes) === null || _y === void 0 ? void 0 : _y['gutter']) === null || _z === void 0 ? void 0 : _z.value;
const gutter = (_4 = (_3 = el.attributes) === null || _3 === void 0 ? void 0 : _3['gutter']) === null || _4 === void 0 ? void 0 : _4.value;
const attrs = {

@@ -427,3 +430,3 @@ type: 'grid-container',

if (dataType === 'grid-child') {
const gridRatio = (_1 = (_0 = el.attributes) === null || _0 === void 0 ? void 0 : _0['grid-ratio']) === null || _1 === void 0 ? void 0 : _1.value;
const gridRatio = (_6 = (_5 = el.attributes) === null || _5 === void 0 ? void 0 : _5['grid-ratio']) === null || _6 === void 0 ? void 0 : _6.value;
const attrs = {

@@ -457,3 +460,3 @@ type: 'grid-child',

}
if (((_2 = el.parentNode) === null || _2 === void 0 ? void 0 : _2.nodeName) === 'PRE') {
if (((_7 = el.parentNode) === null || _7 === void 0 ? void 0 : _7.nodeName) === 'PRE') {
return el.outerHTML;

@@ -520,3 +523,3 @@ }

};
if ((_3 = el.style) === null || _3 === void 0 ? void 0 : _3.width) {
if ((_8 = el.style) === null || _8 === void 0 ? void 0 : _8.width) {
sizeAttrs.width = el.style.width;

@@ -530,3 +533,3 @@ if (sizeAttrs.width[sizeAttrs.width.length - 1] === '%') {

}
if ((_4 = el.style) === null || _4 === void 0 ? void 0 : _4['max-width']) {
if ((_9 = el.style) === null || _9 === void 0 ? void 0 : _9['max-width']) {
sizeAttrs['max-width'] = el.style['max-width'];

@@ -552,12 +555,12 @@ if (sizeAttrs['max-width'][sizeAttrs['max-width'].length - 1] === '%') {

extraAttrs['captionAttrs'] = captionAttrs;
extraAttrs['caption'] = (_5 = captionElements === null || captionElements === void 0 ? void 0 : captionElements[0]) === null || _5 === void 0 ? void 0 : _5.textContent;
extraAttrs['caption'] = (_10 = captionElements === null || captionElements === void 0 ? void 0 : captionElements[0]) === null || _10 === void 0 ? void 0 : _10.textContent;
}
if (((_6 = newChildren[0]) === null || _6 === void 0 ? void 0 : _6.type) === 'img') {
if (((_11 = newChildren[0]) === null || _11 === void 0 ? void 0 : _11.type) === 'img') {
elementAttrs = getFinalImageAttributes({ elementAttrs, newChildren, extraAttrs, sizeAttrs });
}
if (((_7 = newChildren[0]) === null || _7 === void 0 ? void 0 : _7.type) === 'reference') {
if (((_12 = newChildren[0]) === null || _12 === void 0 ? void 0 : _12.type) === 'reference') {
elementAttrs = getReferenceAttributes({ elementAttrs, newChildren, extraAttrs, sizeAttrs });
}
if (((_8 = newChildren[0]) === null || _8 === void 0 ? void 0 : _8.type) === 'a') {
const { href, target } = (_9 = newChildren[0].attrs) === null || _9 === void 0 ? void 0 : _9["redactor-attributes"];
if (((_13 = newChildren[0]) === null || _13 === void 0 ? void 0 : _13.type) === 'a') {
const { href, target } = (_14 = newChildren[0].attrs) === null || _14 === void 0 ? void 0 : _14["redactor-attributes"];
extraAttrs['anchorLink'] = href;

@@ -579,5 +582,5 @@ if (target && target !== '') {

let newChildren = children.filter(trimChildString);
if (((_10 = newChildren[0]) === null || _10 === void 0 ? void 0 : _10.type) === 'reference' && ((_12 = (_11 = newChildren[0]) === null || _11 === void 0 ? void 0 : _11.attrs) === null || _12 === void 0 ? void 0 : _12.default)) {
if (((_15 = newChildren[0]) === null || _15 === void 0 ? void 0 : _15.type) === 'reference' && ((_17 = (_16 = newChildren[0]) === null || _16 === void 0 ? void 0 : _16.attrs) === null || _17 === void 0 ? void 0 : _17.default)) {
let extraAttrs = {};
const { href, target } = (_13 = elementAttrs.attrs) === null || _13 === void 0 ? void 0 : _13["redactor-attributes"];
const { href, target } = (_18 = elementAttrs.attrs) === null || _18 === void 0 ? void 0 : _18["redactor-attributes"];
extraAttrs['link'] = href || el.getAttribute('href');

@@ -593,3 +596,3 @@ if (target && target !== '') {

if (nodeName === 'IMG' || nodeName === 'IFRAME') {
if ((_15 = (_14 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _14 === void 0 ? void 0 : _14["redactor-attributes"]) === null || _15 === void 0 ? void 0 : _15.width) {
if ((_20 = (_19 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _19 === void 0 ? void 0 : _19["redactor-attributes"]) === null || _20 === void 0 ? void 0 : _20.width) {
let width = elementAttrs.attrs["redactor-attributes"].width;

@@ -606,4 +609,4 @@ if (width.slice(width.length - 1) === '%') {

}
if ((_17 = (_16 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _16 === void 0 ? void 0 : _16["redactor-attributes"]) === null || _17 === void 0 ? void 0 : _17.inline) {
elementAttrs.attrs.inline = Boolean((_19 = (_18 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _18 === void 0 ? void 0 : _18["redactor-attributes"]) === null || _19 === void 0 ? void 0 : _19.inline);
if ((_22 = (_21 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _21 === void 0 ? void 0 : _21["redactor-attributes"]) === null || _22 === void 0 ? void 0 : _22.inline) {
elementAttrs.attrs.inline = Boolean((_24 = (_23 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _23 === void 0 ? void 0 : _23["redactor-attributes"]) === null || _24 === void 0 ? void 0 : _24.inline);
}

@@ -638,3 +641,3 @@ return (0, slate_hyperscript_1.jsx)('element', elementAttrs, [{ text: '' }]);

let colWidths = Array.from({ length: col }).fill(250);
if (((_21 = (_20 = el === null || el === void 0 ? void 0 : el.childNodes) === null || _20 === void 0 ? void 0 : _20[0]) === null || _21 === void 0 ? void 0 : _21.nodeName) === 'COLGROUP') {
if (((_26 = (_25 = el === null || el === void 0 ? void 0 : el.childNodes) === null || _25 === void 0 ? void 0 : _25[0]) === null || _26 === void 0 ? void 0 : _26.nodeName) === 'COLGROUP') {
let colGroupWidth = [];

@@ -665,4 +668,4 @@ let totalWidth = parseFloat(el.childNodes[0].getAttribute('data-width')) || col * 250;

if (nodeName === 'P') {
if (((_23 = (_22 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _22 === void 0 ? void 0 : _22["redactor-attributes"]) === null || _23 === void 0 ? void 0 : _23['data-checked']) &&
((_25 = (_24 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _24 === void 0 ? void 0 : _24["redactor-attributes"]) === null || _25 === void 0 ? void 0 : _25['data-type'])) {
if (((_28 = (_27 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _27 === void 0 ? void 0 : _27["redactor-attributes"]) === null || _28 === void 0 ? void 0 : _28['data-checked']) &&
((_30 = (_29 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _29 === void 0 ? void 0 : _29["redactor-attributes"]) === null || _30 === void 0 ? void 0 : _30['data-type'])) {
elementAttrs.type = 'check-list';

@@ -675,3 +678,3 @@ elementAttrs.attrs.checked = elementAttrs.attrs["redactor-attributes"]['data-checked'] === 'true';

if (nodeName === 'SPAN') {
if (((_27 = (_26 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _26 === void 0 ? void 0 : _26["redactor-attributes"]) === null || _27 === void 0 ? void 0 : _27['data-type']) === 'inlineCode') {
if (((_32 = (_31 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _31 === void 0 ? void 0 : _31["redactor-attributes"]) === null || _32 === void 0 ? void 0 : _32['data-type']) === 'inlineCode') {
let attrsStyle = { attrs: { style: {} }, inlineCode: true };

@@ -683,3 +686,3 @@ if ((0, isEmpty_1.default)(children)) {

}
if ((_29 = (_28 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _28 === void 0 ? void 0 : _28["redactor-attributes"]) === null || _29 === void 0 ? void 0 : _29.style) {
if ((_34 = (_33 = elementAttrs === null || elementAttrs === void 0 ? void 0 : elementAttrs.attrs) === null || _33 === void 0 ? void 0 : _33["redactor-attributes"]) === null || _34 === void 0 ? void 0 : _34.style) {
return children;

@@ -703,3 +706,3 @@ }

let noOfInlineElement = 0;
Array.from(((_30 = el.parentNode) === null || _30 === void 0 ? void 0 : _30.childNodes) || []).forEach((child) => {
Array.from(((_35 = el.parentNode) === null || _35 === void 0 ? void 0 : _35.childNodes) || []).forEach((child) => {
if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A') {

@@ -709,3 +712,3 @@ noOfInlineElement += 1;

});
if (noOfInlineElement !== ((_31 = el.parentNode) === null || _31 === void 0 ? void 0 : _31.childNodes.length)) {
if (noOfInlineElement !== ((_36 = el.parentNode) === null || _36 === void 0 ? void 0 : _36.childNodes.length)) {
elementAttrs = {

@@ -712,0 +715,0 @@ type: 'div',

@@ -51,2 +51,4 @@ "use strict";

p: (attrs, child) => {
if (child.includes("<figure"))
return `<div${attrs} style="overflow: hidden"><span>${child}</span></div>`;
return `<p${attrs}>${child}</p>`;

@@ -112,2 +114,3 @@ },

reference: (attrs, child, jsonBlock, extraAttrs) => {
var _a, _b, _c, _d, _e, _f;
if ((extraAttrs === null || extraAttrs === void 0 ? void 0 : extraAttrs.displayType) === 'inline') {

@@ -125,2 +128,43 @@ return `<span${attrs}>${child}</span>`;

}
else if ((extraAttrs === null || extraAttrs === void 0 ? void 0 : extraAttrs.displayType) === "display") {
const anchor = (_a = jsonBlock === null || jsonBlock === void 0 ? void 0 : jsonBlock["attrs"]) === null || _a === void 0 ? void 0 : _a["link"];
const caption = (_b = jsonBlock === null || jsonBlock === void 0 ? void 0 : jsonBlock["attrs"]) === null || _b === void 0 ? void 0 : _b["asset-caption"];
const position = (_c = jsonBlock === null || jsonBlock === void 0 ? void 0 : jsonBlock["attrs"]) === null || _c === void 0 ? void 0 : _c["position"];
const inline = (_d = jsonBlock === null || jsonBlock === void 0 ? void 0 : jsonBlock["attrs"]) === null || _d === void 0 ? void 0 : _d["inline"];
let figureAttrs = "";
const figureStyles = {
margin: "0",
};
attrs = ` src="${(_e = jsonBlock === null || jsonBlock === void 0 ? void 0 : jsonBlock["attrs"]) === null || _e === void 0 ? void 0 : _e["asset-link"]}"` + attrs;
let img = `<img${attrs}/>`;
if (anchor) {
const target = (_f = jsonBlock === null || jsonBlock === void 0 ? void 0 : jsonBlock["attrs"]) === null || _f === void 0 ? void 0 : _f["target"];
let anchorAttrs = `href="${anchor}"`;
if (target) {
anchorAttrs = `${anchorAttrs} target="${target}"`;
}
img = `<a ${anchorAttrs}>${img}</a>`;
}
if (caption || (position && position !== "none")) {
const figcaption = caption
? `<figcaption style="text-align:center">${caption}</figcaption>`
: "";
if (inline && position !== "right" && position !== "left") {
figureStyles["display"] = "inline-block";
}
if (position && position !== "none") {
figureStyles[inline ? "float" : "text-align"] = position;
}
if (figcaption) {
img = `<div style="display: inline-block">${img}${figcaption}</div>`;
}
}
if (!(0, isEmpty_1.default)(figureStyles)) {
figureAttrs = ` style="${getStyleStringFromObject(figureStyles)}"`;
}
if (inline && !caption && (!position || position === 'none')) {
return img;
}
return `<figure${figureAttrs ? figureAttrs : ""}>${img}</figure>`;
}
return `<span${attrs}>${child}</span>`;

@@ -165,3 +209,3 @@ },

const toRedactor = (jsonValue, options) => {
var _a, _b, _c, _d;
var _a, _b, _c, _d, _e, _f;
//TODO: optimize assign once per function call

@@ -404,2 +448,12 @@ if ((options === null || options === void 0 ? void 0 : options.customTextWrapper) && !(0, isEmpty_1.default)(options.customTextWrapper)) {

}
if ((attrsJson === null || attrsJson === void 0 ? void 0 : attrsJson["display-type"]) === "display") {
const styleObj = (_e = (_d = jsonValue === null || jsonValue === void 0 ? void 0 : jsonValue["attrs"]) === null || _d === void 0 ? void 0 : _d["style"]) !== null && _e !== void 0 ? _e : {};
if (!styleObj["width"]) {
styleObj["width"] = "auto";
}
delete styleObj["float"];
// (attrsJson["style"] && typeof attrsJson["style"] === 'string')
// ? (attrsJson["style"] += getStyleStringFromObject(styleObj)) :
(attrsJson["style"] = getStyleStringFromObject(styleObj));
}
delete attrsJson['display-type'];

@@ -450,3 +504,3 @@ }

if (jsonValue['type'] === 'reference') {
figureStyles.displayType = (_d = jsonValue === null || jsonValue === void 0 ? void 0 : jsonValue.attrs) === null || _d === void 0 ? void 0 : _d["display-type"];
figureStyles.displayType = (_f = jsonValue === null || jsonValue === void 0 ? void 0 : jsonValue.attrs) === null || _f === void 0 ? void 0 : _f["display-type"];
}

@@ -464,1 +518,6 @@ if (jsonValue['type'] === 'span' && jsonValue.children.length === 1 && jsonValue.children[0].type === 'span') {

exports.toRedactor = toRedactor;
function getStyleStringFromObject(styleObj) {
return Object.keys(styleObj)
.map((key) => `${key}: ${styleObj[key]}`)
.join("; ");
}
{
"name": "@contentstack/json-rte-serializer",
"version": "2.0.3",
"version": "2.0.4",
"description": "This Package converts Html Document to Json and vice-versa.",

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