Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 2, 2021
1 parent 0e3981a commit 0cb2fdc
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 113 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
hast-util-to-parse5.js
hast-util-to-parse5.min.js
yarn.lock
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
coverage/
hast-util-to-parse5.js
hast-util-to-parse5.min.js
*.json
*.md
43 changes: 17 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
'use strict'

var xtend = require('xtend')
var html = require('property-information/html')
var svg = require('property-information/svg')
var find = require('property-information/find')
var toH = require('hast-to-hyperscript')
var ns = require('web-namespaces')
var zwitch = require('zwitch')

module.exports = toParse5

var one = zwitch('type', {
handlers: {
root: root,
element: element,
text: text,
comment: comment,
doctype: doctype
}
})
import {html, svg, find} from 'property-information'
import {toH} from 'hast-to-hyperscript'
import {webNamespaces} from 'web-namespaces'
import {zwitch} from 'zwitch'

var own = {}.hasOwnProperty

var one = zwitch('type', {handlers: {root, element, text, comment, doctype}})

// Transform a tree from hast to Parse5’s AST.
function toParse5(tree, space) {
export function toParse5(tree, space) {
return one(tree, space === 'svg' ? svg : html)
}

Expand Down Expand Up @@ -62,7 +49,7 @@ function comment(node, schema) {
}

function element(node, schema) {
return toH(h, xtend(node, {children: []}), {space: schema.space})
return toH(h, Object.assign({}, node, {children: []}), {space: schema.space})

function h(name, attrs) {
var values = []
Expand All @@ -73,9 +60,13 @@ function element(node, schema) {
var p5

for (key in attrs) {
if (!own.call(attrs, key) || attrs[key] === false) {
continue
}

info = find(schema, key)

if (attrs[key] === false || (info.boolean && !attrs[key])) {
if (info.boolean && !attrs[key]) {
continue
}

Expand All @@ -91,7 +82,7 @@ function element(node, schema) {
value.prefix = key.slice(0, index)
}

value.namespace = ns[info.space]
value.namespace = webNamespaces[info.space]
}

values.push(value)
Expand All @@ -115,7 +106,7 @@ function patch(node, p5, parentSchema) {

if (node.type === 'element') {
if (schema.space === 'html' && node.tagName === 'svg') schema = svg
p5.namespaceURI = ns[schema.space]
p5.namespaceURI = webNamespaces[schema.space]
}

if (node.type === 'element' || node.type === 'root') {
Expand Down
43 changes: 15 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,33 @@
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"index.js"
],
"dependencies": {
"hast-to-hyperscript": "^9.0.0",
"property-information": "^5.0.0",
"web-namespaces": "^1.0.0",
"xtend": "^4.0.0",
"zwitch": "^1.0.0"
"hast-to-hyperscript": "^10.0.0",
"property-information": "^6.0.0",
"web-namespaces": "^2.0.0",
"zwitch": "^2.0.0"
},
"devDependencies": {
"browserify": "^17.0.0",
"c8": "^7.0.0",
"json-stringify-safe": "^5.0.0",
"nyc": "^15.0.0",
"parse5": "^6.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"xo": "^0.38.0"
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify . -s hastUtilToParse5 -o hast-util-to-parse5.js",
"build-mangle": "browserify . -s hastUtilToParse5 -o hast-util-to-parse5.min.js -p tinyify",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test",
"test": "npm run format && npm run build && npm run test-coverage"
"test-api": "node test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
"test": "npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -65,20 +62,10 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"unicorn/prefer-includes": "off",
"guard-for-in": "off"
},
"ignores": [
"hast-util-to-parse5.js"
]
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"remarkConfig": {
"plugins": [
Expand Down
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
## Install

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.

[npm][]:

```sh
Expand All @@ -24,7 +27,7 @@ npm install hast-util-to-parse5
## Use

```js
var toParse5 = require('hast-util-to-parse5')
import {toParse5} from 'hast-util-to-parse5'

var ast = toParse5({
type: 'element',
Expand All @@ -48,6 +51,9 @@ Yields:

## API

This package exports the following identifiers: `toParse5`.
There is no default export.

### `toParse5(tree[, space])`

Transform a [**hast**][hast] [*tree*][tree] to [Parse5’s AST][ast].
Expand Down
8 changes: 3 additions & 5 deletions test/comment.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

var test = require('tape')
var parse5 = require('parse5')
var toParse5 = require('..')
import test from 'tape'
import parse5 from 'parse5'
import {toParse5} from '../index.js'

test('comment', function (t) {
var actual = toParse5({type: 'comment', value: 'Alpha'})
Expand Down
8 changes: 3 additions & 5 deletions test/doctype.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

var test = require('tape')
var parse5 = require('parse5')
var toParse5 = require('..')
import test from 'tape'
import parse5 from 'parse5'
import {toParse5} from '../index.js'

test('doctype', function (t) {
t.test('should transform a doctype (legacy)', function (st) {
Expand Down
10 changes: 4 additions & 6 deletions test/element.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'

var test = require('tape')
var parse5 = require('parse5')
var json = require('./json')
var toParse5 = require('..')
import test from 'tape'
import parse5 from 'parse5'
import {json} from './json.js'
import {toParse5} from '../index.js'

test('element', function (t) {
t.test('should transform elements', function (st) {
Expand Down
14 changes: 7 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

/* eslint-disable import/no-unassigned-import */

require('./root')
require('./doctype')
require('./text')
require('./comment')
require('./element')
require('./position')
require('./svg')
import './root.js'
import './doctype.js'
import './text.js'
import './comment.js'
import './element.js'
import './position.js'
import './svg.js'

/* eslint-enable import/no-unassigned-import */
8 changes: 2 additions & 6 deletions test/json.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict'
import stringify from 'json-stringify-safe'

var stringify = require('json-stringify-safe')

module.exports = json

function json(value) {
export function json(value) {
return JSON.parse(stringify(value))
}
10 changes: 4 additions & 6 deletions test/position.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'

var test = require('tape')
var parse5 = require('parse5')
var json = require('./json')
var toParse5 = require('..')
import test from 'tape'
import parse5 from 'parse5'
import {json} from './json.js'
import {toParse5} from '../index.js'

test('position', function (t) {
var actual = toParse5({
Expand Down
10 changes: 4 additions & 6 deletions test/root.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'

var test = require('tape')
var parse5 = require('parse5')
var json = require('./json')
var toParse5 = require('..')
import test from 'tape'
import parse5 from 'parse5'
import {json} from './json.js'
import {toParse5} from '../index.js'

test('root', function (t) {
t.test('should transform a root (quirks)', function (st) {
Expand Down
10 changes: 4 additions & 6 deletions test/svg.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'

var test = require('tape')
var parse5 = require('parse5')
var json = require('./json')
var toParse5 = require('..')
import test from 'tape'
import parse5 from 'parse5'
import {json} from './json.js'
import {toParse5} from '../index.js'

test('svg', function (t) {
t.test('should transform SVG in HTML', function (st) {
Expand Down
8 changes: 3 additions & 5 deletions test/text.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

var test = require('tape')
var parse5 = require('parse5')
var toParse5 = require('..')
import test from 'tape'
import parse5 from 'parse5'
import {toParse5} from '../index.js'

test('text', function (t) {
var expected = parse5.parseFragment('Alpha').childNodes[0]
Expand Down

0 comments on commit 0cb2fdc

Please sign in to comment.