Skip to content

Commit

Permalink
plain text nodes inside blockquote, ol, and ul, are now properly inde…
Browse files Browse the repository at this point in the history
…nted
  • Loading branch information
jeffmikels committed Jan 31, 2019
1 parent 7136a59 commit dc92077
Showing 1 changed file with 60 additions and 11 deletions.
71 changes: 60 additions & 11 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class ParseContext {
String blockType; // blockType can be 'p', 'div', 'ul', 'ol', 'blockquote'
bool condenseWhitespace = true;
bool spansOnly = false;
bool inBlock = false;
TextStyle childStyle;

ParseContext(
Expand All @@ -113,6 +114,7 @@ class ParseContext {
this.blockType,
this.condenseWhitespace = true,
this.spansOnly = false,
this.inBlock = false,
this.childStyle}) {
childStyle = childStyle ?? TextStyle();
}
Expand All @@ -126,6 +128,7 @@ class ParseContext {
blockType = parseContext.blockType;
condenseWhitespace = parseContext.condenseWhitespace;
spansOnly = parseContext.spansOnly;
inBlock = parseContext.inBlock;
childStyle = parseContext.childStyle ?? TextStyle();
}
}
Expand Down Expand Up @@ -298,14 +301,14 @@ class HtmlRichTextParser extends StatelessWidget {
return;
}

// empty strings of whitespace might be significant or not, condense it by default
if (node.text.trim() == "" &&
node.text.indexOf(" ") != -1 &&
parseContext.condenseWhitespace) {
node.text = " ";
}
// if (node.text.trim() == "" &&
// node.text.indexOf(" ") != -1 &&
// parseContext.condenseWhitespace) {
// node.text = " ";
// }

// we might want to preserve internal whitespace
// empty strings of whitespace might be significant or not, condense it by default
String finalText = parseContext.condenseWhitespace
? condenseHtmlWhitespace(node.text)
: node.text;
Expand All @@ -315,6 +318,9 @@ class HtmlRichTextParser extends StatelessWidget {
parseContext.parentElement is LinkTextSpan))
finalText = finalText.trim();

// if the finalText is actually empty, just return
if (finalText.isEmpty) return;

// NOW WE HAVE OUR TRULY FINAL TEXT
// debugPrint("Plain Text Node: '$finalText'");

Expand All @@ -326,10 +332,48 @@ class HtmlRichTextParser extends StatelessWidget {

// in this class, a ParentElement must be a BlockText, LinkTextSpan, Row, Column, TextSpan

// the parseContext might actually be a block level style element, so we
// need to honor the indent and styling specified by that block style.
// e.g. ol, ul, blockquote
bool treatLikeBlock =
['blockquote', 'ul', 'ol'].indexOf(parseContext.blockType) != -1;

// if there is no parentElement, contain the span in a BlockText
if (parseContext.parentElement == null) {
// if this is inside a context that should be treated like a block
// but the context is not actually a block, create a block
// and append it to the root widget tree
if (treatLikeBlock) {
Decoration decoration;
if (parseContext.blockType == 'blockquote') {
decoration = BoxDecoration(
border:
Border(left: BorderSide(color: Colors.black38, width: 2.0)),
);
parseContext.childStyle = parseContext.childStyle.merge(TextStyle(
fontStyle: FontStyle.italic,
));
}
BlockText blockText = BlockText(
margin: EdgeInsets.only(
top: 8.0,
bottom: 8.0,
left: parseContext.indentLevel * indentSize),
padding: EdgeInsets.all(2.0),
decoration: decoration,
child: RichText(
textAlign: TextAlign.left,
text: span,
),
);
parseContext.rootWidgetList.add(blockText);
} else {
parseContext.rootWidgetList
.add(BlockText(child: RichText(text: span)));
}

// this allows future items to be added as children
parseContext.parentElement = span;
parseContext.rootWidgetList.add(BlockText(child: RichText(text: span)));

// if the parent is a LinkTextSpan, keep the main attributes of that span going.
} else if (parseContext.parentElement is LinkTextSpan) {
Expand All @@ -348,6 +392,7 @@ class HtmlRichTextParser extends StatelessWidget {
}
return;
}

// OTHER ELEMENT NODES
else if (node is dom.Element) {
assert(() {
Expand Down Expand Up @@ -460,6 +505,7 @@ class HtmlRichTextParser extends StatelessWidget {
child: RichText(text: span),
);
parseContext.rootWidgetList.add(blockElement);
nextContext.inBlock = true;
}
nextContext.childStyle = linkStyle;
nextContext.parentElement = span;
Expand Down Expand Up @@ -559,6 +605,7 @@ class HtmlRichTextParser extends StatelessWidget {
parseContext.rootWidgetList.add(blockText);
nextContext.parentElement = blockText.child.text;
nextContext.spansOnly = true;
nextContext.inBlock = true;
break;

case "h1":
Expand Down Expand Up @@ -614,10 +661,11 @@ class HtmlRichTextParser extends StatelessWidget {
));
}
BlockText blockText = BlockText(
margin: EdgeInsets.symmetric(vertical: 8.0),
padding: EdgeInsets.only(
left: parseContext.indentLevel * indentSize,
),
margin: EdgeInsets.only(
top: 8.0,
bottom: 8.0,
left: parseContext.indentLevel * indentSize),
padding: EdgeInsets.all(2.0),
decoration: decoration,
child: RichText(
textAlign: textAlign,
Expand All @@ -631,6 +679,7 @@ class HtmlRichTextParser extends StatelessWidget {
parseContext.rootWidgetList.add(blockText);
nextContext.parentElement = blockText.child.text;
nextContext.spansOnly = true;
nextContext.inBlock = true;
}
}

Expand Down

0 comments on commit dc92077

Please sign in to comment.