This Powershell script helps locate any unmatching bracers in a .css file/string.
- Powershell v3
- Windows / *nix environment
- Works for both minified and non-minified css.
Validate-CSS-Bracers
can be used as a script or a module.
This method can only check a single file at a time.
- Open the
Validate-CSS-Bracers.ps1
in your favourite text editor and configure scripts settings:
# Full Path to css file
# E.g. For *nix:
# $css_file_fullpath = "/path/to/file.css"
# E.g. For Windows:
# $css_file_fullpath = "C:/path/to/file.css"
$css_file_fullpath = ""
# Alternatively, paste the css within this here-string (i.e. @' <here> '@)
# NOTE: This section is used if $css_file_fullpath is empty
$css_as_string = @'
html {
font-size: 10px;
}
'@
- Run the script:
- WinNT: Right click on the script in explorer and select
Run with Powershell
. (should be present on Windows 7 and up). Alternatively, open command prompt in the script directory, and runPowershell .\Validate-CSS-Bracers.ps1
- *nix: Run
powershell ./Validate-CSS-Bracers.ps1
orpwsh ./Validate-CSS-Bracers.ps1
depending on which version of powershell you're running.
This method supports checking multiple css files by utilizing pipelining.
-
Install the
Validate-CSS-Bracers.psm1
module into any of the following directories:Windows
%Windir%\System32\WindowsPowerShell\v1.0\Modules %UserProfile%\Documents\WindowsPowerShell\Modules %ProgramFiles%\WindowsPowerShell\Modules
*nix
Note: These may vary between *nix distros. Check
$Env:PSModulePath
insidePowershell
.~/.local/share/powershell/Modules /usr/local/share/powershell/Modules /opt/microsoft/powershell/6.0.0-rc/Modules
-
Import the module, then pipe the
.css
files into the module:Import-Module Validate-CSS-Bracers # You can either Pipe the file/files $files | Validate-CSS-Bracers # Or use the full Command (for single file) Validate-CSS-Bracers -File $css_file_fullpath # Or if you're using a CSS string Validate-CSS-Bracers -CssAsString $css_as_string
Validate-CSS-Bracers [[-File] <String>] [[-CssAsString] <String>] [[-NearLength] <Int32>] [<CommonParameters>]
PARAMETERS
-File <String>
Full Path to css file, accepting input from the pipeline.
Required? false
Position? 1
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false
-CssAsString <String>
CSS as a string.
Required? false
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
-NearLength <Int32>
Number of chars to spit out left and right of a found unmatching bracer
Required? false
Position? 3
Default value 0
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
Q: Help! I am getting an error 'File C:...Validate-CSS-Bracers.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.'
- You need to allow the execution of unverified scripts. Open Powershell as administrator, type
Set-ExecutionPolicy Unrestricted -Force
and press ENTER. Try running the script again. You can easily restore the security setting back by usingSet-ExecutionPolicy Undefined -Force
.
Q: Help! Upon running the script I am getting an error File C:...Validate-CSS-Bracers.ps1 cannot be loaded. The file
C:...\Validate-CSS-Bracers.ps1 is not digitally signed. You cannot run
this script on the current system. For more information about running scripts and setting
execution policy, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
- You need to allow the execution of unverified scripts. Open Powershell as administrator, type
Set-ExecutionPolicy Unrestricted -Force
and press ENTER. Try running the script again. You can easily restore the security setting back by usingSet-ExecutionPolicy Undefined -Force
.
Q: Help! Upon running the script I am getting a warning 'Execution Policy change. The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at http://go.microsoft.com/?LinkID=135170. Do you want to change the execution policy?
- You need to allow the execution of unverified scripts. Type
Y
for yes and press enter. You can easily restore the security setting back opening Powershell as administrator, and using the codeSet-ExecutionPolicy Undefined -Force
.
Nil
Nil
- Every web developer knows that when a page is suddenly broken, it's a css unmatching bracer. The cause of the problem becomes more obscure for minified css files. The script can help developers find those unmatching bracers and fix their css. It works for both minified and non-minified css.