Skip to content

Commit 557ce48

Browse files
committed
use look-behind and look-forward to allow overlaps, simplify replacements
1 parent e84cf32 commit 557ce48

File tree

3 files changed

+80
-17
lines changed

3 files changed

+80
-17
lines changed

bin/format-php-prettier.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,50 @@ export function tokenizeHTML(htmlContent) {
4646
// const pattern = /<\?(?:php|=)[\s\S]*?\?>/gs;
4747
// const pattern =
4848
// /(?<before>(?:[^\s]|\s|^)\s*)(?<php><\?(?:php|=).*?(?:\?>|$))(?<after>(?:\s*)[^\s]|$)/gs;
49+
// const pattern =
50+
// /((?:[^\s]|\s|^)\s*)(<\?(?:php|=).*?(?:\?>|$))((?:\s*)[^\s]|$)/gms;
51+
// // const pattern = /([^\s]+)\s*(<\?(?:php|=).*?(?:\?>|$))\s*([^\s]*)/gms;
52+
// const pattern =
53+
// /([^\s]?\s*)?(<\?(?:php|=).*?(?:\?>|$))((?:\s*)[^\s]|$)/gms;
4954
const pattern =
50-
/((?:[^\s]|\s|^)\s*)(<\?(?:php|=).*?(?:\?>|$))((?:\s*)[^\s]|$)/gs;
55+
/(?<=((?:[^\s]|\s|^)\s*))(<\?(?:php|=).*?\?>)(?=((?:\s*)[^\s]|$))/gms;
5156

52-
const tokenizedHTML = htmlContent.replace(
57+
let tokenizedHTML = htmlContent.replace(
5358
pattern,
5459
(string, before, phpCodeBlock, after, offset) => {
5560
const start = [">", ""].includes(before.trim()) ? "<" : "_";
5661
const end = ["<", ""].includes(after.trim()) ? " />" : "___";
5762

58-
console.log({offset, string, before, phpCodeBlock, after, offset });
63+
// end-pad the token to the length of the span, up to 80 characters
64+
const codeLength = Math.min(phpCodeBlock.length, 80 - end.length);
65+
const token =
66+
`${start}php_${tokenCount++}__`.padEnd(codeLength, "_") + end;
67+
phpCodeBlocks[token] = phpCodeBlock;
68+
69+
return token;
70+
},
71+
);
72+
73+
/**
74+
* special case followup for open-ended PHP tags at the end of the document
75+
* TODO: Merge this back up into a single pattern
76+
*/
77+
tokenizedHTML = tokenizedHTML.replace(
78+
/(?<=((?:[^\s]|\s|^)\s*))(<\?(?:php|=).*$)/gms,
79+
80+
(string, before, phpCodeBlock, offset) => {
81+
const start = [">", ""].includes(before.trim()) ? "<" : "_";
82+
const end = start === "<" ? " />" : "___";
5983

60-
// end-pad the token to the lengh of the span, up to 80 characters
6184
const codeLength = Math.min(phpCodeBlock.length, 80 - end.length);
6285
const token =
6386
`${start}php_${tokenCount++}__`.padEnd(codeLength, "_") + end;
6487
phpCodeBlocks[token] = phpCodeBlock;
65-
return `${before}${token}${after}`;
88+
89+
return token;
6690
},
6791
);
6892

69-
console.log({ tokenizedHTML, phpCodeBlocks });
7093
return { tokenizedHTML, phpCodeBlocks };
7194
}
7295

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
4+
<!-- START template-parts/items/card.php -->
5+
6+
<a
7+
href="<?php the_permalink(); ?>"
8+
<?php post_class('card col-12 col-md-6'); ?>
9+
data-ga-action="Article"
10+
>
11+
<?php get_template_part('template-parts/partials/card-media'); ?>
12+
<div class="card__details">
13+
<h4 class="card__title"><?= $cardTitle ?></h4>
14+
<h5 class="card__desc"><?= $cardDesc ?></h5>
15+
</div>
16+
</a>
17+
18+
<!-- END template-parts/items/card.php -->
19+
20+

test/format-php-prettier.test.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,38 @@ describe("HTML-PHP Prettier", () => {
3434
expect(tokens[3]).toMatch(/^<php_\d+_* \/>$/);
3535
});
3636

37-
test("single open PHP code block #11"),
38-
async () => {
39-
const input = (
40-
await readFile(
41-
"./test/fixtures/format-php-prettier/single-open-php-block.php",
42-
)
43-
).toString();
37+
test("single open PHP code block #11", async () => {
38+
const input = (
39+
await readFile(
40+
"./test/fixtures/format-php-prettier/single-open-php-block.php",
41+
)
42+
).toString();
43+
44+
const { phpCodeBlocks: codeBlocks } = tokenizeHTML(input);
45+
46+
const tokens = Object.keys(codeBlocks);
47+
48+
expect(tokens).toHaveLength(1);
49+
});
4450

45-
const { phpCodeBlocks: codeBlocks } = tokenizeHTML(input);
4651

47-
const tokens = Object.keys(codeBlocks);
52+
test("bare attribute in tag", async () => {
53+
const input = (
54+
await readFile(
55+
"./test/fixtures/format-php-prettier/card-attribute-bug.php",
56+
)
57+
).toString();
4858

49-
expect(tokens).toHaveLength(1);
50-
};
59+
const { phpCodeBlocks: codeBlocks } = tokenizeHTML(input);
60+
61+
const tokens = Object.keys(codeBlocks);
62+
63+
expect(tokens[0]).toMatch(/^_php_\d+_*$/);
64+
expect(tokens[1]).toMatch(/^_php_\d+_*$/);
65+
expect(tokens[2]).toMatch(/^<php_\d+_* \/>$/);
66+
expect(tokens[3]).toMatch(/^<php_\d+_* \/>$/);
67+
expect(tokens[4]).toMatch(/^<php_\d+_* \/>$/);
68+
69+
expect(tokens).toHaveLength(5);
70+
});
5171
});

0 commit comments

Comments
 (0)