@@ -4,28 +4,11 @@ import glob from 'glob';
4
4
5
5
import fromDOM from './index' ;
6
6
7
- function createFragmentFromHtml ( htmlString ) {
8
- const fragment = document . createDocumentFragment ( ) ;
9
- const tempEl = document . createElement ( 'body' ) ;
10
- tempEl . innerHTML = htmlString ;
11
- let child = tempEl . firstChild ;
12
- while ( child ) {
13
- fragment . appendChild ( child ) ;
14
- child = tempEl . firstChild ;
15
- }
16
- return fragment ;
17
- }
18
-
19
- function createDocumentFromHtml ( htmlString ) {
20
- const parser = new DOMParser ( ) ;
21
- return parser . parseFromString ( htmlString , 'text/html' ) ;
22
- }
23
-
24
7
describe ( 'hast-util-from-dom' , ( ) => {
25
8
it ( 'should transform a complete document' , ( ) => {
26
- const fixtureHtml = '<title>Hello!</title><h1>World!' ;
27
- const parsedActual = fromDOM ( createDocumentFromHtml ( fixtureHtml ) ) ;
28
- const parsedExpected = {
9
+ const actual = fromDOM ( doc ( '<title>Hello!</title><h1>World!' ) ) ;
10
+
11
+ expect ( actual ) . toEqual ( {
29
12
type : 'root' ,
30
13
children : [ {
31
14
type : 'element' ,
@@ -58,14 +41,13 @@ describe('hast-util-from-dom', () => {
58
41
} ,
59
42
] ,
60
43
} ] ,
61
- } ;
62
- expect ( parsedActual ) . toEqual ( parsedExpected ) ;
44
+ } ) ;
63
45
} ) ;
64
46
65
47
it ( 'should transform a fragment' , ( ) => {
66
- const fixtureHtml = '<title>Hello!</title><h1>World!' ;
67
- const parsedActual = fromDOM ( createFragmentFromHtml ( fixtureHtml ) ) ;
68
- const parsedExpected = {
48
+ const actual = fromDOM ( fragment ( '<title>Hello!</title><h1>World!' ) ) ;
49
+
50
+ expect ( actual ) . toEqual ( {
69
51
type : 'root' ,
70
52
children : [
71
53
{
@@ -81,30 +63,53 @@ describe('hast-util-from-dom', () => {
81
63
children : [ { type : 'text' , value : 'World!' } ] ,
82
64
} ,
83
65
] ,
84
- } ;
85
- expect ( parsedActual ) . toEqual ( parsedExpected ) ;
66
+ } ) ;
86
67
} ) ;
87
68
} ) ;
88
69
89
70
describe ( 'fixtures' , ( ) => {
90
- const FIXTURES_PATH = path . join ( __dirname , '__fixtures__' ) ;
91
- const fixturePaths = glob . sync ( path . join ( FIXTURES_PATH , '**/*/' ) ) ;
71
+ const root = path . join ( __dirname , '__fixtures__' ) ;
72
+ const fixturePaths = glob . sync ( path . join ( root , '**/*/' ) ) ;
73
+
92
74
fixturePaths . forEach ( ( fixturePath ) => {
93
- const fixture = path . relative ( FIXTURES_PATH , fixturePath ) ;
94
- const fixtureInput = path . join ( fixturePath , 'index.html' ) ;
95
- const fixtureOutput = path . join ( fixturePath , 'index.json' ) ;
75
+ const fixture = path . relative ( root , fixturePath ) ;
76
+ const input = path . join ( fixturePath , 'index.html' ) ;
77
+ const output = path . join ( fixturePath , 'index.json' ) ;
96
78
97
79
test ( fixture , ( ) => {
98
- const fixtureHtml = fs . readFileSync ( fixtureInput ) ;
99
- const parsedActual = fromDOM ( createDocumentFromHtml ( fixtureHtml ) ) ;
80
+ const fixtureHtml = fs . readFileSync ( input ) ;
81
+ const actual = fromDOM ( doc ( fixtureHtml ) ) ;
100
82
let parsedExpected ;
83
+
101
84
try {
102
- parsedExpected = JSON . parse ( fs . readFileSync ( fixtureOutput ) ) ;
85
+ parsedExpected = JSON . parse ( fs . readFileSync ( output ) ) ;
103
86
} catch ( e ) {
104
- fs . writeFileSync ( fixtureOutput , JSON . stringify ( parsedActual , null , 2 ) ) ;
87
+ fs . writeFileSync ( output , JSON . stringify ( actual , null , 2 ) ) ;
105
88
return ;
106
89
}
107
- expect ( parsedActual ) . toEqual ( parsedExpected ) ;
90
+
91
+ expect ( actual ) . toEqual ( parsedExpected ) ;
108
92
} ) ;
109
93
} ) ;
110
94
} ) ;
95
+
96
+ function fragment ( htmlString ) {
97
+ const node = document . createDocumentFragment ( ) ;
98
+ const tempEl = document . createElement ( 'body' ) ;
99
+
100
+ tempEl . innerHTML = htmlString ;
101
+
102
+ let child = tempEl . firstChild ;
103
+
104
+ while ( child ) {
105
+ node . appendChild ( child ) ;
106
+ child = tempEl . firstChild ;
107
+ }
108
+
109
+ return node ;
110
+ }
111
+
112
+ function doc ( htmlString ) {
113
+ const parser = new DOMParser ( ) ;
114
+ return parser . parseFromString ( htmlString , 'text/html' ) ;
115
+ }
0 commit comments