Skip to content

Commit

Permalink
refactor: drop attribute event
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

``attribute`` is not a particularly useful event for parsing XML. The only thing
it adds over looking at attributes on tag objects is that you get the order of
the attributes from the source, but attribute order in XML is irrelevant.
  • Loading branch information
lddubeau committed Jul 10, 2018
1 parent 3287d2c commit c7c2e80
Show file tree
Hide file tree
Showing 17 changed files with 2 additions and 305 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ parser.ontext = function (t) {
parser.onopentag = function (node) {
// opened a tag. node has "name" and "attributes"
};
parser.onattribute = function (attr) {
// an attribute. attr has "name" and "value"
};
parser.onend = function () {
// parser stream is done, and ready to have more stuff written to it.
};
Expand Down
17 changes: 0 additions & 17 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ exports.EVENTS = [
"doctype",
"comment",
"opentagstart",
"attribute",
"opentag",
"closetag",
"cdata",
Expand Down Expand Up @@ -348,15 +347,6 @@ class SaxesParser {
*/
onopentagstart() {}

/**
* Event handler for attributes. The default implementation is a no-op.
*
* @param {{ name: string, value: string }} attribute The attribute name and
* value. ``name`` corresponds to the ``name`` property of the {@link
* Attribute Attribute} structure.
*/
onattribute() {}

/**
* Event handler for an open tag. This is called when the open tag is
* complete. (We've encountered the ">" that ends the open tag.) The default
Expand Down Expand Up @@ -1409,10 +1399,6 @@ class SaxesParser {
this.fail("attribute without value.");
this.tag.attributes[this.attribName] = "";
this.attribValue = "";
this.emitNode("onattribute", {
name: this.attribName,
value: "",
});
this.attribName = "";
if (c === ">") {
this.openTag();
Expand Down Expand Up @@ -1786,17 +1772,14 @@ ${JSON.stringify(this.tagName)}.`);
a.uri = prefix;
}
tag.attributes[name] = a;
this.emitNode("onattribute", a);
}
}
else {
for (const [name, value] of this.attribList) {
const a = { name, value };
if (this.tag.attributes[name]) {
this.fail(`duplicate attribute: ${name}.`);
}
this.tag.attributes[name] = value;
this.emitNode("onattribute", a);
}
}
this.attribList = [];
Expand Down
10 changes: 0 additions & 10 deletions test/attribute-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ require(".").test({
xml: "<root length='12345'></root>",
expect: [
["opentagstart", { name: "root", attributes: {}, ns: {} }],
[
"attribute",
{
name: "length",
value: "12345",
prefix: "",
local: "length",
uri: "",
},
],
[
"opentag",
{
Expand Down
8 changes: 0 additions & 8 deletions test/attribute-no-space.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ require(".").test({
expect: [
["opentagstart", { name: "root", attributes: {} }],
["error", "undefined:1:20: no whitespace between attributes."],
["attribute", { name: "attr1", value: "first" }],
["attribute", { name: "attr2", value: "second" }],
["opentag", { name: "root", attributes: { attr1: "first", attr2: "second" }, isSelfClosing: true }],
["closetag", "root"],
],
Expand All @@ -21,8 +19,6 @@ require(".").test({
xml: "<root attr1=\"first\" attr2=\"second\"/>",
expect: [
["opentagstart", { name: "root", attributes: {} }],
["attribute", { name: "attr1", value: "first" }],
["attribute", { name: "attr2", value: "second" }],
["opentag", { name: "root", attributes: { attr1: "first", attr2: "second" }, isSelfClosing: true }],
["closetag", "root"],
],
Expand All @@ -35,8 +31,6 @@ require(".").test({
xml: "<root attr1=\"first\"\nattr2=\"second\"/>",
expect: [
["opentagstart", { name: "root", attributes: {} }],
["attribute", { name: "attr1", value: "first" }],
["attribute", { name: "attr2", value: "second" }],
["opentag", { name: "root", attributes: { attr1: "first", attr2: "second" }, isSelfClosing: true }],
["closetag", "root"],
],
Expand All @@ -49,8 +43,6 @@ require(".").test({
xml: "<root attr1=\"first\" attr2=\"second\"/>",
expect: [
["opentagstart", { name: "root", attributes: {} }],
["attribute", { name: "attr1", value: "first" }],
["attribute", { name: "attr2", value: "second" }],
["opentag", { name: "root", attributes: { attr1: "first", attr2: "second" }, isSelfClosing: true }],
["closetag", "root"],
],
Expand Down
7 changes: 0 additions & 7 deletions test/attribute-unquoted.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ require(".").test({
expect: [
["opentagstart", { name: "root", attributes: {}, ns: {} }],
["error", "undefined:1:14: unquoted attribute value."],
["attribute", {
name: "length",
value: "12345",
prefix: "",
local: "length",
uri: "",
}],
["opentag", {
name: "root",
attributes: {
Expand Down
1 change: 0 additions & 1 deletion test/bom.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require(".").test({
xml: "\uFEFF<P BOM=\"\uFEFF\">\uFEFFStarts and ends with BOM\uFEFF</P>",
expect: [
["opentagstart", { name: "P", attributes: {} }],
["attribute", { name: "BOM", value: "\uFEFF" }],
["opentag", { name: "P", attributes: { BOM: "\uFEFF" }, isSelfClosing: false }],
["text", "\uFEFFStarts and ends with BOM\uFEFF"],
["closetag", "P"],
Expand Down
2 changes: 0 additions & 2 deletions test/duplicate-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ require(".").test({
name: "span",
attributes: {},
}],
["attribute", { name: "id", value: "hello" }],
["error", "undefined:1:28: duplicate attribute: id."],
["attribute", { name: "id", value: "there" }],
["opentag", {
name: "span",
attributes: { id: "there" },
Expand Down
1 change: 0 additions & 1 deletion test/issue-47.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require(".").test({
xml: "<a href=\"query.svc?x=1&amp;y=2&amp;z=3\"/>",
expect: [
["opentagstart", { name: "a", attributes: {} }],
["attribute", { name: "href", value: "query.svc?x=1&y=2&z=3" }],
["opentag", { name: "a", attributes: { href: "query.svc?x=1&y=2&z=3" }, isSelfClosing: true }],
["closetag", "a"],
],
Expand Down
17 changes: 0 additions & 17 deletions test/opentagstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ require(".").test({
attributes: {},
},
],
[
"attribute",
{
name: "length",
value: "12345",
prefix: "",
local: "length",
uri: "",
},
],
[
"opentag",
{
Expand Down Expand Up @@ -63,13 +53,6 @@ require(".").test({
attributes: {},
},
],
[
"attribute",
{
name: "length",
value: "12345",
},
],
[
"opentag",
{
Expand Down
1 change: 0 additions & 1 deletion test/trailing-attribute-no-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require(".").test({
expect: [
["opentagstart", { name: "root", attributes: {} }],
["error", "undefined:1:13: attribute without value."],
["attribute", { name: "attrib", value: "attrib" }],
["opentag", { name: "root", attributes: { attrib: "attrib" }, isSelfClosing: false }],
["closetag", "root"],
],
Expand Down
14 changes: 0 additions & 14 deletions test/xml-internal-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ const entitiesToTest = {

let xmlStart = "<a test=\"&amp;\" ";
const myAttributes = {};
const attributeEvents = [[
"attribute",
{
name: "test",
value: "&",
},
]];
myAttributes.test = "&";

let entI = 0;
Expand All @@ -46,16 +39,10 @@ for (const entity in entitiesToTest) {
`undefined:1:${xmlStart.length + entitiesToTest[entity][0] + 1}: disallowed \
character in entity name.`,
]);
attributeEvents.push([
"attribute",
{ name: attribName, value: `&${entity};` },
]);
myAttributes[attribName] = `&${entity};`;
}
else {
ENTITIES[entity] = attribValue;
attributeEvents.push(["attribute",
{ name: attribName, value: attribValue }]);
myAttributes[attribName] = attribValue;
}

Expand All @@ -74,7 +61,6 @@ require(".").test({
},
],
...attributeErrors,
...attributeEvents,
[
"opentag",
{
Expand Down
28 changes: 2 additions & 26 deletions test/xmlns-issue-41.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const t = require(".");

const ex1 = [
const expect = [
["opentagstart", { name: "parent", attributes: {}, ns: {} }],
[
"opennamespace",
Expand All @@ -11,26 +11,6 @@ const ex1 = [
uri: "http://ATTRIBUTE",
},
],
[
"attribute",
{
name: "xmlns:a",
value: "http://ATTRIBUTE",
prefix: "xmlns",
local: "a",
uri: "http://www.w3.org/2000/xmlns/",
},
],
[
"attribute",
{
name: "a:attr",
local: "attr",
prefix: "a",
uri: "http://ATTRIBUTE",
value: "value",
},
],
[
"opentag",
{
Expand Down Expand Up @@ -73,10 +53,6 @@ const ex1 = [
],
];

// swap the order of elements 2 and 3
const ex2 = [ex1[0], ex1[1], ex1[3], ex1[2]].concat(ex1.slice(4));
const expected = [ex1, ex2];

// should be the same both ways.
const xmls = [
"<parent xmlns:a=\"http://ATTRIBUTE\" a:attr=\"value\" />",
Expand All @@ -87,7 +63,7 @@ describe("issue 41", () => {
t.test({
name: `order ${i}`,
xml: x,
expect: expected[i],
expect,
opt: {
xmlns: true,
},
Expand Down
Loading

0 comments on commit c7c2e80

Please sign in to comment.