Commit 08c2876d authored by David Sveningsson's avatar David Sveningsson
Browse files

fix(event): pass `null` when attribute value is missing

parent fbbc87cf
Pipeline #235458724 passed with stages
in 10 minutes and 35 seconds
......@@ -66,10 +66,10 @@ export interface AttributeEvent extends Event {
key: string;
/** Attribute value. */
value: string | DynamicValue;
value: string | DynamicValue | null;
/** Quotemark used. */
quote: '"' | "'" | undefined;
quote: '"' | "'" | null;
/** Set to original attribute when a transformer dynamically added this
* attribute. */
......
......@@ -332,7 +332,7 @@ describe("parser", () => {
event: "attr",
key: "foo",
value: "bar",
quote: undefined,
quote: null,
target: "div",
valueLocation: expect.objectContaining({
line: 1,
......@@ -450,8 +450,8 @@ describe("parser", () => {
expect(events.shift()).toEqual({
event: "attr",
key: "foo",
value: undefined,
quote: undefined,
value: null,
quote: null,
target: "div",
valueLocation: null,
});
......@@ -506,8 +506,8 @@ describe("parser", () => {
expect(events.shift()).toEqual({
event: "attr",
key: "foo-bar-baz",
value: undefined,
quote: undefined,
value: null,
quote: null,
target: "div",
valueLocation: null,
});
......
......@@ -337,11 +337,13 @@ export class Parser {
const haveValue = next && next.type === TokenType.ATTR_VALUE;
const attrData: AttributeData = {
key: token.data[1],
value: null,
quote: null,
};
if (haveValue) {
attrData.value = next.data[1];
attrData.quote = next.data[2];
if (next && haveValue) {
attrData.value = next.data[1] ?? null;
attrData.quote = next.data[2] ?? null;
}
/* get callback to process attributes, default is to just return attribute
......@@ -367,7 +369,7 @@ export class Parser {
/* process attribute(s) */
for (const attr of iterator) {
this.trigger("attr", {
const event: AttributeEvent = {
target: node,
key: attr.key,
value: attr.value,
......@@ -375,8 +377,8 @@ export class Parser {
originalAttribute: attr.originalAttribute,
location: keyLocation,
valueLocation,
});
};
this.trigger("attr", event);
node.setAttribute(attr.key, attr.value, keyLocation, valueLocation, attr.originalAttribute);
}
}
......
......@@ -43,11 +43,11 @@ export default class AttrQuotes extends Rule<void, Options> {
public setup(): void {
this.on("attr", (event: AttributeEvent) => {
/* ignore attributes with no value */
if (typeof event.value === "undefined") {
if (event.value === null) {
return;
}
if (typeof event.quote === "undefined") {
if (!event.quote) {
if (this.options.unquoted === false) {
this.report(event.target, `Attribute "${event.key}" using unquoted value`);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment