Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

libxmljs

Package Overview
Dependencies
Maintainers
2
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

libxmljs - npm Package Compare versions

Comparing version 0.6.1 to 0.7.0

docs/Migrating-from-0.4.x-to-0.5.0.md

1

docs/_Sidebar.md

@@ -7,5 +7,4 @@ * [[Libxmljs]]

* [[HtmlDocument]]
* [[Parser]]
* [[SaxParser]]
* [[SaxPushParser]]
* [[SyntaxError]]

@@ -9,40 +9,11 @@ # Element

### new libxml.Element(doc, name)
> Creates a new element object called name
### new libxml.Element(doc, name, [content])
> **args**
*doc* - a Document object to provide context for the node
*name* - a string representing the name of the element
>Creates a new element object called name with attrs as its attributes. It is recommended that you use the *node* method of Document or Element instead.
### new libxml.Element(doc, name, attrs)
>**args**
*doc* - a document object to provide context for the node
*name* - a string representing the name of the element
*content* - a string representing the content to add to the node
>Creates a new element object called name with attrs as its attributes
>**args**
*doc* - a document object to provide context for the node
*name* - a string representing the name of the element
*attrs* - a hash of {name: value} pairs to set as attributes
### new libxml.Element(doc, name, attrs, content)
>Creates a new element object called name with attrs as its attributes
>**args**
*doc* - a document object to provide context for the node
*name* - a string representing the name of the element
*attrs* - a hash of {name: value} pairs to set as attributes
*content* - a string representing the content to add to the node
*callback* - a function that will accept the new object as an argument
### new libxml.Element(doc, name, attrs, content, callback)
>Creates a new element object called name with attrs as its attributes
>**args**
*doc* - a document object to provide context for the node
*name* - a string representing the name of the element
*attrs* - a hash of {name: value} pairs to set as attributes
*content* - a string representing the content to add to the node
*callback* - a function that will accept the new object as an argument
## Methods

@@ -61,3 +32,3 @@

>**args**
>**args**
*new_name* - a string representing the new name of the element

@@ -80,4 +51,4 @@

>**args**
*name* - a string representing the name of the attribute
>**args**
*name* - a string representing the name of the attribute

@@ -91,4 +62,4 @@ >**returns** - an Attribute node or null

>**args**
*attr* - an Attribute node
>**args**
*attr* - an Attribute node

@@ -102,4 +73,4 @@ >**returns** - the element node (not the attribute)

>**args**
*attr* - an object of {name: value} pairs
>**args**
*attr* - an object of {name: value} pairs

@@ -134,4 +105,4 @@ >**returns** - the element object

>**args**
*idx* - a zero indexed integer representing the child node to return
>**args**
*idx* - a zero indexed integer representing the child node to return

@@ -141,3 +112,3 @@ >**returns** - a Node object or null

### elements.childNodes()
### element.childNodes()

@@ -153,4 +124,4 @@ >Get a list of all the immediate children of the element

>**args**
*child* - an Element node to be added as a child
>**args**
*child* - an Element node to be added as a child

@@ -178,3 +149,3 @@ >**returns** - the element node (not the child)

>**args**
>**args**
*siblingNode* - a Node to add as a sibling

@@ -203,3 +174,3 @@

>**args**
>**args**
*siblingNode* - a Node to add as a sibling

@@ -214,4 +185,4 @@

>**args**
*xpath* - a string representing the xpath to search
>**args**
*xpath* - a string representing the xpath to search

@@ -225,10 +196,11 @@ >**returns** - an array of Node objects. may be empty

>**args**
*xpath* - a string representing the xpath to search
*ns_uri* - a string representing the namespace URI
>**args**
*xpath* - a string representing the xpath to search
*ns_uri* - a string representing the namespace URI
>**returns** - an array of Node objects
*example*
doc.find("xmlns:child-name", "ns:uri")
*examples*
`doc.find("xmlns:child-name", "ns:uri")
doc.find('//y:child-name', { y: 'http://some.com/for/y'});`

@@ -238,8 +210,8 @@

