CVS commit by waba:
Save html as utf-8 (BR55929)
M +55 -3 archivedialog.cpp 1.3.2.4
--- kdeaddons/konq-plugins/webarchiver/archivedialog.cpp #1.3.2.3:1.3.2.4
@@ -45,4 +45,6 @@
#undef DEBUG_WAR
+#define CONTENT_TYPE ""
+
ArchiveDialog::ArchiveDialog(QWidget *parent, const QString &filename,
KHTMLPart *part) :
@@ -127,5 +129,5 @@ void ArchiveDialog::setSavingState()
KTempFile tmpFile;
QTextStream* textStream = tmpFile.textStream();
- textStream->setEncoding(QTextStream::Locale);
+ textStream->setEncoding(QTextStream::UnicodeUTF8);
m_widget->progressBar->setProgress(m_widget->progressBar->totalSteps());
@@ -176,4 +178,41 @@ void ArchiveDialog::saveToArchive(QTextS
}
+static bool hasAttribute(const DOM::Node &pNode, const QString &attrName, const QString &attrValue)
+{
+ const DOM::Element element = (const DOM::Element) pNode;
+ DOM::Attr attr;
+ DOM::NamedNodeMap attrs = element.attributes();
+ unsigned long lmap = attrs.length();
+ for( unsigned int j=0; j(attrs.item(j));
+ if ((attr.name().string().upper() == attrName) &&
+ (attr.value().string().upper() == attrValue))
+ return true;
+ }
+ return false;
+}
+
+static bool hasChildNode(const DOM::Node &pNode, const QString &nodeName)
+{
+ DOM::Node child;
+ try
+ {
+ // We might throw a DOM exception
+ child = pNode.firstChild();
+ }
+ catch (...)
+ {
+ // No children, stop recursion here
+ child = DOM::Node();
+ }
+
+ while(!child.isNull()) {
+ if (child.nodeName().string().upper() == nodeName)
+ return true;
+ child = child.nextSibling();
+ }
+ return false;
+}
+
/* Transform DOM-Tree to HTML */
@@ -199,4 +238,7 @@ void ArchiveDialog::saveArchiveRecursive
* Saving SCRIPT but they can cause trouble!
*/
+ } else if ((nodeName == "META") && hasAttribute(pNode, "HTTP-EQUIV", "CONTENT-TYPE")) {
+ /* Skip content-type meta tag, we provide our own.
+ */
} else {
if (!m_bPreserveWS) {
@@ -255,4 +297,14 @@ void ArchiveDialog::saveArchiveRecursive
text += attributes.simplifyWhiteSpace();
text += ">";
+
+ if (nodeName == "HTML") {
+ /* Search for a HEAD tag, if not found, generate one.
+ */
+ if (!hasChildNode(pNode, "HEAD"))
+ text += "\n" + strIndent + " " CONTENT_TYPE "";
+ }
+ else if (nodeName == "HEAD") {
+ text += "\n" + strIndent + " " + CONTENT_TYPE;
+ }
}
} else {