Skip to content

Commit a303ce8

Browse files
author
Oleksandr Gorkun
committed
MC-17489: Require specific suffix for HTML binding
1 parent f89689d commit a303ce8

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

dev/tests/static/framework/Magento/Sniffs/Html/HtmlBindingSniff.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,30 @@ public function process(File $phpcsFile, $stackPtr)
4141
$loaded = false;
4242
}
4343
if ($loaded) {
44+
/** @var string[] $htmlBindings */
45+
$htmlBindings = [];
4446
$domXpath = new \DOMXPath($dom);
4547
$dataBindAttributes = $domXpath->query('//@*[name() = "data-bind"]');
4648
foreach ($dataBindAttributes as $dataBindAttribute) {
4749
$knockoutBinding = $dataBindAttribute->nodeValue;
48-
preg_match('/^(.+\s*?)?html\s*?\:\s*?([a-z0-9\.\(\)\_]+)/ims', $knockoutBinding, $htmlBinding);
49-
if ($htmlBinding && !preg_match('/UnsanitizedHtml[\(\)]*?$/', $htmlBinding[2])) {
50+
preg_match('/^(.+\s*?)?html\s*?\:(.+)/ims', $knockoutBinding, $htmlBindingStart);
51+
if ($htmlBindingStart) {
52+
$htmlBinding = trim(preg_replace('/\,[a-z0-9\_\s]+\:.+/ims', '', $htmlBindingStart[2]));
53+
$htmlBindings[] = $htmlBinding;
54+
}
55+
}
56+
$htmlAttributes = $domXpath->query('//@*[name() = "html"]');
57+
foreach ($htmlAttributes as $htmlAttribute) {
58+
$magentoBinding = $htmlAttribute->nodeValue;
59+
$htmlBindings[] = trim($magentoBinding);
60+
}
61+
foreach ($htmlBindings as $htmlBinding) {
62+
if (!preg_match('/^[0-9\\\'\"]/ims', $htmlBinding)
63+
&& !preg_match('/UnsanitizedHtml(\(.*?\))*?$/', $htmlBinding)
64+
) {
5065
$phpcsFile->addError(
5166
'Variables/functions used for HTML binding must have UnsanitizedHtml suffix'
52-
.' - "' .$htmlBinding[2] .'" doesn\'t,' .PHP_EOL
67+
.' - "' .$htmlBinding .'" doesn\'t,' .PHP_EOL
5368
.'consider using text binding if the value is supposed to be text',
5469
null,
5570
'UIComponentTemplate.KnockoutBinding.HtmlSuffix'
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2-
FOUND 3 ERRORS AFFECTING 1 LINE
3-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1+
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2+
FOUND 6 ERRORS AFFECTING 1 LINE
3+
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
44
1 | ERROR | Variables/functions used for HTML binding must have UnsanitizedHtml suffix - "testError()" doesn't,
55
| | consider using text binding if the value is supposed to be text
66
1 | ERROR | Variables/functions used for HTML binding must have UnsanitizedHtml suffix - "test.getSomething().value.error()" doesn't,
77
| | consider using text binding if the value is supposed to be text
8+
1 | ERROR | Variables/functions used for HTML binding must have UnsanitizedHtml suffix - "bind_stuff(1, 2)" doesn't,
9+
| | consider using text binding if the value is supposed to be text
10+
1 | ERROR | Variables/functions used for HTML binding must have UnsanitizedHtml suffix - "testError()" doesn't,
11+
| | consider using text binding if the value is supposed to be text
12+
1 | ERROR | Variables/functions used for HTML binding must have UnsanitizedHtml suffix - "test.getSomething().value.error(1)" doesn't,
13+
| | consider using text binding if the value is supposed to be text
814
1 | ERROR | Variables/functions used for HTML binding must have UnsanitizedHtml suffix - "bind_stuff()" doesn't,
915
| | consider using text binding if the value is supposed to be text
10-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
16+
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1117

1218

dev/tests/static/framework/tests/unit/testsuite/Magento/Sniffs/Html/_files/test-html-binding.html

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@
1212
attr : tst,
1313
html: test.getSomething().value.error()
1414
"></div>
15-
<p data-bind="html: '<b>Some html</b>'"></p>
15+
<p data-bind="html: '<b>Some html</b>', attr: test"></p>
1616
<div data-bind="html: valueUnsanitizedHtml"></div>
1717
<div data-bind="attr: testhtml, html: valueUnsanitizedHtml()"></div>
18-
<p data-bind="other_html: bind, html: bind_stuff()"></p>
18+
<p data-bind="other_html: bind, html: bind_stuff(1, 2)"></p>
19+
20+
<div style="tst()"></div>
21+
<span html="testError()"></span>
22+
<div html="
23+
test.getSomething().value.error(1)
24+
"></div>
25+
<p html="'<b>Some html</b>'"></p>
26+
<div html="valueUnsanitizedHtml"></div>
27+
<div html="
28+
valueUnsanitizedHtml('test')
29+
"></div>
30+
<p html="bind_stuff()"></p>

0 commit comments

Comments
 (0)