unsafeReason static method
Retourne la raison pour laquelle value ne peut pas être écrite dans
une déclaration CSS, ou null si elle est sûre.
value est la valeur décodée (voir decodeArbitrary).
Implementation
static String? unsafeReason(String value) {
if (value.trim().isEmpty) return 'empty value';
if (value.length > maxLength) {
return 'value too long (more than $maxLength characters)';
}
int? quote;
var parens = 0;
// Profondeur de parenthèses à laquelle un `url(` est ouvert, ou -1. Un
// `;` y est permis : il fait partie de l'URL (`data:image/png;base64,…`).
var urlAt = -1;
for (var i = 0; i < value.length; i++) {
final code = value.codeUnitAt(i);
if (code < 0x20 || code == 0x7F) return 'control character not allowed';
if (code == _backslash) return 'character "\\" not allowed';
if (code == 0x3C) return 'character "<" not allowed';
if (code == 0x2F &&
i + 1 < value.length &&
value.codeUnitAt(i + 1) == 0x2A) {
return 'CSS comments are not allowed';
}
if (code == 0x2A &&
i + 1 < value.length &&
value.codeUnitAt(i + 1) == 0x2F) {
return 'CSS comments are not allowed';
}
if (quote != null) {
if (code == quote) quote = null;
continue;
}
switch (code) {
case _doubleQuote || _singleQuote:
quote = code;
case _openParen:
parens++;
if (urlAt < 0 && i >= 3 && _startsUrl(value, i - 3)) urlAt = parens;
case _closeParen:
if (parens == urlAt) urlAt = -1;
if (--parens < 0) return 'unbalanced parentheses';
case 0x3B: // ;
if (urlAt < 0) return 'character ";" not allowed';
case 0x7B || 0x7D: // { }
return 'braces are not allowed';
case 0x21: // !
return 'character "!" not allowed (use the class ! suffix)';
}
}
if (quote != null) return 'unclosed quotes';
if (parens != 0) return 'unbalanced parentheses';
final compact = value.toLowerCase().replaceAll(RegExp(r'\s+'), '');
if (compact.startsWith('@')) return '@ rule not allowed';
for (final entry in _forbidden.entries) {
if (compact.contains(entry.key)) return entry.value;
}
// `data:` n'est accepté que pour des images et des polices.
var index = compact.indexOf('data:');
while (index != -1) {
final rest = compact.substring(index + 5);
if (!rest.startsWith('image/') && !rest.startsWith('font/')) {
return 'only image or font data: URLs are allowed';
}
index = compact.indexOf('data:', index + 5);
}
return null;
}