├── LICENSE ├── README.md ├── index.js ├── lib ├── css-builder.js ├── remove-style.js └── xbase.js ├── package.json └── test.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Bismark Yamoah 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # remove-style 2 | Remove inline styles from html files 3 | 4 | ## Why Remove Inline Styles 5 | Inlining styles in HTML is an easy way to style HTML elements but not a good practice 6 | especially, if there are other better approaches like using a separate CSS file or 7 | styling inside the HTML style tag. Have you ever tried changing the theme of your 8 | website after inlining your styles in the HTML code? 9 | 10 | - Inline styling makes code maintainability very difficult. 11 | - Inline styling does not allow reusing style rules for different elements. 12 | - It also increases the size of the HTML file which makes page loading very slow. 13 | - It also doesn't make you enjoy the benefits of caching by the browser. 14 | - and many more... Read these articles to get more insight about inline styling: 15 | - [Why is inline styling bad](https://www.lostsaloon.com/technology/why-is-inline-css-bad-is-it-really-that-bad/) 16 | - [Avoid using inline css styles](https://dev.to/alim1496/avoid-using-inline-css-styles-5b6p) 17 | 18 | ## Why You May Choose Remove-Style Package 19 | Below examples demonstrates why you should use remove-style 20 | 21 | File1.html 22 | ```html 23 | 24 |
My first text content
25 |
My second text content
26 |
My third text content
27 |
My fourth text content
28 | ``` 29 | File2.html 30 | ```html 31 | 32 |
My first text content
33 |
My second text content
34 | ``` 35 | 36 | **After using remove-style** 37 | 38 | File1.html 39 | ```html 40 | 41 |
My first text content
42 |
My second text content
43 |
My third text content
44 |
My fourth text content
45 | ``` 46 | File2.html 47 | ```html 48 | 49 |
My first text content
50 |
My second text content
51 | ``` 52 | 53 | **CSS generated by remove-style** 54 | 55 | ```css 56 | /* Remove-Style generated css */ 57 | .rs-a{ 58 | color:red; 59 | } 60 | .rs-b{ 61 | font-size:10px; 62 | } 63 | .rs-c{ 64 | font-size:20px; 65 | } 66 | .rs-d{ 67 | color:blue; 68 | } 69 | ``` 70 | 71 | ### What Does Remove-Style Gives You 72 | - Small file size for both HTML and CSS 73 | - No inline styling in your production files 74 | - Style rules usability. Remove-Style ensures every CSS rule is set once throughout your HTML files. 75 | - No switching forth and back from CSS files to HTML files. It allows you to inline all the style rules you want 76 | in the HTML file then, it takes care of removing them for you. 77 | 78 | 79 | ## How To Use 80 | First install Remove-Style from NPM. 81 | ``` 82 | npm install remove-style 83 | ``` 84 | 85 | ### Usage 1 - Passing HTML strings as argument 86 | You can pass HTML strings as argument to Remove-Style to remove inline styles. In that case, 87 | Remove-Style returns the removed styles' HTML strings and the CSS string. 88 | 89 | ```js 90 | var rs = require("remove-style"); 91 | var result = rs({ 92 | 93 | //Put all your HTML strings here 94 | htmlStrings:["your first html string","your second html string","your third html string"] 95 | }); 96 | 97 | console.log(result.htmlOutputs);//["your first html string","your second html string","your third html string"] 98 | console.log(result.styleSheet);//CSS string generated by Remove-Style 99 | 100 | ``` 101 | 102 | ### Usage 2 - Passing files or a directory as argument 103 | You can pass files or a directory as argument to Remove-Style to remove inline styles from all those files 104 | or all the files in the directory. 105 | Stylesheet is always generated once (universal) for all the files. This ensures each style rule can 106 | be reused by other files. 107 | In the case that you want to pass files or a directory as argument: 108 | - You have to set the CSS destination where the final style sheet should be written to. 109 | If a CSS destination is not set then, the stylesheet will be returned. 110 | - You have to choose whether the files you provide should be overwritten with the removed styles' HTML. 111 | By default, Remove-Styles will overwrite the files you provide with the removed styles' HTML. Set 112 | `overWriteFiles` to false to prevent that behaviour. If `overWriteFiles` is set to false then, 113 | Remove-Style returns the removed styles' HTML strings. 114 | 115 | **With files as argument** 116 | ```js 117 | var rs = require("remove-style"); 118 | var result = rs({ 119 | 120 | //Put the path to all your HTML files here 121 | filePaths:["filename1", "filename2", "filename13"], 122 | 123 | //Choose whether to overwrite files or not 124 | overWriteFiles:false||true||undefined, 125 | 126 | //Set a the style sheet destination or ignore to get it as string 127 | cssDestination:"path to style sheet destination"||undefined 128 | }); 129 | 130 | console.log(result.htmlOutputs);//["filename1 html string","filename2 html string","filename3 html string"]||[] 131 | console.log(result.styleSheet);//CSS string generated by Remove-Style or empty string "" 132 | 133 | ``` 134 | 135 | 136 | **With a directory as argument** 137 | ```js 138 | var rs = require("remove-style"); 139 | var result = rs({ 140 | 141 | //Place your directory path here 142 | dirPath:"directory path", 143 | 144 | //Choose whether to overwrite files or not 145 | overWriteFiles:false||true||undefined, 146 | 147 | //Set a the style sheet destination or ignore to get it as string 148 | cssDestination:"path to style sheet destination"||undefined 149 | }); 150 | 151 | console.log(result.htmlOutputs);//["filename1 html string","filename2 html string","filename3 html string"]||[] 152 | console.log(result.styleSheet);//CSS string generated by Remove-Style or empty string "" 153 | 154 | ``` 155 | 156 | ### Order of preference 157 | In the case when all arguments are given together, HTML strings takes the higher preference. 158 | A directory takes the lowest preference if passed as argument with any other possible argument. 159 | 160 | ### A Note On the "rs-" Prefix 161 | In order to prevent class names collision with existing class names, Remove-Style prefix 162 | class names with "rs-". 163 | 164 | ### A Note On the Class Names Used by Remove-Style 165 | Remove-Style can produce over 13 million distinct or unique class names. 166 | You can do the maths: 167 | - 62 Permutation 4 168 | - 62 Permutation 3 169 | - 62 Permutation 2 170 | - 62 Permutation 1 171 | 172 | That is to say; Remove-Style produces the class names from 62 characters, alpha-numerals 173 | (both lowercase and uppercase). 174 | Class names are generated starting from single characters to maximum of 4 characters (excludeing the "rs-" prefix). 175 | So in the worst case, 7 characters is used for each class name. 176 | 177 | ## [MIT Lincensed](https://github.com/KBismark/remove-style/blob/master/LICENSE) 178 | 179 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const {RemoveInlineStyles,getStyleSheet,writeToCSSFile,readDir} = require("./lib/remove-style"); 2 | /** 3 | * 4 | * @param {{htmlStrings:string[],filePaths:string[],dirPath:string,overWriteFiles:boolean,cssDestination:string}} input 5 | */ 6 | module.exports = function InputTaker(input){ 7 | var result = { 8 | htmlOutputs:[], 9 | styleSheet:"" 10 | }; 11 | //If HTML strings are provided, use that 12 | if(Array.isArray(input.htmlStrings)){ 13 | var i; 14 | for(i=0;i|<(\/|\s?)\s*([a-zA-Z][-.:0-9_a-zA-Z]*)((?:\s+[^>]*?(?:(?:'[^']*')|(?:"[^"]*"))?)*)\s*(\/?)>/g, 5 | attributePattern = /([a-zA-Z()[\]#][a-zA-Z0-9-_:()[\]#]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/gi; 6 | 7 | /** 8 | * 9 | * @param {string} html 10 | */ 11 | function getHtml(html){ 12 | return html.match(markupPattern); 13 | } 14 | /** 15 | * 16 | * @param {string} html 17 | */ 18 | function getAttributes(html){ 19 | return html.match(attributePattern); 20 | } 21 | /** 22 | * 23 | * @param {{value:string|undefined,filePath:string|undefined,overWriteFile:boolean}} input 24 | * @returns 25 | */ 26 | function RemoveInlineStyles(input){ 27 | if(typeof (input.overWriteFile)!=="boolean"){ 28 | input.overWriteFile = true; 29 | } 30 | if(typeof (input.value)!="undefined"){ 31 | var fileContent = input.value; 32 | input.overWriteFile = false; 33 | }else if(typeof (input.filePath)=="string"){ 34 | //Read file contents as string 35 | var fileContent = fs.readFileSync(input.filePath,"utf8"); 36 | }else{ 37 | return ""; 38 | } 39 | var count = 0; 40 | var replacers = []; 41 | var cursor; 42 | //Get all HTML tags (Opening, Closing, Self-closing) 43 | var HTMLTags = getHtml(fileContent); 44 | if(HTMLTags){ 45 | var i,j,attributes="",giveSpace; 46 | var attributesObject = {},key="",value=""; 47 | var styles,classNames,styleCount,classCount; 48 | for(i=0;i1){ 59 | //Tag has attributes 60 | styleCount = 0; 61 | classCount = 0; 62 | styles=[]; 63 | classNames=[]; 64 | //Starts from index 1 to ignore the tag name itself 65 | for(j=1;j That's too much geek; 83 | throw new Error( 84 | "You have more than 14 million distinct css rules."+ 85 | "Try to break your css file into two or more by parsing some of your HTML files separately." 86 | ); 87 | } 88 | this.TOTAL++; 89 | } 90 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "remove-style", 3 | "version": "1.0.0", 4 | "description": "Remove inline styles from html file", 5 | "main": "index.js", 6 | "directories": { 7 | "lib": "lib" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/KBismark/remove-style.git" 15 | }, 16 | "keywords": [ 17 | "remove-style", 18 | "remove-inline-style", 19 | "minimize-css", 20 | "css-minimizer", 21 | "html-style-romever", 22 | "remove-html-styles" 23 | ], 24 | "author": "Bismark Yamoah", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/KBismark/remove-style/issues" 28 | }, 29 | "homepage": "https://github.com/KBismark/remove-style#readme" 30 | } 31 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var removeStyles = require("./index"); 2 | 3 | var testString1 = ` 4 |
5 | Sign in 6 | Sign up 7 |
8 | `; 9 | var testString2 = ` 10 |
11 | Sign in 12 | Sign up 13 |
14 | `; 15 | 16 | 17 | var result = removeStyles({ 18 | htmlStrings:[testString1,testString2] 19 | }); 20 | 21 | console.log(JSON.stringify(result,undefined,2)); 22 | 23 | --------------------------------------------------------------------------------