DiegoAstiazaran created this revision.
DiegoAstiazaran added reviewers: jakehehrlich, juliehockett.
DiegoAstiazaran added a project: clang-tools-extra.

Replace <, > and " with its corresponding html entities before rendering text 
nodes.


https://reviews.llvm.org/D65107

Files:
  clang-tools-extra/clang-doc/HTMLGenerator.cpp
  clang-tools-extra/unittests/clang-doc/HTMLGeneratorTest.cpp


Index: clang-tools-extra/unittests/clang-doc/HTMLGeneratorTest.cpp
===================================================================
--- clang-tools-extra/unittests/clang-doc/HTMLGeneratorTest.cpp
+++ clang-tools-extra/unittests/clang-doc/HTMLGeneratorTest.cpp
@@ -258,6 +258,14 @@
   Extended->Children.back()->Kind = "TextComment";
   Extended->Children.back()->Text = " continues onto the next line.";
 
+  Top.Children.emplace_back(llvm::make_unique<CommentInfo>());
+  CommentInfo *Entities = Top.Children.back().get();
+  Entities->Kind = "ParagraphComment";
+  Entities->Children.emplace_back(llvm::make_unique<CommentInfo>());
+  Entities->Children.back()->Kind = "TextComment";
+  Entities->Children.back()->Name = "ParagraphComment";
+  Entities->Children.back()->Text = " Comment with html entities: <, > and 
\".";
+
   I.Description.emplace_back(std::move(Top));
 
   auto G = getHTMLGenerator();
@@ -285,6 +293,9 @@
       <p>
          Extended description that continues onto the next line.
       </p>
+      <p>
+         Comment with html entities: &lt;, &gt; and &quot;.
+      </p>
     </div>
   </div>
 </div>
Index: clang-tools-extra/clang-doc/HTMLGenerator.cpp
===================================================================
--- clang-tools-extra/clang-doc/HTMLGenerator.cpp
+++ clang-tools-extra/clang-doc/HTMLGenerator.cpp
@@ -71,7 +71,12 @@
 
   std::string Text; // Content of node
   bool Indented; // Indicates if an indentation must be rendered before the 
text
+
+  void FixText();
   void Render(llvm::raw_ostream &OS, int IndentationLevel) override;
+
+private:
+  std::string getHTMLEntity(const char &C);
 };
 
 struct TagNode : public HTMLNode {
@@ -173,7 +178,30 @@
   llvm_unreachable("Unhandled HTMLTag::TagType");
 }
 
+std::string TextNode::getHTMLEntity(const char &C) {
+  switch (C) {
+  case '<':
+    return "&lt;";
+  case '>':
+    return "&gt;";
+  case '"':
+    return "&quot;";
+  default:
+    return std::string(&C, 1);
+  }
+}
+
+void TextNode::FixText() {
+  static const std::string CharactersToReplace = "<>\"";
+  std::size_t found = Text.find_first_of(CharactersToReplace);
+  while (found != std::string::npos) {
+    Text.replace(found, 1, getHTMLEntity(Text[found]));
+    found = Text.find_first_of(CharactersToReplace, found + 1);
+  }
+}
+
 void TextNode::Render(llvm::raw_ostream &OS, int IndentationLevel) {
+  FixText();
   if (Indented)
     OS.indent(IndentationLevel * 2);
   OS << Text;


Index: clang-tools-extra/unittests/clang-doc/HTMLGeneratorTest.cpp
===================================================================
--- clang-tools-extra/unittests/clang-doc/HTMLGeneratorTest.cpp
+++ clang-tools-extra/unittests/clang-doc/HTMLGeneratorTest.cpp
@@ -258,6 +258,14 @@
   Extended->Children.back()->Kind = "TextComment";
   Extended->Children.back()->Text = " continues onto the next line.";
 
+  Top.Children.emplace_back(llvm::make_unique<CommentInfo>());
+  CommentInfo *Entities = Top.Children.back().get();
+  Entities->Kind = "ParagraphComment";
+  Entities->Children.emplace_back(llvm::make_unique<CommentInfo>());
+  Entities->Children.back()->Kind = "TextComment";
+  Entities->Children.back()->Name = "ParagraphComment";
+  Entities->Children.back()->Text = " Comment with html entities: <, > and \".";
+
   I.Description.emplace_back(std::move(Top));
 
   auto G = getHTMLGenerator();
@@ -285,6 +293,9 @@
       <p>
          Extended description that continues onto the next line.
       </p>
+      <p>
+         Comment with html entities: &lt;, &gt; and &quot;.
+      </p>
     </div>
   </div>
 </div>
Index: clang-tools-extra/clang-doc/HTMLGenerator.cpp
===================================================================
--- clang-tools-extra/clang-doc/HTMLGenerator.cpp
+++ clang-tools-extra/clang-doc/HTMLGenerator.cpp
@@ -71,7 +71,12 @@
 
   std::string Text; // Content of node
   bool Indented; // Indicates if an indentation must be rendered before the text
+
+  void FixText();
   void Render(llvm::raw_ostream &OS, int IndentationLevel) override;
+
+private:
+  std::string getHTMLEntity(const char &C);
 };
 
 struct TagNode : public HTMLNode {
@@ -173,7 +178,30 @@
   llvm_unreachable("Unhandled HTMLTag::TagType");
 }
 
+std::string TextNode::getHTMLEntity(const char &C) {
+  switch (C) {
+  case '<':
+    return "&lt;";
+  case '>':
+    return "&gt;";
+  case '"':
+    return "&quot;";
+  default:
+    return std::string(&C, 1);
+  }
+}
+
+void TextNode::FixText() {
+  static const std::string CharactersToReplace = "<>\"";
+  std::size_t found = Text.find_first_of(CharactersToReplace);
+  while (found != std::string::npos) {
+    Text.replace(found, 1, getHTMLEntity(Text[found]));
+    found = Text.find_first_of(CharactersToReplace, found + 1);
+  }
+}
+
 void TextNode::Render(llvm::raw_ostream &OS, int IndentationLevel) {
+  FixText();
   if (Indented)
     OS.indent(IndentationLevel * 2);
   OS << Text;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to