Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # html-parse-stringify2
  2. ## Quick note
  3. This is a fork of [html-parse-stringify](https://github.com/HenrikJoreteg/html-parse-stringify) that I made because I wanted to get some important fixes into the repo and available as an NPM package and I'm not sure whether the old repo is still being maintained. Hence this. This could be temporary - I'll gladly drop this if activity picks back up on the original repo. But for now we've got a new npm package `html-parse-stringify2`. Install with:
  4. ```
  5. npm install --save html-parse-stringify2
  6. ```
  7. Use as documented below...
  8. ### OK, on to the original README...
  9. This is an *experimental lightweight approach* to enable quickly parsing HTML into an AST and stringify'ing it back to the original string.
  10. As it turns out, if you can make a the simplifying assumptions about HTML that all tags must be closed or self-closing. Which is OK for *this* particular application. You can write a super light/fast parser in JS with regex.
  11. "Why on earth would you do this?! Haven't you read: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags ?!?!"
  12. Why yes, yes I have :)
  13. But the truth is. If you *could* do this in a whopping grand total of ~600 bytes (min+gzip) as this repo shows. It potentially enables DOM diffing based on a HTML strings to be super light and fast in a browser. What is that you say? DOM-diffing?
  14. Yes.
  15. React.js essentially pioneered the approach. With Reach you render to a "virtual DOM" whenever you want to, and the virtual DOM can then diff against the real DOM (or the last virtual DOM) and then turn that diff into whatever transformations are necessary to get the *real* DOM to match what you rendered as efficiently as possible.
  16. As a result, when you're building a single page app, you don't have to worry so much about bindings. Instead, you simple re-render to the virtual DOM whenever you know something's changed. All of a sudden being able to have `change` events for individual properties becomes less important, instead you can just reference those values in your template whenever you think something changed.
  17. Cool idea, right?!
  18. ## So why this?
  19. Well, there are other things React expects me to do if I use it that I don't like. Such as the custom templating and syntax you have to use.
  20. If, hypothetically, you could instead diff an HTML string (generated by *whatever* templating language of your choice) against the DOM, then you'd get the same benefit, sans React's impositions.
  21. This may all turn out to be a bad idea altogether, but initial results seem promising when paired with [virtual-dom](https://github.com/Matt-Esch/virtual-dom).
  22. But you can't just diff HTML strings, as simple strings, very easily, in order to diff two HTML node trees you have to first turn that string into a tree structure of some sort. Typically, the thing you generate from parsing something like this is called an AST (abstract syntax tree).
  23. This lib does exactly that.
  24. It has two methods:
  25. 1. parse
  26. 2. stringify
  27. ## `.parse(htmlString, options)`
  28. Takes a string of HTML and turns it into an AST, the only option you can currently pass is an object of registered `components` whose children will be ignored when generating the AST.
  29. ## `.stringify(AST)`
  30. Takes an AST and turns it back into a string of HTML.
  31. ## What does the AST look like?
  32. See comments in the following example:
  33. ```js
  34. var HTML = require('html-parse-stringify2')
  35. // this html:
  36. var html = '<div class="oh"><p>hi</p></div>';
  37. // becomes this AST:
  38. var ast = HTML.parse(html);
  39. console.log(ast);
  40. /*
  41. {
  42. // can be `tag`, `text` or `component`
  43. type: 'tag',
  44. // name of tag if relevant
  45. name: 'div',
  46. // parsed attribute object
  47. attrs: {
  48. class: 'oh'
  49. },
  50. // whether this is a self-closing tag
  51. // such as <img/>
  52. voidElement: false,
  53. // an array of child nodes
  54. // we see the same structure
  55. // repeated in each of these
  56. children: [
  57. {
  58. type: 'tag',
  59. name: 'p',
  60. attrs: {},
  61. voidElement: false,
  62. children: [
  63. // this is a text node
  64. // it also has a `type`
  65. // but nothing other than
  66. // a `content` containing
  67. // its text.
  68. {
  69. type: 'text',
  70. content: 'hi'
  71. }
  72. ]
  73. }
  74. ]
  75. }
  76. */
  77. ```
  78. ## the AST node types
  79. ### 1. tag
  80. properties:
  81. - `type` - will always be `tag` for this type of node
  82. - `name` - tag name, such as 'div'
  83. - `attrs` - an object of key/value pairs. If an attribute has multiple space-separated items such as classes, they'll still be in a single string, for example: `class: "class1 class2"`
  84. - `voidElement` - `true` or `false`. Whether this tag is a known void element as defined by [spec](http://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements).
  85. - `children` - array of child nodes. Note that any continuous string of text is a text node child, see below.
  86. ### 2. text
  87. properties:
  88. - `type` - will always be `text` for this type of node
  89. - `content` - text content of the node
  90. ### 3. component
  91. If you pass an object of `components` as part of the `options` object passed as the second argument to `.parse()` then the AST won't keep parsing that branch of the DOM tree when it one of those registered components.
  92. This is so that it's possible to ignore sections of the tree that you may want to handle by another "subview" in your application that handles it's own DOM diffing.
  93. properties:
  94. - `type` - will always be `component` for this type of node
  95. - `name` - tag name, such as 'div'
  96. - `attrs` - an object of key/value pairs. If an attribute has multiple space-separated items such as classes, they'll still be in a single string, for example: `class: "class1 class2"`
  97. - `voidElement` - `true` or `false`. Whether this tag is a known void element as defined by [spec](http://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements).
  98. - `children` - it will still have a `children` array, but it will always be empty.
  99. ## credits
  100. If this sounds interesting you should probably follow [@HenrikJoreteg](http://twitter.com/henrikjoreteg) and [@Philip_Roberts](twitter.com/philip_roberts) on twitter to see how this all turns out.
  101. ## license
  102. MIT