Skip to content

Commit 2c48fde

Browse files
authored
Merge pull request #22 from contentstack/bugs/dom-parser
HTML parser added Dom parser
2 parents 6597529 + 049b490 commit 2c48fde

File tree

15 files changed

+4275
-2337
lines changed

15 files changed

+4275
-2337
lines changed

.commitlintrc.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"extends": ["@commitlint/config-conventional"],
3+
"rules": {
4+
"subject-case": [
5+
2,
6+
"always",
7+
["sentence-case", "start-case", "pascal-case", "upper-case", "lower-case"]
8+
],
9+
"subject-empty": [2, "never"],
10+
"subject-full-stop": [2, "never", "."],
11+
"type-enum": [
12+
2,
13+
"always",
14+
[
15+
"build",
16+
"chore",
17+
"ci",
18+
"docs",
19+
"feat",
20+
"fix",
21+
"perf",
22+
"refactor",
23+
"revert",
24+
"style",
25+
"test",
26+
"sample"
27+
]
28+
],
29+
"type-empty": [2, "never"]
30+
}
31+
}

.github/workflows/ci.yml

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
# This is a basic workflow to help you get started with Actions
2-
31
name: Unit-Test-CI
42

5-
# Controls when the action will run. Triggers the workflow on push or pull request
6-
# events but only for the master branch
73
on:
84
push:
95
branches: [ master ]
106
pull_request:
117
branches: [ master ]
128

139
jobs:
14-
build:
10+
build-test:
1511
runs-on: ubuntu-latest
16-
# Steps represent a sequence of tasks that will be executed as part of the job
1712
steps:
18-
- uses: actions/checkout@v2
19-
- name: Install modules
20-
run: npm install
21-
- name: Run tests
22-
run: npm run test
13+
- uses: actions/checkout@v3
14+
- uses: ArtiomTr/jest-coverage-report-action@v2
15+
id: coverage-utils-js
16+
with:
17+
output: comment, report-markdown
18+
- uses: marocchino/sticky-pull-request-comment@v2
19+
with:
20+
header: Contentstack Utils JS Coverage
21+
recreate: true
22+
message: ${{ steps.coverage-utils-js.outputs.report }}
23+
- name: Test Report
24+
uses: dorny/test-reporter@v1
25+
if: success() || failure()
26+
with:
27+
name: JEST Tests
28+
path: reports/junit/jest-*.xml
29+
reporter: jest-junit
30+
fail-on-error: true

