11// @ts -nocheck
2- import { fromRedactor } from "../src/fromRedactor"
2+ import { fromRedactor , getNestedValueIfAvailable } from "../src/fromRedactor"
33import { JSDOM } from "jsdom"
44import { isEqual } from "lodash"
55import omitdeep from "omit-deep-lodash"
@@ -227,6 +227,8 @@ describe("Testing html to json conversion", () => {
227227 expect ( testResult ) . toBe ( true )
228228 } )
229229
230+ describe ( "Nested attrs" , ( ) => {
231+
230232 test ( "should convert stringified attrs to proper nested JSON attrs" , ( ) => {
231233 for ( const testCase of expectedValue [ "nested-attrs" ] ) {
232234 const { json, html } = testCase ;
@@ -236,4 +238,35 @@ describe("Testing html to json conversion", () => {
236238 expect ( jsonValue ) . toStrictEqual ( json ) ;
237239 }
238240 } ) ;
239- } )
241+
242+ test ( "should not convert stringify attrs when `allowNonStandardTags` is not true" , ( ) => {
243+ const html = `<p><span from="Paul, Addy" to="[object Object]">Hi There!</span></p>` ;
244+ const json = { "attrs" : { } , "children" : [ { "attrs" : { } , "children" : [ { "attrs" : { "redactor-attributes" : { "from" : "Paul, Addy" , "to" : "[object Object]" } , "style" : { } } , "children" : [ { "attrs" : { "style" : { } } , "text" : "Hi There!" } ] , "type" : "span" , "uid" : "uid" } ] , "type" : "p" , "uid" : "uid" } ] , "type" : "doc" , "uid" : "uid" } ;
245+
246+ const dom = new JSDOM ( html ) ;
247+ let htmlDoc = dom . window . document . querySelector ( "body" ) ;
248+ const jsonValue = fromRedactor ( htmlDoc ) ;
249+ expect ( jsonValue ) . toStrictEqual ( json ) ;
250+ } ) ;
251+ } )
252+
253+ } )
254+
255+
256+ describe ( 'getNestedValueIfAvailable' , ( ) => {
257+
258+ it ( 'should return the input value when it\'s not a string containing JSON' , ( ) => {
259+ expect ( getNestedValueIfAvailable ( 10 ) ) . toBe ( 10 ) ;
260+ expect ( getNestedValueIfAvailable ( null ) ) . toBeNull ( ) ;
261+ expect ( getNestedValueIfAvailable ( '{ "name": "John", "age": }' ) ) . toBe ( '{ "name": "John", "age": }' ) ;
262+ expect ( getNestedValueIfAvailable ( { "name" : "John" , "age" : 30 } ) ) . toStrictEqual ( { "name" : "John" , "age" : 30 } ) ;
263+ expect ( getNestedValueIfAvailable ( '[Object Object]' ) ) . toBe ( '[Object Object]' ) ;
264+ } ) ;
265+
266+ it ( 'should return the parsed JSON when the input value is a string containing JSON' , ( ) => {
267+ const value = '{ "name": "John", "age": 30 }' ;
268+ const result = getNestedValueIfAvailable ( value ) ;
269+ expect ( result ) . toEqual ( { name : "John" , age : 30 } ) ;
270+ } ) ;
271+
272+ } ) ;
0 commit comments