Skip to content

Commit

Permalink
Merge pull request #190 from ymrl/add_xlinkhref_to_link
Browse files Browse the repository at this point in the history
👍 add xlink:href of a in svg
  • Loading branch information
ymrl authored Sep 5, 2024
2 parents 06065f9 + 2473b89 commit a538cf1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/rules/link-href/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ describe("link-href", () => {
]);
});

test("svg a with xlink:href", () => {
const element = document.createElementNS("http://www.w3.org/2000/svg", "a");
element.setAttribute("xlink:href", "https://example.com");
document.body.appendChild(element);
const result = LinkHref.evaluate(element, { enabled: true }, {});
expect(result).toBeUndefined();
});

test("svg a without xlink:href", () => {
const element = document.createElementNS("http://www.w3.org/2000/svg", "a");
document.body.appendChild(element);
const result = LinkHref.evaluate(element, { enabled: true }, {});
expect(result).toEqual([
{
type: "warning",
ruleName: "link-href",
message: "No href attribute",
},
]);
});

test("disabled", () => {
const element = document.createElement("a");
document.body.appendChild(element);
Expand Down
4 changes: 3 additions & 1 deletion src/rules/link-href/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export const LinkHref: RuleObject = {
if (!enabled) {
return undefined;
}
const isInSVG = element.namespaceURI === "http://www.w3.org/2000/svg";
const href = element.getAttribute("href");
if (!href) {
const xlinkHref = element.getAttribute("xlink:href");
if (!href && !(isInSVG && xlinkHref)) {
return [
{
type: "warning",
Expand Down

0 comments on commit a538cf1

Please sign in to comment.