Skip to content

Commit

Permalink
findAncestorNs: replace type coercion with type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneRifle committed Jul 15, 2023
1 parent 493e7bf commit c9ba5b6
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ function findNSPrefix(subset) {
return subset.prefix || "";
}

function isElementSubset(docSubset: Node[]): docSubset is Element[] {
return docSubset.every((node) => xpath.isElement(node))
}

/**
* Extract ancestor namespaces in order to import it to root of document subset
* which is being canonicalized for non-exclusive c14n.
Expand All @@ -222,20 +226,17 @@ export function findAncestorNs(
namespaceResolver?: XPathNSResolver
) {
const docSubset = xpath.selectWithResolver(docSubsetXpath, doc, namespaceResolver);
let elementSubset: Element[] = [];

if (!isArrayHasLength(docSubset)) {
return [];
}

if (!docSubset.every((node) => xpath.isElement(node))) {
if (!isElementSubset(docSubset)) {
throw new Error("Document subset must be list of elements");
} else {
elementSubset = docSubset as Element[];
}

// Remove duplicate on ancestor namespace
const ancestorNs = collectAncestorNamespaces(elementSubset[0]);
const ancestorNs = collectAncestorNamespaces(docSubset[0]);
const ancestorNsWithoutDuplicate: NamespacePrefix[] = [];
for (let i = 0; i < ancestorNs.length; i++) {
let notOnTheList = true;
Expand Down

0 comments on commit c9ba5b6

Please sign in to comment.