File tree Expand file tree Collapse file tree 10 files changed +91
-16
lines changed Expand file tree Collapse file tree 10 files changed +91
-16
lines changed Original file line number Diff line number Diff line change 44coverage /
55node_modules /
66yarn.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`.
62export { toXml } from './lib/index.js'
Original file line number Diff line number Diff line change 3131 * @typedef {'"' | "'" } Quote
3232 * XML quotes for attribute values.
3333 *
34- * @typedef {Literal & {type: 'raw'} } Raw
35- * To do: improved `Raw` type?
36- *
37- *
3834 * @typedef State
3935 * Info passed around about the current state.
4036 * @property {Options } options
Original file line number Diff line number Diff line change 11/**
2- * @typedef {import('./index.js').Raw } Raw
2+ * @typedef {import('.. /index.js').Raw } Raw
33 * @typedef {import('./index.js').State } State
44 */
55
Original file line number Diff line number Diff line change 11/**
22 * @typedef {import('xast').Text } Text
3- * @typedef {import('./index.js').Raw } Raw
3+ * @typedef {import('.. /index.js').Raw } Raw
44 */
55
66import { escape } from './util-escape.js'
Original file line number Diff line number Diff line change 7575 "strict" : true
7676 },
7777 "xo" : {
78+ "overrides" : [
79+ {
80+ "files" : [
81+ " **/*.ts"
82+ ],
83+ "rules" : {
84+ "@typescript-eslint/consistent-type-definitions" : " off"
85+ }
86+ }
87+ ],
7888 "prettier" : true ,
7989 "rules" : {
8090 "unicorn/prefer-string-replace-all" : " off"
Original file line number Diff line number Diff line change 2020 * [ ` toXml(tree[, options]) ` ] ( #toxmltree-options )
2121 * [ ` Options ` ] ( #options )
2222 * [ ` Quote ` ] ( #quote-1 )
23+ * [ ` Raw ` ] ( #raw )
2324* [ Types] ( #types )
2425* [ Compatibility] ( #compatibility )
2526* [ Security] ( #security )
@@ -181,11 +182,47 @@ XML quotes for attribute values (TypeScript type).
181182type Quote = ' "' | " '"
182183` ` `
183184
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+
184202## Types
185203
186204This 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+ ```
189226
190227## Compatibility
191228
@@ -287,3 +324,5 @@ abide by its terms.
287324[ api-options ] : #options
288325
289326[ 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'
66test ( '`raw`' , async function ( t ) {
77 await t . test ( 'should encode `raw`s' , async function ( ) {
88 assert . deepEqual (
9- // @ts -expect-error: check how the runtime handles `raw` nodes.
109 toXml ( u ( 'raw' , '<script>alert("XSS!")</script>' ) ) ,
1110 '<script>alert("XSS!")</script>'
1211 )
@@ -16,7 +15,6 @@ test('`raw`', async function (t) {
1615 'should not encode `raw`s in `allowDangerousXml` mode' ,
1716 async function ( ) {
1817 assert . deepEqual (
19- // @ts -expect-error: check how the runtime handles `raw` nodes.
2018 toXml ( u ( 'raw' , '<script>alert("XSS!")</script>' ) , {
2119 allowDangerousXml : true
2220 } ) ,
Original file line number Diff line number Diff line change 1111 "target" : " es2020"
1212 },
1313 "exclude" : [" coverage/" , " node_modules/" ],
14- "include" : [" **/*.js" ]
14+ "include" : [" **/*.js" , " index.d.ts " ]
1515}
You can’t perform that action at this time.
0 commit comments