diff --git a/lib/tree_construction_stage/tree_serializer.js b/lib/tree_construction_stage/tree_serializer.js
index <HASH>..<HASH> 100644
--- a/lib/tree_construction_stage/tree_serializer.js
+++ b/lib/tree_construction_stage/tree_serializer.js
@@ -153,13 +153,16 @@ TreeSerializer.prototype._serializeDocumentTypeNode = function (node) {
 
     this.html += '<!DOCTYPE ' + name;
 
-    if (publicId !== null)
-        this.html += ' PUBLIC "' + publicId + '"';
-    else if (systemId !== null)
+    if (publicId !== null) {
+        var escapedPublicId = publicId.indexOf('"') !== -1 ? '\'' + publicId + '\'' : '"' + publicId + '"';
+        this.html += ' PUBLIC ' + escapedPublicId;
+    } else if (systemId !== null)
         this.html += ' SYSTEM';
 
-    if (systemId !== null)
-        this.html += ' "' + systemId + '"';
+    if (systemId !== null) {
+        var escapedSystemId = systemId.indexOf('"') !== -1 ? '\'' + systemId + '\'' : '"' + systemId + '"';
+        this.html += ' ' + escapedSystemId;
+    }
 
     this.html += '>';
 };
Fix escaping of doctypes with quotes in them