|
| 1 | +import 'dart:collection'; |
| 2 | + |
| 3 | +import 'package:flutter/widgets.dart'; |
| 4 | +import 'package:flutter_html/html_parser.dart'; |
| 5 | +import 'package:flutter_html/src/html_elements.dart'; |
| 6 | +import 'package:html/dom.dart' as html; |
| 7 | + |
| 8 | +/// Provides information about the current element on the Html tree for |
| 9 | +/// an [Extension] to use. |
| 10 | +class ExtensionContext { |
| 11 | + /// The HTML node being represented as a Flutter widget. |
| 12 | + final html.Node node; |
| 13 | + |
| 14 | + /// Returns the name of the Html element, or an empty string if the node is |
| 15 | + /// a text content node, comment node, or any other node without a name. |
| 16 | + String get elementName { |
| 17 | + if (node is html.Element) { |
| 18 | + return (node as html.Element).localName ?? ''; |
| 19 | + } |
| 20 | + |
| 21 | + return ''; |
| 22 | + } |
| 23 | + |
| 24 | + /// Returns the HTML within this element, or an empty string if there is none. |
| 25 | + String get innerHtml { |
| 26 | + return node.sourceSpan?.text ?? ''; |
| 27 | + } |
| 28 | + |
| 29 | + /// Returns the list of child Elements on this html Node, or an empty list if |
| 30 | + /// there are no children. |
| 31 | + List<html.Element> get elementChildren { |
| 32 | + return node.children; |
| 33 | + } |
| 34 | + |
| 35 | + /// Returns a linked hash map representing the attributes of the node, or an |
| 36 | + /// empty map if it has no attributes. |
| 37 | + LinkedHashMap<String, String> get attributes { |
| 38 | + return LinkedHashMap.from(node.attributes.map((key, value) { |
| 39 | + // Key is either a String or html.AttributeName |
| 40 | + return MapEntry(key.toString(), value); |
| 41 | + })); |
| 42 | + } |
| 43 | + |
| 44 | + /// Returns the id of the element, or an empty string if it is not present |
| 45 | + String get id { |
| 46 | + if (node is html.Element) { |
| 47 | + return (node as html.Element).id; |
| 48 | + } |
| 49 | + |
| 50 | + return ''; |
| 51 | + } |
| 52 | + |
| 53 | + /// Returns a set of classes on the element, or an empty set if none are |
| 54 | + /// present. |
| 55 | + Set<String> get classes { |
| 56 | + if (node is html.Element) { |
| 57 | + return (node as html.Element).classes; |
| 58 | + } |
| 59 | + |
| 60 | + return <String>{}; |
| 61 | + } |
| 62 | + |
| 63 | + /// A reference to the [HtmlParser] instance. Useful for calling callbacks |
| 64 | + /// on the [Html] widget like [onLinkTap]. |
| 65 | + final HtmlParser parser; |
| 66 | + |
| 67 | + /// A reference to the [StyledElement] representation of this node. |
| 68 | + /// Guaranteed to be non-null only after the lexing step |
| 69 | + final StyledElement? styledElement; |
| 70 | + |
| 71 | + /// Guaranteed only when in the `parse` method of an Extension, but it might not necessarily be the nearest BuildContext. Probably should use a `Builder` Widget if you absolutely need the most relevant BuildContext. |
| 72 | + final BuildContext? context; |
| 73 | + |
| 74 | + /// Constructs a new [ExtensionContext] object with the given information. |
| 75 | + const ExtensionContext({ |
| 76 | + required this.node, |
| 77 | + required this.parser, |
| 78 | + this.styledElement, |
| 79 | + this.context, |
| 80 | + }); |
| 81 | +} |
0 commit comments