@@ -4,7 +4,6 @@ import { TreeAdapter, TreeAdapterTypeMap } from 'parse5/dist/tree-adapters/inter
4
4
import * as assert from 'node:assert' ;
5
5
import * as fs from 'node:fs' ;
6
6
import * as path from 'node:path' ;
7
- import { escapeString } from 'parse5/dist/serializer/index.js' ;
8
7
import * as parse5 from 'parse5/dist/index.js' ;
9
8
import {
10
9
removeNewLines ,
@@ -13,22 +12,19 @@ import {
13
12
normalizeNewLine ,
14
13
generateTestsForEachTreeAdapter ,
15
14
} from './common.js' ;
15
+ import * as doctype from 'parse5/dist/common/doctype.js' ;
16
16
17
17
function walkTree < T extends TreeAdapterTypeMap > (
18
- document : T [ 'document ' ] ,
18
+ parent : T [ 'parentNode ' ] ,
19
19
treeAdapter : TreeAdapter < T > ,
20
20
handler : ( node : T [ 'node' ] ) => void
21
21
) : void {
22
- const stack = [ ... treeAdapter . getChildNodes ( document ) ] ;
23
- let node ;
24
- while ( ( node = stack . shift ( ) ) ) {
25
- const children = treeAdapter . getChildNodes ( node ) ;
22
+ for ( const node of treeAdapter . getChildNodes ( parent ) ) {
23
+ if ( treeAdapter . isElementNode ( node ) ) {
24
+ walkTree ( node , treeAdapter , handler ) ;
25
+ }
26
26
27
27
handler ( node ) ;
28
-
29
- if ( children ?. length ) {
30
- stack . unshift ( ...children ) ;
31
- }
32
28
}
33
29
}
34
30
@@ -75,7 +71,10 @@ function assertAttrsLocation(location: ElementLocation, serializedNode: string,
75
71
assert . ok ( location . attrs , 'Expected attrs to be defined' ) ;
76
72
77
73
for ( const attr of Object . values ( location . attrs ) ) {
78
- const expected = serializedNode . slice ( attr . startOffset , attr . endOffset ) ;
74
+ const expected = serializedNode . slice (
75
+ attr . startOffset - location . startOffset ,
76
+ attr . endOffset - location . startOffset
77
+ ) ;
79
78
80
79
assertLocation ( attr , expected , html , lines ) ;
81
80
}
@@ -113,42 +112,44 @@ export function generateLocationInfoParserTests(
113
112
//Then for each node in the tree we run the serializer and compare results with the substring
114
113
//obtained via the location info from the expected serialization results.
115
114
it ( `Location info (Parser) - ${ test . name } ` , async ( ) => {
116
- const serializerOpts = { treeAdapter } ;
117
- const html = escapeString ( test . data ) ;
115
+ const html = test . data ;
118
116
const lines = html . split ( / \r ? \n / g) ;
119
117
120
118
const parserOpts = {
121
119
treeAdapter,
122
120
sourceCodeLocationInfo : true ,
123
121
} ;
124
122
125
- const parsingResult = await parse ( html , parserOpts ) ;
123
+ const parsingResult = parse ( html , parserOpts ) ;
126
124
const document = parsingResult . node ;
127
125
128
126
walkTree ( document , treeAdapter , ( node ) => {
129
127
const location = treeAdapter . getNodeSourceCodeLocation ( node ) ;
130
128
131
- if ( location ) {
132
- const fragment = treeAdapter . createDocumentFragment ( ) ;
129
+ assert . ok ( location ) ;
133
130
134
- treeAdapter . appendChild ( fragment , node ) ;
131
+ const serializedNode = treeAdapter . isDocumentTypeNode ( node )
132
+ ? `<${ doctype . serializeContent (
133
+ treeAdapter . getDocumentTypeNodeName ( node ) ,
134
+ treeAdapter . getDocumentTypeNodePublicId ( node ) ,
135
+ treeAdapter . getDocumentTypeNodeSystemId ( node )
136
+ ) } >`
137
+ : parse5 . serializeOuter ( node , { treeAdapter } ) ;
135
138
136
- const serializedNode = parse5 . serialize ( fragment , serializerOpts ) ;
139
+ assertLocation ( location , serializedNode , html , lines ) ;
137
140
138
- assertNodeLocation ( location , serializedNode , html , lines ) ;
139
-
140
- // TODO: None of the cases below are ever matched.
141
-
142
- if ( location . startTag ) {
143
- assertStartTagLocation ( location , serializedNode , html , lines ) ;
144
- }
141
+ if ( treeAdapter . isElementNode ( node ) ) {
142
+ assertStartTagLocation ( location , serializedNode , html , lines ) ;
145
143
146
144
if ( location . endTag ) {
147
145
assertEndTagLocation ( location , serializedNode , html , lines ) ;
148
146
}
149
147
150
148
if ( location . attrs ) {
151
149
assertAttrsLocation ( location , serializedNode , html , lines ) ;
150
+ } else {
151
+ // If we don't have `location.attrs`, we expect that the node has no attributes.
152
+ assert . strictEqual ( treeAdapter . getAttrList ( node ) . length , 0 ) ;
152
153
}
153
154
}
154
155
} ) ;
0 commit comments