Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to change the default base tag in documents #112

Merged
merged 3 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Include/RmlUi/Core/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ class RMLUICORE_API Context : public ScriptInterface
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.
Expand Down Expand Up @@ -239,13 +239,22 @@ class RMLUICORE_API Context : public ScriptInterface
/// @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;

private:
String name;
Vector2i dimensions;
float density_independent_pixel_ratio;
String documents_base_tag = "body";

ContextInstancer* instancer;

Expand Down
22 changes: 16 additions & 6 deletions Source/Core/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
10 changes: 7 additions & 3 deletions Source/Core/Factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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.");
Expand Down
1 change: 0 additions & 1 deletion Source/Core/XMLNodeHandlerBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion Source/Debugger/MenuSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";