@imwz/wp-pattern-sentinel 1.0.1 → 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 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.1",
3
+ "version": "1.0.2",
4
4
  "description": "Browser-based WordPress block pattern validator using Playwright",
5
5
  "type": "module",
6
6
  "engines": {
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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
18
19
  return content
19
- .replace(/<\?php\s+(?:esc_html_e|esc_html__)\s*\(\s*'([^']+)'\s*,\s*'[^']+'\s*\)\s*;?\s*\?>/g, '$1')
20
- .replace(/<\?php\s+(?:esc_html_e|esc_html__)\s*\(\s*"([^"]+)"\s*,\s*"[^"]+"\s*\)\s*;?\s*\?>/g, '$1')
21
- .replace(/<\?php\s+echo\s+esc_html__\s*\(\s*'([^']+)'\s*,\s*'[^']+'\s*\)\s*;?\s*\?>/g, '$1')
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
@@ -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(stripNavRef(savedContent)) === normalize(stripNavRef(originalContent))) return result;
98
+ if (normalize(normalizeForComparison(savedContent)) === normalize(normalizeForComparison(originalContent))) return result;
59
99
 
60
100
  result.matches = false;
61
101
 
62
- const origLines = stripNavRef(originalContent).split('\n').map(l => l.trim()).filter(Boolean);
63
- const savedLines = stripNavRef(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);
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);