Bug 737013 - Expose sanitizer fragments on parseFragment(), migrate callers to the new interface. r=smaug.

This commit is contained in:
Henri Sivonen 2012-03-20 17:28:42 +02:00
Родитель c754e3ed45
Коммит d230625735
7 изменённых файлов: 68 добавлений и 30 удалений

Просмотреть файл

@ -4,12 +4,16 @@
#include "nsISupports.idl"
interface nsIDOMElement;
interface nsIDOMDocumentFragment;
interface nsIURI;
/**
* Non-Web HTML parser functionality to Firefox extensions and XULRunner apps.
* Don't use this from within Gecko--use nsContentUtils, nsTreeSanitizer, etc.
* directly instead.
*/
[scriptable, uuid(290f49bb-0619-4bda-8006-ab31bec7231a)]
[scriptable, uuid(a1101145-0025-411e-8873-fdf57bf28128)]
interface nsIParserUtils : nsISupports
{
@ -100,6 +104,22 @@ interface nsIParserUtils : nsISupports
AString convertToPlainText(in AString src,
in unsigned long flags,
in unsigned long wrapCol);
/**
* Parses markup into a sanitized document fragment.
*
* @param fragment the input markup
* @param flags sanitization option flags defined above
* @param isXML true if |fragment| is XML and false if HTML
* @param baseURI the base URL for this fragment
* @param element the context node for the fragment parsing algorithm
*/
nsIDOMDocumentFragment parseFragment(in AString fragment,
in unsigned long flags,
in boolean isXML,
in nsIURI baseURI,
in nsIDOMElement element);
};
%{ C++

Просмотреть файл

@ -41,7 +41,7 @@ interface nsIDOMDocumentFragment;
interface nsIURI;
/**
* A utility class for HTML parsing in the feed processor.
* This interface is OBSOLETE and exists solely for legacy extensions.
*/
[scriptable, uuid(3ab244a9-f09d-44da-9e3f-ee4d67367f2d)]
interface nsIScriptableUnescapeHTML : nsISupports
@ -52,16 +52,20 @@ interface nsIScriptableUnescapeHTML : nsISupports
* nsIDocumentEncoder::OutputSelectionOnly |
* nsIDocumentEncoder::OutputAbsoluteLinks, 0).
*
* You should most likely call nsIParserUtils::convertToPlainText()
* instead of calling this method.
* You should call nsIParserUtils::convertToPlainText() instead of calling
* this method.
*
* @param src The HTML string to convert to plain text.
*/
AString unescape(in AString src);
/**
* Parses markup into a sanitized document fragment.
* Parses markup into a sanitized document fragment. This is equivalent to
* calling nsIParserUtils::parseFragment(fragment, 0, isXML, baseURI,
* element).
*
* You should call nsIParserUtils::parseFragment() instead of calling this
* method.
* @param fragment the input markup
* @param isXML true if |fragment| is XML and false if HTML
* @param baseURI the base URL for this fragment

Просмотреть файл

@ -79,9 +79,9 @@ static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID);
NS_IMETHODIMP
nsParserUtils::ConvertToPlainText(const nsAString& aFromStr,
PRUint32 aFlags,
PRUint32 aWrapCol,
nsAString& aToStr)
PRUint32 aFlags,
PRUint32 aWrapCol,
nsAString& aToStr)
{
return nsContentUtils::ConvertToPlainText(aFromStr,
aToStr,
@ -142,15 +142,28 @@ nsParserUtils::Sanitize(const nsAString& aFromStr,
return encoder->EncodeToString(aToStr);
}
// The feed version of nsContentUtils::CreateContextualFragment It
// creates a fragment, but doesn't go to all the effort to preserve
// context like innerHTML does, because feed DOMs shouldn't have that.
NS_IMETHODIMP
nsParserUtils::ParseFragment(const nsAString& aFragment,
bool aIsXML,
nsIURI* aBaseURI,
nsIDOMElement* aContextElement,
nsIDOMDocumentFragment** aReturn)
bool aIsXML,
nsIURI* aBaseURI,
nsIDOMElement* aContextElement,
nsIDOMDocumentFragment** aReturn)
{
return nsParserUtils::ParseFragment(aFragment,
0,
aIsXML,
aBaseURI,
aContextElement,
aReturn);
}
NS_IMETHODIMP
nsParserUtils::ParseFragment(const nsAString& aFragment,
PRUint32 aFlags,
bool aIsXML,
nsIURI* aBaseURI,
nsIDOMElement* aContextElement,
nsIDOMDocumentFragment** aReturn)
{
NS_ENSURE_ARG(aContextElement);
*aReturn = nsnull;
@ -239,7 +252,7 @@ nsParserUtils::ParseFragment(const nsAString& aFragment,
}
}
if (fragment) {
nsTreeSanitizer sanitizer;
nsTreeSanitizer sanitizer(aFlags);
sanitizer.Sanitize(fragment);
}
}

Просмотреть файл

@ -41,7 +41,7 @@
#include "nsIParserUtils.h"
class nsParserUtils : public nsIScriptableUnescapeHTML,
public nsIParserUtils
public nsIParserUtils
{
public:
NS_DECL_ISUPPORTS

Просмотреть файл

@ -149,12 +149,11 @@ var qaTools = {
return newArray;
},
writeSafeHTML : function(elementID, htmlstr) {
document.getElementById(elementID).innerHTML = ""; //clear it.
var gUnescapeHTML = Components.classes["@mozilla.org/feed-unescapehtml;1"].getService(Components.interfaces.nsIScriptableUnescapeHTML);
document.getElementById(elementID).textContent = ""; //clear it.
var utils = Components.classes["@mozilla.org/parserutils;1"].getService(Components.interfaces.nsIParserUtils);
var context = document.getElementById(elementID);
var fragment = gUnescapeHTML.parseFragment(htmlstr, false, null, context);
var fragment = utils.parseFragment(htmlstr, 0, false, null, context);
context.appendChild(fragment);
},
assignLinkHandlers : function(node) {

Просмотреть файл

@ -75,7 +75,7 @@ const IO_CONTRACTID = "@mozilla.org/network/io-service;1"
const BAG_CONTRACTID = "@mozilla.org/hash-property-bag;1"
const ARRAY_CONTRACTID = "@mozilla.org/array;1";
const SAX_CONTRACTID = "@mozilla.org/saxparser/xmlreader;1";
const UNESCAPE_CONTRACTID = "@mozilla.org/feed-unescapehtml;1";
const PARSERUTILS_CONTRACTID = "@mozilla.org/parserutils;1";
var gIoService = null;
@ -644,14 +644,16 @@ function TextConstruct() {
this.base = null;
this.type = "text";
this.text = null;
this.unescapeHTML = Cc[UNESCAPE_CONTRACTID].
getService(Ci.nsIScriptableUnescapeHTML);
this.parserUtils = Cc[PARSERUTILS_CONTRACTID].getService(Ci.nsIParserUtils);
}
TextConstruct.prototype = {
plainText: function TC_plainText() {
if (this.type != "text") {
return this.unescapeHTML.unescape(stripTags(this.text));
return this.parserUtils.convertToPlainText(stripTags(this.text),
Ci.nsIDocumentEncoder.OutputSelectionOnly |
Ci.nsIDocumentEncoder.OutputAbsoluteLinks,
0);
}
return this.text;
},
@ -672,8 +674,8 @@ TextConstruct.prototype = {
else
return null;
return this.unescapeHTML.parseFragment(this.text, isXML,
this.base, element);
return this.parserUtils.parseFragment(this.text, 0, isXML,
this.base, element);
},
// XPCOM stuff

Просмотреть файл

@ -22,9 +22,9 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=675492
/** Test for Bug 675492 **/
Components
.classes["@mozilla.org/feed-unescapehtml;1"]
.getService(Components.interfaces.nsIScriptableUnescapeHTML)
.parseFragment("<p>test</p>", false, null, document.createElementNS("http://www.w3.org/1999/xhtml", "body"));
.classes["@mozilla.org/parserutils;1"]
.getService(Components.interfaces.nsIParserUtils)
.parseFragment("<p>test</p>", 0, false, null, document.createElementNS("http://www.w3.org/1999/xhtml", "body"));
ok(true, "No crash!");
]]>
</script>