Discovering Refactoring Opportunities in Cascading Style Sheets Davood Mazinanian Nikolaos Tsantalis Ali Mesbah Concordia University, Montréal, Canada.

Slides:



Advertisements
Similar presentations
HTML and CSS. HTML Hyper Text Markup Language Tells browser how to display text and images.
Advertisements

CSS Cascading Style Sheets. Objectives Using Inline Styles Working with Selectors Using Embedded Styles Using an External Style Sheet Applying a Style.
Web Pages Week 10 This week: CSS Next week: CSS – Part 2.
1 Cascading Style Sheets Continued Different kinds of selectors in a style sheet –Simple- Pseudo-Class –Contextual- Pseudo-Element –Class Image Styles.
Cascading Style Sheets
Cascading Style Sheets
Programming Club IIT Kanpur. Work environment Before you begin coding,always set up your work environment to your needs IDE Notepad++ Sublime Text 2.
/k/k 1212 Cascading Style Sheets Part one Marko Boon
Cascading Style Sheets: Basics I450 Technology Seminar Copyright 2003, Matt Hottell.
Today CSS HTML A project.
CSS. Intro to CSS Cascading Style Sheets – styles and enhances appearance of webpage.css extension.
Introduction to CSS.
Cascading Style Sheets. CSS stands for Cascading Style Sheets and is a simple styling language which allows attaching style to HTML elements. CSS is a.
HyperText Markup Language (HTML) Uses plain text to add markup instructions to a file. If you can't produce it with a standard keyboard, it can't go into.
1 Pengantar Teknologi Internet W03: CSS Cascading Style Sheets.
Web Pages and Style Sheets Bert Wachsmuth. HTML versus XHTML XHTML is a stricter version of HTML: HTML + stricter rules = XHTML. XHTML Rule violations:
Measurement Many CSS properties (values within the declaration) require that you specify a unit of measurement, such as spacing between elements, border.
An empirical study on the use of CSS Preprocessors Davood Mazinanian - Nikolaos Tsantalis Department of Computer Science and Software Engineering Concordia.
CSS normally control the html elements. Three Ways to Insert CSS There are three ways of inserting a style sheet: External style sheet Internal style.
Using Cascading Style Sheets CSS Basics. Goals Understand basic syntax of Cascading Style Sheets (CSS) Understand basic syntax of Cascading Style Sheets.
CM143 Week 4 Introducing CSS. Cascading Style Sheets A definition for the style in which an element of the webpage will be displayed Border width, colour,
Lecture 7 Cascading Style Sheets (CSS) Boriana Koleva Room: C54
Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are files or forms that describe the layout and appearance of a document.
Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving.
INTRODUCTION TO HTML5 Styling Text. Change the Font Size  You can use the font-size property to change the font size for a document’s text.  Instead.
 This presentation introduces the following: › 3 types of CSS › CSS syntax › CSS comments › CSS and color › The box model.
 A style sheet is a single page of formatting instructions that can control the appearance of many HTML pages at once.  If style sheets accomplished.
Tutorial #3 Cascading Style Sheets. Tutorial #2 Review - Anchors Links to Site DMACC Internal Links Go to Top Mail To me Local.
CS134 Web Design & Development Cascading Style Sheets (CSS) Mehmud Abliz.
Davood Mazinanian, Nikolaos Tsantalis Concordia University, Montreal CSER FALL 2013 MEETING 1 An Empirical Study of Duplication in Cascading Style Sheets.
HTML GUIDE Press F5 and then Click on the links on the left to get to the section you want Section 1: Getting Started Section 2: Moving Banner Section.
Cascading Style Sheets Objective: Create an external style sheet, embedded style sheet, and an inline style to change the look and feel of a web site.
Cascading Style Sheets Eugenia Fernandez IUPUI. CSS Purpose CSS allow you to specify the style in which your XML elements are displayed. CSS were originally.
XP Review 2 New Perspectives on JavaScript, Comprehensive1 Introducing Cascading Style Sheets Formatting Web Pages with CSS.
Introduction to CSS. Why CSS? CSS Provides Efficiency in Design and Updates CSS relatively easy to use Can give you more flexibility and control Faster.
Cascading Style Sheets I Embedded Style Sheets. Page Design ICSS 2Instructor: Sean Griffin What is a Style Sheet? A style sheet is a text file that contains.
Department of Computer Science, Florida State University CGS 3066: Web Programming and Design Spring
CSS Cascading Style Sheets *referenced from
Blended HTML and CSS Fundamentals 3 rd EDITION Tutorial 3 Introducing Cascading Style Sheets.
An empirical study on the use of CSS Preprocessors Davood Mazinanian Nikolaos Tsantalis Department of Computer Science and Software Engineering Concordia.
Internet & World Wide Web How to Program, 5/e 1. 2.
WebD Introduction to CSS By Manik Rastogi.
CSS.
Migrating CSS to Preprocessors by Introducing Mixins
Cascading Style Sheets™ (CSS)
4.01 Cascading Style Sheets
( Cascading style sheet )
The Box Model in CSS Web Design – Sec 4-8
Introduction to CSS.
Chapter 6 Cascading Style Sheets™ (CSS)
Cascading Style Sheets (Formatting)
Cascading Style Sheets
IS333: MULTI-TIER APPLICATION DEVELOPMENT
Introduction to CSS.
Table CSS Create a new CSS called tablestyle.CSS Green Background
CSS Borders and Margins.
Software Refactoring Group
CS134 Web Design & Development
Introduction to Cascading Style Sheets (CSS)
Cascading Style Sheets™ (CSS)
Introduction to CSS.
Web Design & Development
Cascading Style Sheets
4.01 Cascading Style Sheets
Cascading Style Sheets III B. Com (Paper - VI) & III B
Introduction to Cascading Style Sheets (CSS)
Cascading Style Sheets™ (CSS)
CGS 3066: Web Programming and Design Fall 2019
Presentation transcript:

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

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

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

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