>Return the first node node that matches the xpath expression
>Return the first node node that matches the xpath expression
This this is syntactic sugar for `find(xpath, ns_uri)[0]`
>**args**
*xpath* - a string representing the xpath to search
*ns_uri* - a string representing the namespace URI
>**args**
*xpath* - a string representing the xpath to search
*ns_uri* - a string representing the namespace URI

@@ -251,12 +223,12 @@ >**returns** - a Node or null

>Search the element's children by xpath within any of the URI
>Search the element's children by xpath within any of the URI
namespaces
>**args**
*xpath* - a string representing the xpath to search
*namespaces* - a hash representing the namespaces
>**args**
*xpath* - a string representing the xpath to search
*namespaces* - a hash representing the namespaces
>**returns** - an array of Node objects
*example*
*example*
doc.find("ns-1:child-name", {"ns-1": "ns:uri"})

@@ -268,3 +240,3 @@ doc.find("ns-1:child1/ns-2:child2", {"ns-1": "ns:uri1", "ns-2": "ns:uri2"})

>Return the first node that matches the xpath
>Return the first node that matches the xpath
This is syntactic sugar for `find(xpath, namespaces)[0]`

@@ -280,4 +252,4 @@

>**args**
*href* - a string representing the URI of the new namespace
>**args**
*href* - a string representing the URI of the new namespace

@@ -293,5 +265,5 @@ >**returns** - the new Namespace object

>**args**
*prefix* - a string representing the prefix for the new namespace
*href* - a string representing the URI for the new namespace
>**args**
*prefix* - a string representing the prefix for the new namespace
*href* - a string representing the URI for the new namespace

@@ -312,4 +284,4 @@ >**returns** - the new Namespace object

>**args**
*ns* - a Namespace object
>**args**
*ns* - a Namespace object

@@ -325,4 +297,4 @@ >**returns** - the element object

>**args**
*href* - a string representing the namespace URI
>**args**
*href* - a string representing the namespace URI

@@ -339,5 +311,5 @@ >**returns** - the namespace object

>**args**
*prefix* - a string representing the namespace prefix
*href* - a string representing the namespace URI
>**args**
*prefix* - a string representing the namespace prefix
*href* - a string representing the namespace URI

@@ -344,0 +316,0 @@ >**returns** - the Namespace object

@@ -25,1 +25,15 @@ # Libxmljs

>**returns** a string
### Functions
**parseXmlString(string)**
>Parse the string as xml and return a Document object. Throws if there was an error parsing
>**returns** a Document
**parseHtmlString(string)**
>Parse the string as html and return a Document object. Throws if there was an error parsing
>**returns** a Document
# SaxParser
libxmljs provides a SAX2 parser interface that can take a string, file.
libxmljs provides a SAX2 parser interface that can take a string, file. A SaxParser inherits from EventEmitter to emit parsing events.
### Constructor
#### new libxml.SaxParser()

@@ -12,31 +11,2 @@

#### new libxml.SaxParser(callbacks)
>Instantiate a new SaxParser
>**args**
*callbacks* - an object with event names as properties and event handler functions as values
### Parse Events
>**startDocument** - Fired at the start of a document
>**endDocument** - Fired at the end of the document parse
>**startElementNS** - Fired on an open element tag
>**endElementNS** - Fired at the close of an element
>**characters** - Fired when a set of content characters is encountered
>**cdata** - Fired when a CDATA is encountered
>**comment** - Fired when an comment is encountered
>**warning** - Fired when an warning is encountered
>**error** - Fired when an error is encountered
### Methods

@@ -52,14 +22,16 @@

#### parser.onStartDocument(function() {})
### Parse Events
>Called at the start of a document
#### startDocument
#### parse.onEndDocument(function() {})
>Emitted at the start of a document
>Called at the end of the document parse
#### endDocument
#### parser.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {})
>Emitted at the end of the document parse
>Called on an open element tag
#### startElementNS(elem, attrs, prefix, uri, namespace)
>Emitted on an open element tag
>**args**

@@ -72,7 +44,6 @@ *elem* - a string representing the element name

#### endElementNS(elem, prefix, uri)
#### parser.onEndElementNS(function(elem, prefix, uri) {})
>Emitted at the close of an element
>Called at the close of an element
>**args**

