Skip to content

HTML parser added Dom parser #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"extends": ["@commitlint/config-conventional"],
"rules": {
"subject-case": [
2,
"always",
["sentence-case", "start-case", "pascal-case", "upper-case", "lower-case"]
],
"subject-empty": [2, "never"],
"subject-full-stop": [2, "never", "."],
"type-enum": [
2,
"always",
[
"build",
"chore",
"ci",
"docs",
"feat",
"fix",
"perf",
"refactor",
"revert",
"style",
"test",
"sample"
]
],
"type-empty": [2, "never"]
}
}
30 changes: 19 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
# This is a basic workflow to help you get started with Actions

name: Unit-Test-CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
build-test:
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout@v2
- name: Install modules
run: npm install
- name: Run tests
run: npm run test
- uses: actions/checkout@v3
- uses: ArtiomTr/jest-coverage-report-action@v2
id: coverage-utils-js
with:
output: comment, report-markdown
- uses: marocchino/sticky-pull-request-comment@v2
with:
header: Contentstack Utils JS Coverage
recreate: true
message: ${{ steps.coverage-utils-js.outputs.report }}
- name: Test Report
uses: dorny/test-reporter@v1
if: success() || failure()
with:
name: JEST Tests
path: reports/junit/jest-*.xml
reporter: jest-junit
fail-on-error: true
12 changes: 6 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
*.DS_Store
examples/*
node_modules/*
node_modules/
.idea/*
reports/*
apidocs-templates/*
testcase-report/*
reports/
apidocs-templates/
testcase-report/
test/smtpconfig.js/*
test/config.js/*
test/sync_config.js/*
test/report.json/*
tap-html.html
*html-report
dist/*
dist/
coverage/
.dccache
.dccache
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run test
26 changes: 0 additions & 26 deletions .jsdoc.json

This file was deleted.

5 changes: 4 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ src
tsconfig.json
tslint.json
.prettierrc
.talismanrc
jestconfig.json
CODEOWNERS
jest.config.ts
CODEOWNERS
reports/
35 changes: 14 additions & 21 deletions __test__/html-to-json.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { elementToJson } from '../src/helper/html-to-json';
import { assetDisplayJson, assetDisplayLink, assetDisplayLinkJson } from './mock/embedded-object-mock';
import { parse } from 'node-html-parser';
import { entryBlock,
entryInline,
entryLink,
Expand All @@ -12,27 +11,31 @@ import { entryBlock,
entryInlineJson,
entryBlockJson
} from './mock/embedded-object-mock';
const noChildNodeHTML = parse(noChildNode)
const dom = new DOMParser();
const getBody = (content: string) => {
return dom.parseFromString(content, 'text/html').body
}

describe('HTML To JSON test', () => {
it('HTML To JSON figure tag with no child test', done => {
expect(elementToJson(noChildNodeHTML)).toEqual({ figure: noChildNodeJson})
expect(elementToJson(getBody(noChildNode))).toEqual({ figure: noChildNodeJson})
done()
})

it('HTML to JSON entry figure tag test', done => {
expect(elementToJson(parse(entryLink))).toEqual({ figure: entryLinkJson })
expect(elementToJson(parse(entryInline))).toEqual({ figure: entryInlineJson })
expect(elementToJson(parse(entryBlock))).toEqual({ figure: entryBlockJson })
expect(elementToJson(getBody(entryLink))).toEqual({ figure: entryLinkJson })
expect(elementToJson(getBody(entryInline))).toEqual({ figure: entryInlineJson })
expect(elementToJson(getBody(entryBlock))).toEqual({ figure: entryBlockJson })
done()
})

it('HTML to JSON asset figure tag test', done => {
expect(elementToJson(parse(assetDisplay))).toEqual({ figure: assetDisplayJson })
expect(elementToJson(parse(assetDisplayLink))).toEqual({ figure: assetDisplayLinkJson })
expect(elementToJson(getBody(assetDisplay))).toEqual({ figure: assetDisplayJson })
expect(elementToJson(getBody(assetDisplayLink))).toEqual({ figure: assetDisplayLinkJson })
done()
})
it('HTML to JSON false tag test', done => {
expect(elementToJson(parse(unexpectedCloseTag))).toEqual({ "figur2":{
expect(elementToJson(getBody(unexpectedCloseTag))).toEqual({ "figur2":{
"#text": " \n",
"class": "embedded-asset",
"data-sys-content-type-uid": "data-sys-content-type-uid",
Expand All @@ -41,18 +44,8 @@ describe('HTML To JSON test', () => {
"sys-style-type": "inline",
"type": "asset"
} })
expect(elementToJson(parse('String'))).toEqual({ "#text": "String" })
expect(elementToJson(parse(''))).toEqual({})
expect(elementToJson(parse(`<data><![CDATA[This text contains a CEND ]]]]><![CDATA[>]]></data>`))).toEqual( {
"data": {
"#text": "<![CDATA[This text contains a CEND ]]]]><![CDATA[>]]>",
},
})
done()
})

it('HTML comment to JSON false tag test', done => {
expect(elementToJson(parse('<html><!-- Comment in html --><html>', { comment: true}))).toEqual({ html: {} })
expect(elementToJson(getBody('String'))).toEqual({ "#text": "String" })
expect(elementToJson(getBody(''))).toEqual({})
done()
})
})
4 changes: 2 additions & 2 deletions __test__/mock/embedded-object-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const assetDisplayLinkJson ={
"sys-style-type": "display",
"type": "asset",
}
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>`
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>`
export const entryBlockJson = {
class: "embedded-entry block-entry",
type: 'entry',
Expand All @@ -43,7 +43,7 @@ export const entryBlockJson = {
"data-sys-entry-locale": "en-us"
}

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}}
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}}
</figure>`
export const entryInlineJson = {
class: "embedded-entry inline-entry",
Expand Down
2 changes: 1 addition & 1 deletion badges/badge-branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 17 additions & 3 deletions jestconfig.json → jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
export default {
"preset": "ts-jest",
"testEnvironment": "jsdom",
"transform": {
"^.+\\.(t|j)sx?$": "ts-jest",
"^.+\\.(ts|tsx|js|jsx)$": "babel-jest"
Expand All @@ -13,6 +14,7 @@
"node_modules/(?!@ngrx|(?!deck.gl)|ng-dynamic)"
],
"collectCoverage": true,
coverageDirectory: './reports/coverage',
"coverageReporters": [
"json",
"html",
Expand All @@ -36,10 +38,22 @@
"default",
["jest-html-reporters",
{
"publicPath": "./testcase-report",
"publicPath": "./reports/html",
"filename": "index.html",
"expand": true
}
],
["jest-junit",
{
"outputDirectory": "reports/junit",
"outputName": "jest-junit.xml",
"ancestorSeparator": " › ",
"uniqueOutputName": "false",
"suiteNameTemplate": "{filepath}",
"classNameTemplate": "{classname}",
"titleTemplate": "{title}"
}
]
]
}
};

Loading