diff --git a/parser/html/nsIParserUtils.idl b/parser/html/nsIParserUtils.idl index 8e48978434a..a982cb2de76 100644 --- a/parser/html/nsIParserUtils.idl +++ b/parser/html/nsIParserUtils.idl @@ -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++ diff --git a/parser/html/nsIScriptableUnescapeHTML.idl b/parser/html/nsIScriptableUnescapeHTML.idl index 71e909a2c9a..5a6af141a89 100644 --- a/parser/html/nsIScriptableUnescapeHTML.idl +++ b/parser/html/nsIScriptableUnescapeHTML.idl @@ -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 diff --git a/parser/html/nsParserUtils.cpp b/parser/html/nsParserUtils.cpp index 0f0b0f3f028..aa954911038 100644 --- a/parser/html/nsParserUtils.cpp +++ b/parser/html/nsParserUtils.cpp @@ -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); } } diff --git a/parser/html/nsParserUtils.h b/parser/html/nsParserUtils.h index 1c43e86187d..d680c0d60f9 100644 --- a/parser/html/nsParserUtils.h +++ b/parser/html/nsParserUtils.h @@ -41,7 +41,7 @@ #include "nsIParserUtils.h" class nsParserUtils : public nsIScriptableUnescapeHTML, - public nsIParserUtils + public nsIParserUtils { public: NS_DECL_ISUPPORTS diff --git a/testing/extensions/community/chrome/content/common.js b/testing/extensions/community/chrome/content/common.js index 0e558ceafbd..e8da287e704 100644 --- a/testing/extensions/community/chrome/content/common.js +++ b/testing/extensions/community/chrome/content/common.js @@ -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) { diff --git a/toolkit/components/feeds/FeedProcessor.js b/toolkit/components/feeds/FeedProcessor.js index ad9208d736b..675428f854d 100644 --- a/toolkit/components/feeds/FeedProcessor.js +++ b/toolkit/components/feeds/FeedProcessor.js @@ -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 diff --git a/toolkit/components/feeds/test/test_bug675492.xul b/toolkit/components/feeds/test/test_bug675492.xul index 9cc0b405273..b1c52d11aa6 100644 --- a/toolkit/components/feeds/test/test_bug675492.xul +++ b/toolkit/components/feeds/test/test_bug675492.xul @@ -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("
test
", false, null, document.createElementNS("http://www.w3.org/1999/xhtml", "body")); + .classes["@mozilla.org/parserutils;1"] + .getService(Components.interfaces.nsIParserUtils) + .parseFragment("test
", 0, false, null, document.createElementNS("http://www.w3.org/1999/xhtml", "body")); ok(true, "No crash!"); ]]>