svg-path-interpolator 0.3.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,304 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- class SaxEventType {
6
- // 1
7
- static Text = 0b1;
8
- // 2
9
- static ProcessingInstruction = 0b10;
10
- // 4
11
- static SGMLDeclaration = 0b100;
12
- // 8
13
- static Doctype = 0b1000;
14
- // 16
15
- static Comment = 0b10000;
16
- // 32
17
- static OpenTagStart = 0b100000;
18
- // 64
19
- static Attribute = 0b1000000;
20
- // 128
21
- static OpenTag = 0b10000000;
22
- // 256
23
- static CloseTag = 0b100000000;
24
- // 512
25
- static Cdata = 0b1000000000;
26
- }
27
- class Reader {
28
- data;
29
- cache = {};
30
- ptr;
31
- constructor(data, ptr = 0) {
32
- this.data = data;
33
- this.ptr = ptr;
34
- }
35
- }
36
- class Position {
37
- line;
38
- character;
39
- constructor(line, character) {
40
- this.line = line;
41
- this.character = character;
42
- }
43
- }
44
- exports.AttributeType = void 0;
45
- (function (AttributeType) {
46
- AttributeType[AttributeType["Normal"] = 0] = "Normal";
47
- AttributeType[AttributeType["JSX"] = 1] = "JSX";
48
- })(exports.AttributeType || (exports.AttributeType = {}));
49
- class Attribute extends Reader {
50
- type;
51
- name;
52
- value;
53
- constructor(buffer, ptr = 0) {
54
- super(buffer, ptr);
55
- this.type = buffer[ptr];
56
- ptr += 1;
57
- const len = readU32(buffer, ptr);
58
- ptr += 4;
59
- this.name = new Text(buffer, ptr);
60
- ptr += len;
61
- this.value = new Text(buffer, ptr);
62
- }
63
- toJSON() {
64
- const { name, value, type } = this;
65
- return { name, value, type };
66
- }
67
- toString() {
68
- const { name, value } = this;
69
- return `${name}="${value}"`;
70
- }
71
- }
72
- class ProcInst extends Reader {
73
- target;
74
- content;
75
- constructor(buffer, ptr = 0) {
76
- super(buffer, ptr);
77
- ptr += 16;
78
- const len = readU32(buffer, ptr);
79
- ptr += 4;
80
- this.target = new Text(buffer, ptr);
81
- ptr += len;
82
- this.content = new Text(buffer, ptr);
83
- }
84
- get start() {
85
- return this.cache.start || (this.cache.start = readPosition(this.data, this.ptr));
86
- }
87
- get end() {
88
- return this.cache.end || (this.cache.end = readPosition(this.data, this.ptr + 8));
89
- }
90
- toJSON() {
91
- const { start, end, target, content } = this;
92
- return { start, end, target, content };
93
- }
94
- toString() {
95
- const { target, content } = this;
96
- return `<? ${target} ${content} ?>`;
97
- }
98
- }
99
- class Text extends Reader {
100
- get start() {
101
- return this.cache.start || (this.cache.start = readPosition(this.data, this.ptr));
102
- }
103
- get end() {
104
- return this.cache.end || (this.cache.end = readPosition(this.data, this.ptr + 8));
105
- }
106
- get value() {
107
- if (this.cache.value) {
108
- return this.cache.value;
109
- }
110
- const valueLen = readU32(this.data, this.ptr + 16);
111
- return (this.cache.value = readString(this.data, this.ptr + 20, valueLen));
112
- }
113
- toJSON() {
114
- const { start, end, value } = this;
115
- return { start, end, value };
116
- }
117
- toString() {
118
- return this.value;
119
- }
120
- }
121
- class Tag extends Reader {
122
- get openStart() {
123
- return this.cache.openStart || (this.cache.openStart = readPosition(this.data, this.ptr + 8));
124
- }
125
- get openEnd() {
126
- return this.cache.openEnd || (this.cache.openEnd = readPosition(this.data, this.ptr + 16));
127
- }
128
- get closeStart() {
129
- return this.cache.closeStart || (this.cache.closeStart = readPosition(this.data, this.ptr + 24));
130
- }
131
- get closeEnd() {
132
- return this.cache.closeEnd || (this.cache.closeEnd = readPosition(this.data, this.ptr + 32));
133
- }
134
- get selfClosing() {
135
- return !!this.data[this.ptr + 40];
136
- }
137
- get name() {
138
- if (this.cache.name) {
139
- return this.cache.name;
140
- }
141
- const nameLen = readU32(this.data, this.ptr + 41);
142
- return (this.cache.name = readString(this.data, this.ptr + 45, nameLen));
143
- }
144
- get attributes() {
145
- if (this.cache.attributes) {
146
- return this.cache.attributes;
147
- }
148
- // starting location of the attribute block
149
- let ptr = readU32(this.data, this.ptr);
150
- const numAttrs = readU32(this.data, ptr);
151
- ptr += 4;
152
- const attributes = [];
153
- for (let i = 0; i < numAttrs; i++) {
154
- const attrLen = readU32(this.data, ptr);
155
- ptr += 4;
156
- attributes[i] = new Attribute(this.data, ptr);
157
- ptr += attrLen;
158
- }
159
- return (this.cache.attributes = attributes);
160
- }
161
- get textNodes() {
162
- if (this.cache.textNodes) {
163
- return this.cache.textNodes;
164
- }
165
- // starting location of the text nodes block
166
- let ptr = readU32(this.data, this.ptr + 4);
167
- const numTextNodes = readU32(this.data, ptr);
168
- const textNodes = [];
169
- ptr += 4;
170
- for (let i = 0; i < numTextNodes; i++) {
171
- const textLen = readU32(this.data, ptr);
172
- ptr += 4;
173
- textNodes[i] = new Text(this.data, ptr);
174
- ptr += textLen;
175
- }
176
- return (this.cache.textNodes = textNodes);
177
- }
178
- toJSON() {
179
- const { openStart, openEnd, closeStart, closeEnd, name, attributes, textNodes, selfClosing } = this;
180
- return { openStart, openEnd, closeStart, closeEnd, name, attributes, textNodes, selfClosing };
181
- }
182
- get value() {
183
- return this.name;
184
- }
185
- }
186
- class SAXParser {
187
- static textDecoder; // Web only
188
- events;
189
- wasmSaxParser;
190
- eventHandler;
191
- options;
192
- writeBuffer;
193
- constructor(events = 0, options = { highWaterMark: 32 * 1024 }) {
194
- this.options = options;
195
- const self = this;
196
- Object.defineProperties(this, {
197
- events: {
198
- get: () => ~~events,
199
- set: (value) => {
200
- events = ~~value;
201
- if (self.wasmSaxParser) {
202
- self.wasmSaxParser.parser(events);
203
- }
204
- }, configurable: false, enumerable: true
205
- }
206
- });
207
- }
208
- write(chunk) {
209
- if (!this.wasmSaxParser) {
210
- return;
211
- }
212
- const { write, memory } = this.wasmSaxParser;
213
- // Allocations within the WASM process
214
- // invalidate reference to the memory buffer.
215
- // We check for this and create a new Uint8Array
216
- // with the new memory buffer reference if needed.
217
- // **NOTE** These allocations can slow down parsing
218
- // if they become excessive. Consider adjusting the
219
- // highWaterMark in the options up or down to find the optimal
220
- // memory allocation to prevent too many new Uint8Array instances.
221
- if (!this.writeBuffer || this.writeBuffer.buffer !== memory.buffer) {
222
- this.writeBuffer = new Uint8Array(memory.buffer);
223
- }
224
- this.writeBuffer.set(chunk, 0);
225
- write(0, chunk.byteLength);
226
- }
227
- end() {
228
- this.writeBuffer = undefined;
229
- this.wasmSaxParser?.end();
230
- }
231
- async prepareWasm(saxWasm) {
232
- const result = await WebAssembly.instantiate(saxWasm, {
233
- env: {
234
- memoryBase: 0,
235
- tableBase: 0,
236
- memory: new WebAssembly.Memory({ initial: 10 }),
237
- table: new WebAssembly.Table({ initial: 1, element: 'anyfunc' }),
238
- event_listener: this.eventTrap
239
- }
240
- });
241
- if (result && typeof this.events === 'number') {
242
- const { parser } = this.wasmSaxParser = result.instance.exports;
243
- parser(this.events);
244
- return true;
245
- }
246
- throw new Error(`Failed to instantiate the parser.`);
247
- }
248
- eventTrap = (event, ptr, len) => {
249
- if (!this.wasmSaxParser) {
250
- return;
251
- }
252
- const uint8array = new Uint8Array(this.wasmSaxParser.memory.buffer, ptr, len).slice();
253
- let detail;
254
- switch (event) {
255
- case SaxEventType.Attribute:
256
- detail = new Attribute(uint8array);
257
- break;
258
- case SaxEventType.ProcessingInstruction:
259
- detail = new ProcInst(uint8array);
260
- break;
261
- case SaxEventType.OpenTag:
262
- case SaxEventType.CloseTag:
263
- case SaxEventType.OpenTagStart:
264
- detail = new Tag(uint8array);
265
- break;
266
- case SaxEventType.Text:
267
- case SaxEventType.Cdata:
268
- case SaxEventType.Comment:
269
- case SaxEventType.Doctype:
270
- case SaxEventType.SGMLDeclaration:
271
- detail = new Text(uint8array);
272
- break;
273
- default:
274
- throw new Error('No reader for this event type');
275
- }
276
- if (this.eventHandler) {
277
- this.eventHandler(event, detail);
278
- }
279
- };
280
- }
281
- const readString = (data, offset, length) => {
282
- // Node
283
- if (globalThis.hasOwnProperty('Buffer')) {
284
- return Buffer.from(data.buffer, data.byteOffset + offset, length).toString();
285
- }
286
- // Web
287
- return (SAXParser.textDecoder || (SAXParser.textDecoder = new TextDecoder()))
288
- .decode(data.subarray(offset, offset + length));
289
- };
290
- const readU32 = (uint8Array, ptr) => (uint8Array[ptr + 3] << 24) | (uint8Array[ptr + 2] << 16) | (uint8Array[ptr + 1] << 8) | uint8Array[ptr];
291
- const readPosition = (uint8Array, ptr = 0) => {
292
- const line = readU32(uint8Array, ptr);
293
- const character = readU32(uint8Array, ptr + 4);
294
- return new Position(line, character);
295
- };
296
-
297
- exports.Attribute = Attribute;
298
- exports.Position = Position;
299
- exports.ProcInst = ProcInst;
300
- exports.Reader = Reader;
301
- exports.SAXParser = SAXParser;
302
- exports.SaxEventType = SaxEventType;
303
- exports.Tag = Tag;
304
- exports.Text = Text;
@@ -1,293 +0,0 @@
1
- class SaxEventType {
2
- // 1
3
- static Text = 0b1;
4
- // 2
5
- static ProcessingInstruction = 0b10;
6
- // 4
7
- static SGMLDeclaration = 0b100;
8
- // 8
9
- static Doctype = 0b1000;
10
- // 16
11
- static Comment = 0b10000;
12
- // 32
13
- static OpenTagStart = 0b100000;
14
- // 64
15
- static Attribute = 0b1000000;
16
- // 128
17
- static OpenTag = 0b10000000;
18
- // 256
19
- static CloseTag = 0b100000000;
20
- // 512
21
- static Cdata = 0b1000000000;
22
- }
23
- class Reader {
24
- data;
25
- cache = {};
26
- ptr;
27
- constructor(data, ptr = 0) {
28
- this.data = data;
29
- this.ptr = ptr;
30
- }
31
- }
32
- class Position {
33
- line;
34
- character;
35
- constructor(line, character) {
36
- this.line = line;
37
- this.character = character;
38
- }
39
- }
40
- var AttributeType;
41
- (function (AttributeType) {
42
- AttributeType[AttributeType["Normal"] = 0] = "Normal";
43
- AttributeType[AttributeType["JSX"] = 1] = "JSX";
44
- })(AttributeType || (AttributeType = {}));
45
- class Attribute extends Reader {
46
- type;
47
- name;
48
- value;
49
- constructor(buffer, ptr = 0) {
50
- super(buffer, ptr);
51
- this.type = buffer[ptr];
52
- ptr += 1;
53
- const len = readU32(buffer, ptr);
54
- ptr += 4;
55
- this.name = new Text(buffer, ptr);
56
- ptr += len;
57
- this.value = new Text(buffer, ptr);
58
- }
59
- toJSON() {
60
- const { name, value, type } = this;
61
- return { name, value, type };
62
- }
63
- toString() {
64
- const { name, value } = this;
65
- return `${name}="${value}"`;
66
- }
67
- }
68
- class ProcInst extends Reader {
69
- target;
70
- content;
71
- constructor(buffer, ptr = 0) {
72
- super(buffer, ptr);
73
- ptr += 16;
74
- const len = readU32(buffer, ptr);
75
- ptr += 4;
76
- this.target = new Text(buffer, ptr);
77
- ptr += len;
78
- this.content = new Text(buffer, ptr);
79
- }
80
- get start() {
81
- return this.cache.start || (this.cache.start = readPosition(this.data, this.ptr));
82
- }
83
- get end() {
84
- return this.cache.end || (this.cache.end = readPosition(this.data, this.ptr + 8));
85
- }
86
- toJSON() {
87
- const { start, end, target, content } = this;
88
- return { start, end, target, content };
89
- }
90
- toString() {
91
- const { target, content } = this;
92
- return `<? ${target} ${content} ?>`;
93
- }
94
- }
95
- class Text extends Reader {
96
- get start() {
97
- return this.cache.start || (this.cache.start = readPosition(this.data, this.ptr));
98
- }
99
- get end() {
100
- return this.cache.end || (this.cache.end = readPosition(this.data, this.ptr + 8));
101
- }
102
- get value() {
103
- if (this.cache.value) {
104
- return this.cache.value;
105
- }
106
- const valueLen = readU32(this.data, this.ptr + 16);
107
- return (this.cache.value = readString(this.data, this.ptr + 20, valueLen));
108
- }
109
- toJSON() {
110
- const { start, end, value } = this;
111
- return { start, end, value };
112
- }
113
- toString() {
114
- return this.value;
115
- }
116
- }
117
- class Tag extends Reader {
118
- get openStart() {
119
- return this.cache.openStart || (this.cache.openStart = readPosition(this.data, this.ptr + 8));
120
- }
121
- get openEnd() {
122
- return this.cache.openEnd || (this.cache.openEnd = readPosition(this.data, this.ptr + 16));
123
- }
124
- get closeStart() {
125
- return this.cache.closeStart || (this.cache.closeStart = readPosition(this.data, this.ptr + 24));
126
- }
127
- get closeEnd() {
128
- return this.cache.closeEnd || (this.cache.closeEnd = readPosition(this.data, this.ptr + 32));
129
- }
130
- get selfClosing() {
131
- return !!this.data[this.ptr + 40];
132
- }
133
- get name() {
134
- if (this.cache.name) {
135
- return this.cache.name;
136
- }
137
- const nameLen = readU32(this.data, this.ptr + 41);
138
- return (this.cache.name = readString(this.data, this.ptr + 45, nameLen));
139
- }
140
- get attributes() {
141
- if (this.cache.attributes) {
142
- return this.cache.attributes;
143
- }
144
- // starting location of the attribute block
145
- let ptr = readU32(this.data, this.ptr);
146
- const numAttrs = readU32(this.data, ptr);
147
- ptr += 4;
148
- const attributes = [];
149
- for (let i = 0; i < numAttrs; i++) {
150
- const attrLen = readU32(this.data, ptr);
151
- ptr += 4;
152
- attributes[i] = new Attribute(this.data, ptr);
153
- ptr += attrLen;
154
- }
155
- return (this.cache.attributes = attributes);
156
- }
157
- get textNodes() {
158
- if (this.cache.textNodes) {
159
- return this.cache.textNodes;
160
- }
161
- // starting location of the text nodes block
162
- let ptr = readU32(this.data, this.ptr + 4);
163
- const numTextNodes = readU32(this.data, ptr);
164
- const textNodes = [];
165
- ptr += 4;
166
- for (let i = 0; i < numTextNodes; i++) {
167
- const textLen = readU32(this.data, ptr);
168
- ptr += 4;
169
- textNodes[i] = new Text(this.data, ptr);
170
- ptr += textLen;
171
- }
172
- return (this.cache.textNodes = textNodes);
173
- }
174
- toJSON() {
175
- const { openStart, openEnd, closeStart, closeEnd, name, attributes, textNodes, selfClosing } = this;
176
- return { openStart, openEnd, closeStart, closeEnd, name, attributes, textNodes, selfClosing };
177
- }
178
- get value() {
179
- return this.name;
180
- }
181
- }
182
- class SAXParser {
183
- static textDecoder; // Web only
184
- events;
185
- wasmSaxParser;
186
- eventHandler;
187
- options;
188
- writeBuffer;
189
- constructor(events = 0, options = { highWaterMark: 32 * 1024 }) {
190
- this.options = options;
191
- const self = this;
192
- Object.defineProperties(this, {
193
- events: {
194
- get: () => ~~events,
195
- set: (value) => {
196
- events = ~~value;
197
- if (self.wasmSaxParser) {
198
- self.wasmSaxParser.parser(events);
199
- }
200
- }, configurable: false, enumerable: true
201
- }
202
- });
203
- }
204
- write(chunk) {
205
- if (!this.wasmSaxParser) {
206
- return;
207
- }
208
- const { write, memory } = this.wasmSaxParser;
209
- // Allocations within the WASM process
210
- // invalidate reference to the memory buffer.
211
- // We check for this and create a new Uint8Array
212
- // with the new memory buffer reference if needed.
213
- // **NOTE** These allocations can slow down parsing
214
- // if they become excessive. Consider adjusting the
215
- // highWaterMark in the options up or down to find the optimal
216
- // memory allocation to prevent too many new Uint8Array instances.
217
- if (!this.writeBuffer || this.writeBuffer.buffer !== memory.buffer) {
218
- this.writeBuffer = new Uint8Array(memory.buffer);
219
- }
220
- this.writeBuffer.set(chunk, 0);
221
- write(0, chunk.byteLength);
222
- }
223
- end() {
224
- this.writeBuffer = undefined;
225
- this.wasmSaxParser?.end();
226
- }
227
- async prepareWasm(saxWasm) {
228
- const result = await WebAssembly.instantiate(saxWasm, {
229
- env: {
230
- memoryBase: 0,
231
- tableBase: 0,
232
- memory: new WebAssembly.Memory({ initial: 10 }),
233
- table: new WebAssembly.Table({ initial: 1, element: 'anyfunc' }),
234
- event_listener: this.eventTrap
235
- }
236
- });
237
- if (result && typeof this.events === 'number') {
238
- const { parser } = this.wasmSaxParser = result.instance.exports;
239
- parser(this.events);
240
- return true;
241
- }
242
- throw new Error(`Failed to instantiate the parser.`);
243
- }
244
- eventTrap = (event, ptr, len) => {
245
- if (!this.wasmSaxParser) {
246
- return;
247
- }
248
- const uint8array = new Uint8Array(this.wasmSaxParser.memory.buffer, ptr, len).slice();
249
- let detail;
250
- switch (event) {
251
- case SaxEventType.Attribute:
252
- detail = new Attribute(uint8array);
253
- break;
254
- case SaxEventType.ProcessingInstruction:
255
- detail = new ProcInst(uint8array);
256
- break;
257
- case SaxEventType.OpenTag:
258
- case SaxEventType.CloseTag:
259
- case SaxEventType.OpenTagStart:
260
- detail = new Tag(uint8array);
261
- break;
262
- case SaxEventType.Text:
263
- case SaxEventType.Cdata:
264
- case SaxEventType.Comment:
265
- case SaxEventType.Doctype:
266
- case SaxEventType.SGMLDeclaration:
267
- detail = new Text(uint8array);
268
- break;
269
- default:
270
- throw new Error('No reader for this event type');
271
- }
272
- if (this.eventHandler) {
273
- this.eventHandler(event, detail);
274
- }
275
- };
276
- }
277
- const readString = (data, offset, length) => {
278
- // Node
279
- if (globalThis.hasOwnProperty('Buffer')) {
280
- return Buffer.from(data.buffer, data.byteOffset + offset, length).toString();
281
- }
282
- // Web
283
- return (SAXParser.textDecoder || (SAXParser.textDecoder = new TextDecoder()))
284
- .decode(data.subarray(offset, offset + length));
285
- };
286
- const readU32 = (uint8Array, ptr) => (uint8Array[ptr + 3] << 24) | (uint8Array[ptr + 2] << 16) | (uint8Array[ptr + 1] << 8) | uint8Array[ptr];
287
- const readPosition = (uint8Array, ptr = 0) => {
288
- const line = readU32(uint8Array, ptr);
289
- const character = readU32(uint8Array, ptr + 4);
290
- return new Position(line, character);
291
- };
292
-
293
- export { Attribute, AttributeType, Position, ProcInst, Reader, SAXParser, SaxEventType, Tag, Text };
Binary file