MCPcopy Create free account
hub / github.com/NaturalIntelligence/fast-xml-parser / buildAttributesMap

Function buildAttributesMap

src/xmlparser/OrderedObjParser.js:208–291  ·  view source on GitHub ↗
(attrStr, jPath, tagName, force = false)

Source from the content-addressed store, hash-verified

206}
207
208function buildAttributesMap(attrStr, jPath, tagName, force = false) {
209 const options = this.options;
210 if (force === true || (options.ignoreAttributes !== true && typeof attrStr === 'string')) {
211 // attrStr = attrStr.replace(/\r?\n/g, ' ');
212 //attrStr = attrStr || attrStr.trim();
213
214 const matches = getAllMatches(attrStr, attrsRegx);
215 const len = matches.length; //don't make it inline
216 const attrs = {};
217
218 // Pre-process values once: trim + entity replacement
219 // Reused in both matcher update and second pass
220 const processedVals = new Array(len);
221 let hasRawAttrs = false;
222 const rawAttrsForMatcher = {};
223
224 for (let i = 0; i < len; i++) {
225 const attrName = this.resolveNameSpace(matches[i][1]);
226 const oldVal = attrValue(matches[i]);
227
228 if (attrName.length && oldVal !== undefined) {
229 let val = oldVal;
230 if (options.trimValues) val = val.trim();
231 val = this.replaceEntitiesValue(val, tagName, this.readonlyMatcher);
232 processedVals[i] = val;
233
234 rawAttrsForMatcher[attrName] = val;
235 hasRawAttrs = true;
236 }
237 }
238
239 // Update matcher ONCE before second pass, if applicable
240 if (hasRawAttrs && typeof jPath === 'object' && jPath.updateCurrent) {
241 jPath.updateCurrent(rawAttrsForMatcher);
242 }
243
244 // Hoist toString() once — path doesn't change during attribute processing
245 const jPathStr = options.jPath ? jPath.toString() : this.readonlyMatcher;
246
247 // Second pass: apply processors, build final attrs
248 let hasAttrs = false;
249 for (let i = 0; i < len; i++) {
250 const attrName = this.resolveNameSpace(matches[i][1]);
251
252 if (this.ignoreAttributesFn(attrName, jPathStr)) continue;
253
254 let aName = options.attributeNamePrefix + attrName;
255
256 if (attrName.length) {
257 if (options.transformAttributeName) {
258 aName = options.transformAttributeName(aName);
259 }
260 aName = sanitizeName(aName, options);
261
262 if (attrValue(matches[i]) !== undefined) {
263 // Reuse already-processed value — no double entity replacement
264 const oldVal = processedVals[i];
265

Callers

nothing calls this directly

Calls 7

getAllMatchesFunction · 0.90
attrValueFunction · 0.85
sanitizeNameFunction · 0.85
parseValueFunction · 0.85
replaceEntitiesValueMethod · 0.45
updateCurrentMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected