@@ -82,8 +82,9 @@ class WidgetsHTMLDecoder {
8282
8383 Future <List <Widget >> _parseElement (
8484 Iterable <dom.Node > domNodes,
85- TextStyle baseTextStyle,
86- ) async {
85+ TextStyle baseTextStyle, {
86+ bool preTag = false ,
87+ }) async {
8788 final result = < Widget > [];
8889 final delta = < TextSpan > [];
8990 TextAlign ? textAlign;
@@ -98,10 +99,22 @@ class WidgetsHTMLDecoder {
9899 delta.add (const TextSpan (
99100 text: "\n " ,
100101 ));
102+ } else if (localName == HTMLTags .pre) {
103+ final childrens =
104+ await _parseElement (domNode.nodes, baseTextStyle, preTag: true );
105+ delta.add (TextSpan (children: [
106+ WidgetSpan (
107+ child: Container (
108+ width: double .infinity,
109+ decoration: customStyles.codeDecoration ??
110+ BoxDecoration (color: customStyles.codeblockColor),
111+ child: Column (children: childrens)))
112+ ]));
101113 } else if (HTMLTags .formattingElements.contains (localName)) {
102114 /// Check if the element is a simple formatting element like <span>, <bold>, or <italic>
103- final attributes =
104- await _parserFormattingElementAttributes (domNode, baseTextStyle);
115+ final attributes = await _parserFormattingElementAttributes (
116+ domNode, baseTextStyle,
117+ preTag: preTag);
105118
106119 textAlign = attributes.$1;
107120
@@ -156,6 +169,12 @@ class WidgetsHTMLDecoder {
156169 }
157170
158171 /// Handle special elements (e.g., headings, lists, images)
172+ } else if (localName == HTMLTags .horizontalDivider) {
173+ result.add (Divider (
174+ color: customStyles.dividerColor,
175+ thickness: customStyles.dividerthickness,
176+ height: customStyles.dividerHight,
177+ borderStyle: customStyles.dividerBorderStyle));
159178 }
160179 } else if (domNode is dom.Text ) {
161180 if (delta.isNotEmpty && domNode.text.trim ().isNotEmpty) {
@@ -270,7 +289,8 @@ class WidgetsHTMLDecoder {
270289
271290 //// Parses the attributes of a formatting element and returns a TextStyle.
272291 Future <(TextAlign ?, TextStyle , String ?)> _parserFormattingElementAttributes (
273- dom.Element element, TextStyle baseTextStyle) async {
292+ dom.Element element, TextStyle baseTextStyle,
293+ {bool preTag = false }) async {
274294 final localName = element.localName;
275295 TextAlign ? textAlign;
276296 String ? link;
@@ -331,17 +351,23 @@ class WidgetsHTMLDecoder {
331351
332352 /// Handle <code> element
333353 case HTMLTags .code:
334- attributes = attributes
335- .copyWith (background: const BoxDecoration (color: PdfColors .red))
336- .merge (customStyles.codeStyle);
354+ if (! preTag) {
355+ attributes = attributes
356+ .copyWith (
357+ background: BoxDecoration (
358+ color: customStyles.codeBlockBackgroundColor))
359+ .merge (customStyles.codeStyle);
360+ }
361+
337362 break ;
338363 default :
339364 break ;
340365 }
341366
342367 for (final child in element.children) {
343- final nattributes =
344- await _parserFormattingElementAttributes (child, baseTextStyle);
368+ final nattributes = await _parserFormattingElementAttributes (
369+ child, baseTextStyle,
370+ preTag: preTag);
345371 attributes = attributes.merge (nattributes.$2);
346372 if (nattributes.$2.decoration != null ) {
347373 decoration.add (nattributes.$2.decoration! );
@@ -402,7 +428,7 @@ class WidgetsHTMLDecoder {
402428
403429 ///iterate over <tr>children
404430 for (final data in element.children) {
405- if (data.children .isEmpty) {
431+ if (data.nodes .isEmpty) {
406432 ///if single <th> or<td> tag found
407433 final node = paragraphNode (text: data.text);
408434
@@ -423,18 +449,15 @@ class WidgetsHTMLDecoder {
423449
424450 ///parse the nodes and handle theem accordingly
425451 Future <Iterable <Widget >> _parseTableSpecialNodes (
426- dom.Element element , TextStyle baseTextStyle) async {
452+ dom.Element node , TextStyle baseTextStyle) async {
427453 final List <Widget > nodes = [];
428454
429455 ///iterate over multiple childrens
430- if (element.children.isNotEmpty) {
431- for (final childrens in element.children) {
432- ///parse them according to their widget
433- nodes.addAll (
434- await _parseTableDataElementsData (childrens, baseTextStyle));
435- }
456+ if (node.nodes.isNotEmpty) {
457+ ///parse them according to their widget
458+ nodes.addAll (await _parseElement (node.nodes, baseTextStyle));
436459 } else {
437- nodes.addAll (await _parseTableDataElementsData (element , baseTextStyle));
460+ nodes.addAll (await _parseTableDataElementsData (node , baseTextStyle));
438461 }
439462 return nodes;
440463 }
@@ -452,6 +475,7 @@ class WidgetsHTMLDecoder {
452475
453476 /// Check if the element is a simple formatting element like <span>, <bold>, or <italic>
454477 if (localName == HTMLTags .br) {
478+ result.add (Text ('\n ' ));
455479 } else if (HTMLTags .formattingElements.contains (localName)) {
456480 final attributes =
457481 await _parserFormattingElementAttributes (element, baseTextStyle);
@@ -519,9 +543,9 @@ class WidgetsHTMLDecoder {
519543 text: TextSpan (
520544 children: delta,
521545 style: baseTextStyle
522- .merge ( TextStyle (
546+ .copyWith (
523547 fontSize: level.getHeadingSize,
524- fontWeight: FontWeight .bold))
548+ fontWeight: FontWeight .bold)
525549 .merge (level.getHeadingStyle (customStyles)))));
526550 }
527551
@@ -669,6 +693,19 @@ class WidgetsHTMLDecoder {
669693 HTMLTags .formattingElements.contains (child.localName) == false ) {
670694 childNodes.addAll (await _parseElement (child.nodes, baseTextStyle));
671695 } else {
696+ if (child.localName == HTMLTags .pre) {
697+ final childrens =
698+ await _parseElement (child.nodes, baseTextStyle, preTag: true );
699+ delta.add (TextSpan (children: [
700+ WidgetSpan (
701+ child: Container (
702+ width: double .infinity,
703+ decoration: customStyles.codeDecoration ??
704+ BoxDecoration (color: customStyles.codeblockColor),
705+ child: Column (children: childrens)))
706+ ]));
707+ } else
708+
672709 /// Handle special elements (e.g., headings, lists) within a paragraph
673710 if (HTMLTags .specialElements.contains (child.localName)) {
674711 childNodes.addAll (
@@ -678,6 +715,12 @@ class WidgetsHTMLDecoder {
678715 type: BuiltInAttributeKey .bulletedList,
679716 ),
680717 );
718+ } else if (child.localName == HTMLTags .horizontalDivider) {
719+ childNodes.add (Divider (
720+ color: customStyles.dividerColor,
721+ thickness: customStyles.dividerthickness,
722+ height: customStyles.dividerHight,
723+ borderStyle: customStyles.dividerBorderStyle));
681724 } else {
682725 if (child.localName == HTMLTags .br) {
683726 delta.add (const TextSpan (
0 commit comments