@@ -4,28 +4,11 @@ import glob from 'glob';
44
55import fromDOM from './index' ;
66
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-
247describe ( 'hast-util-from-dom' , ( ) => {
258 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 ( {
2912 type : 'root' ,
3013 children : [ {
3114 type : 'element' ,
@@ -58,14 +41,13 @@ describe('hast-util-from-dom', () => {
5841 } ,
5942 ] ,
6043 } ] ,
61- } ;
62- expect ( parsedActual ) . toEqual ( parsedExpected ) ;
44+ } ) ;
6345 } ) ;
6446
6547 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 ( {
6951 type : 'root' ,
7052 children : [
7153 {
@@ -81,30 +63,53 @@ describe('hast-util-from-dom', () => {
8163 children : [ { type : 'text' , value : 'World!' } ] ,
8264 } ,
8365 ] ,
84- } ;
85- expect ( parsedActual ) . toEqual ( parsedExpected ) ;
66+ } ) ;
8667 } ) ;
8768} ) ;
8869
8970describe ( '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+
9274 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' ) ;
9678
9779 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 ) ) ;
10082 let parsedExpected ;
83+
10184 try {
102- parsedExpected = JSON . parse ( fs . readFileSync ( fixtureOutput ) ) ;
85+ parsedExpected = JSON . parse ( fs . readFileSync ( output ) ) ;
10386 } catch ( e ) {
104- fs . writeFileSync ( fixtureOutput , JSON . stringify ( parsedActual , null , 2 ) ) ;
87+ fs . writeFileSync ( output , JSON . stringify ( actual , null , 2 ) ) ;
10588 return ;
10689 }
107- expect ( parsedActual ) . toEqual ( parsedExpected ) ;
90+
91+ expect ( actual ) . toEqual ( parsedExpected ) ;
10892 } ) ;
10993 } ) ;
11094} ) ;
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