]> git.lyx.org Git - lyx.git/blobdiff - src/xml.cpp
Move <QTimer> from TocWidget.h
[lyx.git] / src / xml.cpp
index b4004af4cb400c71ccd413a1ded36efacb9a3de2..e50e55972b042b53ca6459b64ff371d62bf78efa 100644 (file)
@@ -28,6 +28,7 @@
 #include "support/lstrings.h"
 #include "support/textutils.h"
 
+#include <algorithm>
 #include <atomic>
 #include <map>
 #include <functional>
@@ -108,12 +109,9 @@ docstring StartTag::writeTag() const
 {
        docstring output = '<' + tag_;
        if (!attr_.empty()) {
-               docstring attributes = xml::escapeString(attr_, XMLStream::ESCAPE_NONE);
-               attributes.erase(attributes.begin(), std::find_if(attributes.begin(), attributes.end(),
-                                                          [](int c) {return !std::isspace(c);}));
-               if (!attributes.empty()) {
+               docstring attributes = xml::trimLeft(xml::escapeString(attr_, XMLStream::ESCAPE_NONE));
+               if (!attributes.empty())
                        output += ' ' + attributes;
-               }
        }
        output += ">";
        return output;
@@ -140,11 +138,11 @@ docstring EndTag::writeEndTag() const
 
 docstring CompTag::writeTag() const
 {
-       docstring output = '<' + from_utf8(tag_);
+       docstring output = '<' + tag_;
        if (!attr_.empty()) {
                // Erase the beginning of the attributes if it contains space characters: this function deals with that
                // automatically.
-               docstring attributes = escapeString(from_utf8(attr_), XMLStream::ESCAPE_NONE);
+               docstring attributes = escapeString(attr_, XMLStream::ESCAPE_NONE);
                attributes.erase(attributes.begin(), std::find_if(attributes.begin(), attributes.end(),
                                                           [](int c) {return !std::isspace(c);}));
                if (!attributes.empty()) {
@@ -167,17 +165,27 @@ bool FontTag::operator==(StartTag const & tag) const
 } // namespace xml
 
 
-void XMLStream::writeError(std::string const &s) const
+void XMLStream::writeError(std::string const &s)
 {
        LYXERR0(s);
-       os_ << from_utf8("<!-- Output Error: " + s + " -->\n");
+       *this << ESCAPE_NONE << from_utf8("<!-- Output Error: " + s + " -->");
+       *this << xml::CR();
 }
 
 
-void XMLStream::writeError(docstring const &s) const
+void XMLStream::writeError(docstring const &s)
 {
        LYXERR0(s);
-       os_ << from_utf8("<!-- Output Error: ") << s << from_utf8(" -->\n");
+       *this << ESCAPE_NONE << from_utf8("<!-- Output Error: ");
+       *this << s;
+       *this << ESCAPE_NONE << from_utf8(" -->");
+       *this << xml::CR();
+}
+
+
+XMLStream::TagPtr XMLStream::getLastStackTag()
+{
+       return tag_stack_.back();
 }
 
 
@@ -198,9 +206,6 @@ bool XMLStream::closeFontTags()
                if (**curtag != xml::parsep_tag)
                        os_ << (*curtag)->writeEndTag();
                tag_stack_.pop_back();
-               // this shouldn't happen, since then the font tags
-               // weren't in any other tag.
-               LASSERT(!tag_stack_.empty(), return true);
                if (tag_stack_.empty())
                        return true;
                curtag = &tag_stack_.back();
@@ -473,7 +478,8 @@ XMLStream &XMLStream::operator<<(xml::EndTag const &etag)
                                                   + "' when other tags were pending. Last pending tag is `"
                                                   + to_utf8(pending_tags_.back()->writeTag())
                                                   + "'. Tag discarded.");
-                               pending_tags_.erase(dit);
+                               if (!pending_tags_.empty())
+                                       pending_tags_.erase(dit);
                                return *this;
                        }
                }
@@ -594,6 +600,28 @@ docstring xml::uniqueID(docstring const & label)
 }
 
 
+bool xml::isNotOnlySpace(docstring const & str)
+{
+       for (auto const & c: str) {
+               if (c != ' ' && c != '\t' && c != '\n' && c != '\v' && c != '\f' && c != '\r')
+               return true;
+       }
+       return false;
+}
+
+
+docstring xml::trimLeft(docstring const & str)
+{
+       size_t i = 0;
+       for (auto const & c: str) {
+               if (c != ' ' && c != '\t' && c != '\n' && c != '\v' && c != '\f' && c != '\r')
+                       return str.substr(i, docstring::npos);
+               i++;
+       }
+       return str;
+}
+
+
 docstring xml::cleanID(docstring const & orig)
 {
        // The standard xml:id only allows letters, digits, '-' and '.' in a name.
@@ -641,7 +669,8 @@ docstring xml::cleanID(docstring const & orig)
        // as both of them would be transformed as "a.b". With this procedure, one will become "a.b" and the other "a.b-1".
        if (mangle && mangledNames.find(content) != mangledNames.end()) {
                int & mangleID = tMangleID.localData();
-               content += "-" + convert<docstring>(mangleID);
+               if (mangleID > 0)
+                       content += "-" + convert<docstring>(mangleID);
                mangleID += 1;
        }