Skip to content

Commit 2a64114

Browse files
committed
refactor: c14n options
1 parent b48b429 commit 2a64114

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

src/c14n.mts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export type XmlC14NIsVisibleCallback = (node: XmlNodePtr, parent: XmlNodePtr) =>
102102
/**
103103
* Options for XML canonicalization
104104
*/
105-
export interface C14NOptionsBase {
105+
export interface C14NOptions {
106106
/** The canonicalization mode to use
107107
* @default XmlC14NMode.XML_C14N_1_0
108108
*/
@@ -117,25 +117,22 @@ export interface C14NOptionsBase {
117117
* Only applies when mode is XML_C14N_EXCLUSIVE_1_0
118118
*/
119119
inclusiveNamespacePrefixes?: string[];
120-
}
121120

122-
export interface C14NOptionsWithCallback extends C14NOptionsBase {
123121
/** Custom callback to determine node visibility
124-
* Cannot be used together with nodeSet
122+
* Must not be used together with {@link nodeSet}
125123
*/
126-
isVisible: XmlC14NIsVisibleCallback;
127-
nodeSet?: never;
128-
}
124+
isVisible?: XmlC14NIsVisibleCallback;
129125

130-
export interface C14NOptionsWithNodeSet extends C14NOptionsBase {
131126
/** Set of nodes to include in canonicalization
132-
* Cannot be used together with isVisible
127+
* Must not be used together with {@link isVisible}
133128
*/
134-
nodeSet: Set<XmlNode>;
135-
isVisible?: never;
129+
nodeSet?: Set<XmlNode>;
136130
}
137131

138-
export type C14NOptions = C14NOptionsWithCallback | C14NOptionsWithNodeSet | C14NOptionsBase;
132+
/**
133+
* C14N options without filtering callbacks (for subtree canonicalization)
134+
*/
135+
export type SubtreeC14NOptions = Omit<C14NOptions, 'isVisible' | 'nodeSet'>;
139136

140137
/**
141138
* Check if a node is within a subtree rooted at a specific node by walking
@@ -172,7 +169,9 @@ function canonicalizeInternal(
172169
cascade: boolean = true,
173170
): void {
174171
const hasIsVisible = (opts: C14NOptions):
175-
opts is C14NOptions & { isVisible: XmlC14NIsVisibleCallback } => typeof (opts as any).isVisible === 'function';
172+
opts is C14NOptions & {
173+
isVisible: XmlC14NIsVisibleCallback
174+
} => typeof (opts as any).isVisible === 'function';
176175

177176
const hasNodeSet = (opts: C14NOptions):
178177
opts is C14NOptions & { nodeSet: Set<XmlNode> } => (opts as any).nodeSet instanceof Set;
@@ -195,7 +194,8 @@ function canonicalizeInternal(
195194
const context: C14NCallbackContext = {
196195
jsCallback: hasIsVisible(options) ? options.isVisible : null,
197196
rootPtrs: hasNodeSet(options)
198-
? new Set(Array.from(options.nodeSet).map((n) => n._nodePtr))
197+
? new Set(Array.from(options.nodeSet)
198+
.map((n) => n._nodePtr))
199199
: null,
200200
cascade,
201201
invisible: cascade ? new Set<number>() : null,
@@ -286,7 +286,7 @@ export function canonicalizeSubtree(
286286
handler: XmlOutputBufferHandler,
287287
doc: XmlDocument,
288288
subtreeRoot: XmlNode,
289-
options: C14NOptionsBase = {},
289+
options: SubtreeC14NOptions = {},
290290
): void {
291291
const subtreeRootPtr = subtreeRoot._nodePtr;
292292
const isVisible = (nodePtr: number, parentPtr: number) => (

src/index.mts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ export {
5757
export {
5858
XmlC14NMode,
5959
type C14NOptions,
60-
type C14NOptionsBase,
61-
type C14NOptionsWithCallback,
62-
type C14NOptionsWithNodeSet,
60+
type SubtreeC14NOptions,
6361
type XmlC14NIsVisibleCallback,
6462
} from './c14n.mjs';

src/nodes.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import type { XmlDocPtr, XmlNodePtr, XmlNsPtr } from './libxml2raw.mjs';
4343
import { XmlStringOutputBufferHandler } from './utils.mjs';
4444
import { NamespaceMap, XmlXPath } from './xpath.mjs';
4545
import {
46-
canonicalizeSubtree, C14NOptionsBase,
46+
canonicalizeSubtree, SubtreeC14NOptions,
4747
} from './c14n.mjs';
4848

4949
function compiledXPathEval(nodePtr: XmlNodePtr, xpath: XmlXPath) {
@@ -198,7 +198,7 @@ export abstract class XmlNode {
198198
* @see {@link canonicalizeSubtree}
199199
* @see {@link canonicalizeToString}
200200
*/
201-
canonicalize(handler: XmlOutputBufferHandler, options?: C14NOptionsBase): void {
201+
canonicalize(handler: XmlOutputBufferHandler, options?: SubtreeC14NOptions): void {
202202
canonicalizeSubtree(handler, this.doc, this, options);
203203
}
204204

@@ -209,7 +209,7 @@ export abstract class XmlNode {
209209
* @returns The canonicalized XML string.
210210
* @see {@link canonicalize}
211211
*/
212-
canonicalizeToString(options?: C14NOptionsBase): string {
212+
canonicalizeToString(options?: SubtreeC14NOptions): string {
213213
const handler = new XmlStringOutputBufferHandler();
214214
this.canonicalize(handler, options);
215215
return handler.result;

0 commit comments

Comments
 (0)