@@ -83,42 +54,36 @@ *elem* - a string representing the element name

#### characters(chars)
#### parser.onCharacters(function(chars) {})
>Emitted when a set of content characters is encountered
>Called when a set of content characters is encountered
>**args**
*chars* - a string of characters
#### cdata(cdata)
#### parser.onCdata(function(cdata) {})
>Emitted when a CDATA is encountered
>Called when a CDATA is encountered
>**args**
*cdata* - a string representing the CDATA
#### comment(msg)
#### parser.onComment(function(msg) {})
>Emitted when a comment is encountered
>Called when a comment is encountered
>**args**
*msg* - a string representing the comment
#### warning(msg)
#### parser.onWarning(function(msg) {})
>Emitted when a warning is encountered
>Called when a warning is encountered
>**args**
*msg* - a string representing the warning message
#### error(msg)
#### parser.onError(function(msg) {})
>Emitted when an error is encountered
>Called when an error is encountered
>**args**
*msg* - a string representing the error message
# SaxPushParser
libxmljs provides a SAX2 push parser interface that accepts chunks of data.
libxmljs provides a SAX2 push parser interface that accepts chunks of data. SaxPush parser inherits from EventEmitter to emit parse events.
### Constructor
#### new libxmljs.SaxPushParser()

@@ -12,33 +11,4 @@

#### new libxmljs.SaxPushParser(callbacks)
>Instantiate a new SaxParser
>**args**
*callbacks* - an object with event names as properties and event handler functions as values
### Parse Events
>**startDocument** - Fired at the start of a document
>**endDocument** - Fired at the end of the document parse
>**startElementNS** - Fired on an open element tag
>**endElementNS** - Fired at the close of an element
>**characters** - Fired when a set of content characters is encountered
>**cdata** - Fired when a CDATA is encountered
>**comment** - Fired when an comment is encountered
>**warning** - Fired when an warning is encountered
>**error** - Fired when an error is encountered
### Methods
#### parser.push(string)

@@ -51,14 +21,15 @@

### Parse Events
#### parser.onStartDocument(function() {})
#### startDocument
>Called at the start of a document
>Emitted at the start of a document
#### parse.onEndDocument(function() {})
#### endDocument
>Called at the end of the document parse
>Emitted at the end of the document parse
#### parser.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {})
#### startElementNS(elem, attrs, prefix, uri, namespace)
>Called on an open element tag
>Emitted on an open element tag

@@ -72,7 +43,6 @@ >**args**

#### endElementNS(elem, prefix, uri)
#### parser.onEndElementNS(function(elem, prefix, uri) {})
>Emitted at the close of an element
>Called at the close of an element
>**args**

@@ -83,47 +53,36 @@ *elem* - a string representing the element name

