parseString static method

List<XmlAttribute>? parseString(
  1. String string, {
  2. bool parseCharacterEntities = true,
  3. bool trimWhitespace = true,
  4. int start = 0,
  5. int? stop,
})

Returns a list of every attribute found in string. string must not be null.

If trimWhitespace is true, unnecessary whitespace between nodes will be removed and all remaining whitespace will be replaced with a single space. trimWhitespace must not be null.

If parseCharacterEntities is true, attribute values will be parsed and replace all encoded character entities with their corresponding character. parseCharacterEntities must not be null.

start and stop refer to the indexes of the identified Attlist Declarations. Only matches found between start and stop will be returned. start must not be null and must be >= 0. stop may be null, but must be >= start if provided.

Returns null if no attributes are found.

Implementation

static List<XmlAttribute>? parseString(
  String string, {
  bool parseCharacterEntities = true,
  bool trimWhitespace = true,
  int start = 0,
  int? stop,
}) {
  assert(start >= 0);
  assert(stop == null || stop >= start);

  string = string.removeComments();
  if (trimWhitespace) string = string.trimWhitespace();

  final matches = Delimiters.attribute.allMatches(string);
  if (matches.isEmpty || start >= matches.length) return null;

  final attributes = <XmlAttribute>[];
  var attributeCount = 0;

  for (var match in matches) {
    final name = match.namedGroup('name');
    if (name == null) continue;
    if (attributeCount >= start) {
      var value = match.namedGroup('value');
      if (value != null) value = value.stripDelimiters();
      if (parseCharacterEntities) {
        value = HtmlCharacterEntities.decode(value!);
      }
      attributes.add(XmlAttribute(name, value!));
    }
    attributeCount++;
    if (stop != null && attributeCount > stop) break;
  }

  if (attributes.isEmpty) return null;

  return attributes;
}