Skip to content

Commit

Permalink
chore: migrate to upstream sax
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed May 28, 2024
1 parent 78403d3 commit 845d413
Show file tree
Hide file tree
Showing 4 changed files with 480 additions and 52 deletions.
125 changes: 125 additions & 0 deletions .yarn/patches/sax-npm-1.4.1-503b1923cb.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
diff --git a/lib/sax.js b/lib/sax.js
index 122ad8e5a478339d56fc86d00d21c5d142739f0e..2557c46482ff651026436cc6c7142ebfe68ddfc5 100644
--- a/lib/sax.js
+++ b/lib/sax.js
@@ -1,8 +1,6 @@
;(function (sax) { // wrapper for non-node envs
sax.parser = function (strict, opt) { return new SAXParser(strict, opt) }
sax.SAXParser = SAXParser
- sax.SAXStream = SAXStream
- sax.createStream = createStream

// When we pass the MAX_BUFFER_LENGTH position, start checking for buffer overruns.
// When we check, schedule the next check for MAX_BUFFER_LENGTH - (max(buffer lengths)),
@@ -164,111 +162,6 @@
flush: function () { flushBuffers(this) }
}

- var Stream
- try {
- Stream = require('stream').Stream
- } catch (ex) {
- Stream = function () {}
- }
- if (!Stream) Stream = function () {}
-
- var streamWraps = sax.EVENTS.filter(function (ev) {
- return ev !== 'error' && ev !== 'end'
- })
-
- function createStream (strict, opt) {
- return new SAXStream(strict, opt)
- }
-
- function SAXStream (strict, opt) {
- if (!(this instanceof SAXStream)) {
- return new SAXStream(strict, opt)
- }
-
- Stream.apply(this)
-
- this._parser = new SAXParser(strict, opt)
- this.writable = true
- this.readable = true
-
- var me = this
-
- this._parser.onend = function () {
- me.emit('end')
- }
-
- this._parser.onerror = function (er) {
- me.emit('error', er)
-
- // if didn't throw, then means error was handled.
- // go ahead and clear error, so we can write again.
- me._parser.error = null
- }
-
- this._decoder = null
-
- streamWraps.forEach(function (ev) {
- Object.defineProperty(me, 'on' + ev, {
- get: function () {
- return me._parser['on' + ev]
- },
- set: function (h) {
- if (!h) {
- me.removeAllListeners(ev)
- me._parser['on' + ev] = h
- return h
- }
- me.on(ev, h)
- },
- enumerable: true,
- configurable: false
- })
- })
- }
-
- SAXStream.prototype = Object.create(Stream.prototype, {
- constructor: {
- value: SAXStream
- }
- })
-
- SAXStream.prototype.write = function (data) {
- if (typeof Buffer === 'function' &&
- typeof Buffer.isBuffer === 'function' &&
- Buffer.isBuffer(data)) {
- if (!this._decoder) {
- var SD = require('string_decoder').StringDecoder
- this._decoder = new SD('utf8')
- }
- data = this._decoder.write(data)
- }
-
- this._parser.write(data.toString())
- this.emit('data', data)
- return true
- }
-
- SAXStream.prototype.end = function (chunk) {
- if (chunk && chunk.length) {
- this.write(chunk)
- }
- this._parser.end()
- return true
- }
-
- SAXStream.prototype.on = function (ev, handler) {
- var me = this
- if (!me._parser['on' + ev] && streamWraps.indexOf(ev) !== -1) {
- me._parser['on' + ev] = function () {
- var args = arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)
- args.splice(0, 0, ev)
- me.emit.apply(me, args)
- }
- }
-
- return Stream.prototype.on.call(me, ev, handler)
- }
-
// this really needs to be replaced with character classes.
// XML allows all manner of ridiculous numbers and digits.
var CDATA = '[CDATA['
31 changes: 6 additions & 25 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

// @ts-ignore sax will be replaced with something else later
import SAX from '@trysound/sax';
import SAX from 'sax';
import { textElems } from '../plugins/_collections.js';

class SvgoParserError extends Error {
Expand Down Expand Up @@ -80,6 +80,7 @@ const config = {
lowercase: true,
xmlns: true,
position: true,
unparsedEntities: true,
};

/**
Expand Down Expand Up @@ -114,9 +115,6 @@ export const parseSvg = (data, from) => {
current.children.push(node);
};

/**
* @type {(doctype: string) => void}
*/
sax.ondoctype = (doctype) => {
/**
* @type {XastDoctype}
Expand All @@ -141,9 +139,6 @@ export const parseSvg = (data, from) => {
}
};

/**
* @type {(data: { name: string, body: string }) => void}
*/
sax.onprocessinginstruction = (data) => {
/**
* @type {XastInstruction}
Expand All @@ -156,9 +151,6 @@ export const parseSvg = (data, from) => {
pushToContent(node);
};

/**
* @type {(comment: string) => void}
*/
sax.oncomment = (comment) => {
/**
* @type {XastComment}
Expand All @@ -170,9 +162,6 @@ export const parseSvg = (data, from) => {
pushToContent(node);
};

/**
* @type {(cdata: string) => void}
*/
sax.oncdata = (cdata) => {
/**
* @type {XastCdata}
Expand All @@ -184,9 +173,6 @@ export const parseSvg = (data, from) => {
pushToContent(node);
};

/**
* @type {(data: { name: string, attributes: Record<string, { value: string }>}) => void}
*/
sax.onopentag = (data) => {
/**
* @type {XastElement}
Expand All @@ -205,9 +191,6 @@ export const parseSvg = (data, from) => {
stack.push(element);
};

/**
* @type {(text: string) => void}
*/
sax.ontext = (text) => {
if (current.type === 'element') {
// prevent trimming of meaningful whitespace inside textual tags
Expand Down Expand Up @@ -238,14 +221,12 @@ export const parseSvg = (data, from) => {
current = stack[stack.length - 1];
};

/**
* @type {(e: any) => void}
*/
sax.onerror = (e) => {
const reason = e.message.split('\n')[0];
const error = new SvgoParserError(
e.reason,
e.line + 1,
e.column,
reason,
sax.line + 1,
sax.column,
data,
from,
);
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@
]
},
"dependencies": {
"@trysound/sax": "0.2.0",
"commander": "^7.2.0",
"css-select": "^5.1.0",
"css-tree": "^2.3.1",
"css-what": "^6.1.0",
"csso": "^5.0.5",
"picocolors": "^1.0.0"
"picocolors": "^1.0.0",
"sax": "^1.4.1"
},
"devDependencies": {
"@eslint/js": "^9.3.0",
Expand All @@ -118,10 +118,12 @@
"@types/csso": "^5.0.4",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.11",
"@types/sax": "^1.2.7",
"cross-env": "^7.0.3",
"eslint": "^9.3.0",
"globals": "^14.0.0",
"jest": "^29.7.0",
"patch-package": "^8.0.0",
"pixelmatch": "^5.3.0",
"playwright": "^1.44.0",
"pngjs": "^7.0.0",
Expand All @@ -130,5 +132,8 @@
"rollup": "^4.17.2",
"tar-stream": "^3.1.7",
"typescript": "^5.4.5"
},
"resolutions": {
"sax@^1.4.1": "patch:sax@npm%3A1.4.1#./.yarn/patches/sax-npm-1.4.1-503b1923cb.patch"
}
}
Loading

0 comments on commit 845d413

Please sign in to comment.