Bug 1312050 - Make loadAndRegisterSheet log a helpful error message if the URI contains a potentially-truncating unescaped #id. r=dholbert
MozReview-Commit-ID: 21WnD8nJRsp
--- a/layout/base/nsStyleSheetService.cpp
+++ b/layout/base/nsStyleSheetService.cpp
@@ -16,16 +16,17 @@
#include "mozilla/dom/ContentParent.h"
#include "mozilla/ipc/URIUtils.h"
#include "nsIURI.h"
#include "nsCOMPtr.h"
#include "nsICategoryManager.h"
#include "nsISupportsPrimitives.h"
#include "nsISimpleEnumerator.h"
#include "nsNetUtil.h"
+#include "nsIConsoleService.h"
#include "nsIObserverService.h"
#include "nsLayoutStatics.h"
using namespace mozilla;
nsStyleSheetService *nsStyleSheetService::gInstance = nullptr;
nsStyleSheetService::nsStyleSheetService()
@@ -130,17 +131,37 @@ nsStyleSheetService::Init()
return NS_OK;
}
NS_IMETHODIMP
nsStyleSheetService::LoadAndRegisterSheet(nsIURI *aSheetURI,
uint32_t aSheetType)
{
- nsresult rv = LoadAndRegisterSheetInternal(aSheetURI, aSheetType);
+ // Warn developers if their stylesheet URL has a #ref at the end.
+ // Stylesheet URIs don't benefit from having a #ref suffix -- and if the
+ // sheet is a data URI, someone might've created this #ref by accident (and
+ // truncated their data-URI stylesheet) by using an unescaped # character in
+ // a #RRGGBB color or #foo() ID-selector in their data-URI representation.
+ bool hasRef;
+ nsresult rv = aSheetURI->GetHasRef(&hasRef);
+ NS_ENSURE_SUCCESS(rv, rv);
+ if (aSheetURI && hasRef) {
+ nsCOMPtr<nsIConsoleService> consoleService =
+ do_GetService(NS_CONSOLESERVICE_CONTRACTID);
+ NS_WARNING_ASSERTION(consoleService, "Failed to get console service!");
+ if (consoleService) {
+ const char16_t* message = u"nsStyleSheetService::LoadAndRegisterSheet: "
+ u"URI contains unescaped hash character, which might be truncating "
+ u"the sheet, if it's a data URI.";
+ consoleService->LogStringMessage(message);
+ }
+ }
+
+ rv = LoadAndRegisterSheetInternal(aSheetURI, aSheetType);
if (NS_SUCCEEDED(rv)) {
const char* message;
switch (aSheetType) {
case AGENT_SHEET:
message = "agent-sheet-added";
break;
case USER_SHEET:
message = "user-sheet-added";