Skip to content

Commit 8e3b90f

Browse files
committed
Refactor code-style
1 parent f57d356 commit 8e3b90f

File tree

2 files changed

+48
-58
lines changed

2 files changed

+48
-58
lines changed

src/index.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import h from 'hastscript';
22

33
const ELEMENT_NODE = 1;
4-
// const ATTRIBUTE_NODE = 2;
54
const TEXT_NODE = 3;
6-
// const CDATA_SECTION_NODE = 4;
7-
// const ENTITY_REFERENCE_NODE = 5;
8-
// const ENTITY_NODE = 6;
9-
// const PROCESSING_INSTRUCTION_NODE = 7;
105
const COMMENT_NODE = 8;
116
const DOCUMENT_NODE = 9;
127
const DOCUMENT_TYPE_NODE = 10;
138
const DOCUMENT_FRAGMENT_NODE = 11;
14-
// const NOTATION_NODE = 12;
159

1610
function transform(el) {
1711
const children = [];
1812
const length = el.childNodes ? el.childNodes.length : 0;
13+
1914
for (let i = 0; i < length; i += 1) {
2015
children.push(transform(el.childNodes[i]));
2116
}
@@ -52,16 +47,12 @@ function transform(el) {
5247
}
5348
}
5449

55-
/**
56-
* Transform a document
57-
*/
50+
// Transform a document.
5851
function root(el, children) {
5952
return { type: 'root', children };
6053
}
6154

62-
/**
63-
* Transform a DOCTYPE
64-
*/
55+
// Transform a doctype.
6556
function doctype(el) {
6657
return {
6758
type: 'doctype',
@@ -71,23 +62,17 @@ function doctype(el) {
7162
};
7263
}
7364

74-
/**
75-
* Transform text node
76-
*/
65+
// Transform text.
7766
function text(el) {
7867
return { type: 'text', value: el.nodeValue };
7968
}
8069

81-
/**
82-
* Transform a comment node
83-
*/
70+
// Transform a comment.
8471
function comment(el) {
8572
return { type: 'comment', value: el.data };
8673
}
8774

88-
/**
89-
* Transform an element
90-
*/
75+
// Transform an element.
9176
function element(el, children) {
9277
const tagName = el.tagName.toLowerCase();
9378
const props = {};

src/index.test.js

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,11 @@ import glob from 'glob';
44

55
import 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-
247
describe('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

8970
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+
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

Comments
 (0)