]> git.lyx.org Git - features.git/blobdiff - src/xml.cpp
Improved character count statistics for letter based insets (e.g. the LyX logo).
[features.git] / src / xml.cpp
index 3864a3f4009426bc3be163d423e5d35bcfb0182e..e16a7840b6db099e1a8048da629bad979bff6b1c 100644 (file)
@@ -44,40 +44,30 @@ docstring escapeChar(char_type c, XMLStream::EscapeSettings e)
 {
        docstring str;
        switch (e) { // For HTML: always ESCAPE_NONE. For XML: it depends, hence the parameter.
-               case XMLStream::ESCAPE_NONE:
-                       str += c;
+       case XMLStream::ESCAPE_NONE:
+       case XMLStream::ESCAPE_COMMENTS:
+               str += c;
+               break;
+       case XMLStream::ESCAPE_ALL:
+               if (c == '<') {
+                       str += "&lt;";
                        break;
-               case XMLStream::ESCAPE_ALL:
-                       if (c == '<') {
-                               str += "&lt;";
-                               break;
-                       } else if (c == '>') {
-                               str += "&gt;";
-                               break;
-                       }
-                       // fall through
-               case XMLStream::ESCAPE_AND:
-                       if (c == '&')
-                               str += "&amp;";
-                       else
-                               str     +=c ;
+               } else if (c == '>') {
+                       str += "&gt;";
                        break;
+               }
+               // fall through
+       case XMLStream::ESCAPE_AND:
+               if (c == '&')
+                       str += "&amp;";
+               else
+                       str     +=c ;
+               break;
        }
        return str;
 }
 
 
-// escape what needs escaping
-docstring xmlize(docstring const &str, XMLStream::EscapeSettings e) {
-       odocstringstream d;
-       docstring::const_iterator it = str.begin();
-       docstring::const_iterator en = str.end();
-       for (; it != en; ++it)
-               d << escapeChar(*it, e);
-       return d.str();
-}
-
-
 docstring escapeChar(char c, XMLStream::EscapeSettings e)
 {
        LATTEST(static_cast<unsigned char>(c) < 0x80);
@@ -85,6 +75,22 @@ docstring escapeChar(char c, XMLStream::EscapeSettings e)
 }
 
 
+docstring escapeString(docstring const & raw, XMLStream::EscapeSettings e)
+{
+       docstring bin;
+       bin.reserve(raw.size() * 2); // crude approximation is sufficient
+       for (size_t i = 0; i != raw.size(); ++i) {
+               char_type c = raw[i];
+               if (e == XMLStream::ESCAPE_COMMENTS && c == '-' && i > 0 && raw[i - 1] == '-')
+                       bin += "&#45;";
+               else
+                       bin += xml::escapeChar(c, e);
+       }
+
+       return bin;
+}
+
+
 docstring cleanAttr(docstring const & str)
 {
        docstring newname;
@@ -102,7 +108,7 @@ docstring StartTag::writeTag() const
 {
        docstring output = '<' + tag_;
        if (!attr_.empty()) {
-               docstring attributes = xml::xmlize(attr_, XMLStream::ESCAPE_NONE);
+               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()) {
@@ -138,7 +144,7 @@ docstring CompTag::writeTag() const
        if (!attr_.empty()) {
                // Erase the beginning of the attributes if it contains space characters: this function deals with that
                // automatically.
-               docstring attributes = xmlize(from_utf8(attr_), XMLStream::ESCAPE_NONE);
+               docstring attributes = escapeString(from_utf8(attr_), XMLStream::ESCAPE_NONE);
                attributes.erase(attributes.begin(), std::find_if(attributes.begin(), attributes.end(),
                                                           [](int c) {return !std::isspace(c);}));
                if (!attributes.empty()) {
@@ -285,7 +291,7 @@ void XMLStream::clearTagDeque()
 XMLStream &XMLStream::operator<<(docstring const &d)
 {
        clearTagDeque();
-       os_ << xml::xmlize(d, escape_);
+       os_ << xml::escapeString(d, escape_);
        escape_ = ESCAPE_ALL;
        return *this;
 }
@@ -295,7 +301,7 @@ XMLStream &XMLStream::operator<<(const char *s)
 {
        clearTagDeque();
        docstring const d = from_ascii(s);
-       os_ << xml::xmlize(d, escape_);
+       os_ << xml::escapeString(d, escape_);
        escape_ = ESCAPE_ALL;
        return *this;
 }
@@ -470,7 +476,7 @@ XMLStream &XMLStream::operator<<(xml::EndTag const &etag)
                string estr = "Closing tag `" + to_utf8(etag.tag_)
                                          + "' when other tags are pending. Discarded pending tags:\n";
                for (dit = pending_tags_.begin(); dit != den; ++dit)
-                       estr += to_utf8(xml::xmlize((*dit)->writeTag(), XMLStream::ESCAPE_ALL)) + "\n";
+                       estr += to_utf8(xml::escapeString((*dit)->writeTag(), XMLStream::ESCAPE_ALL)) + "\n";
                writeError(estr);
                // clear the pending tags...
                pending_tags_.clear();
@@ -567,18 +573,7 @@ XMLStream &XMLStream::operator<<(xml::EndTag const &etag)
 }
 
 
-docstring xml::escapeString(docstring const & raw, XMLStream::EscapeSettings e)
-{
-       docstring bin;
-       bin.reserve(raw.size() * 2); // crude approximation is sufficient
-       for (size_t i = 0; i != raw.size(); ++i)
-               bin += xml::escapeChar(raw[i], e);
-
-       return bin;
-}
-
-
-docstring const xml::uniqueID(docstring const & label)
+docstring xml::uniqueID(docstring const & label)
 {
        // thread-safe
        static atomic_uint seed(1000);