@ungap/processing-instruction 0.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.
Files changed (2) hide show
  1. package/index.js +53 -0
  2. package/package.json +19 -0
package/index.js ADDED
@@ -0,0 +1,53 @@
1
+ // <?t a="b" x="yy"?>
2
+
3
+ const { defineProperty } = Object;
4
+ const { prototype } = ProcessingInstruction;
5
+ const attributes = /(\S+?)=("|')([^\2]*?)\2/g;
6
+ const attribute = name => new RegExp(`(\\s*)\\b${name}\\b=("|')([^\\2]*?)\\2`);
7
+
8
+ const es = /[&<>"']/g;
9
+ const cape = c => {
10
+ if (c === '&') return '&amp;';
11
+ if (c === '<') return '&lt;';
12
+ if (c === '>') return '&gt;';
13
+ if (c === '"') return '&quot;';
14
+ return '&#39;';
15
+ };
16
+
17
+ for (const [key, value] of [
18
+ ['hasAttribute', function hasAttribute(name) {
19
+ return attribute(name).test(this.data);
20
+ }],
21
+ ['hasAttributes', function hasAttributes() {
22
+ return !![...this.data.matchAll(attributes)].length;
23
+ }],
24
+ ['getAttribute', function getAttribute(name) {
25
+ return attribute(name).test(this.data) ? RegExp.$3 : null;
26
+ }],
27
+ ['getAttributeNames', function getAttributeNames() {
28
+ return [...this.data.matchAll(attributes)].map(([_, name]) => name);
29
+ }],
30
+ ['removeAttribute', function removeAttribute(name) {
31
+ this.data = this.data.replace(attribute(name), '').trim();
32
+ }],
33
+ ['setAttribute', function setAttribute(name, value) {
34
+ const { data } = this;
35
+ value = String(value).replace(es, cape);
36
+ if (attribute(name).test(data)) {
37
+ const { $1, $2, $3 } = RegExp, prefix = $1 + name + '=' + $2;
38
+ this.data = data.replace(prefix + $3, prefix + value);
39
+ }
40
+ else this.data = data + ` ${name}="${value}"`;
41
+ }],
42
+ ['toggleAttribute', function toggleAttribute(name, force) {
43
+ if (force === undefined) force = !this.hasAttribute(name);
44
+ if (force) this.setAttribute(name, '');
45
+ else this.removeAttribute(name);
46
+ }],
47
+ ]) {
48
+ if (!(key in prototype)) defineProperty(prototype, key, {
49
+ configurable: true,
50
+ writable: true,
51
+ value
52
+ });
53
+ }
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@ungap/processing-instruction",
3
+ "version": "0.0.0",
4
+ "description": "Polyfill for ProcessingInstruction",
5
+ "main": "index.js",
6
+ "module": "index.js",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/ungap/processing-instruction.git"
10
+ },
11
+ "keywords": ["ProcessingInstruction", "polyfill", "shim"],
12
+ "author": "Andrea Giammarchi",
13
+ "license": "MIT",
14
+ "type": "module",
15
+ "bugs": {
16
+ "url": "https://github.com/ungap/processing-instruction/issues"
17
+ },
18
+ "homepage": "https://github.com/ungap/processing-instruction#readme"
19
+ }