Browse Source

Merge pull request #112 from aquawicket/documents_base_tag

Add ability to change the default base tag in documents
Michael R. P. Ragazzon 5 years ago
parent
commit
53924dddf0

+ 11 - 2
Include/RmlUi/Core/Context.h

@@ -88,9 +88,9 @@ public:
 	bool Render();
 
 	/// Creates a new, empty document and places it into this context.
-	/// @param[in] tag The document type to create.
+	/// @param[in] instancer_name The name of the instancer used to create the document.
 	/// @return The new document, or nullptr if no document could be created.
-	ElementDocument* CreateDocument(const String& tag = "body");
+	ElementDocument* CreateDocument(const String& instancer_name = "body");
 	/// Load a document into the context.
 	/// @param[in] document_path The path to the document to load.
 	/// @return The loaded document, or nullptr if no document was loaded.
@@ -239,6 +239,14 @@ public:
 	/// @return True if succesfully removed, false if no data model was found.
 	bool RemoveDataModel(const String& name);
 
+	/// This will set the documents base <tag> before creation. Default = "body"
+	/// @param[in] tag The name of the base tag. Example: "html"
+	void SetDocumentsBaseTag(const String& tag);
+
+	/// Gets the name of the documents base tag.
+	/// @return The current documents base tag name.
+	const String& GetDocumentsBaseTag();
+
 protected:
 	void Release() override;
 
@@ -246,6 +254,7 @@ private:
 	String name;
 	Vector2i dimensions;
 	float density_independent_pixel_ratio;
+	String documents_base_tag = "body";
 
 	ContextInstancer* instancer;
 

+ 16 - 6
Source/Core/Context.cpp

@@ -63,7 +63,7 @@ Context::Context(const String& name) : name(name), dimensions(0, 0), density_ind
 	root->SetOffset(Vector2f(0, 0), nullptr);
 	root->SetProperty(PropertyId::ZIndex, Property(0, Property::NUMBER));
 
-	cursor_proxy = Factory::InstanceElement(nullptr, "body", "body", XMLAttributes());
+	cursor_proxy = Factory::InstanceElement(nullptr, documents_base_tag, documents_base_tag, XMLAttributes());
 	ElementDocument* cursor_proxy_document = rmlui_dynamic_cast< ElementDocument* >(cursor_proxy.get());
 	if (cursor_proxy_document)
 		cursor_proxy_document->context = this;
@@ -214,20 +214,20 @@ bool Context::Render()
 	return true;
 }
 
-// Creates a new, empty document and places it into this context.
-ElementDocument* Context::CreateDocument(const String& tag)
+// Creates a new, empty document and places it into this context. 
+ElementDocument* Context::CreateDocument(const String& instancer_name)
 {
-	ElementPtr element = Factory::InstanceElement(nullptr, tag, "body", XMLAttributes());
+	ElementPtr element = Factory::InstanceElement(nullptr, instancer_name, documents_base_tag, XMLAttributes());
 	if (!element)
 	{
-		Log::Message(Log::LT_ERROR, "Failed to instance document on tag '%s', instancer returned nullptr.", tag.c_str());
+		Log::Message(Log::LT_ERROR, "Failed to instance document on instancer_name '%s', instancer returned nullptr.", instancer_name.c_str());
 		return nullptr;
 	}
 
 	ElementDocument* document = rmlui_dynamic_cast< ElementDocument* >(element.get());
 	if (!document)
 	{
-		Log::Message(Log::LT_ERROR, "Failed to instance document on tag '%s', Found type '%s', was expecting derivative of ElementDocument.", tag.c_str(), rmlui_type_name(*element));
+		Log::Message(Log::LT_ERROR, "Failed to instance document on instancer_name '%s', Found type '%s', was expecting derivative of ElementDocument.", instancer_name.c_str(), rmlui_type_name(*element));
 		return nullptr;
 	}
 
@@ -1298,4 +1298,14 @@ void Context::Release()
 	}
 }
 
+void Context::SetDocumentsBaseTag(const String& tag)
+{
+	documents_base_tag = tag;
+}
+
+const String& Context::GetDocumentsBaseTag()
+{
+	return documents_base_tag;
+}
+
 } // namespace Rml

+ 7 - 3
Source/Core/Factory.cpp

@@ -410,9 +410,12 @@ bool Factory::InstanceElementText(Element* parent, const String& in_text)
 	{
 		RMLUI_ZoneScopedNC("InstanceStream", 0xDC143C);
 		auto stream = MakeUnique<StreamMemory>(text.size() + 32);
-		stream->Write("<body>", 6);
+		String tag = parent->GetContext()->GetDocumentsBaseTag();
+		String open_tag = "<" + tag + ">";
+		String close_tag = "</" + tag + ">";
+		stream->Write(open_tag.c_str(), open_tag.size());
 		stream->Write(text);
-		stream->Write("</body>", 7);
+		stream->Write(close_tag.c_str(), close_tag.size());
 		stream->Seek(0, SEEK_SET);
 
 		InstanceElementStream(parent, stream.get());
@@ -464,8 +467,9 @@ bool Factory::InstanceElementStream(Element* parent, Stream* stream)
 ElementPtr Factory::InstanceDocumentStream(Context* context, Stream* stream)
 {
 	RMLUI_ZoneScoped;
+	RMLUI_ASSERT(context);
 
-	ElementPtr element = Factory::InstanceElement(nullptr, "body", "body", XMLAttributes());
+	ElementPtr element = Factory::InstanceElement(nullptr, context->GetDocumentsBaseTag(), context->GetDocumentsBaseTag(), XMLAttributes());
 	if (!element)
 	{
 		Log::Message(Log::LT_ERROR, "Failed to instance document, instancer returned nullptr.");

+ 0 - 1
Source/Core/XMLNodeHandlerBody.cpp

@@ -45,7 +45,6 @@ XMLNodeHandlerBody::~XMLNodeHandlerBody()
 Element* XMLNodeHandlerBody::ElementStart(XMLParser* parser, const String& RMLUI_UNUSED_ASSERT_PARAMETER(name), const XMLAttributes& attributes)
 {
 	RMLUI_UNUSED_ASSERT(name);
-	RMLUI_ASSERT(name == "body");
 
 	Element* element = parser->GetParseFrame()->element;
 

+ 1 - 1
Source/Debugger/MenuSource.h

@@ -89,5 +89,5 @@ static const char* menu_rml = R"RML(
 	<button id="event-log-button">Event Log</button>
 	<button id="debug-info-button">Element Info</button>
 	<button id="outlines-button">Outlines</button>
-</div>;
+</div>
 )RML";