.gitignore

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
*.DS_Store
22
examples/*
3-
node_modules/*
3+
node_modules/
44
.idea/*
5-
reports/*
6-
apidocs-templates/*
7-
testcase-report/*
5+
reports/
6+
apidocs-templates/
7+
testcase-report/
88
test/smtpconfig.js/*
99
test/config.js/*
1010
test/sync_config.js/*
1111
test/report.json/*
1212
tap-html.html
1313
*html-report
14-
dist/*
14+
dist/
1515
coverage/
16-
.dccache
16+
.dccache

.husky/commit-msg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
npx --no-install commitlint --edit "$1"

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
npm run test

.jsdoc.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

.npmignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ src
22
tsconfig.json
33
tslint.json
44
.prettierrc
5+
.talismanrc
56
jestconfig.json
6-
CODEOWNERS
7+
jest.config.ts
8+
CODEOWNERS
9+
reports/

__test__/html-to-json.test.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { elementToJson } from '../src/helper/html-to-json';
22
import { assetDisplayJson, assetDisplayLink, assetDisplayLinkJson } from './mock/embedded-object-mock';
3-
import { parse } from 'node-html-parser';
43
import { entryBlock,
54
entryInline,
65
entryLink,
@@ -12,27 +11,31 @@ import { entryBlock,
1211
entryInlineJson,
1312
entryBlockJson
1413
} from './mock/embedded-object-mock';
15-
const noChildNodeHTML = parse(noChildNode)
14+
const dom = new DOMParser();
15+
const getBody = (content: string) => {
16+
return dom.parseFromString(content, 'text/html').body
17+
}
18+
1619
describe('HTML To JSON test', () => {
1720
it('HTML To JSON figure tag with no child test', done => {
18-
expect(elementToJson(noChildNodeHTML)).toEqual({ figure: noChildNodeJson})
21+
expect(elementToJson(getBody(noChildNode))).toEqual({ figure: noChildNodeJson})
1922
done()
2023
})
2124

2225
it('HTML to JSON entry figure tag test', done => {
23-
expect(elementToJson(parse(entryLink))).toEqual({ figure: entryLinkJson })
24-
expect(elementToJson(parse(entryInline))).toEqual({ figure: entryInlineJson })
25-
expect(elementToJson(parse(entryBlock))).toEqual({ figure: entryBlockJson })
26+
expect(elementToJson(getBody(entryLink))).toEqual({ figure: entryLinkJson })
27+
expect(elementToJson(getBody(entryInline))).toEqual({ figure: entryInlineJson })
28+
expect(elementToJson(getBody(entryBlock))).toEqual({ figure: entryBlockJson })
2629
done()
2730
})
2831

2932
it('HTML to JSON asset figure tag test', done => {
30-
expect(elementToJson(parse(assetDisplay))).toEqual({ figure: assetDisplayJson })
31-
expect(elementToJson(parse(assetDisplayLink))).toEqual({ figure: assetDisplayLinkJson })
33+
expect(elementToJson(getBody(assetDisplay))).toEqual({ figure: assetDisplayJson })
34+
expect(elementToJson(getBody(assetDisplayLink))).toEqual({ figure: assetDisplayLinkJson })
3235
done()
3336
})
3437
it('HTML to JSON false tag test', done => {
35-
expect(elementToJson(parse(unexpectedCloseTag))).toEqual({ "figur2":{
38+
expect(elementToJson(getBody(unexpectedCloseTag))).toEqual({ "figur2":{
3639
"#text": " \n",
3740
"class": "embedded-asset",
3841
"data-sys-content-type-uid": "data-sys-content-type-uid",
@@ -41,18 +44,8 @@ describe('HTML To JSON test', () => {
4144
"sys-style-type": "inline",
4245
"type": "asset"
4346
} })
44-
expect(elementToJson(parse('String'))).toEqual({ "#text": "String" })
45-
expect(elementToJson(parse(''))).toEqual({})
46-
expect(elementToJson(parse(`<data><![CDATA[This text contains a CEND ]]]]><![CDATA[>]]></data>`))).toEqual( {
47-
"data": {
48-
"#text": "<![CDATA[This text contains a CEND ]]]]><![CDATA[>]]>",
49-
},
50-
})
51-
done()
52-
})
53-
54-
it('HTML comment to JSON false tag test', done => {
55-
expect(elementToJson(parse('<html><!-- Comment in html --><html>', { comment: true}))).toEqual({ html: {} })
47+
expect(elementToJson(getBody('String'))).toEqual({ "#text": "String" })
48+
expect(elementToJson(getBody(''))).toEqual({})
5649
done()
5750
})
5851
})

__test__/mock/embedded-object-mock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const assetDisplayLinkJson ={
3131
"sys-style-type": "display",
3232
"type": "asset",
3333
}
34-
export const entryBlock = `<figure class="embedded-entry block-entry" data-redactor-type="embed" type="entry" data-sys-entry-uid="asset_uid_5" data-sys-entry-locale="en-us" data-sys-content-type-uid="article" sys-style-type="block">{{title}}</figure>`
34+
export const entryBlock = `<figure class="embedded-entry block-entry" data-redactor-type="embed" type="entry" data-sys-entry-uid="asset_uid_5" data-sys-entry-locale="en-us" data-sys-content-type-uid="article" sys-style-type="block">{{title}}</figure>`
3535
export const entryBlockJson = {
3636
class: "embedded-entry block-entry",
3737
type: 'entry',
@@ -43,7 +43,7 @@ export const entryBlockJson = {
4343
"data-sys-entry-locale": "en-us"
4444
}
4545

46-
export const entryInline = `<figure class="embedded-entry inline-entry" data-redactor-type="embed" type="entry" data-sys-entry-uid="asset_uid_5" data-sys-entry-locale="en-us" data-sys-content-type-uid="article" style="display:inline;" sys-style-type="inline">{{title}}
46+
export const entryInline = `<figure class="embedded-entry inline-entry" data-redactor-type="embed" type="entry" data-sys-entry-uid="asset_uid_5" data-sys-entry-locale="en-us" data-sys-content-type-uid="article" style="display:inline;" sys-style-type="inline">{{title}}
4747
</figure>`
4848
export const entryInlineJson = {
4949
class: "embedded-entry inline-entry",

badges/badge-branches.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)