@imwz/wp-pattern-sentinel 1.0.0 → 1.0.2
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/bin/sentinel.js +0 -0
- package/package.json +1 -1
- package/src/editor.js +4 -3
- package/src/validation.js +52 -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/bin/sentinel.js
CHANGED
|
File without changes
|
package/package.json
CHANGED
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')
|
package/src/validation.js
CHANGED
|
@@ -31,6 +31,55 @@ export async function checkBlockValidation(page) {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Strip the `ref` key WordPress injects into wp:navigation block comments when
|
|
36
|
+
* a pattern is first loaded in the editor. All FSE themes ship navigation
|
|
37
|
+
* patterns without a ref; WordPress assigns one automatically and it must not
|
|
38
|
+
* be treated as a content mismatch.
|
|
39
|
+
*/
|
|
40
|
+
const stripNavRef = str =>
|
|
41
|
+
str.replace(/(<!-- wp:navigation \{)"ref":\d+,\s*/g, '$1');
|
|
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
|
+
|
|
34
83
|
/**
|
|
35
84
|
* Compare the editor's serialized output against the original source.
|
|
36
85
|
* Whitespace-normalizes both sides before diffing to avoid false positives
|
|
@@ -46,12 +95,12 @@ export async function compareContent(page, originalContent) {
|
|
|
46
95
|
result.savedContent = savedContent;
|
|
47
96
|
|
|
48
97
|
const normalize = str => str.replace(/\s+/g, ' ').trim();
|
|
49
|
-
if (normalize(savedContent) === normalize(originalContent)) return result;
|
|
98
|
+
if (normalize(normalizeForComparison(savedContent)) === normalize(normalizeForComparison(originalContent))) return result;
|
|
50
99
|
|
|
51
100
|
result.matches = false;
|
|
52
101
|
|
|
53
|
-
const origLines = originalContent.split('\n').map(l => l.trim()).filter(Boolean);
|
|
54
|
-
const savedLines = savedContent.split('\n').map(l => l.trim()).filter(Boolean);
|
|
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);
|
|
55
104
|
|
|
56
105
|
const removed = origLines.filter(l => !savedLines.includes(l)).slice(0, 5);
|
|
57
106
|
const added = savedLines.filter(l => !origLines.includes(l)).slice(0, 5);
|