Skip to content

Commit

Permalink
feat: added installTo static method
Browse files Browse the repository at this point in the history
  • Loading branch information
teclone committed Mar 2, 2020
1 parent 49fd182 commit fc61d3a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,46 +43,47 @@ Following the specification, the `XMLSerializer` interface is a constructor and
```javascript
import XMLSerializer from '@teclone/xml-serializer';

let instance = new XMLSerializer();
const instance = new XMLSerializer();
console.log(instance.serializeToString(someXmlNode));
```

The constructor can take a boolean argument that indicates if whitespace should be preserved in the serialized output. Default value is `true`;

```javascript
// do not preserve white space
let instance = new XMLSerializer(false);
let xmlString = instance.serializeToString(document);
const instance = new XMLSerializer(false);
const xmlString = instance.serializeToString(document);
```

### Using with [JSDOM](https://github.com/jsdom/jsdom)

Currently, JSDOM has not implemented the `XMLSerializer` interface. This can be easily integrated with JSDOM and any other similar mockup environment or for web scrapping and xml feed parsing like below.
Currently [at the time of creating this], JSDOM has not implemented the `XMLSerializer` interface. This can be easily integrated with JSDOM and any other similar mockup environment or for web scrapping and xml feed parsing like below.

```javascript
//assumes jsdom has been installed.
import XMLSerializer from '@teclone/xml-serializer';
import { JSDOM } from 'jsdom';

let dom = new JSDOM();
dom.window.XMLSerializer = XMLSerializer;
const dom = new JSDOM();
XMLSerializer.installTo(dom.window);

global.window = dom.window;

//start running your tests or do something else.
```

### Using on the browser

The browser build is available inside the `dist` folder when you npm install the package. You can also this repo and run the build command locally. It exposes an `XMLSerializer` construct on the `window` object.
The browser build is available inside the `build/dist` folder when you npm install the package. You can also clone this repo and run the build command locally. It exposes an `XMLSerializer` construct on the `window` object.

```html
<script
type="text/javascript"
src="node_modules/@teclone/xml-serializer/dist/main.min.js"
src="node_modules/@teclone/xml-serializer/build/dist/main.js"
>
<script>
<script type="text/javascript">
let serializer = new XMLSerializer();
const serializer = new XMLSerializer();
// do some serialization stuffs
</script>
```
Expand All @@ -93,7 +94,7 @@ By default, the serializer preserves white space during the serialization proces

```javascript
//do not preserve white space
let instance = new XMLSerializer(false);
const instance = new XMLSerializer(false);
```

Another improvement is that it removes all duplicate xml prefix definition on as recommended in the specification document unlike what web browsers do. Below is an example of
Expand Down
6 changes: 4 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Serializer from './modules/Serializer.js';

export class XMLSerializer {
class XMLSerializer {
/**
*@param {boolean} [preserveWhiteSpace=true] - boolean value indicating if white spaces
* should be preserved as it is in the source
Expand Down Expand Up @@ -32,6 +32,8 @@ export class XMLSerializer {
/**
* installs the serialize to the given target object
*/
XMLSerializer.install = target => {
XMLSerializer.installTo = target => {
target.XMLSerializer = XMLSerializer;
};

export default XMLSerializer;

0 comments on commit fc61d3a

Please sign in to comment.