Git Product home page Git Product logo

jsx-parser's Introduction

jsx-parser

a lightweight jsx parser

npm

npm install jsx-parser

how to require

import JSXParser form 'index'

or

<script src="index.umd"></script>

how to use

var str = '<div {...[<div/>]} />'
console.log(JSON.stringify(JSXParser(str), null, " "))

output

{
 "type": "div",
 "props": {
  "spreadAttribute": {
   "type": "#jsx",
   "nodeValue": [
    {
     "type": "#jsx",
     "nodeValue": "["
    },
    {
     "type": "div",
     "props": {},
     "children": [],
     "isVoidTag": true
    },
    {
     "type": "#jsx",
     "nodeValue": "]"
    }
   ]
  }
 },
 "children": [],
 "isVoidTag": true
}
var str = `<div id={ 2222 } kkk={<div class={aaa}/>} className="{111}" >xxx{[1,2,3].map(el=> <div>{el}</div>)}yyy
<span>{xxx}--{yyy}</span></div>`
console.log(JSON.stringify(JSXParser(str), null, " "))
{
 "type": "div",
 "props": {
  "id": {
   "type": "#jsx",
   "nodeValue": " 2222 "
  },
  "kkk": {
   "type": "#jsx",
   "nodeValue": [
    {
     "type": "div",
     "props": {
      "class": {
       "type": "#jsx",
       "nodeValue": "aaa"
      }
     },
     "children": [],
     "isVoidTag": true
    }
   ]
  },
  "className": "{111}"
 },
 "children": [
  {
   "type": "#text",
   "nodeValue": "xxx"
  },
  {
   "type": "#jsx",
   "nodeValue": [
    {
     "type": "#jsx",
     "nodeValue": "[1,2,3].map(el=> "
    },
    {
     "type": "div",
     "props": {},
     "children": [
      {
       "type": "#jsx",
       "nodeValue": "el"
      }
     ]
    },
    {
     "type": "#jsx",
     "nodeValue": ")"
    }
   ]
  },
  {
   "type": "#text",
   "nodeValue": "yyy\n"
  },
  {
   "type": "span",
   "props": {},
   "children": [
    {
     "type": "#jsx",
     "nodeValue": "xxx"
    },
    {
     "type": "#text",
     "nodeValue": "--"
    },
    {
     "type": "#jsx",
     "nodeValue": "yyy"
    }
   ]
  }
 ]
}

generative rule:

  1. tagName --> type
  2. attributes --> props
  3. voidTag that like <br > <hr/> or closing tag that like <element /> has isVoidTag = true property
  4. {...obj} will add spreadAttribute property to props object
  5. text node type is "#text"
  6. {} will generate #jsx node , that has object nodeValue or array nodeValue

You also can be used directly evalJSX, that already contains jsx-parser

evalJSX.globalNs  = 'React' // or `anu`  https://github.com/RubyLouvre/anu or `preact`
var Parent = React.createClass({
        getChildContext: function() {
            return {
                papa: 'papa'
            };
        },

        render: function() {
            return evalJSX(`
    <div class="parent">{this.props.children}</div>`, {
                this: this
            });
        }
    });

example

<!doctype html>
<html>

<head>
    <meta charset="utf-8" />
    <title>JSX Parser</title>
    <script src="https://cdn.bootcss.com/react/15.5.4/react.js"></script>
    <script src="https://cdn.bootcss.com/react/15.5.4/react-dom.js"></script>
    <script src='./index.umd.js'>
        jsx - parser
    </script>
    <script src='./evalJSX.js'></script>

    <script>
        evalJSX.globalNs = 'React'
        class A extends React.Component {
            constructor(props) {
                super(props)
                this.state = {
                    aaa: 111
                }
            }
            render() {
                return evalJSX(`<div className={"hello"}>{this.state.aaa}</div>`, {
                    this: this
                })
            }
        }
        window.onload = function() {
            ReactDOM.render(React.createElement(A), document.getElementById('example'))
        }
    </script>
</head>

<body>

    <div id="example"></div>

</body>


</html>

image

jsx-parser's People

Contributors

rubylouvre avatar

Watchers

 avatar

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.