From e4a5f5fd4250c5d851125a504abedd45531ed819 Mon Sep 17 00:00:00 2001 From: actboy168 Date: Fri, 6 Nov 2020 17:09:59 +0800 Subject: [PATCH 1/6] Fixes script loading order. --- Include/RmlUi/Core/ElementDocument.h | 13 +++++++---- Source/Core/DocumentHeader.cpp | 21 +++++++++++++++-- Source/Core/DocumentHeader.h | 17 ++++++++++---- Source/Core/ElementDocument.cpp | 35 ++++++++++++++++------------ Source/Core/XMLNodeHandlerHead.cpp | 4 ++-- Source/Lua/LuaDocument.cpp | 29 +++++++++++------------ Source/Lua/LuaDocument.h | 5 ++-- 7 files changed, 80 insertions(+), 44 deletions(-) diff --git a/Include/RmlUi/Core/ElementDocument.h b/Include/RmlUi/Core/ElementDocument.h index 9adbeb760..eb26f67bb 100644 --- a/Include/RmlUi/Core/ElementDocument.h +++ b/Include/RmlUi/Core/ElementDocument.h @@ -113,11 +113,16 @@ class RMLUICORE_API ElementDocument : public Element /// @return True if the document is hogging focus. bool IsModal() const; - /// Load a script into the document. Note that the base implementation does nothing, scripting language addons hook + /// Load a inline script into the document. Note that the base implementation does nothing, scripting language addons hook /// this method. - /// @param[in] stream Stream of code to process. - /// @param[in] source_name Name of the the script the source comes from, useful for debug information. - virtual void LoadScript(Stream* stream, const String& source_name); + /// @param[in] source The script source. + /// @param[in] line Line of the script the source comes from, useful for debug information. + virtual void LoadInlineScript(const String& source, int line); + + /// Load a external script into the document. Note that the base implementation does nothing, scripting language addons hook + /// this method. + /// @param[in] source_name The script file path. + virtual void LoadExternalScript(const String& source_name); /// Updates the document, including its layout. Users must call this manually before requesting information such as /// size or position of an element if any element in the document was recently changed, unless Context::Update has diff --git a/Source/Core/DocumentHeader.cpp b/Source/Core/DocumentHeader.cpp index 8e7bab11c..1953d8b12 100644 --- a/Source/Core/DocumentHeader.cpp +++ b/Source/Core/DocumentHeader.cpp @@ -46,12 +46,12 @@ void DocumentHeader::MergeHeader(const DocumentHeader& header) // Combine internal data rcss_inline.insert(rcss_inline.end(), header.rcss_inline.begin(), header.rcss_inline.end()); rcss_inline_line_numbers.insert(rcss_inline_line_numbers.end(), header.rcss_inline_line_numbers.begin(), header.rcss_inline_line_numbers.end()); - scripts_inline.insert(scripts_inline.end(), header.scripts_inline.begin(), header.scripts_inline.end()); + // Combine external data, keeping relative paths MergePaths(template_resources, header.template_resources, header.source); MergePaths(rcss_external, header.rcss_external, header.source); - MergePaths(scripts_external, header.scripts_external, header.source); + MergeReources(scripts, header.scripts, header.source); } void DocumentHeader::MergePaths(StringList& target, const StringList& source, const String& source_path) @@ -65,4 +65,21 @@ void DocumentHeader::MergePaths(StringList& target, const StringList& source, co } } +void DocumentHeader::MergeReources(Vector& target, const Vector& source, const String& base_path) +{ + for (auto const& s : source) + { + if (s.line) + { + target.push_back(s); + } + else + { + String joined_path; + ::Rml::GetSystemInterface()->JoinPath(joined_path, StringUtilities::Replace(base_path, '|', ':'), StringUtilities::Replace(s.context, '|', ':')); + target.push_back({StringUtilities::Replace(joined_path, ':', '|')}); + } + } +} + } // namespace Rml diff --git a/Source/Core/DocumentHeader.h b/Source/Core/DocumentHeader.h index b20b424a0..16d4d4837 100644 --- a/Source/Core/DocumentHeader.h +++ b/Source/Core/DocumentHeader.h @@ -52,16 +52,22 @@ class DocumentHeader /// A list of template resources that can used while parsing the document StringList template_resources; + struct Resource { + String context; + int line = 0; + }; /// Inline RCSS definitions StringList rcss_inline; LineNumberList rcss_inline_line_numbers; /// External RCSS definitions that should be loaded StringList rcss_external; - /// Inline script source - StringList scripts_inline; - /// External scripts that should be loaded - StringList scripts_external; + struct Script { + int line = 0; + String context; + }; + /// script source + Vector scripts; /// Merges the specified header with this one /// @param header Header to merge @@ -69,6 +75,9 @@ class DocumentHeader /// Merges paths from one string list to another, preserving the base_path void MergePaths(StringList& target, const StringList& source, const String& base_path); + + /// Merges reources + void MergeReources(Vector& target, const Vector& source, const String& base_path); }; } // namespace Rml diff --git a/Source/Core/ElementDocument.cpp b/Source/Core/ElementDocument.cpp index b01d07697..c60b0618b 100644 --- a/Source/Core/ElementDocument.cpp +++ b/Source/Core/ElementDocument.cpp @@ -126,19 +126,18 @@ void ElementDocument::ProcessHeader(const DocumentHeader* document_header) SetStyleSheet(std::move(new_style_sheet)); } - // Load external scripts. - for (size_t i = 0; i < header.scripts_external.size(); i++) + // Load scripts. + for (auto const& s : header.scripts) { - auto stream = MakeUnique(); - if (stream->Open(header.scripts_external[i])) - LoadScript(stream.get(), header.scripts_external[i]); - } - - // Load internal scripts. - for (size_t i = 0; i < header.scripts_inline.size(); i++) - { - auto stream = MakeUnique((const byte*) header.scripts_inline[i].c_str(), header.scripts_inline[i].size()); - LoadScript(stream.get(), ""); + if (s.line) + { + auto stream = MakeUnique((const byte*) s.context.c_str(), s.context.size()); + LoadInlineScript(s.context, s.line); + } + else + { + LoadExternalScript(s.context); + } } // Hide this document. @@ -341,10 +340,16 @@ bool ElementDocument::IsModal() const return modal && IsVisible(); } -// Default load script implementation -void ElementDocument::LoadScript(Stream* RMLUI_UNUSED_PARAMETER(stream), const String& RMLUI_UNUSED_PARAMETER(source_name)) +// Default load inline script implementation +void ElementDocument::LoadInlineScript(const String& RMLUI_UNUSED_PARAMETER(source), int RMLUI_UNUSED_PARAMETER(line)) +{ + RMLUI_UNUSED(source); + RMLUI_UNUSED(line); +} + +// Default load external script implementation +void ElementDocument::LoadExternalScript(const String& RMLUI_UNUSED_PARAMETER(source_name)) { - RMLUI_UNUSED(stream); RMLUI_UNUSED(source_name); } diff --git a/Source/Core/XMLNodeHandlerHead.cpp b/Source/Core/XMLNodeHandlerHead.cpp index c501f7ae4..927c12975 100644 --- a/Source/Core/XMLNodeHandlerHead.cpp +++ b/Source/Core/XMLNodeHandlerHead.cpp @@ -95,7 +95,7 @@ Element* XMLNodeHandlerHead::ElementStart(XMLParser* parser, const String& name, String src = Get(attributes, "src", ""); if (src.size() > 0) { - parser->GetDocumentHeader()->scripts_external.push_back(src); + parser->GetDocumentHeader()->scripts.push_back({src}); } } @@ -134,7 +134,7 @@ bool XMLNodeHandlerHead::ElementData(XMLParser* parser, const String& data, XMLD // Store an inline script if (tag == "script" && data.size() > 0) - parser->GetDocumentHeader()->scripts_inline.push_back(data); + parser->GetDocumentHeader()->scripts.push_back({data, parser->GetLineNumberOpenTag()}); // Store an inline style if (tag == "style" && data.size() > 0) diff --git a/Source/Lua/LuaDocument.cpp b/Source/Lua/LuaDocument.cpp index 4fc61e0bd..22c98da40 100644 --- a/Source/Lua/LuaDocument.cpp +++ b/Source/Lua/LuaDocument.cpp @@ -39,22 +39,21 @@ LuaDocument::LuaDocument(const String& tag) : ElementDocument(tag) { } -void LuaDocument::LoadScript(Stream* stream, const String& source_name) +void LuaDocument::LoadInlineScript(const String& source, int line) { - //if it is loaded from a file - if(!source_name.empty()) - { - Interpreter::LoadFile(source_name); - } - else - { - String buffer; - buffer += "--"; - buffer += this->GetSourceURL(); - buffer += "\n"; - stream->Read(buffer,stream->Length()); //just do the whole thing - Interpreter::DoString(buffer, buffer); - } + String buffer; + buffer += "--"; + buffer += this->GetSourceURL(); + buffer += ":"; + buffer += std::to_string(line); + buffer += "\n"; + buffer += source; + Interpreter::DoString(buffer, buffer); +} + +void LuaDocument::LoadExternalScript(const String& source_name) +{ + Interpreter::LoadFile(source_name); } } // namespace Lua diff --git a/Source/Lua/LuaDocument.h b/Source/Lua/LuaDocument.h index 3497dd771..52436d964 100644 --- a/Source/Lua/LuaDocument.h +++ b/Source/Lua/LuaDocument.h @@ -29,7 +29,7 @@ #ifndef RMLUI_LUA_LUADOCUMENT_H #define RMLUI_LUA_LUADOCUMENT_H /* - This class is an ElementDocument that overrides the LoadScript function + This class is an ElementDocument that overrides the LoadInlineScript and LoadExternalScript function */ #include @@ -40,7 +40,8 @@ class LuaDocument : public ::Rml::ElementDocument { public: LuaDocument(const String& tag); - void LoadScript(Stream* stream, const String& source_name) override; + void LoadInlineScript(const String& source, int line) override; + void LoadExternalScript(const String& source_name) override; }; } // namespace Lua From b4e23fe58a1478b177819b5c3a9cbabd7b26d096 Mon Sep 17 00:00:00 2001 From: actboy168 Date: Fri, 6 Nov 2020 17:40:40 +0800 Subject: [PATCH 2/6] cleanup --- Source/Core/DocumentHeader.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Source/Core/DocumentHeader.h b/Source/Core/DocumentHeader.h index 16d4d4837..bdc9c3777 100644 --- a/Source/Core/DocumentHeader.h +++ b/Source/Core/DocumentHeader.h @@ -62,10 +62,6 @@ class DocumentHeader /// External RCSS definitions that should be loaded StringList rcss_external; - struct Script { - int line = 0; - String context; - }; /// script source Vector scripts; From ce327aedef4189327354bff93f2a6c50bfb0f207 Mon Sep 17 00:00:00 2001 From: actboy168 Date: Mon, 9 Nov 2020 10:04:49 +0800 Subject: [PATCH 3/6] Some fixes. --- Source/Core/DocumentHeader.cpp | 26 ++++++++++++++------------ Source/Core/DocumentHeader.h | 11 +++++++---- Source/Core/ElementDocument.cpp | 9 ++++----- Source/Core/XMLNodeHandlerHead.cpp | 2 +- Source/Lua/LuaDocument.cpp | 2 +- 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/Source/Core/DocumentHeader.cpp b/Source/Core/DocumentHeader.cpp index 1953d8b12..187d7c30c 100644 --- a/Source/Core/DocumentHeader.cpp +++ b/Source/Core/DocumentHeader.cpp @@ -34,6 +34,13 @@ namespace Rml { +static String MergePath(const String& source, const String& base) +{ + String joined_path; + ::Rml::GetSystemInterface()->JoinPath(joined_path, StringUtilities::Replace(base, '|', ':'), StringUtilities::Replace(source, '|', ':')); + return StringUtilities::Replace(joined_path, ':', '|'); +} + void DocumentHeader::MergeHeader(const DocumentHeader& header) { // Copy the title across if ours is empty @@ -51,33 +58,28 @@ void DocumentHeader::MergeHeader(const DocumentHeader& header) // Combine external data, keeping relative paths MergePaths(template_resources, header.template_resources, header.source); MergePaths(rcss_external, header.rcss_external, header.source); - MergeReources(scripts, header.scripts, header.source); + MergeResources(scripts, header.scripts, header.source); } void DocumentHeader::MergePaths(StringList& target, const StringList& source, const String& source_path) { for (size_t i = 0; i < source.size(); i++) { - String joined_path; - ::Rml::GetSystemInterface()->JoinPath(joined_path, StringUtilities::Replace(source_path, '|', ':'), StringUtilities::Replace(source[i], '|', ':')); - - target.push_back(StringUtilities::Replace(joined_path, ':', '|')); + target.push_back(MergePath(source[i], source_path)); } } -void DocumentHeader::MergeReources(Vector& target, const Vector& source, const String& base_path) +void DocumentHeader::MergeResources(ResourceList& target, const ResourceList& source, const String& base_path) { - for (auto const& s : source) + for (const Resource& script : source) { - if (s.line) + if (script.is_inline) { - target.push_back(s); + target.push_back(script); } else { - String joined_path; - ::Rml::GetSystemInterface()->JoinPath(joined_path, StringUtilities::Replace(base_path, '|', ':'), StringUtilities::Replace(s.context, '|', ':')); - target.push_back({StringUtilities::Replace(joined_path, ':', '|')}); + target.push_back({MergePath(script.content_or_path, base_path)}); } } } diff --git a/Source/Core/DocumentHeader.h b/Source/Core/DocumentHeader.h index bdc9c3777..06353ce02 100644 --- a/Source/Core/DocumentHeader.h +++ b/Source/Core/DocumentHeader.h @@ -53,9 +53,12 @@ class DocumentHeader StringList template_resources; struct Resource { - String context; - int line = 0; + String content_or_path; // Content for inline resources, path for external resources. + bool is_inline = false; + int line = 0; // Only set for inline resources. }; + using ResourceList = Vector; + /// Inline RCSS definitions StringList rcss_inline; LineNumberList rcss_inline_line_numbers; @@ -72,8 +75,8 @@ class DocumentHeader /// Merges paths from one string list to another, preserving the base_path void MergePaths(StringList& target, const StringList& source, const String& base_path); - /// Merges reources - void MergeReources(Vector& target, const Vector& source, const String& base_path); + /// Merges resources + void MergeResources(ResourceList& target, const ResourceList& source, const String& base_path); }; } // namespace Rml diff --git a/Source/Core/ElementDocument.cpp b/Source/Core/ElementDocument.cpp index c60b0618b..db304afe2 100644 --- a/Source/Core/ElementDocument.cpp +++ b/Source/Core/ElementDocument.cpp @@ -127,16 +127,15 @@ void ElementDocument::ProcessHeader(const DocumentHeader* document_header) } // Load scripts. - for (auto const& s : header.scripts) + for (const DocumentHeader::Resource& script : header.scripts) { - if (s.line) + if (script.is_inline) { - auto stream = MakeUnique((const byte*) s.context.c_str(), s.context.size()); - LoadInlineScript(s.context, s.line); + LoadInlineScript(script.content_or_path, script.line); } else { - LoadExternalScript(s.context); + LoadExternalScript(script.content_or_path); } } diff --git a/Source/Core/XMLNodeHandlerHead.cpp b/Source/Core/XMLNodeHandlerHead.cpp index 927c12975..7d766e9a9 100644 --- a/Source/Core/XMLNodeHandlerHead.cpp +++ b/Source/Core/XMLNodeHandlerHead.cpp @@ -134,7 +134,7 @@ bool XMLNodeHandlerHead::ElementData(XMLParser* parser, const String& data, XMLD // Store an inline script if (tag == "script" && data.size() > 0) - parser->GetDocumentHeader()->scripts.push_back({data, parser->GetLineNumberOpenTag()}); + parser->GetDocumentHeader()->scripts.push_back({data, true, parser->GetLineNumberOpenTag()}); // Store an inline style if (tag == "style" && data.size() > 0) diff --git a/Source/Lua/LuaDocument.cpp b/Source/Lua/LuaDocument.cpp index 22c98da40..eca57c753 100644 --- a/Source/Lua/LuaDocument.cpp +++ b/Source/Lua/LuaDocument.cpp @@ -45,7 +45,7 @@ void LuaDocument::LoadInlineScript(const String& source, int line) buffer += "--"; buffer += this->GetSourceURL(); buffer += ":"; - buffer += std::to_string(line); + buffer += Rml::ToString(line); buffer += "\n"; buffer += source; Interpreter::DoString(buffer, buffer); From 031b3fa074a707bde1a8cbbb8432326f952380fd Mon Sep 17 00:00:00 2001 From: actboy168 Date: Mon, 9 Nov 2020 11:18:02 +0800 Subject: [PATCH 4/6] Fix the source path error after merging templates --- Include/RmlUi/Core/ElementDocument.h | 11 ++++++----- Source/Core/DocumentHeader.cpp | 16 +++------------- Source/Core/DocumentHeader.h | 5 +++-- Source/Core/ElementDocument.cpp | 13 +++++++------ Source/Core/XMLNodeHandlerHead.cpp | 18 ++++++++++++++++-- Source/Lua/LuaDocument.cpp | 12 ++++++------ Source/Lua/LuaDocument.h | 4 ++-- 7 files changed, 43 insertions(+), 36 deletions(-) diff --git a/Include/RmlUi/Core/ElementDocument.h b/Include/RmlUi/Core/ElementDocument.h index eb26f67bb..61bb587f3 100644 --- a/Include/RmlUi/Core/ElementDocument.h +++ b/Include/RmlUi/Core/ElementDocument.h @@ -115,14 +115,15 @@ class RMLUICORE_API ElementDocument : public Element /// Load a inline script into the document. Note that the base implementation does nothing, scripting language addons hook /// this method. - /// @param[in] source The script source. - /// @param[in] line Line of the script the source comes from, useful for debug information. - virtual void LoadInlineScript(const String& source, int line); + /// @param[in] content The script content. + /// @param[in] source_path Path of the script the source comes from, useful for debug information. + /// @param[in] source_line Line of the script the source comes from, useful for debug information. + virtual void LoadInlineScript(const String& content, const String& source_path, int source_line); /// Load a external script into the document. Note that the base implementation does nothing, scripting language addons hook /// this method. - /// @param[in] source_name The script file path. - virtual void LoadExternalScript(const String& source_name); + /// @param[in] source_path The script file path. + virtual void LoadExternalScript(const String& source_path); /// Updates the document, including its layout. Users must call this manually before requesting information such as /// size or position of an element if any element in the document was recently changed, unless Context::Update has diff --git a/Source/Core/DocumentHeader.cpp b/Source/Core/DocumentHeader.cpp index 187d7c30c..e1e4ca128 100644 --- a/Source/Core/DocumentHeader.cpp +++ b/Source/Core/DocumentHeader.cpp @@ -58,7 +58,7 @@ void DocumentHeader::MergeHeader(const DocumentHeader& header) // Combine external data, keeping relative paths MergePaths(template_resources, header.template_resources, header.source); MergePaths(rcss_external, header.rcss_external, header.source); - MergeResources(scripts, header.scripts, header.source); + MergeResources(scripts, header.scripts); } void DocumentHeader::MergePaths(StringList& target, const StringList& source, const String& source_path) @@ -69,19 +69,9 @@ void DocumentHeader::MergePaths(StringList& target, const StringList& source, co } } -void DocumentHeader::MergeResources(ResourceList& target, const ResourceList& source, const String& base_path) +void DocumentHeader::MergeResources(ResourceList& target, const ResourceList& source) { - for (const Resource& script : source) - { - if (script.is_inline) - { - target.push_back(script); - } - else - { - target.push_back({MergePath(script.content_or_path, base_path)}); - } - } + target.insert(target.end(), source.begin(), source.end()); } } // namespace Rml diff --git a/Source/Core/DocumentHeader.h b/Source/Core/DocumentHeader.h index 06353ce02..8a182f6fe 100644 --- a/Source/Core/DocumentHeader.h +++ b/Source/Core/DocumentHeader.h @@ -53,7 +53,8 @@ class DocumentHeader StringList template_resources; struct Resource { - String content_or_path; // Content for inline resources, path for external resources. + String path; // Content path for inline resources, source path for external resources. + String content; // Only set for inline resources. bool is_inline = false; int line = 0; // Only set for inline resources. }; @@ -76,7 +77,7 @@ class DocumentHeader void MergePaths(StringList& target, const StringList& source, const String& base_path); /// Merges resources - void MergeResources(ResourceList& target, const ResourceList& source, const String& base_path); + void MergeResources(ResourceList& target, const ResourceList& source); }; } // namespace Rml diff --git a/Source/Core/ElementDocument.cpp b/Source/Core/ElementDocument.cpp index db304afe2..bf1c51d30 100644 --- a/Source/Core/ElementDocument.cpp +++ b/Source/Core/ElementDocument.cpp @@ -131,11 +131,11 @@ void ElementDocument::ProcessHeader(const DocumentHeader* document_header) { if (script.is_inline) { - LoadInlineScript(script.content_or_path, script.line); + LoadInlineScript(script.content, script.path, script.line); } else { - LoadExternalScript(script.content_or_path); + LoadExternalScript(script.path); } } @@ -340,16 +340,17 @@ bool ElementDocument::IsModal() const } // Default load inline script implementation -void ElementDocument::LoadInlineScript(const String& RMLUI_UNUSED_PARAMETER(source), int RMLUI_UNUSED_PARAMETER(line)) +void ElementDocument::LoadInlineScript(const String& RMLUI_UNUSED_PARAMETER(content), const String& RMLUI_UNUSED_PARAMETER(source_path), int RMLUI_UNUSED_PARAMETER(line)) { - RMLUI_UNUSED(source); + RMLUI_UNUSED(content); + RMLUI_UNUSED(source_path); RMLUI_UNUSED(line); } // Default load external script implementation -void ElementDocument::LoadExternalScript(const String& RMLUI_UNUSED_PARAMETER(source_name)) +void ElementDocument::LoadExternalScript(const String& RMLUI_UNUSED_PARAMETER(source_path)) { - RMLUI_UNUSED(source_name); + RMLUI_UNUSED(source_path); } // Updates the document, including its layout diff --git a/Source/Core/XMLNodeHandlerHead.cpp b/Source/Core/XMLNodeHandlerHead.cpp index 7d766e9a9..716d4cdfe 100644 --- a/Source/Core/XMLNodeHandlerHead.cpp +++ b/Source/Core/XMLNodeHandlerHead.cpp @@ -39,6 +39,13 @@ namespace Rml { +static String Absolutepath(const String& source, const String& base) +{ + String joined_path; + ::Rml::GetSystemInterface()->JoinPath(joined_path, StringUtilities::Replace(base, '|', ':'), StringUtilities::Replace(source, '|', ':')); + return StringUtilities::Replace(joined_path, ':', '|'); +} + XMLNodeHandlerHead::XMLNodeHandlerHead() { } @@ -95,7 +102,7 @@ Element* XMLNodeHandlerHead::ElementStart(XMLParser* parser, const String& name, String src = Get(attributes, "src", ""); if (src.size() > 0) { - parser->GetDocumentHeader()->scripts.push_back({src}); + parser->GetDocumentHeader()->scripts.push_back({Absolutepath(src, parser->GetSourceURL().GetURL())}); } } @@ -134,7 +141,14 @@ bool XMLNodeHandlerHead::ElementData(XMLParser* parser, const String& data, XMLD // Store an inline script if (tag == "script" && data.size() > 0) - parser->GetDocumentHeader()->scripts.push_back({data, true, parser->GetLineNumberOpenTag()}); + { + DocumentHeader::Resource resource; + resource.is_inline = true; + resource.content = data; + resource.path = parser->GetSourceURL().GetURL(); + resource.line = parser->GetLineNumberOpenTag(); + parser->GetDocumentHeader()->scripts.push_back(resource); + } // Store an inline style if (tag == "style" && data.size() > 0) diff --git a/Source/Lua/LuaDocument.cpp b/Source/Lua/LuaDocument.cpp index eca57c753..57e3fbe04 100644 --- a/Source/Lua/LuaDocument.cpp +++ b/Source/Lua/LuaDocument.cpp @@ -39,21 +39,21 @@ LuaDocument::LuaDocument(const String& tag) : ElementDocument(tag) { } -void LuaDocument::LoadInlineScript(const String& source, int line) +void LuaDocument::LoadInlineScript(const String& context, const String& source_path, int source_line) { String buffer; buffer += "--"; - buffer += this->GetSourceURL(); + buffer += source_path; buffer += ":"; - buffer += Rml::ToString(line); + buffer += Rml::ToString(source_line); buffer += "\n"; - buffer += source; + buffer += context; Interpreter::DoString(buffer, buffer); } -void LuaDocument::LoadExternalScript(const String& source_name) +void LuaDocument::LoadExternalScript(const String& source_path) { - Interpreter::LoadFile(source_name); + Interpreter::LoadFile(source_path); } } // namespace Lua diff --git a/Source/Lua/LuaDocument.h b/Source/Lua/LuaDocument.h index 52436d964..5c5700895 100644 --- a/Source/Lua/LuaDocument.h +++ b/Source/Lua/LuaDocument.h @@ -40,8 +40,8 @@ class LuaDocument : public ::Rml::ElementDocument { public: LuaDocument(const String& tag); - void LoadInlineScript(const String& source, int line) override; - void LoadExternalScript(const String& source_name) override; + void LoadInlineScript(const String& content, const String& source_path, int source_line) override; + void LoadExternalScript(const String& source_path) override; }; } // namespace Lua From 0e8691aa03dc94a6f64f70de018e03d60f9c2a4d Mon Sep 17 00:00:00 2001 From: actboy168 Date: Mon, 9 Nov 2020 11:39:59 +0800 Subject: [PATCH 5/6] Fixes rcss loading order. --- Source/Core/DocumentHeader.cpp | 19 ++++--------- Source/Core/DocumentHeader.h | 9 +++---- Source/Core/ElementDocument.cpp | 43 +++++++++++++++++++++--------- Source/Core/XMLNodeHandlerHead.cpp | 29 +++++++++++++------- 4 files changed, 57 insertions(+), 43 deletions(-) diff --git a/Source/Core/DocumentHeader.cpp b/Source/Core/DocumentHeader.cpp index e1e4ca128..1d3b10665 100644 --- a/Source/Core/DocumentHeader.cpp +++ b/Source/Core/DocumentHeader.cpp @@ -34,13 +34,6 @@ namespace Rml { -static String MergePath(const String& source, const String& base) -{ - String joined_path; - ::Rml::GetSystemInterface()->JoinPath(joined_path, StringUtilities::Replace(base, '|', ':'), StringUtilities::Replace(source, '|', ':')); - return StringUtilities::Replace(joined_path, ':', '|'); -} - void DocumentHeader::MergeHeader(const DocumentHeader& header) { // Copy the title across if ours is empty @@ -50,14 +43,9 @@ void DocumentHeader::MergeHeader(const DocumentHeader& header) if (source.empty()) source = header.source; - // Combine internal data - rcss_inline.insert(rcss_inline.end(), header.rcss_inline.begin(), header.rcss_inline.end()); - rcss_inline_line_numbers.insert(rcss_inline_line_numbers.end(), header.rcss_inline_line_numbers.begin(), header.rcss_inline_line_numbers.end()); - - // Combine external data, keeping relative paths MergePaths(template_resources, header.template_resources, header.source); - MergePaths(rcss_external, header.rcss_external, header.source); + MergeResources(rcss, header.rcss); MergeResources(scripts, header.scripts); } @@ -65,7 +53,10 @@ void DocumentHeader::MergePaths(StringList& target, const StringList& source, co { for (size_t i = 0; i < source.size(); i++) { - target.push_back(MergePath(source[i], source_path)); + String joined_path; + ::Rml::GetSystemInterface()->JoinPath(joined_path, StringUtilities::Replace(source_path, '|', ':'), StringUtilities::Replace(source[i], '|', ':')); + + target.push_back(StringUtilities::Replace(joined_path, ':', '|')); } } diff --git a/Source/Core/DocumentHeader.h b/Source/Core/DocumentHeader.h index 8a182f6fe..85cf75e04 100644 --- a/Source/Core/DocumentHeader.h +++ b/Source/Core/DocumentHeader.h @@ -60,14 +60,11 @@ class DocumentHeader }; using ResourceList = Vector; - /// Inline RCSS definitions - StringList rcss_inline; - LineNumberList rcss_inline_line_numbers; - /// External RCSS definitions that should be loaded - StringList rcss_external; + /// RCSS definitions + ResourceList rcss; /// script source - Vector scripts; + ResourceList scripts; /// Merges the specified header with this one /// @param header Header to merge diff --git a/Source/Core/ElementDocument.cpp b/Source/Core/ElementDocument.cpp index bf1c51d30..7072e46bb 100644 --- a/Source/Core/ElementDocument.cpp +++ b/Source/Core/ElementDocument.cpp @@ -96,28 +96,45 @@ void ElementDocument::ProcessHeader(const DocumentHeader* document_header) // If a style-sheet (or sheets) has been specified for this element, then we load them and set the combined sheet // on the element; all of its children will inherit it by default. SharedPtr new_style_sheet; - if (header.rcss_external.size() > 0) - new_style_sheet = StyleSheetFactory::GetStyleSheet(header.rcss_external); // Combine any inline sheets. - for (size_t i = 0; i < header.rcss_inline.size(); i++) + for (const DocumentHeader::Resource& rcss : header.rcss) { - UniquePtr inline_sheet = MakeUnique(); - auto stream = MakeUnique((const byte*)header.rcss_inline[i].c_str(), header.rcss_inline[i].size()); - stream->SetSourceURL(document_header->source); + if (rcss.is_inline) + { + UniquePtr inline_sheet = MakeUnique(); + auto stream = MakeUnique((const byte*)rcss.content.c_str(), rcss.content.size()); + stream->SetSourceURL(rcss.path); + + if (inline_sheet->LoadStyleSheet(stream.get(), rcss.line)) + { + if (new_style_sheet) + { + SharedPtr combined_sheet = new_style_sheet->CombineStyleSheet(*inline_sheet); + new_style_sheet = combined_sheet; + } + else + new_style_sheet = std::move(inline_sheet); + } - if (inline_sheet->LoadStyleSheet(stream.get(), header.rcss_inline_line_numbers[i])) + stream.reset(); + } + else { - if (new_style_sheet) + SharedPtr sub_sheet = StyleSheetFactory::GetStyleSheet(rcss.path); + if (sub_sheet) { - SharedPtr combined_sheet = new_style_sheet->CombineStyleSheet(*inline_sheet); - new_style_sheet = combined_sheet; + if (new_style_sheet) + { + SharedPtr combined_sheet = new_style_sheet->CombineStyleSheet(*sub_sheet); + new_style_sheet = std::move(combined_sheet); + } + else + new_style_sheet = sub_sheet; } else - new_style_sheet = std::move(inline_sheet); + Log::Message(Log::LT_ERROR, "Failed to load style sheet %s.", rcss.path.c_str()); } - - stream.reset(); } // If a style sheet is available, set it on the document and release it. diff --git a/Source/Core/XMLNodeHandlerHead.cpp b/Source/Core/XMLNodeHandlerHead.cpp index 716d4cdfe..6d391b155 100644 --- a/Source/Core/XMLNodeHandlerHead.cpp +++ b/Source/Core/XMLNodeHandlerHead.cpp @@ -46,6 +46,21 @@ static String Absolutepath(const String& source, const String& base) return StringUtilities::Replace(joined_path, ':', '|'); } +static DocumentHeader::Resource MakeInlineResource(XMLParser* parser, const String& data) +{ + DocumentHeader::Resource resource; + resource.is_inline = true; + resource.content = data; + resource.path = parser->GetSourceURL().GetURL(); + resource.line = parser->GetLineNumberOpenTag(); + return resource; +} + +static DocumentHeader::Resource MakeExternalResource(XMLParser* parser, const String& path) +{ + return {Absolutepath(path, parser->GetSourceURL().GetURL())}; +} + XMLNodeHandlerHead::XMLNodeHandlerHead() { } @@ -75,7 +90,7 @@ Element* XMLNodeHandlerHead::ElementStart(XMLParser* parser, const String& name, if (type == "text/rcss" || type == "text/css") { - parser->GetDocumentHeader()->rcss_external.push_back(href); + parser->GetDocumentHeader()->rcss.push_back(MakeExternalResource(parser, href)); } // If its an template, add to the template fields @@ -102,7 +117,7 @@ Element* XMLNodeHandlerHead::ElementStart(XMLParser* parser, const String& name, String src = Get(attributes, "src", ""); if (src.size() > 0) { - parser->GetDocumentHeader()->scripts.push_back({Absolutepath(src, parser->GetSourceURL().GetURL())}); + parser->GetDocumentHeader()->scripts.push_back(MakeExternalResource(parser, src)); } } @@ -142,19 +157,13 @@ bool XMLNodeHandlerHead::ElementData(XMLParser* parser, const String& data, XMLD // Store an inline script if (tag == "script" && data.size() > 0) { - DocumentHeader::Resource resource; - resource.is_inline = true; - resource.content = data; - resource.path = parser->GetSourceURL().GetURL(); - resource.line = parser->GetLineNumberOpenTag(); - parser->GetDocumentHeader()->scripts.push_back(resource); + parser->GetDocumentHeader()->scripts.push_back(MakeInlineResource(parser, data)); } // Store an inline style if (tag == "style" && data.size() > 0) { - parser->GetDocumentHeader()->rcss_inline.push_back(data); - parser->GetDocumentHeader()->rcss_inline_line_numbers.push_back(parser->GetLineNumberOpenTag()); + parser->GetDocumentHeader()->rcss.push_back(MakeInlineResource(parser, data)); } return true; From 749db72b28877fe9a2ba5c27110412817c145bab Mon Sep 17 00:00:00 2001 From: actboy168 Date: Mon, 9 Nov 2020 13:10:17 +0800 Subject: [PATCH 6/6] Fixes warnning --- Source/Core/XMLNodeHandlerHead.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Source/Core/XMLNodeHandlerHead.cpp b/Source/Core/XMLNodeHandlerHead.cpp index 6d391b155..8f44b4273 100644 --- a/Source/Core/XMLNodeHandlerHead.cpp +++ b/Source/Core/XMLNodeHandlerHead.cpp @@ -58,7 +58,10 @@ static DocumentHeader::Resource MakeInlineResource(XMLParser* parser, const Stri static DocumentHeader::Resource MakeExternalResource(XMLParser* parser, const String& path) { - return {Absolutepath(path, parser->GetSourceURL().GetURL())}; + DocumentHeader::Resource resource; + resource.is_inline = false; + resource.path = Absolutepath(path, parser->GetSourceURL().GetURL()); + return resource; } XMLNodeHandlerHead::XMLNodeHandlerHead()