Skip to content

Commit

Permalink
only search for attributes in the top-level tag
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Dec 6, 2022
1 parent 92bfc87 commit a1f3d3f
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions get-attribute.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
function getAttribute(tag, attributeName, options) {
const debug = (options && options.debug) || false;
if (debug) console.log("getting " + attributeName + " in " + tag);
if (debug) console.log("[xml-utils] getting " + attributeName + " in " + tag);

const xml = typeof tag === "object" ? tag.outer : tag;

// only search for attributes in the opening tag
const opening = xml.slice(0, xml.indexOf(">") + 1);

const pattern = `${attributeName}\\="\([^"]*\)"`;
if (debug) console.log("pattern:", pattern);
if (debug) console.log("[xml-utils] pattern:", pattern);

const re = new RegExp(pattern);
const match = re.exec(xml);
if (debug) console.log("match:", match);
const match = re.exec(opening);
if (debug) console.log("[xml-utils] match:", match);
if (match) return match[1];
}

Expand Down

0 comments on commit a1f3d3f

Please sign in to comment.