Bug 436728 - Support SAX's namespace-prefixes feature, r=smaug

This commit is contained in:
Alex Vincent 2012-11-04 12:38:00 -08:00
parent b2b4aaae0b
commit cd3969599f
5 changed files with 78 additions and 4 deletions

View File

@ -79,7 +79,7 @@ interface nsISAXXMLReader : nsIStreamListener {
attribute nsISAXLexicalHandler lexicalHandler;
/**
* Set the value of a feature flag. NOT CURRENTLY IMPLEMENTED.
* Set the value of a feature flag.
*
* The feature name is any fully-qualified URI. It is possible
* for an XMLReader to expose a feature value but to be unable to
@ -93,11 +93,15 @@ interface nsISAXXMLReader : nsIStreamListener {
*
* @param name String flag for a parser feature.
* @param value Turn the feature on/off.
*
* @note This is currently supported only for
* http://xml.org/sax/features/namespace-prefixes . All other
* features will result in a NOT_IMPLEMENTED exception.
*/
void setFeature(in AString name, in boolean value);
/**
* Look up the value of a feature flag. NOT CURRENTLY IMPLEMENTED.
* Look up the value of a feature flag.
*
* The feature name is any fully-qualified URI. It is
* possible for an XMLReader to recognize a feature name but
@ -110,6 +114,10 @@ interface nsISAXXMLReader : nsIStreamListener {
* http://xml.org/sax/features/namespace-prefixes feature names.
*
* @param name String flag for a parser feature.
*
* @note This is currently supported only for
* http://xml.org/sax/features/namespace-prefixes . All other
* features will result in a NOT_IMPLEMENTED exception.
*/
boolean getFeature(in AString name);

View File

@ -52,7 +52,9 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsSAXXMLReader)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISAXXMLReader)
NS_INTERFACE_MAP_END
nsSAXXMLReader::nsSAXXMLReader() : mIsAsyncParse(false)
nsSAXXMLReader::nsSAXXMLReader() :
mIsAsyncParse(false),
mEnableNamespacePrefixes(false)
{
}
@ -101,7 +103,7 @@ nsSAXXMLReader::HandleStartElement(const PRUnichar *aName,
// XXX don't have attr type information
NS_NAMED_LITERAL_STRING(cdataType, "CDATA");
// could support xmlns reporting, it's a standard SAX feature
if (!uri.EqualsLiteral(XMLNS_URI)) {
if (mEnableNamespacePrefixes || !uri.EqualsLiteral(XMLNS_URI)) {
NS_ASSERTION(aAtts[1], "null passed to handler");
atts->AddAttribute(uri, localName, qName, cdataType,
nsDependentString(aAtts[1]));
@ -397,12 +399,20 @@ nsSAXXMLReader::SetErrorHandler(nsISAXErrorHandler *aErrorHandler)
NS_IMETHODIMP
nsSAXXMLReader::SetFeature(const nsAString &aName, bool aValue)
{
if (aName.EqualsLiteral("http://xml.org/sax/features/namespace-prefixes")) {
mEnableNamespacePrefixes = aValue;
return NS_OK;
}
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsSAXXMLReader::GetFeature(const nsAString &aName, bool *aResult)
{
if (aName.EqualsLiteral("http://xml.org/sax/features/namespace-prefixes")) {
*aResult = mEnableNamespacePrefixes;
return NS_OK;
}
return NS_ERROR_NOT_IMPLEMENTED;
}

View File

@ -94,6 +94,9 @@ private:
nsString &aQName);
nsString mPublicId;
nsString mSystemId;
// Feature flags
bool mEnableNamespacePrefixes;
};
#endif // nsSAXXMLReader_h__

View File

@ -0,0 +1,52 @@
function noop() {}
function run_test() {
var contentHandler = {
attrs: null,
reset: function() {
this.attrs = [];
},
startDocument: noop,
endDocument: noop,
startElement: function startElement(aNamespaceURI, aLocalName, aNodeName, aAttrs) {
for (var i = 0; i < aAttrs.length; i++)
this.attrs.push(aAttrs.getQName(i));
},
endElement: noop,
characters: noop,
processingInstruction: noop,
ignorableWhitespace: noop,
startPrefixMapping: noop,
endPrefixMapping: noop
};
const nsISAXXMLReader = Components.interfaces.nsISAXXMLReader;
const src = "<a:x xmlns:a='foo' y='bar'/>";
const NS_PREFIX = "http://xml.org/sax/features/namespace-prefixes";
var saxReader = Components.classes["@mozilla.org/saxparser/xmlreader;1"]
.createInstance(nsISAXXMLReader);
do_check_false(saxReader.getFeature(NS_PREFIX));
saxReader.contentHandler = contentHandler;
contentHandler.reset();
saxReader.parseFromString(src, "application/xml");
do_check_eq(contentHandler.attrs.length, 1);
do_check_eq(contentHandler.attrs[0], "y");
saxReader.setFeature(NS_PREFIX, true);
do_check_true(saxReader.getFeature(NS_PREFIX));
contentHandler.reset();
saxReader.parseFromString(src, "application/xml");
do_check_eq(contentHandler.attrs.length, 2);
do_check_eq(contentHandler.attrs[0], "xmlns:a");
do_check_eq(contentHandler.attrs[1], "y");
saxReader.setFeature(NS_PREFIX, false);
do_check_false(saxReader.getFeature(NS_PREFIX));
contentHandler.reset();
saxReader.parseFromString(src, "application/xml");
do_check_eq(contentHandler.attrs.length, 1);
do_check_eq(contentHandler.attrs[0], "y");
}

View File

@ -3,3 +3,4 @@ head =
tail =
[test_parser.js]
[test_namespace_support.js]