LCOV - code coverage report
Current view: top level - lib - Utils.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 45 65 69.2 %
Date: 2021-04-02 02:16:57 Functions: 0 0 -

          Line data    Source code
       1             : library contentstack_utils;
       2             : 
       3             : import 'dart:collection';
       4             : 
       5             : import 'package:html/parser.dart' show parse;
       6             : import 'package:logger/logger.dart';
       7             : 
       8             : import 'package:contentstack_utils/src/helper/Metadata.dart';
       9             : import 'package:contentstack_utils/src/helper/UtilityHelper.dart';
      10             : import 'package:contentstack_utils/src/model/Option.dart';
      11             : 
      12             : export 'src/embedded/StyleType.dart';
      13             : export 'src/helper/Metadata.dart';
      14             : 
      15           4 : var logger = Logger(printer: PrettyPrinter());
      16             : 
      17             : class Utils {
      18           1 :   static void render(jsonObject, List<String> rteKeys, Option option) {
      19           1 :     if (!UtilityHelper.isValidJson(jsonObject)) {
      20           2 :       logger.i('Invalid file, Can\'t process the json file');
      21           1 :       FormatException('Invalid file, Can\'t process the json file');
      22             :     }
      23             : 
      24             :     // Case: when list of Maps provided
      25           1 :     if (jsonObject is List) {
      26           2 :       for (var entry in jsonObject) {
      27           1 :         render(entry, rteKeys, option);
      28             :       }
      29             :     }
      30             :     // Case: when Map provided
      31           1 :     else if (jsonObject is Map) {
      32           1 :       if (jsonObject.containsKey('_embedded_items')) {
      33           1 :         if (rteKeys != null && rteKeys.isNotEmpty) {
      34           2 :           for (var path in rteKeys) {
      35           1 :             _findContent(jsonObject, path, (rteContent) {
      36           0 :               return renderContent(rteContent, jsonObject, option);
      37             :             });
      38             :           }
      39             :         } else {
      40           0 :           LinkedHashMap embeddedKeys = jsonObject['_embedded_items'];
      41           0 :           rteKeys = embeddedKeys.keys.toList();
      42           0 :           embeddedKeys.keys.forEach((keyPath) {
      43           0 :             _findContent(jsonObject, keyPath, (rteContent) {
      44           0 :               return renderContent(rteContent, jsonObject, option);
      45             :             });
      46             :           });
      47             :         }
      48             :       }
      49             :     } else {
      50           1 :       FormatException('Invalid file for embedded objects');
      51             :     }
      52             :   }
      53             : 
      54           1 :   static void _findContent(
      55             :       LinkedHashMap jsonObj, String path, Function(String) callback) {
      56           1 :     var arrayString = path.split('.');
      57           1 :     _getContent(arrayString, jsonObj, callback);
      58             :   }
      59             : 
      60           1 :   static void _getContent(
      61             :       List<String> arrayString, Map jsonObj, Function(String) callback) {
      62           1 :     if (arrayString.isNotEmpty) {
      63           1 :       var key = arrayString[0];
      64           2 :       if (arrayString.length == 1) {
      65           0 :         var varContent = jsonObj[key];
      66           0 :         if (varContent is String || varContent is List) {
      67             :           if (callback != null) {
      68           0 :             jsonObj[key] = callback(varContent);
      69             :           }
      70             :         }
      71             :       } else {
      72           1 :         arrayString.remove(key);
      73           2 :         if (jsonObj[key] is Map<dynamic, dynamic>) {
      74           0 :           _getContent(arrayString, jsonObj[key], callback);
      75             :         }
      76           2 :         if (jsonObj[key] is List<dynamic>) {
      77           0 :           var jsonArray = jsonObj[key];
      78           0 :           for (var item in jsonArray) {
      79           0 :             _getContent(arrayString, item, callback);
      80             :           }
      81             :         }
      82             :       }
      83             :     }
      84             :   }
      85             : 
      86           1 :   static Object renderContent(rteString, Map embedObject, Option option) async {
      87           1 :     if (rteString is String) {
      88           1 :       var available = embedObject.containsKey('_embedded_items');
      89             :       if (available) {
      90           1 :         var jsonArray = embedObject['_embedded_items'];
      91           3 :         await _getEmbeddedItems(rteString, (metadata) {
      92           1 :           var filteredContent = _findEmbeddedItems(jsonArray, metadata);
      93             :           if (filteredContent != null) {
      94           1 :             var stringData = option.renderOption(filteredContent, metadata);
      95           2 :             rteString = rteString.replaceAll(metadata.outerHTML, stringData);
      96             :           }
      97             :         });
      98             :       }
      99             :       return rteString;
     100           0 :     } else if (rteString is List<String>) {
     101           0 :       var storeRTE = [];
     102           0 :       rteString.forEach((element) {
     103           0 :         var rte = renderContent(element, embedObject, option);
     104           0 :         storeRTE.add(rte);
     105             :       });
     106             : 
     107             :       return storeRTE;
     108             :     }
     109             :     return rteString;
     110             :   }
     111             : 
     112           1 :   static Map _findEmbeddedItems(Map jsonObject, Metadata metadata) {
     113           1 :     var keys = jsonObject.keys;
     114           2 :     for (var item in keys) {
     115           1 :       List jsonArray = jsonObject[item];
     116             :       var filteredContent = jsonArray
     117           5 :           .firstWhere((element) => element['uid'] == metadata.getItemUid);
     118             :       return filteredContent;
     119             :     }
     120             :     return null;
     121             :   }
     122             : 
     123           1 :   static void _getEmbeddedItems(stringHtml, Function(Metadata) callback) {
     124           1 :     final document = parse(stringHtml);
     125           1 :     var containerAsset = document.getElementsByClassName('embedded-asset');
     126           1 :     var containerEntry = document.getElementsByClassName('embedded-entry');
     127           1 :     if (containerAsset.isNotEmpty) {
     128           0 :       containerAsset.forEach((element) {
     129           0 :         callback(Metadata.element(element));
     130             :       });
     131             :     }
     132           1 :     if (containerEntry.isNotEmpty) {
     133           2 :       containerEntry.forEach((element) {
     134           2 :         callback(Metadata.element(element));
     135             :       });
     136             :     }
     137             :   }
     138             : }

Generated by: LCOV version 1.15