parseString static method
- String string, {
- bool parseCharacterEntities = true,
- bool parseComments = false,
- bool trimWhitespace = true,
- bool parseCdataAsText = true,
- bool global = false,
- int start = 0,
- int? stop,
Returns a list of all XML conditional sections found in string
.
string
must not be null
.
If parseCharacterEntities
is true
, text values will be parsed
and replace all encoded character entities with their corresponding
character. parseCharacterEntities
must not be null
.
If parseComments
is true
, commments will be scrubbed
from string
before parsing.
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 parseCdataAsText
is true
, all CDATA sections will be
returned as XmlText nodes. parseCdataAsText
must not be null
.
If global
is true
, all conditional sections will be returned
regardless of whether they're nested within other conditional
sections. If false
, only the root level conditional sections
will be returned.
start
and stop
refer to the indexes of the identified conditional
sections. 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 conditional sections were found.
Implementation
static List<XmlConditional>? parseString(
String string, {
bool parseCharacterEntities = true,
bool parseComments = false,
bool trimWhitespace = true,
bool parseCdataAsText = true,
bool global = false,
int start = 0,
int? stop,
}) {
assert(start >= 0);
assert(stop == null || stop >= start);
if (!parseComments) string = string.removeComments();
if (trimWhitespace) string = string.trimWhitespace();
final matches =
Delimiters.conditional.copyWith(global: global).allMatches(string);
if (start >= matches.length) return null;
final conditionals = <XmlConditional>[];
for (var i = start; i < matches.length; i++) {
final match = matches[i];
final condition = match.namedGroup('condition');
if (condition == null) continue;
final value = match.namedGroup('value')?.trim();
if (value == null) continue;
conditionals.add(
XmlConditional(
condition: condition,
children: XmlNode.parseString(
value,
parseCharacterEntities: parseCharacterEntities,
parseComments: true,
trimWhitespace: false,
parseCdataAsText: true,
)!,
),
);
if (stop != null && i > stop) break;
}
if (conditionals.isEmpty) return null;
return conditionals;
}