Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 18, 2020
0 parents commit 4d1a20c
Show file tree
Hide file tree
Showing 10 changed files with 905 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- lts/dubnium
- node
after_script: bash <(curl -s https://codecov.io/bash)
177 changes: 177 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
'use strict'

var xtend = require('xtend')
var zwitch = require('zwitch')
var namespaces = require('web-namespaces')
var html = require('property-information/html')
var svg = require('property-information/svg')
var find = require('property-information/find')
var spaces = require('space-separated-tokens').stringify
var commas = require('comma-separated-tokens').stringify
var position = require('unist-util-position')

module.exports = toXast

var one = zwitch('type')

one.invalid = invalid
one.unknown = unknown
one.handlers.root = root
one.handlers.element = element
one.handlers.text = text
one.handlers.comment = comment
one.handlers.doctype = doctype

function invalid(value) {
throw new Error('Expected node, not `' + value + '`')
}

function unknown(value) {
throw new Error('Cannot transform node of type `' + value.type + '`')
}

function toXast(tree, options) {
var opts = typeof options === 'string' ? {space: options} : options || {}
var space = opts.space === 'svg' ? 'svg' : 'html'

return one(tree, {schema: space === 'svg' ? svg : html, ns: null})
}

function root(node, config) {
return patch(node, {type: 'root'}, config)
}

function text(node, config) {
return patch(node, {type: 'text', value: node.value || ''}, config)
}

function comment(node, config) {
return patch(node, {type: 'comment', value: node.value || ''}, config)
}

function doctype(node, config) {
return patch(
node,
{
type: 'doctype',
name: node.name || '',
public: node.public || undefined,
system: node.system || undefined
},
config
)
}

function element(node, parentConfig) {
var schema = parentConfig.schema
var name = node.tagName
var props = node.properties || {}
var xmlns = props.xmlns || null
var ns = namespaces[schema.space]
var attrs = {}
var config

if (xmlns) {
if (xmlns === namespaces.svg) {
schema = svg
ns = xmlns
} else if (xmlns === namespaces.html) {
schema = html
ns = xmlns
} else {
// We don’t support non-HTML, non-SVG namespaces, so stay in the same.
}
} else if (ns === namespaces.html && name === 'svg') {
schema = svg
ns = namespaces.svg
}

if (parentConfig.ns !== ns) {
attrs.xmlns = ns
}

config = xtend(parentConfig, {schema: schema, ns: ns})
attrs = xtend(attrs, toAttributes(props, config))

return patch(node, {type: 'element', name: name, attributes: attrs}, config)
}

function patch(origin, node, config) {
var pos = origin.position
var hastChildren = origin.children
var length
var children
var index

if (
config.ns === namespaces.html &&
origin.type === 'element' &&
origin.tagName === 'template'
) {
node.children = root(origin.content, config).children
} else if (origin.type === 'element' || origin.type === 'root') {
length = hastChildren && hastChildren.length
children = []
index = -1

while (++index < length) {
children[index] = one(hastChildren[index], config)
}

node.children = children
}

if (pos) {
node.position = {
start: position.start(origin),
end: position.end(origin)
}
}

return node
}

function toAttributes(props, config) {
var attributes = {}
var value
var key
var info
var name

for (key in props) {
info = find(config.schema, key)
name = info.attribute
value = props[key]

// Ignore nully, false, and `NaN` values, and falsey known booleans.
if (
value === null ||
value === undefined ||
value === false ||
value !== value ||
(info.boolean && !value)
) {
continue
}

// Accept `array`.
// Most props are space-separated.
if (typeof value === 'object' && 'length' in value) {
value = (info.commaSeparated ? commas : spaces)(value)
}

// Treat `true` and truthy known booleans.
if (value === true || info.boolean) {
value = ''
}

// Cast everything else to string.
if (typeof value !== 'string') {
value = String(value)
}

attributes[name] = value
}

return attributes
}
22 changes: 22 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2020 Titus Wormer <tituswormer@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
81 changes: 81 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"name": "hast-util-to-xast",
"version": "0.0.0",
"description": "hast utility to transform to xast",
"license": "MIT",
"keywords": [
"xast",
"hast",
"unist",
"dsl",
"xml",
"html"
],
"repository": "syntax-tree/hast-util-to-xast",
"bugs": "https://github.com/syntax-tree/hast-util-to-xast/issues",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"files": [
"index.js"
],
"dependencies": {
"comma-separated-tokens": "^1.0.0",
"property-information": "^5.0.0",
"space-separated-tokens": "^1.0.0",
"unist-util-position": "^3.0.0",
"web-namespaces": "^1.0.0",
"xtend": "^4.0.0",
"zwitch": "^1.0.0"
},
"devDependencies": {
"hastscript": "^5.0.0",
"nyc": "^15.0.0",
"prettier": "^1.0.0",
"remark-cli": "^7.0.0",
"remark-preset-wooorm": "^6.0.0",
"tape": "^4.0.0",
"unist-builder": "^2.0.0",
"xastscript": "^1.0.0",
"xo": "^0.25.0"
},
"scripts": {
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"unicorn/prefer-includes": "off",
"no-self-compare": "off",
"guard-for-in": "off"
}
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
}
}
Loading

0 comments on commit 4d1a20c

Please sign in to comment.