@imwz/wp-pattern-sentinel 1.0.2 → 1.0.4
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.
- package/package.json +5 -1
- package/src/editor.js +14 -7
- package/src/validation.js +11 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imwz/wp-pattern-sentinel",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Browser-based WordPress block pattern validator using Playwright",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -25,6 +25,10 @@
|
|
|
25
25
|
"validation"
|
|
26
26
|
],
|
|
27
27
|
"license": "MIT",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/imagewize/wp-pattern-sentinel"
|
|
31
|
+
},
|
|
28
32
|
"dependencies": {
|
|
29
33
|
"js-yaml": "^4.1.0",
|
|
30
34
|
"p-queue": "^8.0.1",
|
package/src/editor.js
CHANGED
|
@@ -29,10 +29,11 @@ function stripPhpForValidation(content) {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
* Strip the PHP file header (opening tag
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* Returns null if no
|
|
32
|
+
* Strip the PHP file header (opening tag, docblock, and any header-only PHP
|
|
33
|
+
* such as a direct-access guard) and return the raw block markup. Inline PHP
|
|
34
|
+
* expressions within the block markup are replaced with static values so the
|
|
35
|
+
* editor receives valid content for round-trip validation. Returns null if no
|
|
36
|
+
* block comment is found.
|
|
36
37
|
*
|
|
37
38
|
* WordPress pattern files look like:
|
|
38
39
|
* <?php
|
|
@@ -40,14 +41,20 @@ function stripPhpForValidation(content) {
|
|
|
40
41
|
* * Title: My Pattern
|
|
41
42
|
* * ...
|
|
42
43
|
* *\/
|
|
44
|
+
*
|
|
45
|
+
* if ( ! defined( 'ABSPATH' ) ) {
|
|
46
|
+
* exit; // Optional direct-access guard (e.g. Aludra patterns).
|
|
47
|
+
* }
|
|
43
48
|
* ?>
|
|
44
49
|
* <!-- wp:group -->...
|
|
50
|
+
*
|
|
51
|
+
* The header is everything up to and including the *first* closing PHP tag,
|
|
52
|
+
* regardless of what it contains (docblock only, or docblock + guard) — so a
|
|
53
|
+
* single non-greedy strip handles both shapes in one pass.
|
|
45
54
|
*/
|
|
46
55
|
export function extractBlockContent(fileContent) {
|
|
47
56
|
const stripped = fileContent
|
|
48
|
-
.replace(
|
|
49
|
-
.replace(/\/\*\*[\s\S]*?\*\//m, '')
|
|
50
|
-
.replace(/^\s*\?>\s*/m, '')
|
|
57
|
+
.replace(/^[\s\S]*?\?>\s*/, '')
|
|
51
58
|
.trim();
|
|
52
59
|
|
|
53
60
|
if (!stripped.startsWith('<!--')) return null;
|
package/src/validation.js
CHANGED
|
@@ -76,9 +76,19 @@ const normalizeBlockAttrJson = str => {
|
|
|
76
76
|
});
|
|
77
77
|
};
|
|
78
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Collapse whitespace (including newlines) between adjacent tags/comments to
|
|
81
|
+
* nothing. Custom blocks that render their wrapper markup via JSX (e.g.
|
|
82
|
+
* `<div><div className="inner">...`) serialize with zero whitespace between
|
|
83
|
+
* elements, while hand-authored pattern PHP conventionally puts each nested
|
|
84
|
+
* element on its own line for readability. That's insignificant whitespace —
|
|
85
|
+
* browsers treat it identically — so it must not produce a content_mismatch.
|
|
86
|
+
*/
|
|
87
|
+
const collapseInterTagWhitespace = str => str.replace(/>\s+</g, '><');
|
|
88
|
+
|
|
79
89
|
/** Apply all WordPress serializer normalizations to both sides before diffing. */
|
|
80
90
|
const normalizeForComparison = str =>
|
|
81
|
-
normalizeBlockAttrJson(normalizeCssProps(stripNavRef(str)));
|
|
91
|
+
collapseInterTagWhitespace(normalizeBlockAttrJson(normalizeCssProps(stripNavRef(str))));
|
|
82
92
|
|
|
83
93
|
/**
|
|
84
94
|
* Compare the editor's serialized output against the original source.
|