@ungap/processing-instruction 0.0.1 → 0.0.3

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 (3) hide show
  1. package/README.md +11 -0
  2. package/index.js +24 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,3 +1,14 @@
1
1
  # @ungap/processing-instruction
2
2
 
3
3
  A polyfill for [ProcessingInstruction attributes](https://github.com/whatwg/dom/pull/1454).
4
+
5
+ ```js
6
+ import patch from 'https://esm.run/@ungap/processing-instruction';
7
+
8
+ const template = document.createElement('template');
9
+ template.innerHTML = 'a<?b c="d" ?>c';
10
+
11
+ [...patch(template.content).childNodes];
12
+
13
+ // [text, b, text]
14
+ ```
package/index.js CHANGED
@@ -50,3 +50,27 @@ for (const [key, value] of [
50
50
  value
51
51
  });
52
52
  }
53
+
54
+ const template = document.createElement('template');
55
+ template.innerHTML = '<?t ?>';
56
+
57
+ export default template.content.firstChild.nodeType === Node.COMMENT_NODE ?
58
+ (dom => {
59
+ const tw = document.createTreeWalker(dom, NodeFilter.SHOW_COMMENT);
60
+ let comments = [], comment;
61
+ while (comment = tw.nextNode()) {
62
+ const { data } = comment;
63
+ if (data.endsWith('?') && /^\?(\S+)/.test(data)) {
64
+ const target = RegExp.$1;
65
+ comments.push(comment, [target, data.slice(1 + target.length, -1).trim()]);
66
+ }
67
+ }
68
+ for (let i = 0; i < comments.length; i += 2) {
69
+ comments[i].replaceWith(
70
+ document.createProcessingInstruction(...comments[i + 1])
71
+ );
72
+ }
73
+ return dom;
74
+ }) :
75
+ (dom => dom)
76
+ ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ungap/processing-instruction",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "A polyfill for ProcessingInstruction attributes",
5
5
  "main": "index.js",
6
6
  "module": "index.js",