Giter Site home page Giter Site logo

xml-to-react's Introduction

XML-to-React

Converts an XML document into a React tree.

license Build Status Coverage Status

Proudly built by:

Prerequisites

This library may only be used in projects using React version 0.13.x or greater.

Installation

npm install --save @condenast/xml-to-react

This assumes you are using npm as your package manager.

Usage

import XMLToReact from '@condenast/xml-to-react';

const xmlToReact = new XMLToReact({/* converters */});
const reactTree = xmlToReact.convert(/* XML string */);

Simple Example

Convert XML nodes into DOM elements with any provided attributes

import ReactDOM from 'react-dom';
import XMLToReact from '@condenast/xml-to-react';
import MyListItem from './MyListItem';

const xmlToReact = new XMLToReact({
  Example: (attrs) => ({ type: 'ul', props: attrs }),
  Item: (attrs) => ({ type: MyListItem, props: attrs })
});

const reactTree = xmlToReact.convert(`
  <Example name="simple">
    <Item i="1">one</Item>
    <Item>two</Item>
    <Item>three</Item>
  </Example>
`);

ReactDOM.render('app-container', reactTree);
export default function MyListItem({ children, i }) {
  return <li data-i={i}>{children}</li>;
}

This example would render the following:

<div id="app-container">
  <ul name="simple">
    <li data-i="1">one</li>
    <li>two</li>
    <li>three</li>
  </ul>
</div>

Converters

Converters are required mapping functions that define how an XML node should be converted to React. A converter must return an object in the format { type, [props] }, which is intended to be passed to React.createElement.

  • type - required tagName, React component, or React fragment
  • props - (optional) props object

Example

function myConverter(attributes) {
  return {
    type: 'div',
    props: {
      className: 'test'
    }
  }
}

XMLToReact constructor

The XMLToReact class is instantiated with a map of converters.

{
  nodeName: converterFunction
}

convert( xml, data )

  • xml {string} - xml node or document
  • data {Object} - (optional) any data to be passed to all converters

Prior Art

  • jsonmltoreact demonstrated this technique using JsonML, and serves as motivation for this project.

Thanks

  • xmldom for providing a solid XML parser.
  • Rollup for simple and quick module bundling.
  • React for the innovation.

Contributors

See the list of contributors who participated in writing this library.

Maintainers

  • Daniel Taveras (@taveras)

xml-to-react's People

Contributors

dependabot[bot] avatar gautamarora avatar nkconnor avatar pgoldrbx avatar renovate[bot] avatar taveras avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xml-to-react's Issues

Allow coercion of XML node attribute values

We currently require our consumers to coerce and validate their attributes themselves within their converter callbacks.

It may be beneficial to coerce the type of each XML node attribute value for use within a converter callback.

e.g.,

<Party>
  <Person name="Mary" age="20"  ready="true" />
</Party>
const xmlToReact = new XMLToReact({ Person: convertPerson });

function convertPerson (attributes) {
   typeof attributes.name; // 'string'
   typeof attributes.age; // 'string'
   typeof attributes.ready; // 'string'

   return { type: 'div', props: attributes };
  }

The value of each XML node attribute is a string.
If we coerce each the type of each value, we would instead the following:

function convertPerson (attributes) {
   typeof attributes.name; // 'string'
   typeof attributes.age; // 'number'
   typeof attributes.ready; // 'boolean'

   return { type: 'div', props: attributes };
  }

for boolean attributes, we should consider HTML's approach

related: #16 (comment)

Complete Open Source Checklist

This is a master issue to work through the CondΓ© Nast Open Source Checklist.

