Comparing version
# Change Log | ||
## [v2.0.0](https://github.com/nfarina/xmldoc/tree/v2.0.0) (2024) | ||
**Major Changes:** | ||
- Complete TypeScript rewrite with full type definitions | ||
- Dual package support for both CommonJS and ESM environments | ||
- Maintained backwards compatibility with existing code | ||
- Updated `sax` dependency to 1.2.4 | ||
- Added support for modern module resolution via package.json exports field | ||
- Improved HTML compatibility with self-closing tags | ||
- Enhanced documentation and examples | ||
- Added proper TypeScript declaration files | ||
## [v1.0.0](https://github.com/nfarina/xmldoc/tree/v1.0.0) (2016-12-26) | ||
[Full Changelog](https://github.com/nfarina/xmldoc/compare/v0.5.1...v1.0.0) | ||
@@ -11,3 +25,3 @@ | ||
- While writing back xml document to a file it removes all comments [\#39](https://github.com/nfarina/xmldoc/issues/39) | ||
- react native using xmldoc to parser xml [\#38](https://github.com/nfarina/xmldoc/issues/38) | ||
- react native using xmldoc to parser xml [\#38](https://github.com/nfarina/xmldoc/issues/38) | ||
- Order of val in relation to children? [\#37](https://github.com/nfarina/xmldoc/issues/37) | ||
@@ -20,2 +34,3 @@ | ||
## [v0.5.1](https://github.com/nfarina/xmldoc/tree/v0.5.1) (2016-05-12) | ||
[Full Changelog](https://github.com/nfarina/xmldoc/compare/v0.5.0...v0.5.1) | ||
@@ -32,2 +47,3 @@ | ||
## [v0.5.0](https://github.com/nfarina/xmldoc/tree/v0.5.0) (2016-04-27) | ||
[Full Changelog](https://github.com/nfarina/xmldoc/compare/v0.4.0...v0.5.0) | ||
@@ -52,2 +68,3 @@ | ||
## [v0.4.0](https://github.com/nfarina/xmldoc/tree/v0.4.0) (2015-11-16) | ||
[Full Changelog](https://github.com/nfarina/xmldoc/compare/v0.3.1...v0.4.0) | ||
@@ -66,2 +83,3 @@ | ||
## [v0.3.1](https://github.com/nfarina/xmldoc/tree/v0.3.1) (2015-05-22) | ||
**Closed issues:** | ||
@@ -90,4 +108,2 @@ | ||
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* | ||
\* _This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)_ |
@@ -146,2 +146,4 @@ import { SAXParser } from "sax"; | ||
startTagPosition: number | null; | ||
/** The SAX parser instance (available only during parsing) */ | ||
parser?: SAXParser; | ||
/** | ||
@@ -148,0 +150,0 @@ * Creates a new XML element |
@@ -112,2 +112,10 @@ "use strict"; | ||
this.type = "element"; | ||
// If you didn't hand us a parser (common case) see if we can grab one | ||
// from the current execution stack. | ||
if (!parser && delegates.length) { | ||
var delegate = delegates[delegates.length - 1]; | ||
if (delegate.parser) { | ||
parser = delegate.parser; | ||
} | ||
} | ||
this.name = tag.name; | ||
@@ -141,2 +149,4 @@ this.attr = tag.attributes; | ||
delegates.unshift(child); | ||
// Remove the parser as it is no longer needed and should not be exposed to clients | ||
delete this.parser; | ||
} | ||
@@ -143,0 +153,0 @@ _closetag() { |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"packageManager": "yarn@4.7.0", | ||
@@ -35,4 +35,6 @@ "type": "commonjs", | ||
"scripts": { | ||
"test": "tap test/*.js", | ||
"coverage": "npm test -- --cov --coverage-report=html", | ||
"test": "yarn test:js || true && yarn test:ts", | ||
"test:js": "tap test/*.js", | ||
"test:ts": "vitest run test/ts", | ||
"coverage": "npm test:js -- --cov --coverage-report=html", | ||
"clean": "rm -rf dist", | ||
@@ -66,3 +68,4 @@ "build": "tsc --sourceMap", | ||
"tap": "^16.3.0", | ||
"typescript": "^5.8.2" | ||
"typescript": "^5.8.2", | ||
"vitest": "^3.1.3" | ||
}, | ||
@@ -69,0 +72,0 @@ "engines": { |
@@ -1,2 +0,1 @@ | ||
[](https://travis-ci.org/nfarina/xmldoc) | ||
@@ -7,32 +6,34 @@ [](https://coveralls.io/github/nfarina/xmldoc?branch=master) | ||
`xmldoc` lets you parse XML documents with ease. It's a pure-JavaScript, one-file XML document class with a single dependency on the excellent [`sax`][sax] parser. | ||
`xmldoc` lets you parse XML documents with ease. It's a lightweight XML document class with a single dependency on the excellent [`sax`][sax] parser. | ||
For more on why I wrote this class, see the [blog post][blog]. | ||
[blog]: http://nfarina.com/post/34302964969/a-lightweight-xml-document-class-for-nodejs-javascript | ||
As of version 2.0, `xmldoc` fully supports TypeScript and can be imported in both CommonJS and ESM environments. | ||
[blog]: http://nfarina.com/post/34302964969/a-lightweight-xml-document-class-for-nodejs-javascript | ||
## Release Notes | ||
See [CHANGELOG.md](./CHANGELOG.md) for details (built with [GitHub Changelog Generator](https://skywinder.github.io/github-changelog-generator/)). | ||
See [CHANGELOG.md](./CHANGELOG.md) for details. | ||
## Installation | ||
npm install xmldoc | ||
```bash | ||
npm install xmldoc | ||
# or | ||
yarn add xmldoc | ||
``` | ||
Or just download the repository and include it in your `node_modules` directly. Or just download the [single JS file][blob]! | ||
[blob]: https://github.com/nfarina/xmldoc/blob/master/lib/xmldoc.js | ||
[blob]: https://github.com/nfarina/xmldoc/blob/master/lib/xmldoc.js | ||
## Installation - React Native | ||
## Usage | ||
I haven't tested this myself but [installing `buffer` and `stream` separately](https://github.com/nfarina/xmldoc/issues/38) may be necessary for `xmldoc` to work on React Native: | ||
### CommonJS (Node.js) | ||
npm install buffer stream xmldoc | ||
## Usage | ||
```js | ||
var xmldoc = require('xmldoc'); | ||
const { XmlDocument } = require("xmldoc"); | ||
var document = new xmldoc.XmlDocument("<some>xml</some>"); | ||
const document = new XmlDocument("<some>xml</some>"); | ||
@@ -42,2 +43,19 @@ // do things | ||
### ESM / TypeScript | ||
```ts | ||
// ESM environments | ||
import { XmlDocument } from "xmldoc"; | ||
const document = new XmlDocument("<some>xml</some>"); | ||
``` | ||
### React Native | ||
If you're using React Native, you may need to install `buffer` and `stream` separately: | ||
```bash | ||
npm install buffer stream xmldoc | ||
``` | ||
## Classes | ||
@@ -51,8 +69,8 @@ | ||
* `name` - the node name, like "tat" for `<tat>`. XML "namespaces" are ignored by the underlying [sax-js](https://github.com/isaacs/sax-js) parser, so you'll simply get "office:body" for `<office:body>`. | ||
* `attr` - an object dict containing attribute properties, like `bookNode.attr.title` for `<book title="...">`. | ||
* `val` - the string "value" of the node, if any, like "world" for `<hello>world</hello>`. | ||
* `children` - an array of `XmlElement` children of the node. | ||
* `firstChild`, `lastChild` - pretty much what it sounds like; null if no children | ||
* `line`, `column`, `position`, `startTagPosition` - information about the element's original position in the XML string. | ||
- `name` - the node name, like "tat" for `<tat>`. XML "namespaces" are ignored by the underlying [sax-js](https://github.com/isaacs/sax-js) parser, so you'll simply get "office:body" for `<office:body>`. | ||
- `attr` - an object dict containing attribute properties, like `bookNode.attr.title` for `<book title="...">`. | ||
- `val` - the string "value" of the node, if any, like "world" for `<hello>world</hello>`. | ||
- `children` - an array of `XmlElement` children of the node. | ||
- `firstChild`, `lastChild` - pretty much what it sounds like; null if no children | ||
- `line`, `column`, `position`, `startTagPosition` - information about the element's original position in the XML string. | ||
@@ -128,5 +146,6 @@ Each member defaults to a sensible "empty" value like `{}` for `attr`, `[]` for `children`, and `""` for `val`. | ||
```js | ||
xml.toString({compressed:true}) // strips indents and linebreaks | ||
xml.toString({trimmed:true}) // trims long strings for easier debugging | ||
xml.toString({preserveWhitespace:true}) // prevents whitespace being removed from around element values | ||
xml.toString({ compressed: true }); // strips indents and linebreaks | ||
xml.toString({ trimmed: true }); // trims long strings for easier debugging | ||
xml.toString({ preserveWhitespace: true }); // prevents whitespace from being removed from around element values | ||
xml.toString({ html: true }); // uses HTML self-closing tag rules for elements without children | ||
``` | ||
@@ -138,3 +157,5 @@ | ||
var xml = "<author><name>looooooong value</name></author>"; | ||
console.log("My document: \n" + new XmlDocument(xml).toString({trimmed:true})) | ||
console.log( | ||
"My document: \n" + new XmlDocument(xml).toString({ trimmed: true }), | ||
); | ||
``` | ||
@@ -151,7 +172,7 @@ | ||
Feel free to file issues or hit me up on [Twitter][twitter]. | ||
Feel free to file issues or hit me up on [X][x]. | ||
[underscore]: http://underscorejs.org | ||
[XPath]: http://en.wikipedia.org/wiki/XPath | ||
[twitter]: http://twitter.com/nfarina | ||
[sax]: https://github.com/isaacs/sax-js | ||
[underscore]: http://underscorejs.org | ||
[XPath]: http://en.wikipedia.org/wiki/XPath | ||
[x]: http://twitter.com/nfarina | ||
[sax]: https://github.com/isaacs/sax-js |
Sorry, the diff of this file is not supported yet
61921
2.74%1062
1.14%173
13.82%5
25%