htmlHasRelativeRefs function
文档里有没有相对资源引用(src / href / CSS url())。
只用来决定「baseUrl 迟到时要不要重载」:没有相对引用的页面,两次提交的内容 一模一样,重载只会让读者白挨一次闪烁(调用方据此决定要不要重新提交页面)。
属性只在标签内部找:脚本里 var href = a.getAttribute('href') 这种赋值
不是引用,拿整篇去扫会把「全内联的模板页」误判成有资源(踩过)。
Implementation
bool htmlHasRelativeRefs(String html) {
final RegExp attr = RegExp(
r'''(?:src|href)\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)''',
caseSensitive: false,
);
for (final RegExpMatch tag in RegExp(
r'<[a-zA-Z][^>]*>',
).allMatches(html)) {
for (final RegExpMatch match in attr.allMatches(tag.group(0)!)) {
if (_isRelativeRef(match.group(1))) {
return true;
}
}
}
final RegExp cssUrl = RegExp(
r'''url\(\s*("[^"]*"|'[^']*'|[^)]*)\)''',
caseSensitive: false,
);
for (final RegExpMatch match in cssUrl.allMatches(html)) {
if (_isRelativeRef(match.group(1))) {
return true;
}
}
return false;
}