#### characters(chars)
#### parser.onCharacters(function(chars) {})
>Emitted when a set of content characters is encountered
>Called when a set of content characters is encountered
>**args**
*chars* - a string of characters
#### cdata(cdata)
#### parser.onCdata(function(cdata) {})
>Emitted when a CDATA is encountered
>Called when a CDATA is encountered
>**args**
*cdata* - a string representing the CDATA
#### comment(msg)
#### parser.onComment(function(msg) {})
>Emitted when a comment is encountered
>Called when a comment is encountered
>**args**
*msg* - a string representing the comment
#### warning(msg)
#### parser.onWarning(function(msg) {})
>Emitted when a warning is encountered
>Called when a warning is encountered
>**args**
*msg* - a string representing the warning message
#### error(msg)
#### parser.onError(function(msg) {})
>Emitted when an error is encountered
>Called when an error is encountered
>**args**
*msg* - a string representing the error message
### Examples
Here's an extended example of using the sax push parser to recreate an xml document.
[http://gist.github.com/484083](http://gist.github.com/484083)

@@ -8,44 +8,2 @@ # XmlDocument

### new libxmljs.Document();
>Instantiate a new document object defaulting the version to 1.0 and the
encoding to UTF-8.
>**returns** a new document object
### new libxmljs.Document(callback);
>Instantiate a new document object defaulting the version to 1.0 and the
encoding to UTF-8.
>**args**
*callback* - a function that accepts the new document object
>**returns** a new document object
### new libxmljs.Document(version);
>Instantiate a new document setting the version to version and defaulting
the encoding to UTF-8.
>**args**
*version* - a string representing the desired document version
>**returns** a new document object
### new libxmljs.Document(version, callback);
>Instantiate a new document setting the version to version and defaulting
the encoding to UTF-8.
>**args**
*version* - a string representing the desired document version
*callback* - a function that accepts the new document object
>**returns** a new document object
### new libxmljs.Document(version, encoding);

@@ -57,4 +15,4 @@

>**args**
*version* - a string representing the desired document version
*encoding* - a string representing the desired document encoding
*version* - a string representing the desired document version (default '1.0')
*encoding* - a string representing the desired document encoding (default 'utf8')

@@ -64,15 +22,2 @@

### new libxmljs.Document(version, encoding, callback);
>Instantiate a new document setting the version to version and the encoding
to encoding.
>**args**
*version* - a string representing the desired document version
*encoding* - a string representing the desired document encoding
*callback* - a function that accepts the new document as an argument
>**returns** a new document object
## Methods

@@ -98,8 +43,2 @@

### doc.document()
>A convenience method that
>**returns** the document object
### doc.errors()

@@ -146,7 +85,12 @@

### doc.node([args])
### doc.node(name, content)
>A convenience method for creating and setting the root node
See new libxmljs.Element for the full spec
>**args**
*name* - the tag name
*content* - text content of the node (a string)
>**returns** the newly created node
### doc.root()

@@ -153,0 +97,0 @@

@@ -5,22 +5,24 @@ # Libxmljs

var libxmljs = require("libxmljs");
var xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<root>' +
'<child foo="bar">' +
'<grandchild baz="fizbuzz">grandchild content</grandchild>' +
'</child>' +
'<sibling>with content!</sibling>' +
'</root>';
```javascript
var libxmljs = require("libxmljs");
var xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<root>' +
'<child foo="bar">' +
'<grandchild baz="fizbuzz">grandchild content</grandchild>' +
'</child>' +
'<sibling>with content!</sibling>' +
'</root>';
var xmlDoc = libxmljs.parseXml(xml);
var xmlDoc = libxmljs.parseXmlString(xml);
// xpath queries
var gchild = xmlDoc.get('//grandchild');
// xpath queries
var gchild = xmlDoc.get('//grandchild');
console.log(gchild.text()); // prints "grandchild content"
console.log(gchild.text()); // prints "grandchild content"
var children = xmlDoc.root().childNodes();
var child = children[0];
var children = xmlDoc.root().childNodes();
var child = children[0];
console.log(child.attr('foo').value()); // prints "bar"
console.log(child.attr('foo').value()); // prints "bar"
```

@@ -44,67 +46,56 @@ ## API

There are two main ways to use the builder. The first:
To build an XML document simply create all the required nodes (indentation is meant to represent location in the tree, you can capture any intermediate variable to reuse):
var doc = new libxml.Document(function(n) {
n.node('root', function(n) {
n.node('child', {foo: 'bar'}, function(n) {
n.node('grandchild', {baz: 'fizbuzz'}, 'grandchild content');
});
n.node('sibling', 'with content!');
});
});
```javascript
var doc = new libxml.Document();
doc.node('root')
.node('child').attr({foo: 'bar'})
.node('grandchild', 'grandchild content').attr({baz: 'fizbuzz'})
.parent()
.parent()
.node('sibling', 'with content!');
```
Calling doc.toString() will yield the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child foo="bar">
<grandchild baz="fizbuzz">grandchild content</grandchild>
</child>
<sibling>with content!</sibling>
</root>
```xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child foo="bar">
<grandchild baz="fizbuzz">grandchild content</grandchild>
</child>
<sibling>with content!</sibling>
</root>
```
The other way is through chaining (indentation is meant to represent location in the tree):
var doc = new libxml.Document();
doc.node('root')
.node('child', {foo: 'bar'})
.node('grandchild', {baz: 'fizbuzz'}, 'grandchild content')
.parent()
.parent()
.node('sibling', 'with content!');
This will yield the same results.
### Parsing
By file:
```javascript
var doc = libxml.parseXmlString([xmlString]);
```
var doc = libxml.parseXmlFile([filename]);
#### SAX Parsing
By String:
SAX parsing objects are event emitters and callbacks can be connected in typical node.js fashion.
var doc = libxml.parseXml([xmlString]);
```javascript
var parser = new libxml.SaxParser();
#### SAX Parsing
parser.on('startDocument', ...);
parser.on('startElement', ...);
```
Callbacks for SAX are created during the instantiation of the parser object. You can omit any callback you're not interested in. Below is just a list of callbacks currently supported with the arguments they'll be sent.
There is also a shorthand syntax for adding event callbacks during parser creation.
var parser = new libxml.SaxParser(function(cb) {
cb.onStartDocument(function() {});
cb.onEndDocument(function() {});
cb.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {});
cb.onEndElementNS(function(elem, prefix, uri) {});
cb.onCharacters(function(chars) {});
cb.onCdata(function(cdata) {});
cb.onComment(function(msg) {});
cb.onWarning(function(msg) {});
cb.onError(function(msg) {});
});
```javascript
var parser = new libxml.SaxParser({
startDocument: function() {...},
endDocument: function() {...},
startElementNS: function(elem, attrs, prefix, uri, namespaces) {...},
});
// Parse a string:
parser.parseString([xmlString]);
// parse a string
parser.parseString([xmlString]);
```
// Parse a file:
parser.parseFile([filename]);
#### SAX Push Parsing