CSS Importance Separation of concerns Reusability of styles Responsive design Simple syntax 5.centered { text-align: center; }

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

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

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

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

Three types of duplication at the declaration level.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; } left: 53px; color: white; color: #fff; border-color: #A52A2A; border-style: solid; border-width: 1px; border: solid 1px brown; Type I (Lexically identical values) Type II (Equivalent values) Type III (Equivalent shorthand & individuals) 10

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;.c2 { left: 53px; color: #fff; border: solid 1px brown; } &:extend(.c2) text-align: center; }  Still, duplication will exist in the generated code 11

How to refactor it? Grouping selectors.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; 12 } left: 53px; color: #fff; border: solid 1px brown; left: 53px; color: white; border-color: #A52A2A; border-style: solid; border-width: 1px;.c1,.c2 { }.c2 { }

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

Convert all property values related to colors and dimensions to the same reference format or unit (type II) RepresentationValue HTML Named ColorViolet Hexadecimal#EE82EE HSLhsl(300, 76%, 72%) HSLAhsla(300, 76%, 72%, 1) RGBrgb(238, 130, 238) RGBArgba(238, 130, 238, 1) 14 RepresentationValue Pica 6pc Point 72pt Inch 1in Centimeter 2.54cm Millimeter 254mm Pixel 96px Extracting Refactoring opportunities Detecting duplication Pre- processing Ranking Refactoring opportunities Examining Order dependencies

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

16 Extracting Refactoring opportunities Detecting duplication Pre- processing Ranking Refactoring opportunities Examining Order dependencies.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} [color: blue] [color: #00F] {font-weight} [f ont-weight : bold] [f ont-weight : 700] {border} [border: solid 1px #D3D3D3] border-style: solid border-color: LightGray border-width: 1px

Transactions (Selectors) Items (Corresponding Clones).class1{font-weight} {color}.class2{font-weight} {color} {border}.class3{font-weight}.class4{border} Frequent Itemsets/ Refactoring Opportunities Involved Selectors [{border}].class2,.class4 [{color}].class1,.class2 [{color}, {font-weight}].class1,.class2 [{font-weight}].class1,.class2,.class3 17.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; } Extracting Refactoring opportunities Detecting duplication Pre- processing Ranking Refactoring opportunities Examining Order dependencies

Rank based on Size Reduction (SR) 18 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; } SR = __ Extracting Refactoring opportunities Detecting duplication Pre- processing Ranking Refactoring opportunities Examining Order dependencies

content1 content2.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; } content1 content2 19.class2 { color: #00F; font-weight: bold; }.class3 { font-weight: 700; border: solid 3px red; float: left; }.class2,.class4 { border: solid 1px #d3d3d3; } content1 content2 BeforeAfter HTML Browser Extracting Refactoring opportunities Detecting duplication Pre- processing Ranking Refactoring opportunities Examining Order dependencies

Dependencies = Obstacles for refactoring Intra-selector dependencies ◦Moving border-left-width before border will break the presentation 20.class1 { border: solid 1px #ccc; border-left-width: 0; }

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

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

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

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

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

What is the size reduction we can achieve? 26 %Size reduction 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%

Statistical model For CSS files with a similar size, the number of applicable refactorings decreases as the number of order dependencies increases 27 ParameterEstimatep-value Intercept2.989<2e-16 Size8.149e-03<2e-16 #Order dependencies-1.195e-03<2e-16 Response variablePredictor variables

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

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

30