https://github.com/drom/onml
Object Notation for Markup Language
https://github.com/drom/onml
hacktoberfest
Last synced: about 1 year ago
JSON representation
Object Notation for Markup Language
- Host: GitHub
- URL: https://github.com/drom/onml
- Owner: drom
- License: mit
- Created: 2015-10-29T00:06:02.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T06:31:51.000Z (over 3 years ago)
- Last Synced: 2025-03-27T23:41:52.685Z (about 1 year ago)
- Topics: hacktoberfest
- Language: JavaScript
- Homepage:
- Size: 40 KB
- Stars: 23
- Watchers: 4
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ONML
[](https://www.npmjs.org/package/onml)
[](https://github.com/drom/onml/actions)
[jsonml.org](http://www.jsonml.org/) compatible tool set.
## Use
### Node.js
```
npm i onml --save
```
```js
var onml = require('onml');
```
## API
### onml.parse() --- onml.p()
The `onml.parse()` method parses a XML/HTML/SVG string and returns a JavaScript value.
```js
var obj = onml.parse('so me');
console.log(obj);
-->
["text", {a: "5"}, "so me"]
```
### onml.stringify() --- onml.s()
The `onml.stringify(array, [indentation])` method converts a JavaScript value to a XML/HTML/SVG string.
```js
var str = onml.stringify(['text', {a: 55}, 'so me'], 2);
console.log(str);
-->
so me
```
### onml.traverse() --- onml.t()
JSONML object traversal tool. See [test/traverse.js](test/traverse.js) for more details.
```js
onml.traverse(obj, {
enter: function (node, parent) {
...
},
leave: function (node, parent) {
...
}
});
```
Inside `enter` and `leave` functions:
`node` and `parent` objects have the following attributes:
* `.name` -- tag name
* `.attr` -- attributes object
* `.full` -- full node array
`this` will hold additional methods:
* `this.name(string)` -- to change the node tag
* `this.skip()` -- to skip subtree based on the current node
* `this.remove()` -- to remove current node
* `this.replace(array)` -- to replace current node
```js
// count divs on enter
var count = 0;
onml.traverse(
['b',
['div', {a: true},
['span',
'div',
['div',
['div', {},
['div', {a: true}]
]
],
['div', {},
['div']
]
]
]
],
{
enter: function (node) {
if (node.name === 'div') {
count++;
}
}
}
);
console.log(count);
-->
6
```
## Testing
`npm test`
## License
MIT [LICENSE](https://github.com/drom/onml/blob/master/LICENSE).