@imwz/wp-pattern-sentinel 1.0.1 → 1.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.
- package/LICENSE +21 -0
- package/package.json +5 -1
- package/src/editor.js +18 -10
- package/src/validation.js +43 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jasper Frumau
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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.3",
|
|
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
|
@@ -15,10 +15,11 @@ import { log } from './format.js';
|
|
|
15
15
|
function stripPhpForValidation(content) {
|
|
16
16
|
if (!content.includes('<?php')) return content;
|
|
17
17
|
|
|
18
|
+
const esc = t => t.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
18
19
|
return content
|
|
19
|
-
.replace(/<\?php\s+(?:esc_html_e|esc_html__)\s*\(\s*'([^']+)'\s*,\s*'[^']+'\s*\)\s*;?\s*\?>/g,
|
|
20
|
-
.replace(/<\?php\s+(?:esc_html_e|esc_html__)\s*\(\s*"([^"]+)"\s*,\s*"[^"]+"\s*\)\s*;?\s*\?>/g,
|
|
21
|
-
.replace(/<\?php\s+echo\s+esc_html__\s*\(\s*'([^']+)'\s*,\s*'[^']+'\s*\)\s*;?\s*\?>/g,
|
|
20
|
+
.replace(/<\?php\s+(?:esc_html_e|esc_html__)\s*\(\s*'([^']+)'\s*,\s*'[^']+'\s*\)\s*;?\s*\?>/g, (_, t) => esc(t))
|
|
21
|
+
.replace(/<\?php\s+(?:esc_html_e|esc_html__)\s*\(\s*"([^"]+)"\s*,\s*"[^"]+"\s*\)\s*;?\s*\?>/g, (_, t) => esc(t))
|
|
22
|
+
.replace(/<\?php\s+echo\s+esc_html__\s*\(\s*'([^']+)'\s*,\s*'[^']+'\s*\)\s*;?\s*\?>/g, (_, t) => esc(t))
|
|
22
23
|
.replace(/<\?php\s+(?:esc_attr_e|esc_attr__)\s*\(\s*'([^']+)'\s*,\s*'[^']+'\s*\)\s*;?\s*\?>/g, '$1')
|
|
23
24
|
.replace(/<\?php\s+(?:esc_attr_e|esc_attr__)\s*\(\s*"([^"]+)"\s*,\s*"[^"]+"\s*\)\s*;?\s*\?>/g, '$1')
|
|
24
25
|
.replace(/<\?php\s+echo\s+esc_attr__\s*\(\s*'([^']+)'\s*,\s*'[^']+'\s*\)\s*;?\s*\?>/g, '$1')
|
|
@@ -28,10 +29,11 @@ function stripPhpForValidation(content) {
|
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
|
-
* Strip the PHP file header (opening tag
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* 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.
|
|
35
37
|
*
|
|
36
38
|
* WordPress pattern files look like:
|
|
37
39
|
* <?php
|
|
@@ -39,14 +41,20 @@ function stripPhpForValidation(content) {
|
|
|
39
41
|
* * Title: My Pattern
|
|
40
42
|
* * ...
|
|
41
43
|
* *\/
|
|
44
|
+
*
|
|
45
|
+
* if ( ! defined( 'ABSPATH' ) ) {
|
|
46
|
+
* exit; // Optional direct-access guard (e.g. Aludra patterns).
|
|
47
|
+
* }
|
|
42
48
|
* ?>
|
|
43
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.
|
|
44
54
|
*/
|
|
45
55
|
export function extractBlockContent(fileContent) {
|
|
46
56
|
const stripped = fileContent
|
|
47
|
-
.replace(
|
|
48
|
-
.replace(/\/\*\*[\s\S]*?\*\//m, '')
|
|
49
|
-
.replace(/^\s*\?>\s*/m, '')
|
|
57
|
+
.replace(/^[\s\S]*?\?>\s*/, '')
|
|
50
58
|
.trim();
|
|
51
59
|
|
|
52
60
|
if (!stripped.startsWith('<!--')) return null;
|
package/src/validation.js
CHANGED
|
@@ -40,6 +40,46 @@ export async function checkBlockValidation(page) {
|
|
|
40
40
|
const stripNavRef = str =>
|
|
41
41
|
str.replace(/(<!-- wp:navigation \{)"ref":\d+,\s*/g, '$1');
|
|
42
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Sort CSS property declarations within every style="..." attribute
|
|
45
|
+
* alphabetically so WordPress's CSS property reordering does not produce
|
|
46
|
+
* false content_mismatch failures.
|
|
47
|
+
*/
|
|
48
|
+
const normalizeCssProps = str =>
|
|
49
|
+
str.replace(/style="([^"]+)"/g, (_, props) => {
|
|
50
|
+
const sorted = props.split(';').map(p => p.trim()).filter(Boolean).sort().join(';');
|
|
51
|
+
return `style="${sorted}"`;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Deep-sort JSON object keys alphabetically so WordPress's block-attribute
|
|
56
|
+
* serialization order (which varies by block type) does not produce false
|
|
57
|
+
* content_mismatch failures.
|
|
58
|
+
*/
|
|
59
|
+
const deepSortKeys = obj => {
|
|
60
|
+
if (Array.isArray(obj)) return obj.map(deepSortKeys);
|
|
61
|
+
if (obj !== null && typeof obj === 'object') {
|
|
62
|
+
return Object.fromEntries(Object.keys(obj).sort().map(k => [k, deepSortKeys(obj[k])]));
|
|
63
|
+
}
|
|
64
|
+
return obj;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const normalizeBlockAttrJson = str => {
|
|
68
|
+
const BLOCK_COMMENT = /<!-- wp:[^\s]+ ({[\s\S]*?}) (?:\/-->|-->)/g;
|
|
69
|
+
return str.replace(BLOCK_COMMENT, (match, json) => {
|
|
70
|
+
try {
|
|
71
|
+
const sorted = JSON.stringify(deepSortKeys(JSON.parse(json)));
|
|
72
|
+
return match.replace(json, sorted);
|
|
73
|
+
} catch {
|
|
74
|
+
return match;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** Apply all WordPress serializer normalizations to both sides before diffing. */
|
|
80
|
+
const normalizeForComparison = str =>
|
|
81
|
+
normalizeBlockAttrJson(normalizeCssProps(stripNavRef(str)));
|
|
82
|
+
|
|
43
83
|
/**
|
|
44
84
|
* Compare the editor's serialized output against the original source.
|
|
45
85
|
* Whitespace-normalizes both sides before diffing to avoid false positives
|
|
@@ -55,12 +95,12 @@ export async function compareContent(page, originalContent) {
|
|
|
55
95
|
result.savedContent = savedContent;
|
|
56
96
|
|
|
57
97
|
const normalize = str => str.replace(/\s+/g, ' ').trim();
|
|
58
|
-
if (normalize(
|
|
98
|
+
if (normalize(normalizeForComparison(savedContent)) === normalize(normalizeForComparison(originalContent))) return result;
|
|
59
99
|
|
|
60
100
|
result.matches = false;
|
|
61
101
|
|
|
62
|
-
const origLines =
|
|
63
|
-
const savedLines =
|
|
102
|
+
const origLines = normalizeForComparison(originalContent).split('\n').map(l => l.trim()).filter(Boolean);
|
|
103
|
+
const savedLines = normalizeForComparison(savedContent).split('\n').map(l => l.trim()).filter(Boolean);
|
|
64
104
|
|
|
65
105
|
const removed = origLines.filter(l => !savedLines.includes(l)).slice(0, 5);
|
|
66
106
|
const added = savedLines.filter(l => !origLines.includes(l)).slice(0, 5);
|