Bläddra i källkod

ElementDocument::ReloadStyleSheet: More detailed description, pass nullptr context to avoid any potential use of it which will not be cleaned up.

Michael Ragazzon 5 år sedan
förälder
incheckning
baed8d6420

+ 2 - 0
Include/RmlUi/Core/ElementDocument.h

@@ -88,6 +88,8 @@ public:
 	/// Returns the document's style sheet.
 	const SharedPtr<StyleSheet>& GetStyleSheet() const override;
 	/// Reload the document's style sheet from source files.
+	/// Styles will be reloaded from <style> tags and external style sheets, but not inline 'style' attributes.
+	/// @note The source url originally used to load the document must still be a valid RML document.
 	void ReloadStyleSheet();
 
 	/// Brings the document to the front of the document stack.

+ 2 - 1
Include/RmlUi/Core/Factory.h

@@ -112,8 +112,9 @@ public:
 	/// Instances a document from a stream.
 	/// @param[in] context The context that is creating the document.
 	/// @param[in] stream The stream to instance from.
+	/// @param[in] document_base_tag The tag used to wrap the document, eg. 'rml'.
 	/// @return The instanced document, or nullptr if an error occurred.
-	static ElementPtr InstanceDocumentStream(Context* context, Stream* stream);
+	static ElementPtr InstanceDocumentStream(Context* context, Stream* stream, const String& document_base_tag);
 
 	/// Registers a non-owning pointer to an instancer that will be used to instance decorators.
 	/// @param[in] name The name of the decorator the instancer will be called for.

+ 1 - 1
Source/Core/Context.cpp

@@ -261,7 +261,7 @@ ElementDocument* Context::LoadDocument(Stream* stream)
 {
 	PluginRegistry::NotifyDocumentOpen(this, stream->GetSourceURL().GetURL());
 
-	ElementPtr element = Factory::InstanceDocumentStream(this, stream);
+	ElementPtr element = Factory::InstanceDocumentStream(this, stream, GetDocumentsBaseTag());
 	if (!element)
 		return nullptr;
 

+ 11 - 3
Source/Core/ElementDocument.cpp

@@ -213,14 +213,22 @@ const SharedPtr<StyleSheet>& ElementDocument::GetStyleSheet() const
 // Reload the document's style sheet from source files.
 void ElementDocument::ReloadStyleSheet()
 {
+	if (!context)
+		return;
+
 	auto stream = MakeUnique<StreamFile>();
-	if(!stream->Open(source_url))
+	if (!stream->Open(source_url))
+	{
+		Log::Message(Log::LT_WARNING, "Failed to open file to reload style sheet in document: %s", source_url.c_str());
 		return;
+	}
 
 	Factory::ClearStyleSheetCache();
-	ElementPtr temp_doc = Factory::InstanceDocumentStream(context, stream.get());
-	if(!temp_doc)
+	ElementPtr temp_doc = Factory::InstanceDocumentStream(nullptr, stream.get(), context->GetDocumentsBaseTag());
+	if (!temp_doc) {
+		Log::Message(Log::LT_WARNING, "Failed to reload style sheet, could not instance document: %s", source_url.c_str());
 		return;
+	}
 
 	SetStyleSheet(temp_doc->GetStyleSheet());
 }

+ 2 - 3
Source/Core/Factory.cpp

@@ -472,12 +472,11 @@ bool Factory::InstanceElementStream(Element* parent, Stream* stream)
 }
 
 // Instances a element tree based on the stream
-ElementPtr Factory::InstanceDocumentStream(Context* context, Stream* stream)
+ElementPtr Factory::InstanceDocumentStream(Context* context, Stream* stream, const String& document_base_tag)
 {
 	RMLUI_ZoneScoped;
-	RMLUI_ASSERT(context);
 
-	ElementPtr element = Factory::InstanceElement(nullptr, context->GetDocumentsBaseTag(), context->GetDocumentsBaseTag(), XMLAttributes());
+	ElementPtr element = Factory::InstanceElement(nullptr, document_base_tag, document_base_tag, XMLAttributes());
 	if (!element)
 	{
 		Log::Message(Log::LT_ERROR, "Failed to instance document, instancer returned nullptr.");