Skip to content

Commit

Permalink
fix: not escaping double quotes in attribute values
Browse files Browse the repository at this point in the history
  • Loading branch information
kleinfreund committed Jun 26, 2024
1 parent 9281185 commit 88725a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion drupal-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ DrupalAttribute.prototype.toString = function () {
? value.join(" ")
: typeof value === "boolean" ? String(value) : value;

// HTML-escape `"` characters.
const escapedValue = normalizedValue.replace(/"/g, '"');

// Set `attribute` instead of `attribute=""` when the value is the empty string.
components.push(attribute + (normalizedValue === "" ? "" : '="' + normalizedValue + '"'));
components.push(attribute + (escapedValue === "" ? "" : '="' + escapedValue + '"'));
});

let rendered = components.join(" ");
Expand Down
6 changes: 6 additions & 0 deletions drupal-attribute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ describe("DrupalAttribute", () => {
},
` class="one two three"`,
],
[
{
test: '"double-quotes"',
},
' test=""double-quotes""'
],
])("toString", (attributes, htmlAttributeString) => {
const drupalAttribute = new DrupalAttribute(Object.entries(attributes))
expect(drupalAttribute.toString()).toEqual(htmlAttributeString);
Expand Down

0 comments on commit 88725a8

Please sign in to comment.