MCPcopy
hub / github.com/webpack/webpack / querySelectorAll

Method querySelectorAll

test/helpers/FakeDocument.js:86–147  ·  view source on GitHub ↗

* @param {string} selector selector * @returns {FakeElement[]} matching elements

(selector)

Source from the content-addressed store, hash-verified

84 * @returns {FakeElement[]} matching elements
85 */
86 querySelectorAll(selector) {
87 // Simple selector support for common cases
88 // Tag selector: "link", "script", etc.
89 if (/^[a-zA-Z][a-zA-Z0-9-]*$/.test(selector)) {
90 return this.getElementsByTagName(selector);
91 }
92 // Class selector: ".class"
93 if (selector.startsWith(".")) {
94 const className = selector.slice(1);
95 /** @type {FakeElement[]} */
96 const allElements = [];
97 for (const elements of this._elementsByTagName.values()) {
98 for (const element of elements) {
99 if (element.getAttribute("class") === className) {
100 allElements.push(element);
101 }
102 }
103 }
104 return allElements;
105 }
106 // ID selector: "#id"
107 if (selector.startsWith("#")) {
108 const id = selector.slice(1);
109 for (const elements of this._elementsByTagName.values()) {
110 for (const element of elements) {
111 if (element.getAttribute("id") === id) {
112 return [element];
113 }
114 }
115 }
116 return [];
117 }
118 // Attribute selector: "[attr]", "[attr=value]"
119 if (selector.startsWith("[") && selector.endsWith("]")) {
120 const attrSelector = selector.slice(1, -1);
121 /** @type {FakeElement[]} */
122 const allElements = [];
123 if (attrSelector.includes("=")) {
124 const [attr, value] = attrSelector
125 .split("=")
126 .map((s) => s.trim().replace(/^["']|["']$/g, ""));
127 for (const elements of this._elementsByTagName.values()) {
128 for (const element of elements) {
129 if (element.getAttribute(attr) === value) {
130 allElements.push(element);
131 }
132 }
133 }
134 } else {
135 for (const elements of this._elementsByTagName.values()) {
136 for (const element of elements) {
137 if (element.getAttribute(attrSelector) !== undefined) {
138 allElements.push(element);
139 }
140 }
141 }
142 }
143 return allElements;

Callers

nothing calls this directly

Calls 8

getElementsByTagNameMethod · 0.95
sliceMethod · 0.80
getAttributeMethod · 0.80
splitMethod · 0.80
testMethod · 0.45
valuesMethod · 0.45
pushMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected