Skip to content

Commit

Permalink
feat: Expose the quotes that were used in the onattribute event
Browse files Browse the repository at this point in the history
Partially addresses #421
  • Loading branch information
fb55 committed Sep 1, 2020
1 parent ddd2f63 commit 3c86256
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 90 deletions.
4 changes: 2 additions & 2 deletions src/MultiplexHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export default class MultiplexHandler implements Handler {
this._func = func;
}

onattribute(name: string, value: string) {
this._func("onattribute", name, value);
onattribute(name: string, value: string, quote: string | null | undefined) {
this._func("onattribute", name, value, quote);
}
oncdatastart() {
this._func("oncdatastart");
Expand Down
16 changes: 13 additions & 3 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,17 @@ export interface Handler {
onerror(error: Error): void;
onclosetag(name: string): void;
onopentagname(name: string): void;
onattribute(name: string, value: string): void;
/**
*
* @param name Name of the attribute
* @param value Value of the attribute.
* @param quote Quotes used around the attribute. `null` if the attribute has no quotes around the value, `undefined` if the attribute has no value.
*/
onattribute(
name: string,
value: string,
quote?: string | undefined | null
): void;
onopentag(name: string, attribs: { [s: string]: string }): void;
ontext(data: string): void;
oncomment(data: string): void;
Expand Down Expand Up @@ -335,8 +345,8 @@ export class Parser {
this.attribvalue += value;
}

onattribend() {
this.cbs.onattribute?.(this.attribname, this.attribvalue);
onattribend(quote: string | undefined | null) {
this.cbs.onattribute?.(this.attribname, this.attribvalue, quote);
if (
this.attribs &&
!Object.prototype.hasOwnProperty.call(this.attribs, this.attribname)
Expand Down
28 changes: 11 additions & 17 deletions src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function isASCIIAlpha(c: string): boolean {

interface Callbacks {
onattribdata(value: string): void;
onattribend(): void;
onattribend(quote: string | undefined | null): void;
onattribname(name: string): void;
oncdata(data: string): void;
onclosetag(name: string): void;
Expand Down Expand Up @@ -432,11 +432,11 @@ export default class Tokenizer {
if (c === "=") {
this._state = State.BeforeAttributeValue;
} else if (c === "/" || c === ">") {
this.cbs.onattribend();
this.cbs.onattribend(undefined);
this._state = State.BeforeAttributeName;
this._index--;
} else if (!whitespace(c)) {
this.cbs.onattribend();
this.cbs.onattribend(undefined);
this._state = State.InAttributeName;
this.sectionStart = this._index;
}
Expand All @@ -454,10 +454,10 @@ export default class Tokenizer {
this._index--; // Reconsume token
}
}
private stateInAttributeValueDoubleQuotes(c: string) {
if (c === '"') {
private handleInAttributeValue(c: string, quote: string) {
if (c === quote) {
this.emitToken("onattribdata");
this.cbs.onattribend();
this.cbs.onattribend(quote);
this._state = State.BeforeAttributeName;
} else if (this.decodeEntities && c === "&") {
this.emitToken("onattribdata");
Expand All @@ -466,22 +466,16 @@ export default class Tokenizer {
this.sectionStart = this._index;
}
}
private stateInAttributeValueDoubleQuotes(c: string) {
this.handleInAttributeValue(c, '"');
}
private stateInAttributeValueSingleQuotes(c: string) {
if (c === "'") {
this.emitToken("onattribdata");
this.cbs.onattribend();
this._state = State.BeforeAttributeName;
} else if (this.decodeEntities && c === "&") {
this.emitToken("onattribdata");
this.baseState = this._state;
this._state = State.BeforeEntity;
this.sectionStart = this._index;
}
this.handleInAttributeValue(c, "'");
}
private stateInAttributeValueNoQuotes(c: string) {
if (whitespace(c) || c === ">") {
this.emitToken("onattribdata");
this.cbs.onattribend();
this.cbs.onattribend(null);
this._state = State.BeforeAttributeName;
this._index--;
} else if (this.decodeEntities && c === "&") {
Expand Down
2 changes: 1 addition & 1 deletion src/__fixtures__/Events/01-simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
{
"event": "attribute",
"data": ["class", "test"]
"data": ["class", "test", null]
},
{
"event": "opentag",
Expand Down
2 changes: 1 addition & 1 deletion src/__fixtures__/Events/02-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
{
"event": "attribute",
"data": ["type", "text/template"]
"data": ["type", "text/template", "\""]
},
{
"event": "opentag",
Expand Down
2 changes: 1 addition & 1 deletion src/__fixtures__/Events/03-lowercase_tags.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
{
"event": "attribute",
"data": ["class", "test"]
"data": ["class", "test", null]
},
{
"event": "opentag",
Expand Down
2 changes: 1 addition & 1 deletion src/__fixtures__/Events/07-self-closing.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
{
"event": "attribute",
"data": ["href", "http://test.com/"]
"data": ["href", "http://test.com/", null]
},
{
"event": "opentag",
Expand Down
6 changes: 3 additions & 3 deletions src/__fixtures__/Events/08-implicit-close-tags.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
{ "event": "opentagname", "data": ["ol"] },
{ "event": "opentag", "data": ["ol", {}] },
{ "event": "opentagname", "data": ["li"] },
{ "event": "attribute", "data": ["class", "test"] },
{ "event": "attribute", "data": ["class", "test", null] },
{ "event": "opentag", "data": ["li", { "class": "test" }] },
{ "event": "opentagname", "data": ["div"] },
{ "event": "opentag", "data": ["div", {}] },
{ "event": "opentagname", "data": ["table"] },
{ "event": "attribute", "data": ["style", "width:100%"] },
{ "event": "attribute", "data": ["style", "width:100%", null] },
{ "event": "opentag", "data": ["table", { "style": "width:100%" }] },
{ "event": "opentagname", "data": ["tr"] },
{ "event": "opentag", "data": ["tr", {}] },
Expand All @@ -19,7 +19,7 @@
{ "event": "text", "data": ["TH"] },
{ "event": "closetag", "data": ["th"] },
{ "event": "opentagname", "data": ["td"] },
{ "event": "attribute", "data": ["colspan", "2"] },
{ "event": "attribute", "data": ["colspan", "2", null] },
{ "event": "opentag", "data": ["td", { "colspan": "2" }] },
{ "event": "opentagname", "data": ["h3"] },
{ "event": "opentag", "data": ["h3", {}] },
Expand Down
6 changes: 3 additions & 3 deletions src/__fixtures__/Events/09-attributes.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
},
{
"event": "attribute",
"data": ["class", "test0"]
"data": ["class", "test0", "\""]
},
{
"event": "attribute",
"data": ["title", "test1"]
"data": ["title", "test1", "\""]
},
{
"event": "attribute",
"data": ["disabled", ""]
},
{
"event": "attribute",
"data": ["value", "test2"]
"data": ["value", "test2", null]
},
{
"event": "opentag",
Expand Down
2 changes: 1 addition & 1 deletion src/__fixtures__/Events/10-crazy-attrib.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
{
"event": "attribute",
"data": ["<", ""]
"data": ["<", "", "'"]
},
{
"event": "attribute",
Expand Down
4 changes: 2 additions & 2 deletions src/__fixtures__/Events/12-long-comment-end.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"html": "<meta id='before'><!-- text ---><meta id='after'>",
"expected": [
{ "event": "opentagname", "data": ["meta"] },
{ "event": "attribute", "data": ["id", "before"] },
{ "event": "attribute", "data": ["id", "before", "'"] },
{ "event": "opentag", "data": ["meta", { "id": "before" }] },
{ "event": "closetag", "data": ["meta"] },
{ "event": "comment", "data": [" text -"] },
{ "event": "commentend", "data": [] },
{ "event": "opentagname", "data": ["meta"] },
{ "event": "attribute", "data": ["id", "after"] },
{ "event": "attribute", "data": ["id", "after", "'"] },
{ "event": "opentag", "data": ["meta", { "id": "after" }] },
{ "event": "closetag", "data": ["meta"] }
]
Expand Down
4 changes: 2 additions & 2 deletions src/__fixtures__/Events/16-double_attribs.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
},
{
"event": "attribute",
"data": ["class", "test"]
"data": ["class", "test", null]
},
{
"event": "attribute",
"data": ["class", "boo"]
"data": ["class", "boo", null]
},
{
"event": "opentag",
Expand Down
3 changes: 2 additions & 1 deletion src/__fixtures__/Events/21-entity_in_attribute.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"event": "attribute",
"data": [
"href",
"http://example.com/page?param=value&param2&param3=<val&; & &"
"http://example.com/page?param=value&param2&param3=<val&; & &",
"'"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions src/__fixtures__/Events/27-entities_in_attributes.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
},
{
"event": "attribute",
"data": ["bar", "&"]
"data": ["bar", "&", null]
},
{
"event": "attribute",
"data": ["baz", "&"]
"data": ["baz", "&", "\""]
},
{
"event": "attribute",
"data": ["boo", "&"]
"data": ["boo", "&", "'"]
},
{
"event": "attribute",
"data": ["noo", ""]
"data": ["noo", "", null]
},
{
"event": "opentag",
Expand Down
2 changes: 1 addition & 1 deletion src/__fixtures__/Events/36-entity-in-attrib.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
{
"event": "attribute",
"data": ["src", "?&image_uri=1&ℑ=2&image=3"]
"data": ["src", "?&image_uri=1&ℑ=2&image=3", "\""]
},
{
"event": "opentag",
Expand Down
2 changes: 1 addition & 1 deletion src/__fixtures__/Stream/02-RSS.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
{
"event": "attribute",
"data": ["version", "2.0"]
"data": ["version", "2.0", "\""]
},
{
"event": "opentag",
Expand Down
22 changes: 11 additions & 11 deletions src/__fixtures__/Stream/03-Atom.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
{
"event": "attribute",
"data": ["xmlns", "http://www.w3.org/2005/Atom"]
"data": ["xmlns", "http://www.w3.org/2005/Atom", "\""]
},
{
"event": "opentag",
Expand Down Expand Up @@ -90,11 +90,11 @@
},
{
"event": "attribute",
"data": ["href", "http://example.org/feed/"]
"data": ["href", "http://example.org/feed/", "\""]
},
{
"event": "attribute",
"data": ["rel", "self"]
"data": ["rel", "self", "\""]
},
{
"event": "opentag",
Expand All @@ -120,7 +120,7 @@
},
{
"event": "attribute",
"data": ["href", "http://example.org/"]
"data": ["href", "http://example.org/", "\""]
},
{
"event": "opentag",
Expand Down Expand Up @@ -277,7 +277,7 @@
},
{
"event": "attribute",
"data": ["href", "http://example.org/2003/12/13/atom03"]
"data": ["href", "http://example.org/2003/12/13/atom03", "\""]
},
{
"event": "opentag",
Expand All @@ -302,15 +302,15 @@
},
{
"event": "attribute",
"data": ["rel", "alternate"]
"data": ["rel", "alternate", "\""]
},
{
"event": "attribute",
"data": ["type", "text/html"]
"data": ["type", "text/html", "\""]
},
{
"event": "attribute",
"data": ["href", "http://example.org/2003/12/13/atom03.html"]
"data": ["href", "http://example.org/2003/12/13/atom03.html", "\""]
},
{
"event": "opentag",
Expand All @@ -337,11 +337,11 @@
},
{
"event": "attribute",
"data": ["rel", "edit"]
"data": ["rel", "edit", "\""]
},
{
"event": "attribute",
"data": ["href", "http://example.org/2003/12/13/atom03/edit"]
"data": ["href", "http://example.org/2003/12/13/atom03/edit", "\""]
},
{
"event": "opentag",
Expand Down Expand Up @@ -407,7 +407,7 @@
},
{
"event": "attribute",
"data": ["type", "html"]
"data": ["type", "html", "\""]
},
{
"event": "opentag",
Expand Down

0 comments on commit 3c86256

Please sign in to comment.