@@ -66,12 +66,12 @@ class HtmlParser extends StatelessWidget {
66
66
return RichText (text: parsedTree);
67
67
}
68
68
69
- /// [parseHTML] converts a string to a DOM document using the dart `html` library.
69
+ /// [parseHTML] converts a string of HTML to a DOM document using the dart `html` library.
70
70
static dom.Document parseHTML (String data) {
71
71
return htmlparser.parse (data);
72
72
}
73
73
74
- ///TODO document
74
+ /// [parseCSS] converts a string of CSS to a CSS stylesheet using the dart `csslib` library.
75
75
static css.StyleSheet parseCSS (String data) {
76
76
return cssparser.parse (data);
77
77
}
@@ -92,7 +92,10 @@ class HtmlParser extends StatelessWidget {
92
92
return tree;
93
93
}
94
94
95
- ///TODO document
95
+ /// [_recursiveLexer] is the recursive worker function for [lexDomTree] .
96
+ ///
97
+ /// It runs the parse functions of every type of
98
+ /// element and returns a [StyledElement] tree representing the element.
96
99
static StyledElement _recursiveLexer (
97
100
dom.Node node, List <String > customRenderTags, List <String > blacklistedElements) {
98
101
List <StyledElement > children = List <StyledElement >();
@@ -156,7 +159,7 @@ class HtmlParser extends StatelessWidget {
156
159
}
157
160
158
161
/// [applyCustomStyles] applies the [Style] objects passed into the [Html]
159
- /// widget onto the [StyledElement] tree.
162
+ /// widget onto the [StyledElement] tree, no cascading of styles is done at this point .
160
163
StyledElement _applyCustomStyles (StyledElement tree) {
161
164
if (style == null ) return tree;
162
165
style.forEach ((key, style) {
@@ -173,6 +176,8 @@ class HtmlParser extends StatelessWidget {
173
176
return tree;
174
177
}
175
178
179
+ /// [_cascadeStyles] cascades all of the inherited styles down the tree, applying them to each
180
+ /// child that doesn't specify a different style.
176
181
StyledElement _cascadeStyles (StyledElement tree) {
177
182
tree.children? .forEach ((child) {
178
183
child.style = tree.style.copyOnlyInherited (child.style);
@@ -197,6 +202,9 @@ class HtmlParser extends StatelessWidget {
197
202
}
198
203
199
204
/// [parseTree] converts a tree of [StyledElement] s to an [InlineSpan] tree.
205
+ ///
206
+ /// [parseTree] is responsible for handling the [customRender] parameter and
207
+ /// deciding what different `Style.display` options look like as Widgets.
200
208
InlineSpan parseTree (RenderContext context, StyledElement tree) {
201
209
// Merge this element's style into the context so that children
202
210
// inherit the correct style
@@ -347,14 +355,18 @@ class HtmlParser extends StatelessWidget {
347
355
return tree;
348
356
}
349
357
350
- ///TODO document
358
+ /// [_processInlineWhitespace] is responsible for removing redundant whitespace
359
+ /// between and among inline elements. It does so by creating a boolean [Context]
360
+ /// and passing it to the [_processInlineWhitespaceRecursive] function.
351
361
static StyledElement _processInlineWhitespace (StyledElement tree) {
352
362
final whitespaceParsingContext = Context (false );
353
363
tree = _processInlineWhitespaceRecursive (tree, whitespaceParsingContext);
354
364
return tree;
355
365
}
356
366
357
- ///TODO document
367
+ /// [_processInlineWhitespaceRecursive] analyzes the whitespace between and among different
368
+ /// inline elements, and replaces any instance of two or more spaces with a single space, according
369
+ /// to the w3's HTML whitespace processing specification linked to above.
358
370
static StyledElement _processInlineWhitespaceRecursive (StyledElement tree, Context <bool > wpc) {
359
371
if (tree.style.display == Display .BLOCK ) {
360
372
wpc.data = false ;
@@ -394,27 +406,16 @@ class HtmlParser extends StatelessWidget {
394
406
}
395
407
396
408
/// [processListCharacters] adds list characters to the front of all list items.
397
- /// TODO document better
409
+ ///
410
+ /// The function uses the [_processListCharactersRecursive] function to do most of its work.
398
411
static StyledElement _processListCharacters (StyledElement tree) {
399
- // if (tree.name == "ol" || tree.name == "ul") {
400
- // for (int i = 0; i < tree.children?.length; i++) {
401
- // if (tree.children[i].name == "li") {
402
- // switch(tree.style.listStyleType) {
403
- // case ListStyleType.DISC:
404
- // tree.children[i].style.markerContent = '•';
405
- // break;
406
- // case ListStyleType.DECIMAL:
407
- // tree.children[i].style.markerContent = '${i + 1}.';
408
- // break;
409
- // }}
410
- // }
411
- // }
412
- // tree.children?.forEach(_processListCharacters);
413
412
final olStack = ListQueue <Context <int >>();
414
413
tree = _processListCharactersRecursive (tree, olStack);
415
414
return tree;
416
415
}
417
416
417
+ /// [_processListCharactersRecursive] uses a Stack of integers to properly number and
418
+ /// bullet all list items according to the [ListStyleType] they have been given.
418
419
static StyledElement _processListCharactersRecursive (
419
420
StyledElement tree, ListQueue <Context <int >> olStack) {
420
421
if (tree.name == 'ol' ) {
@@ -439,7 +440,9 @@ class HtmlParser extends StatelessWidget {
439
440
return tree;
440
441
}
441
442
442
- /// TODO document better
443
+ /// [_processBeforesAndAfters] adds text content to the beginning and end of
444
+ /// the list of the trees children according to the `before` and `after` Style
445
+ /// properties.
443
446
static StyledElement _processBeforesAndAfters (StyledElement tree) {
444
447
if (tree.style? .before != null ) {
445
448
tree.children.insert (0 , TextContentElement (text: tree.style.before));
@@ -550,6 +553,9 @@ class HtmlParser extends StatelessWidget {
550
553
}
551
554
552
555
/// [removeEmptyElements] recursively removes empty elements.
556
+ ///
557
+ /// An empty element is any [EmptyContentElement] , any empty [TextContentElement] ,
558
+ /// or any block-level [TextContentElement] that contains only whitespace.
553
559
static StyledElement _removeEmptyElements (StyledElement tree) {
554
560
List <StyledElement > toRemove = new List <StyledElement >();
555
561
tree.children? .forEach ((child) {
@@ -561,7 +567,6 @@ class HtmlParser extends StatelessWidget {
561
567
child.style.whiteSpace != WhiteSpace .PRE &&
562
568
tree.style.display == Display .BLOCK &&
563
569
child.text.trim ().isEmpty) {
564
- //TODO should this be moved to the whitespace functions?
565
570
toRemove.add (child);
566
571
} else {
567
572
_removeEmptyElements (child);
@@ -572,6 +577,8 @@ class HtmlParser extends StatelessWidget {
572
577
return tree;
573
578
}
574
579
580
+ /// [_processFontSize] changes percent-based font sizes (negative numbers in this implementation)
581
+ /// to pixel-based font sizes.
575
582
static StyledElement _processFontSize (StyledElement tree) {
576
583
double parentFontSize = tree.style? .fontSize? .size ?? FontSize .medium.size;
577
584
@@ -586,7 +593,10 @@ class HtmlParser extends StatelessWidget {
586
593
}
587
594
}
588
595
589
- ///TODO document better
596
+ /// The [RenderContext] is available when parsing the tree. It contains information
597
+ /// about the [BuildContext] of the `Html` widget, contains the configuration available
598
+ /// in the [HtmlParser] , and contains information about the [Style] of the current
599
+ /// tree root.
590
600
class RenderContext {
591
601
final BuildContext buildContext;
592
602
final HtmlParser parser;
@@ -599,7 +609,10 @@ class RenderContext {
599
609
});
600
610
}
601
611
602
- ///TODO document
612
+ /// A [ContainerSpan] is a widget with an [InlineSpan] child or children.
613
+ ///
614
+ /// A [ContainerSpan] can have a border, background color, height, width, padding, and margin
615
+ /// and can represent either an INLINE or BLOCK-level element.
603
616
class ContainerSpan extends StatelessWidget {
604
617
final Widget child;
605
618
final List <InlineSpan > children;
0 commit comments