Skip to content

Commit

Permalink
refactor: adjust the names used for processing instructions
Browse files Browse the repository at this point in the history
We use "pi" as an abbreviation. This is a very common usage in XML processing.

BREAKING CHANGE:

The "processinginstruction" now produces a "target" field instead of a "name"
field. The nomenclature "target" is the one used in the XML literature.
  • Loading branch information
lddubeau committed Jul 6, 2018
1 parent e86534d commit 3b508e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
44 changes: 22 additions & 22 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ const S_COMMENT_ENDED = S_INDEX++; // <!-- blah --
const S_CDATA = S_INDEX++; // <![CDATA[ something
const S_CDATA_ENDING = S_INDEX++; // ]
const S_CDATA_ENDING_2 = S_INDEX++; // ]]
const S_PROC_INST = S_INDEX++; // <?hi
const S_PROC_INST_BODY = S_INDEX++; // <?hi there
const S_PROC_INST_ENDING = S_INDEX++; // <?hi "there" ?
const S_PI = S_INDEX++; // <?hi
const S_PI_BODY = S_INDEX++; // <?hi there
const S_PI_ENDING = S_INDEX++; // <?hi "there" ?
const S_OPEN_TAG = S_INDEX++; // <strong
const S_OPEN_TAG_SLASH = S_INDEX++; // <strong /
const S_ATTRIB = S_INDEX++; // <a
Expand Down Expand Up @@ -93,8 +93,8 @@ exports.EVENTS = [
];

const buffers = [
"comment", "openWakaBang", "textNode", "tagName", "doctype", "procInstName",
"procInstBody", "entity", "attribName", "attribValue", "cdata",
"comment", "openWakaBang", "textNode", "tagName", "doctype", "piTarget",
"piBody", "entity", "attribName", "attribValue", "cdata",
];

let Stream;
Expand Down Expand Up @@ -330,8 +330,8 @@ class SAXParser {
this.tagName = "";
}
else if (c === "?") {
this.state = S_PROC_INST;
this.procInstName = this.procInstBody = "";
this.state = S_PI;
this.piTarget = this.piBody = "";
}
else {
this.fail("Unencoded <");
Expand Down Expand Up @@ -493,15 +493,15 @@ class SAXParser {
}
continue;

case S_PROC_INST:
case S_PI:
if (c === "?") {
this.state = S_PROC_INST_ENDING;
this.state = S_PI_ENDING;
}
else if (isWhitespace(c)) {
this.state = S_PROC_INST_BODY;
this.state = S_PI_BODY;
}
else {
if (!(isMatch(this.procInstName.length ?
if (!(isMatch(this.piTarget.length ?
NAME_CHAR : NAME_START_CHAR, c) &&
// When namespaces are used, colons are not valid in entity
// names.
Expand All @@ -510,34 +510,34 @@ class SAXParser {
(!this.opt.xmlns || c !== ":"))) {
this.fail("Invalid characer in processing instruction name.");
}
this.procInstName += c;
this.piTarget += c;
}
continue;

case S_PROC_INST_BODY:
if (!this.procInstBody && isWhitespace(c)) {
case S_PI_BODY:
if (!this.piBody && isWhitespace(c)) {
continue;
}
else if (c === "?") {
this.state = S_PROC_INST_ENDING;
this.state = S_PI_ENDING;
}
else {
this.procInstBody += c;
this.piBody += c;
}
continue;

case S_PROC_INST_ENDING:
case S_PI_ENDING:
if (c === ">") {
this.emitNode("onprocessinginstruction", {
name: this.procInstName,
body: this.procInstBody,
target: this.piTarget,
body: this.piBody,
});
this.procInstName = this.procInstBody = "";
this.piTarget = this.piBody = "";
this.state = S_TEXT;
}
else {
this.procInstBody += `?${c}`;
this.state = S_PROC_INST_BODY;
this.piBody += `?${c}`;
this.state = S_PI_BODY;
}
continue;

Expand Down
2 changes: 1 addition & 1 deletion test/issue-84.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require(".").test({
name: "issue 84 (unbalanced quotes in pi)",
xml: "<?has unbalanced \"quotes?><xml>body</xml>",
expect: [
["processinginstruction", { name: "has", body: "unbalanced \"quotes" }],
["processinginstruction", { target: "has", body: "unbalanced \"quotes" }],
["opentagstart", { name: "xml", attributes: {} }],
["opentag", { name: "xml", attributes: {}, isSelfClosing: false }],
["text", "body"],
Expand Down

0 comments on commit 3b508e9

Please sign in to comment.