File tree 10 files changed +91
-16
lines changed
10 files changed +91
-16
lines changed Original file line number Diff line number Diff line change 4
4
coverage /
5
5
node_modules /
6
6
yarn.lock
7
+ ! /index.d.ts
Original file line number Diff line number Diff line change
1
+ import type { Data , Literal } from 'xast'
2
+
3
+ export type { Options , Quote } from './lib/index.js'
4
+ export { toXml } from './lib/index.js'
5
+
6
+ /**
7
+ * Raw.
8
+ */
9
+ export interface Raw extends Literal {
10
+ /**
11
+ * Node type of raw.
12
+ */
13
+ type : 'raw'
14
+
15
+ /**
16
+ * Data associated with the xast raw.
17
+ */
18
+ data ?: RawData | undefined
19
+ }
20
+
21
+ /**
22
+ * Info associated with xast raw nodes by the ecosystem.
23
+ */
24
+ export interface RawData extends Data { }
25
+
26
+ // Add nodes to tree.
27
+ declare module 'xast' {
28
+ interface ElementContentMap {
29
+ raw : Raw
30
+ }
31
+
32
+ interface RootContentMap {
33
+ raw : Raw
34
+ }
35
+ }
Original file line number Diff line number Diff line change 1
- /**
2
- * @typedef {import('./lib/index.js').Options } Options
3
- * @typedef {import('./lib/index.js').Quote } Quote
4
- */
5
-
1
+ // Note: Types exported from `index.d.ts`.
6
2
export { toXml } from './lib/index.js'
Original file line number Diff line number Diff line change 31
31
* @typedef {'"' | "'" } Quote
32
32
* XML quotes for attribute values.
33
33
*
34
- * @typedef {Literal & {type: 'raw'} } Raw
35
- * To do: improved `Raw` type?
36
- *
37
- *
38
34
* @typedef State
39
35
* Info passed around about the current state.
40
36
* @property {Options } options
Original file line number Diff line number Diff line change 1
1
/**
2
- * @typedef {import('./index.js').Raw } Raw
2
+ * @typedef {import('.. /index.js').Raw } Raw
3
3
* @typedef {import('./index.js').State } State
4
4
*/
5
5
Original file line number Diff line number Diff line change 1
1
/**
2
2
* @typedef {import('xast').Text } Text
3
- * @typedef {import('./index.js').Raw } Raw
3
+ * @typedef {import('.. /index.js').Raw } Raw
4
4
*/
5
5
6
6
import { escape } from './util-escape.js'
Original file line number Diff line number Diff line change 75
75
"strict" : true
76
76
},
77
77
"xo" : {
78
+ "overrides" : [
79
+ {
80
+ "files" : [
81
+ " **/*.ts"
82
+ ],
83
+ "rules" : {
84
+ "@typescript-eslint/consistent-type-definitions" : " off"
85
+ }
86
+ }
87
+ ],
78
88
"prettier" : true ,
79
89
"rules" : {
80
90
"unicorn/prefer-string-replace-all" : " off"
Original file line number Diff line number Diff line change 20
20
* [ ` toXml(tree[, options]) ` ] ( #toxmltree-options )
21
21
* [ ` Options ` ] ( #options )
22
22
* [ ` Quote ` ] ( #quote-1 )
23
+ * [ ` Raw ` ] ( #raw )
23
24
* [ Types] ( #types )
24
25
* [ Compatibility] ( #compatibility )
25
26
* [ Security] ( #security )
@@ -181,11 +182,47 @@ XML quotes for attribute values (TypeScript type).
181
182
type Quote = ' "' | " '"
182
183
` ` `
183
184
185
+ ### ` Raw `
186
+
187
+ Raw (TypeScript type).
188
+
189
+ ###### Type
190
+
191
+ ` ` ` ts
192
+ import type {Data , Literal } from ' xast'
193
+
194
+ interface Raw extends Literal {
195
+ type: ' raw'
196
+ data? : RawData | undefined
197
+ }
198
+
199
+ export interface RawData extends Data {}
200
+ ```
201
+
184
202
## Types
185
203
186
204
This package is fully typed with [ TypeScript] [ ] .
187
- It exports the additional types [ ` Options ` ][api-options] and
188
- [ ` Quote ` ][api-quote].
205
+ It exports the additional types [ ` Options ` ] [ api-options ] , [ ` Quote ` ] [ api-quote ] ,
206
+ and [ ` Raw ` ] [ api-raw ] .
207
+
208
+ It also registers the node type with ` @types/xast ` .
209
+ If you’re working with the syntax tree, make sure to import this utility
210
+ somewhere in your types, as that registers the new node types in the tree.
211
+
212
+ ``` js
213
+ /**
214
+ * @typedef {import('xast-util-to-xml')}
215
+ */
216
+
217
+ import {visit } from ' unist-util-visit'
218
+
219
+ /** @type {import('xast').Root} */
220
+ const tree = getXastNodeSomeHow ()
221
+
222
+ visit (tree, function (node ) {
223
+ // `node` can now be a raw node.
224
+ })
225
+ ```
189
226
190
227
## Compatibility
191
228
@@ -287,3 +324,5 @@ abide by its terms.
287
324
[ api-options ] : #options
288
325
289
326
[ api-quote ] : #quote-1
327
+
328
+ [ api-raw ] : #raw
Original file line number Diff line number Diff line change @@ -6,7 +6,6 @@ import {toXml} from '../index.js'
6
6
test ( '`raw`' , async function ( t ) {
7
7
await t . test ( 'should encode `raw`s' , async function ( ) {
8
8
assert . deepEqual (
9
- // @ts -expect-error: check how the runtime handles `raw` nodes.
10
9
toXml ( u ( 'raw' , '<script>alert("XSS!")</script>' ) ) ,
11
10
'<script>alert("XSS!")</script>'
12
11
)
@@ -16,7 +15,6 @@ test('`raw`', async function (t) {
16
15
'should not encode `raw`s in `allowDangerousXml` mode' ,
17
16
async function ( ) {
18
17
assert . deepEqual (
19
- // @ts -expect-error: check how the runtime handles `raw` nodes.
20
18
toXml ( u ( 'raw' , '<script>alert("XSS!")</script>' ) , {
21
19
allowDangerousXml : true
22
20
} ) ,
Original file line number Diff line number Diff line change 11
11
"target" : " es2020"
12
12
},
13
13
"exclude" : [" coverage/" , " node_modules/" ],
14
- "include" : [" **/*.js" ]
14
+ "include" : [" **/*.js" , " index.d.ts " ]
15
15
}
You can’t perform that action at this time.
0 commit comments