Skip to content

Commit

Permalink
fix: properly escape all attribute names
Browse files Browse the repository at this point in the history
fixes #650
  • Loading branch information
fczbkk committed Mar 15, 2024
1 parent c7c2e76 commit 52d159e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
23 changes: 8 additions & 15 deletions src/selector-attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { createPatternMatcher, getIntersection } from "./utilities-data.js";
import { CssSelectorGenerated } from "./types.js";

type AttributeData = {
name: string,
value: string
}
name: string;
value: string;
};

// List of attributes to be ignored. These are handled by different selector types.
export const attributeBlacklistMatch = createPatternMatcher([
Expand All @@ -15,13 +15,6 @@ export const attributeBlacklistMatch = createPatternMatcher([
"ng-*",
]);

/**
* Prevents errors when attribute name contains a colon (e.g. "xlink:href").
*/
function sanitizeAttributeName (name: string) {
return name.replace(/:/g, "\\:");
}

/**
* Get simplified attribute selector for an element.
*/
Expand Down Expand Up @@ -60,9 +53,9 @@ export function isValidAttributeNode(
/**
* Sanitize all attribute data. We want to do it once, before we start to generate simplified/full selectors from the same data.
*/
function sanitizeAttributeData({nodeName, nodeValue}: Node): AttributeData {
function sanitizeAttributeData({ nodeName, nodeValue }: Node): AttributeData {
return {
name: sanitizeAttributeName(nodeName),
name: sanitizeSelectorItem(nodeName),
value: sanitizeSelectorItem(nodeValue),
};
}
Expand All @@ -73,9 +66,9 @@ function sanitizeAttributeData({nodeName, nodeValue}: Node): AttributeData {
export function getElementAttributeSelectors(
element: Element,
): CssSelectorGenerated[] {
const validAttributes = Array.from(element.attributes).filter(
(attributeNode) => isValidAttributeNode(attributeNode, element),
).map(sanitizeAttributeData);
const validAttributes = Array.from(element.attributes)
.filter((attributeNode) => isValidAttributeNode(attributeNode, element))
.map(sanitizeAttributeData);
return [
...validAttributes.map(attributeNodeToSimplifiedSelector),
...validAttributes.map(attributeNodeToSelector),
Expand Down
14 changes: 7 additions & 7 deletions test/selector-attribute.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ describe("selector - attribute", function () {
assert.notMatch(result, /^\[/);
});

it('should escape attributes containing colons', () => {
root.innerHTML = '<div aaa:bbb="ccc" />'
const element = root.firstElementChild
const selectors = getAttributeSelectors([element])
it("should escape attributes containing colons and semicolons", () => {
root.innerHTML = '<div aaa:bbb="ccc" ddd;eee="fff" :="ggg" ;="hhh" : ; />';
const element = root.firstElementChild;
const selectors = getAttributeSelectors([element]);
selectors.forEach((selector) => {
assert.doesNotThrow(() => document.querySelector(selector), selector)
assert.equal(document.querySelector(selector), element)
})
assert.doesNotThrow(() => document.querySelector(selector), selector);
assert.equal(document.querySelector(selector), element);
});
});
});

0 comments on commit 52d159e

Please sign in to comment.