@@ -114,59 +105,10 @@

var parser = new libxml.SaxPushParser(function(cb) {
cb.onStartDocument(function() {});
cb.onEndDocument(function() {});
cb.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {});
cb.onEndElementNS(function(elem, prefix, uri) {});
cb.onCharacters(function(chars) {});
cb.onCdata(function(cdata) {});
cb.onComment(function(msg) {});
cb.onWarning(function(msg) {});
cb.onError(function(msg) {});
});
```javascript
var parser = new libxml.SaxPushParser();
while(xmlChunk)
parser.push(xmlChunk);
// connect any callbacks here
[Here's a more complex example](https://gist.github.com/484083)
## Basics
* GitHub Repo - [http://github.com/polotek/libxmljs](http://github.com/polotek/libxmljs)
* Docs - [http://github.com/polotek/libxmljs/wiki](http://github.com/polotek/libxmljs/wiki)
* Mailing list - [http://groups.google.com/group/libxmljs](http://groups.google.com/group/libxmljs)
## Requirements
* [libxml2](http://www.xmlsoft.org/)
* [node.js](http://nodejs.org/)
* v8 (comes bundled with node, no need to install)
* [scons](http://www.scons.org/) (for building)
**pre-conditions**
You will need have the libxml2 library installed and also the libxml2-devel
package. This comes with the `xml2-config` utility that is needed for
compiling. **This command must be in your path.**
The `scons` command is used for building and must also be in your path.
## Installation
To build the addon, the libxml C library must be installed and the xml2-config helper script must be on your path.
**npm**
npm install libxmljs
**source**
In the root of the source directory, just run `make`. This will
generate the binary `libxmljs.node` in the root of the source folder.
You can copy this file to `~/.node_libraries` or any other directory
in your require path.
**tests**
make test
make test-verbose
while(xmlChunk) {
parser.push(xmlChunk);
}
```

@@ -16,3 +16,3 @@ // js acts as a wrapper to the c++ bindings

// constants
module.exports.version = bindings.version;
module.exports.version = require('./package.json').version;
module.exports.libxml_version = bindings.libxml_version;

@@ -19,0 +19,0 @@ module.exports.libxml_parser_version = bindings.libxml_parser_version;

@@ -1,1 +0,1 @@

module.exports = require('bindings')('libxmljs');
module.exports = require('bindings')('xmljs');

@@ -57,5 +57,5 @@ var bindings = require('./bindings');

/// @return a string representation of the document
Document.prototype.toString = function() {
return this._toString();
}
Document.prototype.toString = function(formatted) {
return this._toString(formatted !== undefined ? formatted : true);
};

