Skip to content

Commit c36ef28

Browse files
author
Oleksandr Gorkun
committed
MC-17489: Require specific suffix for HTML binding
1 parent 46dc03b commit c36ef28

File tree

1 file changed

+52
-38
lines changed

1 file changed

+52
-38
lines changed

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

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,68 @@ public function register()
2323
}
2424

2525
/**
26-
* @inheritDoc
26+
* Load HTML document to validate.
2727
*
28-
* Find HTML data bindings and check variables used.
28+
* @param int $stackPointer
29+
* @param File $file
30+
* @return \DOMDocument|null
2931
*/
30-
public function process(File $phpcsFile, $stackPtr)
32+
private function loadHtmlDocument(int $stackPointer, File $file): ?\DOMDocument
3133
{
32-
if ($stackPtr === 0) {
33-
$html = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()));
34+
if ($stackPointer === 0) {
35+
$html = $file->getTokensAsString($stackPointer, count($file->getTokens()));
3436
$dom = new \DOMDocument();
3537
try {
3638
// phpcs:disable Generic.PHP.NoSilencedErrors
3739
@$dom->loadHTML($html);
38-
$loaded = true;
40+
return $dom;
3941
} catch (\Throwable $exception) {
4042
//Invalid HTML, skipping
41-
$loaded = false;
4243
}
43-
if ($loaded) {
44-
/** @var string[] $htmlBindings */
45-
$htmlBindings = [];
46-
$domXpath = new \DOMXPath($dom);
47-
$dataBindAttributes = $domXpath->query('//@*[name() = "data-bind"]');
48-
foreach ($dataBindAttributes as $dataBindAttribute) {
49-
$knockoutBinding = $dataBindAttribute->nodeValue;
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-
) {
65-
$phpcsFile->addError(
66-
'Variables/functions used for HTML binding must have UnsanitizedHtml suffix'
67-
.' - "' .$htmlBinding .'" doesn\'t,' .PHP_EOL
68-
.'consider using text binding if the value is supposed to be text',
69-
null,
70-
'UIComponentTemplate.KnockoutBinding.HtmlSuffix'
71-
);
72-
}
73-
}
44+
}
45+
46+
return null;
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*
52+
* Find HTML data bindings and check variables used.
53+
*/
54+
public function process(File $phpcsFile, $stackPtr)
55+
{
56+
if (!$dom = $this->loadHtmlDocument($stackPtr, $phpcsFile)) {
57+
return;
58+
}
59+
60+
/** @var string[] $htmlBindings */
61+
$htmlBindings = [];
62+
$domXpath = new \DOMXPath($dom);
63+
$dataBindAttributes = $domXpath->query('//@*[name() = "data-bind"]');
64+
foreach ($dataBindAttributes as $dataBindAttribute) {
65+
$knockoutBinding = $dataBindAttribute->nodeValue;
66+
preg_match('/^(.+\s*?)?html\s*?\:(.+)/ims', $knockoutBinding, $htmlBindingStart);
67+
if ($htmlBindingStart) {
68+
$htmlBinding = trim(preg_replace('/\,[a-z0-9\_\s]+\:.+/ims', '', $htmlBindingStart[2]));
69+
$htmlBindings[] = $htmlBinding;
70+
}
71+
}
72+
$htmlAttributes = $domXpath->query('//@*[name() = "html"]');
73+
foreach ($htmlAttributes as $htmlAttribute) {
74+
$magentoBinding = $htmlAttribute->nodeValue;
75+
$htmlBindings[] = trim($magentoBinding);
76+
}
77+
foreach ($htmlBindings as $htmlBinding) {
78+
if (!preg_match('/^[0-9\\\'\"]/ims', $htmlBinding)
79+
&& !preg_match('/UnsanitizedHtml(\(.*?\))*?$/', $htmlBinding)
80+
) {
81+
$phpcsFile->addError(
82+
'Variables/functions used for HTML binding must have UnsanitizedHtml suffix'
83+
. ' - "' . $htmlBinding . '" doesn\'t,' . PHP_EOL
84+
. 'consider using text binding if the value is supposed to be text',
85+
null,
86+
'UIComponentTemplate.KnockoutBinding.HtmlSuffix'
87+
);
7488
}
7589
}
7690
}

0 commit comments

Comments
 (0)