diff --git a/DataStructures/SearchEngine.h b/DataStructures/SearchEngine.h index e303f3433..75ad9065c 100644 --- a/DataStructures/SearchEngine.h +++ b/DataStructures/SearchEngine.h @@ -295,12 +295,12 @@ public: return ed.distance; } - inline std::string &GetNameForNameID(const NodeID nameID) const { + inline std::string &GetUnescapedNameForNameID(const NodeID nameID) const { return (nameID >= _names->size() ? _names->at(0) : _names->at(nameID) ); } inline std::string GetEscapedNameForNameID(const NodeID nameID) const { - return (nameID >= _names->size() ? _names->at(0) : replaceAll(_names->at(nameID), "\"", "\\\"") ); + return ( nameID >= _names->size() ? std::string("") : HTMLEntitize(_names->at(nameID)) ); } inline short GetTypeOfEdgeForOriginDestinationNodeID(NodeID s, NodeID t) const { diff --git a/Util/StrIngUtil.h b/Util/StrIngUtil.h index 492f9d0af..4de6ba96a 100644 --- a/Util/StrIngUtil.h +++ b/Util/StrIngUtil.h @@ -79,12 +79,31 @@ inline void doubleToString(const double value, std::string & output) output = buffer ; } +inline std::string & replaceAll(std::string &s, const std::string &sub, const std::string &other) { + assert(!sub.empty()); + size_t b = 0; + for (;;) { + b = s.find(sub, b); + if (b == s.npos) break; + s.replace(b, sub.size(), other); + b += other.size(); + } + return s; +} -std::string replaceAll( std::string result, const std::string& replaceWhat, const std::string& replaceWithWhat) { - while(true) { - const int pos = result.find(replaceWhat); - if (pos==-1) break; - result.replace(pos,replaceWhat.size(),replaceWithWhat); +std::string originals[] = {"&", "\"", "<", ">", "'", "[", "]"}; +std::string entities[] = {"&", """, "<", ">", "'", "&91;", "&93;" }; + +std::string HTMLEntitize( std::string result) { + for(unsigned i = 0; i < sizeof(originals)/sizeof(std::string); i++) { + result = replaceAll(result, originals[i], entities[i]); + } + return result; +} + +std::string HTMLDeEntitize( std::string result) { + for(unsigned i = 0; i < sizeof(originals)/sizeof(std::string); i++) { + result = replaceAll(result, entities[i], originals[i]); } return result; }