@@ -23,39 +23,68 @@ public function register()
23
23
}
24
24
25
25
/**
26
- * @inheritDoc
26
+ * Load HTML document to validate.
27
27
*
28
- * Find HTML data bindings and check variables used.
28
+ * @param int $stackPointer
29
+ * @param File $file
30
+ * @return \DOMDocument|null
29
31
*/
30
- public function process ( File $ phpcsFile , $ stackPtr )
32
+ private function loadHtmlDocument ( int $ stackPointer , File $ file )
31
33
{
32
- if ($ stackPtr === 0 ) {
33
- $ html = $ phpcsFile ->getTokensAsString ($ stackPtr , count ($ phpcsFile ->getTokens ()));
34
+ if ($ stackPointer === 0 ) {
35
+ $ html = $ file ->getTokensAsString ($ stackPointer , count ($ file ->getTokens ()));
34
36
$ dom = new \DOMDocument ();
35
37
try {
36
38
// phpcs:disable Generic.PHP.NoSilencedErrors
37
39
@$ dom ->loadHTML ($ html );
38
- $ loaded = true ;
40
+ return $ dom ;
39
41
} catch (\Throwable $ exception ) {
40
- //Invalid HTML, skipping
41
- $ loaded = false ;
42
+ return null ;
42
43
}
43
- if ($ loaded ) {
44
- $ domXpath = new \DOMXPath ($ dom );
45
- $ dataBindAttributes = $ domXpath ->query ('//@*[name() = "data-bind"] ' );
46
- foreach ($ dataBindAttributes as $ dataBindAttribute ) {
47
- $ 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
- $ phpcsFile ->addError (
51
- 'Variables/functions used for HTML binding must have UnsanitizedHtml suffix '
52
- .' - " ' .$ htmlBinding [2 ] .'" doesn \'t, ' .PHP_EOL
53
- .'consider using text binding if the value is supposed to be text ' ,
54
- null ,
55
- 'UIComponentTemplate.KnockoutBinding.HtmlSuffix '
56
- );
57
- }
58
- }
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
+ );
59
88
}
60
89
}
61
90
}
0 commit comments