Convert feed processor to HTML attribute manipulation

This moves the feed processor away from custom XPCOM SAX attributes and over to
standard HTML attribute map APIs.
This commit is contained in:
J. Ryan Stinnett 2021-05-20 01:14:01 +01:00
parent 891ba599a6
commit 1d93296481

View file

@ -621,13 +621,13 @@ Generator.prototype = {
set attributes(value) { set attributes(value) {
this._attributes = value; this._attributes = value;
this.version = this._attributes.getValueFromName("", "version"); this.version = (this._attributes.getNamedItemNS("", "version") || {}).value;
var uriAttribute = this._attributes.getValueFromName("", "uri") var uriAttribute = (this._attributes.getNamedItemNS("", "uri") || {}).value
|| this._attributes.getValueFromName("", "url"); || (this._attributes.getNamedItemNS("", "url") || {}).value;
this.uri = strToURI(uriAttribute, this.baseURI); this.uri = strToURI(uriAttribute, this.baseURI);
// RSS1 // RSS1
uriAttribute = this._attributes.getValueFromName(RDF_NS, "resource"); uriAttribute = (this._attributes.getNamedItemNS(RDF_NS, "resource") || {}).value;
if (uriAttribute) { if (uriAttribute) {
this.agent = uriAttribute; this.agent = uriAttribute;
this.uri = strToURI(uriAttribute, this.baseURI); this.uri = strToURI(uriAttribute, this.baseURI);
@ -809,23 +809,23 @@ XHTMLHandler.prototype = {
this._buf += "<" + localName; this._buf += "<" + localName;
var uri; var uri;
for (var i = 0; i < attributes.length; ++i) { for (var i = 0; i < attributes.length; ++i) {
uri = attributes.getURI(i); uri = attributes.item(i).namespaceURI;
// XHTML attributes aren't in a namespace // XHTML attributes aren't in a namespace
if (uri == "") { if (uri == "") {
this._buf += (" " + attributes.getLocalName(i) + "='" this._buf += (" " + attributes.item(i).localName + "='"
+ xmlEscape(attributes.getValue(i)) + "'"); + xmlEscape(attributes.item(i).value) + "'");
} }
else { else {
// write a small set of allowed attribute namespaces // write a small set of allowed attribute namespaces
var prefix = gAllowedXHTMLNamespaces[uri]; var prefix = gAllowedXHTMLNamespaces[uri];
if (prefix) { if (prefix) {
// The attribute value we'll attempt to write // The attribute value we'll attempt to write
var attributeValue = xmlEscape(attributes.getValue(i)); var attributeValue = xmlEscape(attributes.item(i).value);
// it's an allowed attribute NS. // it's an allowed attribute NS.
// write the attribute // write the attribute
this._buf += (" " + prefix + ":" this._buf += (" " + prefix + ":"
+ attributes.getLocalName(i) + attributes.item(i).localName
+ "='" + attributeValue + "'"); + "='" + attributeValue + "'");
// write an xmlns declaration if necessary // write an xmlns declaration if necessary
@ -1245,7 +1245,7 @@ FeedProcessor.prototype = {
// LOG("<" + localName + ">"); // LOG("<" + localName + ">");
// Check for xml:base // Check for xml:base
var base = attributes.getValueFromName(XMLNS, "base"); var base = (attributes.getNamedItemNS(XMLNS, "base") || {}).value;
if (base) { if (base) {
this._xmlBaseStack[this._depth] this._xmlBaseStack[this._depth]
= strToURI(base, this._xmlBaseStack[this._xmlBaseStack.length - 1]); = strToURI(base, this._xmlBaseStack[this._xmlBaseStack.length - 1]);
@ -1278,8 +1278,8 @@ FeedProcessor.prototype = {
// //
if ((this._result.version == "atom" || this._result.version == "atom03") if ((this._result.version == "atom" || this._result.version == "atom03")
&& this._textConstructs[key]) { && this._textConstructs[key]) {
var type = attributes.getValueFromName("", "type"); var type = (attributes.getNamedItemNS("", "type") || {}).value;
if (type !== null && type.includes("xhtml")) { if (type && type.includes("xhtml")) {
this._xhtmlHandler this._xhtmlHandler
= new XHTMLHandler(this, (this._result.version == "atom")); = new XHTMLHandler(this, (this._result.version == "atom"));
this._reader.contentHandler = this._xhtmlHandler; this._reader.contentHandler = this._xhtmlHandler;
@ -1486,15 +1486,15 @@ FeedProcessor.prototype = {
// Cycle through the attributes, and set our properties using the // Cycle through the attributes, and set our properties using the
// prefix:localNames we find in our namespace dictionary. // prefix:localNames we find in our namespace dictionary.
for (var i = 0; i < attributes.length; ++i) { for (var i = 0; i < attributes.length; ++i) {
var key = this._prefixForNS(attributes.getURI(i)) + attributes.getLocalName(i); var key = this._prefixForNS(attributes.item(i).namespaceURI) + attributes.item(i).localName;
var val = attributes.getValue(i); var val = attributes.item(i).value;
bag[key] = val; bag[key] = val;
} }
}, },
// Only for RSS2esque formats // Only for RSS2esque formats
_findRSSVersion: function (attributes) { _findRSSVersion: function (attributes) {
var versionAttr = attributes.getValueFromName("", "version").trim(); var versionAttr = (attributes.getNamedItemNS("", "version") || {}).value.trim();
var versions = { var versions = {
"0.91": "rss091", "0.91": "rss091",
"0.92": "rss092", "0.92": "rss092",
@ -1588,11 +1588,11 @@ FeedProcessor.prototype = {
newProp.text = chars; newProp.text = chars;
// Look up the default type in our table // Look up the default type in our table
var type = this._textConstructs[propName]; var type = this._textConstructs[propName];
var typeAttribute = attributes.getValueFromName("", "type"); var typeAttribute = (attributes.getNamedItemNS("", "type") || {}).value;
if (this._result.version == "atom" && typeAttribute !== null) { if (this._result.version == "atom" && typeAttribute) {
type = typeAttribute; type = typeAttribute;
} }
else if (this._result.version == "atom03" && typeAttribute !== null) { else if (this._result.version == "atom03" && typeAttribute) {
if (typeAttribute.toLowerCase().includes("xhtml")) { if (typeAttribute.toLowerCase().includes("xhtml")) {
type = "xhtml"; type = "xhtml";
} }