@@ -65,3 +65,3 @@ /// @return the document version

return this._version();
}
};

@@ -71,3 +71,3 @@ /// @return the document encoding

return this._encoding(encoding);
}
};

@@ -77,4 +77,22 @@ /// @return whether the XmlDocument is valid

return this._validate(xsd);
}
};
Document.prototype.setDtd = function(name, ext, sys) {
if (!name) {
throw new Error('Must pass in a DTD name');
} else if (typeof name !== 'string') {
throw new Error('Must pass in a valid DTD name');
}
var params = [name];
if (typeof ext !== 'undefined') {
params.push(ext);
}
if (ext && typeof sys !== 'undefined') {
params.push(sys);
}
return this._setDtd.apply(this, params);
};
/// @return array of namespaces in document

@@ -89,7 +107,15 @@ Document.prototype.namespaces = function() {

/// @param string html string to parse
/// @param {encoding:string, baseUrl:string} opts html string to parse
/// @return a Document
module.exports.fromHtml = function(string) {
return bindings.fromHtml(string);
}
module.exports.fromHtml = function(string, opts) {
opts = opts || {};
// if for some reason user did not specify an object for the options
if (typeof(opts) !== 'object') {
throw new Error('fromHtml options must be an object');
}
return bindings.fromHtml(string, opts);
};
/// parse a string into a xml document

@@ -100,3 +126,3 @@ /// @param string xml string to parse

return bindings.fromXml(string);
}
};