Documentation

  • README
  • CONTRIBUTING (#24)
  • CODE_OF_CONDUCT
  • LICENSE
  • ISSUE_TEMPLATE
  • PULL_REQUEST_TEMPLATE
  • CHANGELOG (#25)
  • Announcement Blog Post

Development

  • Tests
  • Linter
  • Travis
  • Examples
  • Clear Github History

README Checklist

  • Title
  • Badges
  • Description
  • Prerequisite for using software ( if any ) (#33)
  • Install
  • Example Usage
  • Screenshots and GIFs
  • Contributors (#36)
  • Conde Nast Technology Logo (#31)
  • Attributions ( mention 3rd party libs used etc. ) (#32)
  • Benchmarks (if any)
  • Prior Art (if any) (#35)

Badges Checklist:

  • License Badge
  • Testing Badge
  • CI Badge

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Warning

These dependencies are deprecated:

Datasource Name Replacement PR?
npm rollup-plugin-babel Available
npm rollup-plugin-commonjs Unavailable
npm rollup-plugin-node-resolve Available
npm xmldom Available

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency eslint-plugin-jsdoc to v48
  • chore(deps): update dependency rollup to v4
  • πŸ” Create all rate-limited PRs at once πŸ”

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • xmldom ^0.1.27
  • @babel/cli ^7.2.3
  • @babel/core ^7.2.2
  • @babel/preset-env ^7.4.5
  • @babel/preset-react ^7.0.0
  • @babel/register ^7.0.0
  • chai ^4.1.2
  • enzyme ^3.3.0
  • enzyme-adapter-react-16 ^1.1.1
  • eslint ^5.0.0
  • eslint-config-airbnb ^17.0.0
  • eslint-plugin-import ^2.9.0
  • eslint-plugin-jsdoc ^20.0.0
  • eslint-plugin-jsx-a11y ^6.0.3
  • eslint-plugin-react ^7.7.0
  • jest ^24.8.0
  • prop-types ^15.6.1
  • react ^16.2.0
  • react-dom ^16.2.1
  • rollup ^1.0.0
  • rollup-plugin-babel ^4.3.2
  • rollup-plugin-commonjs ^10.0.0
  • rollup-plugin-node-resolve ^5.0.0
  • rollup-plugin-peer-deps-external ^2.0.0
  • react ^0.13.0 || ^0.14.0 || ^15.0.0 || ^16
nvm
.nvmrc
  • node 8.16
travis
.travis.yml
  • node 6
  • node 8

  • Check this box to trigger a request for Renovate to run again on this repository

Open-Source Requirements

What are the requirements for this project to be made public? (please edit this issue with a checklist)

  • README
  • Tests
  • ...

Dependency deprecation warning: babel (npm)

On registry https://registry.npmjs.org/, the "latest" version (v6.23.0) of dependency babel has the following deprecation notice:

In 6.x, the babel package has been deprecated in favor of babel-cli. Check https://opencollective.com/babel to support the Babel maintainers

Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.

Please take the actions necessary to rename or substitute this deprecated package and commit to your base branch. If you wish to ignore this deprecation warning and continue using babel as-is, please add it to your ignoreDeps array in Renovate config before closing this issue, otherwise another issue will be recreated the next time Renovate runs.

Affected package file(s): package.json

Would you like to disable Renovate's deprecation warning issues? Add the following to your config:

"suppressNotifications": ["deprecationWarningIssues"]

Create internal interface for XML parsing

We should extract XML parsing into our own internal helper, which would act as an interface between xmldom and xml-to-react.
This would make it easier to test our other helpers, and to better test XML parsing error conditions.

related:
#16 (comment)
#14

Convert XML nodes to custom elements by default

Currently, we require converters to be defined to do anything. But as of React 16.x (if not earlier?), we can render custom/arbitrary tag names. This means that we can turn an XML document into a React tree without any provided converters.

e.g.

import { createElement } from 'react';
import { renderToStaticMarkup } from 'react-dom/server';

renderToStaticMarkup(createElement('foo'));
// => <foo></foo>

I'd like to propose that this be our default behavior.

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Preset name not found within published preset config (monorepo:babel6). Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

improve handling of invalid XML documents

per @johnkpaul 's suggestion, we should refine the behavior when faced with an invalid XML document.

today, XMLToReact#convert will simply log a warning when supplied with an invalid XML document. it will also log the warning for all xmldom error levels.


Here are a few questions around this:

  • How should we handle the three xmldom error levels (warning, error, fatal)?
  • Should we strictly throw an error when coming across an invalid XML document?
  • Should we have a 'quirks mode' option with forgiving parsing, similar to HTML parsing?
  • If yes to the previous question, should we be in 'quirks mode' by default, and have an opt-in 'strict mode'?

Dependency deprecation warning: babel-preset-es2015 (nvm)

On registry https://registry.npmjs.org/, the "latest" version (v6.24.1) of dependency babel-preset-es2015 has the following deprecation notice:

πŸ™Œ Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!

Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.

Please take the actions necessary to rename or substitute this deprecated package and commit to your base branch. If you wish to ignore this deprecation warning and continue using babel-preset-es2015 as-is, please add it to your ignoreDeps array in Renovate config before closing this issue, otherwise another issue will be recreated the next time Renovate runs.

Affected package file(s): package.json

Run build after tests in travis to help prevent breaks

Expected Behavior

Our tests run against src but not our build pipeline, which uses rollup. This leaves uncertainty when we update dependencies in our build chain.

Current Behavior

Travis only runs npm test currently, and tests run against our src (using babel)

Possible Solution

  • Adding the build script to travis would be a bare minimum to ensure that the build doesn't break.
  • Alternatively, we could run (or re-run) tests against the our build target(s). This would be more effort and possibly less friendly for local development.

Context

Maintenance becomes easier if we know dependency updates can be reliably accepted.

Your Environment

n/a

Dependency deprecation warning: babel-preset-es2015 (npm)

On registry https://registry.npmjs.org/, the "latest" version (v6.24.1) of dependency babel-preset-es2015 has the following deprecation notice:

πŸ™Œ Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!

Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.

Please take the actions necessary to rename or substitute this deprecated package and commit to your base branch. If you wish to ignore this deprecation warning and continue using babel-preset-es2015 as-is, please add it to your ignoreDeps array in Renovate config before closing this issue, otherwise another issue will be recreated the next time Renovate runs.

Affected package file(s): package.json

Would you like to disable Renovate's deprecation warning issues? Add the following to your config:

"suppressNotifications": ["deprecationWarningIssues"]

Deprecate this repository or use atjson internally.

This is a conversation that I'd like to have, but I've realized that atjson provides all the building blocks of this project using a few parts.

The API isn't too dissimilar as well:

import HTMLSource from "@atjson/source-html";
import ReactRenderer from "@atjson/renderer-react";

ReactDOM.render('app-container', ReactRenderer.render(HTMLSource.fromRaw(html)));

This is just a thought, since we're currently actively maintaining atjson, and could leverage those tools for this ❀️

Dependency deprecation warning: babel (nvm)

On registry https://registry.npmjs.org/, the "latest" version (v6.23.0) of dependency babel has the following deprecation notice:

In 6.x, the babel package has been deprecated in favor of babel-cli. Check https://opencollective.com/babel to support the Babel maintainers

Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.

Please take the actions necessary to rename or substitute this deprecated package and commit to your base branch. If you wish to ignore this deprecation warning and continue using babel as-is, please add it to your ignoreDeps array in Renovate config before closing this issue, otherwise another issue will be recreated the next time Renovate runs.

Affected package file(s): package.json

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.