forked from isaacs/sax-js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51fdfe7
commit 388a75a
Showing
68 changed files
with
6,347 additions
and
3,878 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "./node_modules/gts/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
11.10.0 | ||
12.19.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
...require('gts/.prettierrc.json') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
var fs = require('fs'), | ||
/* eslint-disable prefer-const */ | ||
/* eslint-disable no-unused-vars */ | ||
let fs = require('fs'), | ||
util = require('util'), | ||
path = require('path'), | ||
xml = fs.readFileSync(path.join(__dirname, 'test.xml'), 'utf8'), | ||
sax = require('../build/src/sax.js'), | ||
strict = new sax.SAXParser(true), | ||
loose = new sax.SAXParser(false, {trim: true}), | ||
inspector = function (ev) { return function (data) { | ||
console.error('%s %s %j', this.line + ':' + this.column, ev, data) | ||
}} | ||
inspector = function (ev) { | ||
return function (data) { | ||
console.error('%s %s %j', this.line + ':' + this.column, ev, data); | ||
}; | ||
}; | ||
|
||
sax.EVENTS.forEach(function (ev) { | ||
loose['on' + ev] = inspector(ev) | ||
}) | ||
loose.onend = function () { | ||
console.error('end') | ||
console.error(loose) | ||
} | ||
|
||
// do this in random bits at a time to verify that it works. | ||
(function () { | ||
if (xml) { | ||
var c = Math.ceil(Math.random() * 1000) | ||
loose.write(xml.substr(0, c)) | ||
xml = xml.substr(c) | ||
process.nextTick(arguments.callee) | ||
} else loose.close() | ||
})() | ||
sax.EVENTS.forEach(ev => { | ||
loose['on' + ev] = inspector(ev); | ||
}); | ||
loose.onend = (function () { | ||
console.error('end'); | ||
console.error(loose); | ||
})( | ||
// do this in random bits at a time to verify that it works. | ||
function () { | ||
if (xml) { | ||
const c = Math.ceil(Math.random() * 1000); | ||
loose.write(xml.substr(0, c)); | ||
xml = xml.substr(c); | ||
process.nextTick(arguments.callee); | ||
} else loose.close(); | ||
} | ||
)(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,61 @@ | ||
// pull out /GeneralSearchResponse/categories/category/items/product tags | ||
// the rest we don't care about. | ||
|
||
var sax = require('../src/sax.js') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var xmlFile = path.resolve(__dirname, 'shopping.xml') | ||
var util = require('util') | ||
var http = require('http') | ||
|
||
fs.readFile(xmlFile, function (er, d) { | ||
http.createServer(function (req, res) { | ||
if (er) throw er | ||
var xmlstr = d.toString('utf8') | ||
|
||
var parser = new sax.SAXParser(true) | ||
var products = [] | ||
var product = null | ||
var currentTag = null | ||
|
||
parser.onclosetag = function (tagName) { | ||
if (tagName === 'product') { | ||
products.push(product) | ||
currentTag = product = null | ||
return | ||
} | ||
if (currentTag && currentTag.parent) { | ||
var p = currentTag.parent | ||
delete currentTag.parent | ||
currentTag = p | ||
} | ||
} | ||
|
||
parser.onopentag = function (tag) { | ||
if (tag.name !== 'product' && !product) return | ||
if (tag.name === 'product') { | ||
product = tag | ||
} | ||
tag.parent = currentTag | ||
tag.children = [] | ||
tag.parent && tag.parent.children.push(tag) | ||
currentTag = tag | ||
} | ||
|
||
parser.ontext = function (text) { | ||
if (currentTag) currentTag.children.push(text) | ||
} | ||
|
||
parser.onend = function () { | ||
var out = util.inspect(products, false, 3, true) | ||
res.writeHead(200, {'content-type': 'application/json'}) | ||
res.end('{"ok":true}') | ||
// res.end(JSON.stringify(products)) | ||
} | ||
|
||
parser.write(xmlstr).end() | ||
}).listen(1337) | ||
}) | ||
const sax = require('../src/sax.ts'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const xmlFile = path.resolve(__dirname, 'shopping.xml'); | ||
const util = require('util'); | ||
const http = require('http'); | ||
|
||
fs.readFile(xmlFile, (er, d) => { | ||
http | ||
.createServer((req, res) => { | ||
if (er) throw er; | ||
const xmlstr = d.toString('utf8'); | ||
|
||
const parser = new sax.SAXParser(true); | ||
const products = []; | ||
let product = null; | ||
let currentTag = null; | ||
|
||
parser.onclosetag = function (tagName) { | ||
if (tagName === 'product') { | ||
products.push(product); | ||
currentTag = product = null; | ||
return; | ||
} | ||
if (currentTag && currentTag.parent) { | ||
const p = currentTag.parent; | ||
delete currentTag.parent; | ||
currentTag = p; | ||
} | ||
}; | ||
|
||
parser.onopentag = function (tag) { | ||
if (tag.name !== 'product' && !product) return; | ||
if (tag.name === 'product') { | ||
product = tag; | ||
} | ||
tag.parent = currentTag; | ||
tag.children = []; | ||
tag.parent && tag.parent.children.push(tag); | ||
currentTag = tag; | ||
}; | ||
|
||
parser.ontext = function (text) { | ||
if (currentTag) currentTag.children.push(text); | ||
}; | ||
|
||
parser.onend = function () { | ||
// eslint-disable-next-line no-unused-vars | ||
const out = util.inspect(products, false, 3, true); | ||
res.writeHead(200, {'content-type': 'application/json'}); | ||
res.end('{"ok":true}'); | ||
// res.end(JSON.stringify(products)) | ||
}; | ||
|
||
parser.write(xmlstr).end(); | ||
}) | ||
.listen(1337); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
require('http').createServer(function (req, res) { | ||
res.writeHead(200, {'content-type': 'application/json'}) | ||
res.end(JSON.stringify({ok: true})) | ||
}).listen(1337) | ||
require('http') | ||
.createServer((req, res) => { | ||
res.writeHead(200, {'content-type': 'application/json'}); | ||
res.end(JSON.stringify({ok: true})); | ||
}) | ||
.listen(1337); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,76 @@ | ||
var sax = require('../build/src/sax.js'), | ||
const sax = require('../build/src/sax.js'), | ||
printer = sax.createStream(false, {lowercasetags: true, trim: true}), | ||
fs = require('fs') | ||
fs = require('fs'); | ||
|
||
function entity (str) { | ||
return str.replace('"', '"') | ||
function entity(str) { | ||
return str.replace('"', '"'); | ||
} | ||
|
||
printer.tabstop = 2 | ||
printer.level = 0 | ||
printer.tabstop = 2; | ||
printer.level = 0; | ||
printer.indent = function () { | ||
print('\n') | ||
for (var i = this.level; i > 0; i--) { | ||
for (var j = this.tabstop; j > 0; j--) { | ||
print(' ') | ||
print('\n'); | ||
for (let i = this.level; i > 0; i--) { | ||
for (let j = this.tabstop; j > 0; j--) { | ||
print(' '); | ||
} | ||
} | ||
} | ||
}; | ||
printer.on('opentag', function (tag) { | ||
this.indent() | ||
this.level++ | ||
print('<' + tag.name) | ||
for (var i in tag.attributes) { | ||
print(' ' + i + '="' + entity(tag.attributes[i]) + '"') | ||
this.indent(); | ||
this.level++; | ||
print('<' + tag.name); | ||
for (const i in tag.attributes) { | ||
print(' ' + i + '="' + entity(tag.attributes[i]) + '"'); | ||
} | ||
print('>') | ||
}) | ||
print('>'); | ||
}); | ||
|
||
printer.on('text', ontext) | ||
printer.on('doctype', ontext) | ||
function ontext (text) { | ||
this.indent() | ||
print(text) | ||
printer.on('text', ontext); | ||
printer.on('doctype', ontext); | ||
function ontext(text) { | ||
this.indent(); | ||
print(text); | ||
} | ||
|
||
printer.on('closetag', function (tag) { | ||
this.level-- | ||
this.indent() | ||
print('</' + tag + '>') | ||
}) | ||
this.level--; | ||
this.indent(); | ||
print('</' + tag + '>'); | ||
}); | ||
|
||
printer.on('cdata', function (data) { | ||
this.indent() | ||
print('<![CDATA[' + data + ']]>') | ||
}) | ||
this.indent(); | ||
print('<![CDATA[' + data + ']]>'); | ||
}); | ||
|
||
printer.on('comment', function (comment) { | ||
this.indent() | ||
print('<!--' + comment + '-->') | ||
}) | ||
this.indent(); | ||
print('<!--' + comment + '-->'); | ||
}); | ||
|
||
printer.on('error', function (error) { | ||
console.error(error) | ||
throw error | ||
}) | ||
printer.on('error', error => { | ||
console.error(error); | ||
throw error; | ||
}); | ||
|
||
if (!process.argv[2]) { | ||
throw new Error('Please provide an xml file to prettify\n' + | ||
'TODO: read from stdin or take a file') | ||
throw new Error( | ||
'Please provide an xml file to prettify\n' + | ||
'TODO: read from stdin or take a file' | ||
); | ||
} | ||
var xmlfile = require('path').join(process.cwd(), process.argv[2]) | ||
var fstr = fs.createReadStream(xmlfile, { encoding: 'utf8' }) | ||
const xmlfile = require('path').join(process.cwd(), process.argv[2]); | ||
const fstr = fs.createReadStream(xmlfile, {encoding: 'utf8'}); | ||
|
||
function print (c) { | ||
function print(c) { | ||
if (!process.stdout.write(c)) { | ||
fstr.pause() | ||
fstr.pause(); | ||
} | ||
} | ||
|
||
process.stdout.on('drain', function () { | ||
fstr.resume() | ||
}) | ||
process.stdout.on('drain', () => { | ||
fstr.resume(); | ||
}); | ||
|
||
fstr.pipe(printer) | ||
fstr.pipe(printer); |
Oops, something went wrong.