@@ -8,3 +8,3 @@ {

"description": "libxml bindings for v8 javascript engine",
"version": "0.6.1",
"version": "0.7.0",
"scripts": {

@@ -11,0 +11,0 @@ "test": "node --expose_gc ./node_modules/nodeunit/bin/nodeunit test"

@@ -29,22 +29,2 @@ # Libxmljs

CDATA Example:
```javascript
var libxml = require('libxmljs');
var doc = libxml.Document();
var elem = doc.node('name1');
var newChild = libxml.Element(doc, 'new-child');
elem.addChild(newChild);
var child1 = elem.node('child1');
var child2 = elem.node('child2', 'second');
var newChild = libxml.Element(doc, 'new-child');
var name2 = elem.node('name2');
name2.addChild(newChild);
child2.cdata('<h1>cdata test</h1>').cdata('<p>It\'s worked</p>').cdata('<hr/>All done');
console.log('Document with CDATA: %s', doc.toString());
```
## Basics

@@ -57,4 +37,6 @@

Check out the wiki [http://github.com/polotek/libxmljs/wiki](http://github.com/polotek/libxmljs/wiki)
Check out the wiki [http://github.com/polotek/libxmljs/wiki](http://github.com/polotek/libxmljs/wiki).
See the [examples](https://github.com/polotek/libxmljs/tree/master/examples) folder.
## Requirements

@@ -72,10 +54,14 @@

npm install libxmljs
```shell
npm install libxmljs
```
**source**
To build with node waf:
To build with node-gyp:
node-gyp configure
node-gyp build
```shell
node-gyp configure
node-gyp build
```

@@ -86,2 +72,4 @@ Alternatively, run `make`.

npm test
```shell
npm test
```
var libxml = require('../index');
module.exports.setDtd = function(assert) {
var doc = libxml.Document();
doc.setDtd("html");
assert.ok(doc);
assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html>\n', doc.toString());
doc.setDtd("html", "bacon", "bacon");
assert.ok(doc);
assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html PUBLIC "bacon" "bacon">\n', doc.toString());
doc.setDtd("html", null);
assert.ok(doc);
assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html>\n', doc.toString());
assert.throws(function() {
doc.setDtd(5);
});
assert.ok(doc);
assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html>\n', doc.toString());
assert.throws(function() {
doc.setDtd();
});
assert.ok(doc);
assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html>\n', doc.toString());
assert.done();
};
module.exports.blank = function(assert) {

@@ -78,3 +102,8 @@ var doc = libxml.Document();

'<?xml version="1.0" encoding="UTF-8"?>',
'<root><child to="wongfoo"><grandchild from="julie numar">with love</grandchild></child><sibling>with content!</sibling></root>',
'<root>',
' <child to="wongfoo">',
' <grandchild from="julie numar">with love</grandchild>',
' </child>',
' <sibling>with content!</sibling>',
'</root>',
''

@@ -120,3 +149,5 @@ ].join("\n");

'<?xml version="1.0" encoding="UTF-8"?>',
'<root><child to="wongfoo"><![CDATA[<p>Bacon</p>]]></child></root>',
'<root>',
' <child to="wongfoo"><![CDATA[<p>Bacon</p>]]></child>',
'</root>',
'' /* Why?!? */

@@ -158,4 +189,4 @@ ].join("\n");

assert.equal(doc2_string, doc1.toString()); //doc1 should be the same as doc2 str
assert.equal(doc1_string, doc2.toString()); //doc2 should be the same as doc1 str
assert.equal(doc2_string, doc1.toString(false)); //doc1 should be the same as doc2 str (raw output)
assert.equal(doc1_string, doc2.toString(false)); //doc2 should be the same as doc1 str (raw output)
assert.done();

@@ -168,7 +199,7 @@ };

var xml_invalid = '<?xml version="1.0"?><commentt>A comment</commentt>';
var xsdDoc = libxml.parseXml(xsd);
var xmlDocValid = libxml.parseXml(xml_valid);
var xmlDocInvalid = libxml.parseXml(xml_invalid);
assert.equal(xmlDocValid.validate(xsdDoc), true);

@@ -175,0 +206,0 @@ assert.equal(xmlDocInvalid.validate(xsdDoc), false);

@@ -12,2 +12,12 @@ var libxml = require('../index');

module.exports.newWithContent = function(assert) {
var doc = libxml.Document();
var elem = libxml.Element(doc, 'name1', 'content && more content <>');
doc.root(elem);
assert.equal('name1', elem.name());
assert.equal('name1', doc.root().name());
assert.equal('content && more content <>', elem.text());
assert.done();
};
module.exports.setters = function(assert) {

@@ -19,4 +29,4 @@ var doc = libxml.Document();

assert.equal('', elem.text());
elem.text('content');
assert.equal('content', elem.text());
elem.text('content && more content <>');
assert.equal('content && more content <>', elem.text());

@@ -23,0 +33,0 @@ // change name

@@ -35,2 +35,35 @@ var fs = require('fs');

// Although libxml defaults to a utf-8 encoding, if not specifically specified
// it will guess the encoding based on meta http-equiv tags available
// This test shows that the "guessed" encoding can be overridden
module.exports.parse_force_encoding = function(assert) {
var filename = __dirname + '/fixtures/parser.euc_jp.html';
function attempt_parse(encoding, opts) {
var str = fs.readFileSync(filename, encoding);
var doc = libxml.parseHtml(str, opts);
assert.equal('html', doc.root().name());
// make sure libxml rewrite the meta charset of this document
// calling toString on the document ensure that it is converted to the
// correct internal format and the new meta tag is replaced
doc.root().toString();
var fixedCharset = doc.find('/html/head/meta/@content')[0].value();
assert.ok( fixedCharset.indexOf(opts.encoding.toUpperCase() ) !== -1);
assert.equal('テスト', doc.get('head/title').text());
assert.equal('テスト', doc.get('body/div').text());
}
// Parse via a string
attempt_parse('utf-8', {encoding: 'utf-8'});
// Parse via a Buffer
attempt_parse(null, {encoding: 'utf-8'});
assert.done();
};
module.exports.parse_synonym = function(assert) {

@@ -37,0 +70,0 @@ assert.strictEqual(libxml.parseHtml, libxml.parseHtmlString);

@@ -1,5 +0,7 @@

var libxml = require('../index');
var package = require('../package')
, libxml = require('../index');
module.exports.constants = function(assert) {
assert.ok(typeof libxml.version == 'string');
assert.equal(package.version, libxml.version);
assert.ok(typeof libxml.libxml_version == 'string');

@@ -6,0 +8,0 @@ assert.ok(typeof libxml.libxml_parser_version == 'string');

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc