[Psi-devel] first stab at xhtml filter for JEP complience

textshell-I1QKlO@neutronstar.dyndns.org textshell-I1QKlO at neutronstar.dyndns.org
Sat Jul 1 12:13:13 PDT 2006


Hallo,

this is a simple filter to restrict a QDomElement xhtml subset allowed by
xhtml-im.



diff -rN -u old-psi-1/iris/include/im.h new-psi/iris/include/im.h
--- old-psi-1/iris/include/im.h	2006-07-01 21:11:17.000000000 +0200
+++ new-psi/iris/include/im.h	2006-07-01 21:11:23.000000000 +0200
@@ -245,6 +245,8 @@
 		const QDomElement& body() const;
 		QString toString(const QString &rootTagName = "body") const;
 		QString text() const;
+		
+		static QDomElement filterXHtmlIM(const QDomElement &body);
 
 	private:
 		QDomElement body_;
diff -rN -u old-psi-1/iris/xmpp-im/types.cpp new-psi/iris/xmpp-im/types.cpp
--- old-psi-1/iris/xmpp-im/types.cpp	2006-07-01 21:11:17.000000000 +0200
+++ new-psi/iris/xmpp-im/types.cpp	2006-07-01 21:11:23.000000000 +0200
@@ -901,11 +901,11 @@
 {
 }
 
-HTMLElement::HTMLElement(const QDomElement &body) : body_(body) {}
+HTMLElement::HTMLElement(const QDomElement &body) : body_(filterXHtmlIM(body)) {}
 
 void HTMLElement::setBody(const QDomElement &body)
 {
-	body_ = body;
+	body_ = filterXHtmlIM(body);
 }
 
 const QDomElement& HTMLElement::body() const
@@ -913,6 +913,66 @@
 	return body_;
 }
 
+
+// xhtml filter todo: filter attributes, filter style
+
+static char *allowed_xhtmlim_tags[] = {
+	/* Recommendations */
+	"body", "br", "p", "span", "ol", "ul", "li",
+	/* allowed */
+	"abbr", "acronym", "address", 
+	"cite", "code", "dfn", "div", "em", "h1", "h2", "h3", 
+	"h4", "h5", "h6", "kbd", "pre", "samp", "strong",
+	"var", "dl", "dt", "dd", 
+	0};
+
+
+// currently ommited: head html title blockquote q
+
+static void filterXHtmlIM_helper(QDomElement current)
+{
+	QString tagname = current.tagName();
+	bool ok = false;
+	
+	if (tagname == "a") {
+		ok = true;		
+		// allowed attributes: class, id, title; style; accesskey,
+		//       charset, href, hreflang, rel, rev, tabindex, type
+	} else if (tagname == "img") {
+		ok = true;		
+		// allowed attributes: class, id, title; style; alt, 
+		//                     height, longdesc, src, width
+	} else {
+		for (char **i = allowed_xhtmlim_tags; *i; ++i) {
+			if (tagname == *i) {
+				ok = true;
+				// allowed attributes: class, id, title; style
+				break;
+			}
+		}
+	}
+	
+	if (!ok) current.setTagName("span");
+	
+	for(QDomNode n = current.firstChild(); !n.isNull(); n = n.nextSibling()) {
+		if (n.isElement()) {
+			filterXHtmlIM_helper(n.toElement());
+		}
+	}
+
+}
+
+QDomElement HTMLElement::filterXHtmlIM(const QDomElement &body)
+{
+	QDomElement filtered = body.cloneNode(true).toElement();
+	
+	filterXHtmlIM_helper(filtered);
+	
+	return filtered;
+}
+


More information about the psi-devel mailing list