Refactoring Opportunities in Cascading Style Sheets

Refactoring Opportunities in Cascading Style Sheets
Slide Note
Embed
Share

A crucial role in web development, with a domain-specific language widely used by developers worldwide. This study explores the significance of refactoring duplication in CSS, outlining types of duplication and presenting empirical results. Despite CSS's importance for styling web documents, challenges persist in developing CSS due to complex features like cascade specificity and inheritance.

  • CSS
  • Refactoring
  • Web Development
  • Styling
  • Empirical Study

Uploaded on Feb 15, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Discovering Discovering Refactoring Opportunities in Refactoring Opportunities in Cascading Style Sheets Cascading Style Sheets Davood Mazinanian Concordia University, Montr al, Canada Nikolaos Tsantalis Ali Mesbah University of British Columbia, Vancouver, Canada 1

  2. Outline Outline The CSS language Three types of duplication in CSS Refactoring duplication in CSS Results of the empirical study 2

  3. Cascading Style Sheets Cascading Style Sheets A Domain Specific Language for styling Web documents It is widely used 90% of top 10 million websites in the Alexa ranking use CSS (W3Techs Web Technology Surveys) 96% of web developers use CSS (Mozilla Developer Network survey 1,331 participants) 3

  4. CSS CSS Importance Importance Separation of concerns Reusability of styles <div id="div1"> content1 </div> Responsive design Simple syntax Structure document.getElementById("div1") .onclick = function() { alert("clicked!"); } #div1 { color: red } Behavior Style 4

  5. CSS CSS Importance Importance Separation of concerns Reusability of styles Responsive design Simple syntax <div class="centered"></div> .centered { text-align: center; } <p class="centered"></p> <h1 class="centered"></h1> 5

  6. CSS CSS Importance Importance Separation of concerns Reusability of styles Responsive design Simple syntax 6

  7. CSS CSS Importance Importance Separation of concerns Reusability of styles Responsive design Simple syntax Property Value h1 { color: blue; font-size: 12px; } Selector Declaration 7

  8. Challenges in developing CSS Challenges in developing CSS The development is sometimes difficult, due to: Complex features: Cascade Specificity Inheritance Default values Lack of empirically verified best practices No support for variables and functions 8

  9. Duplication is a problem Duplication is a problem Lack of variables and functions inevitable duplication Duplication Increased maintenance effort Higher error-proneness Higher change-proneness Larger file size 9

  10. Three types Three types of duplication of duplication at the declaration level the declaration level at Type I (Lexically identical values) Type II (Equivalent values) Type III (Equivalent shorthand & individuals) .c1 { left: 53px; color: white; border-color: #A52A2A; border-style: solid; border-width: 1px; text-align: center; } left: 53px; color: white; border-color: #A52A2A; border-style: solid; border-width: 1px; .c2 { left: 53px; color: #fff; border: solid 1px brown; } left: 53px; color: #fff; border: solid 1px brown; 10

  11. How to avoid it? How to avoid it? Using preprocessors (LESS, SASS, etc.) .c1 { left: 53px; color: white; border-bottom-color: #A52A2A; border-bottom-style: solid; border-bottom-width: 1px; text-align: center; } Still, duplication will exist in the generated code .c2 { left: 53px; color: #fff; border: solid 1px brown; } &:extend(.c2) 11

  12. How to refactor it? How to refactor it? Grouping selectors .c1 { left: 53px; color: white; border-color: #A52A2A; border-style: solid; border-width: 1px; text-align: center; } .c2 { } .c2 { left: 53px; color: #fff; border: solid 1px brown; } left: 53px; color: white; border-color: #A52A2A; border-style: solid; border-width: 1px; left: 53px; color: #fff; border: solid 1px brown; .c1 , .c2 { } 12

  13. Overview of our approach Overview of our approach Extracting Refactoring opportunities Ranking Refactoring opportunities Examining Order dependencies Pre- Detecting duplication processing 13

  14. Extracting Refactoring opportunities Ranking Refactoring opportunities Examining Order dependencies Pre- Detecting duplication processing Convert all property values related to colors and dimensions to the same reference format or unit (type II) Representation Value Representation Value HTML Named Color Violet Pica 6pc Hexadecimal #EE82EE Point 72pt HSL hsl(300, 76%, 72%) Inch 1in HSLA hsla(300, 76%, 72%, 1) Centimeter 2.54cm RGB rgb(238, 130, 238) Millimeter 254mm RGBA rgba(238, 130, 238, 1) Pixel 96px 14

  15. Extracting Refactoring opportunities Ranking Refactoring opportunities Examining Order dependencies Pre- Detecting duplication processing Create virtual shorthand declarations (for type III) border-color: #A52A2A; border-style: solid; border-width: 1px border: solid 1px brown; 15

  16. Extracting Refactoring opportunities Ranking Refactoring opportunities Examining Order dependencies Pre- Detecting duplication processing {color} .class1 { color: blue; font-weight: bold; } .class2 { color: #00F; font-weight: bold; border: solid 1px #d3d3d3; } .class3 { font-weight: 700; border: solid 3px red; float: left; } .class4 { border-style: solid; border-color: LightGray; border-width: 1px; } [color: blue] [color: #00F] {font-weight} [font-weight: bold] [font-weight: bold] [font-weight: 700] {border} [border: solid 1px #D3D3D3] border-style: solid border-color: LightGray border-width: 1px 16

  17. Extracting Refactoring opportunities Ranking Refactoring opportunities Examining Order dependencies Pre- Detecting duplication processing .class1 { color: blue; font-weight: bold; } .class2 { color: #00F; font-weight: bold; border: solid 1px #d3d3d3; } .class3 { font-weight: 700; border: solid 3px red; float: left; } .class4 { border-style: solid; border-color: LightGray; border-width: 1px; } Frequent Itemsets/ Refactoring Opportunities Involved Selectors Transactions (Selectors) [{border}] Items (Corresponding Clones) .class2, .class4 .class1 {font-weight} {color} .class2 [{color}] .class1, .class2 {font-weight} {color} {border} .class3 [{color}, {font-weight}] {font-weight} .class1, .class2 .class4 [{font-weight}] {border} .class1, .class2, .class3 17

  18. Extracting Refactoring opportunities Ranking Refactoring opportunities Examining Order dependencies Pre- Detecting duplication processing Rank based on Size Reduction (SR) _ _ SR = Before After .c1 { left: 53px; color: white; border-color: #A52A2A; border-style: solid; border-width: 1px; text-align: center; } .c2 { left: 53px; color: #fff; border: solid 1px brown; } .c1 { text-align: center; } .c1, .c2 { left: 53px; color: #fff; border: solid 1px brown; } 18

  19. Extracting Refactoring opportunities Ranking Refactoring opportunities Examining Order dependencies Pre- Detecting duplication processing Before After .class1 { color: blue; font-weight: bold; } .class2 { color: #00F; font-weight: bold; border: solid 1px #d3d3d3; } .class3 { font-weight: 700; border: solid 3px red; float: left; } .class4 { border-style: solid; border-color: LightGray; border-width: 1px; } HTML <div class="class1 class4"> content1 </div> <div class="class2 class3"> content2 </div> .class2 { color: #00F; font-weight: bold; } .class3 { font-weight: 700; border: solid 3px red; float: left; } Browser content1 content1 .class2, .class4 { border: solid 1px #d3d3d3; } content2 content2 19

  20. Extracting Refactoring opportunities Ranking Refactoring opportunities Examining Order dependencies Pre- Detecting duplication processing Extract the new grouping selector to an appropriate position Model as a constraint satisfaction problem! Before After .class2 { color: #00F; font-weight: bold; } class2, .class4 { border: solid 1px #d3d3d3; } .class3 { font-weight: 700; border: solid 3px red; float: left; } .class2 { color: #00F; font-weight: bold; border: solid 1px #d3d3d3; } .class3 { font-weight: 700; border: solid 3px red; float: left; } 21

  21. Empirical Study Empirical Study 38 real web applications (91 external CSS files in total) Crawljax was used for extracting the CSS files and DOM states 22

  22. What is the extent of declaration What is the extent of declaration- - level duplication level duplication in CSS in CSS files? files? 66% of the declarations are repeated at least once! Ratio of the duplicated declarations 23

  23. Number and types of clones Number and types of clones Total number of detected clones Duplication types in the detected clones 24

  24. What is the number of refactoring What is the number of refactoring opportunities opportunities that can that can be be applied? applied? Initial refactoring opportunities vs. applied presentation-preserving refactorings 25

  25. What is the size reduction we What is the size reduction we can can achieve? achieve? Average size reduction is 8% Maximum size reduction is 35% In 12% of the CSS files (11 out of 91) the size reduction is over 20% In 27% of the CSS files (25 out of 91) the size reduction is over 10% %Size reduction 26

  26. Conclusions Conclusions Code duplication is extensive in CSS On average, 66% of the style declarations are repeated at least once There is a significant number of presentation- preserving refactoring opportunities On average, 62 refactoring opportunities The size of CSS files can be decreased On average, 8% size reduction by applying the detected refactoring opportunities 28

  27. Future work Future work Migration of CSS code into CSS Preprocessor code Parameterize differences in property values by introducing Parametric Mixins Before After .class1 { .extracted(20px; #33acfe); float: left; } .class2 { .extracted(10px; #efca44); border: solid 1px red; } .extracted(@margin; @color) { color: @color; margin: @margin; padding: 40px; } .class1 { color: #33acfe; margin: 20px; float: left; padding: 40px; } .class2 { color: #efca44; margin: 10px; padding: 40px; border: solid 1px red } 29

  28. https://github.com/dmazinanian/css-analyser 30

More Related Content