CSC/ECE 517 Spring 2016/Implement Common Parts of the CSSOM API: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(31 intermediate revisions by 2 users not shown)
Line 3: Line 3:
Cascading Style Sheets ([https://en.wikipedia.org/wiki/Cascading_Style_Sheets CSS]) is a style sheet language used for describing the presentation of a document written in a markup language. Although most often used to set the visual style of web pages and user interfaces written in [https://en.wikipedia.org/wiki/HTML HTML] and [https://en.wikipedia.org/wiki/XHTML XHTML], the language can be applied to any [https://en.wikipedia.org/wiki/XML XML] document, including plain XML, [https://en.wikipedia.org/wiki/Scalable_Vector_Graphics SVG] and [https://en.wikipedia.org/wiki/XUL XUL], and is applicable to rendering in speech, or on other media. Along with HTML and [https://en.wikipedia.org/wiki/JavaScript JavaScript], CSS is a cornerstone technology used by most websites to create visually engaging webpages, user interfaces for web applications, and user interfaces for many mobile applications
Cascading Style Sheets ([https://en.wikipedia.org/wiki/Cascading_Style_Sheets CSS]) is a style sheet language used for describing the presentation of a document written in a markup language. Although most often used to set the visual style of web pages and user interfaces written in [https://en.wikipedia.org/wiki/HTML HTML] and [https://en.wikipedia.org/wiki/XHTML XHTML], the language can be applied to any [https://en.wikipedia.org/wiki/XML XML] document, including plain XML, [https://en.wikipedia.org/wiki/Scalable_Vector_Graphics SVG] and [https://en.wikipedia.org/wiki/XUL XUL], and is applicable to rendering in speech, or on other media. Along with HTML and [https://en.wikipedia.org/wiki/JavaScript JavaScript], CSS is a cornerstone technology used by most websites to create visually engaging webpages, user interfaces for web applications, and user interfaces for many mobile applications


=='''CSSOM'''==
==='''CSSOM'''===
CSSOM (CSS object model) defines [https://en.wikipedia.org/wiki/Application_programming_interface APIs] (including generic parsing and serialization rules) for media queries, selectors, and CSS itself.
CSSOM which is the CSS Object Model defines APIs (including generic parsing and serialization rules) for media queries, selectors, and CSS itself<ref>https://drafts.csswg.org/cssom/</ref>. The core features of the CSSOM are oriented towards providing basic capabilities to author-defined scripts to permit access to and manipulation of style related state information and processes. CSSOM aims at proving a way to access the CSS style classes and properties. One can even modify the CSS rules using this API. These changes can be targeted to affect individual elements or the whole webpage. <ref>https://github.com/servo/servo/wiki/CSSOM-student-project</ref>. CSSOM is actually a collection of all the CSS stylesheets found on a web page which is very similar to Document Object Model (DOM) but only stores collection of CSS instead of HTML. <ref>https://varvy.com/performance/cssom.html</ref>. CSSOM identifies which elements on your webpage require styling and modifies them. It is also used to optimize the speed at which webpages get loaded.
The core features of the CSSOM are oriented towards providing basic capabilities to author-defined scripts to permit access to and manipulation of style related state information and processes.
It is an integral part of the process that is used to display content on a webpage. The web browser first creates a DOM by examining the HTMLs, then creates a CSSOM by examining the CSS stylesheets. It then creates a render tree by comibining the DOM and the CSSOM and displays the webpage.<ref>https://varvy.com/performance/cssom.html</ref>.


=='''Servo'''==
==='''Rust'''===


Servo is a project in [https://en.wikipedia.org/wiki/Mozilla Mozilla] research to write a parallel layout engine in rust. It aims to achieve better parallelism, security, modularity and performance. Servo is built with Cargo, the rust package manager. It currently supports 64bit OSX, 64bit Linux, Android, and Gonk(Firefox OS).
We can organize different languages on a spectrum with control on one end and safety on the other. Languages like C and C++ fall on the end of the side of control, where as on the other side we have languages like Javascript, Ruby, Python, etc. What Rust does is stepping off this line. What it does is not a tradeoff between control and safety, but it provides you both the low level of control found in C and C++, and the high level of safety in JavaScript and Python. The three concepts that distinguish rust from other programming languages are: Ownership, Borrowing and lifetime. Ownership is how Rust achieves its largest goal, memory safety.<ref>https://en.wikipedia.org/wiki/Rust_(programming_language)</ref>


=='''Rust'''==
==='''Servo'''===


We can organize different languages on a spectrum with control on one end and safety on the other. Languages like C and C++ fall on the end of the side of control, where as on the other side we have languages like Javascript, Ruby, Python, etc.
Servo is a project in [https://en.wikipedia.org/wiki/Mozilla Mozilla] research to write a parallel layout engine in Rust. It aims to achieve better parallelism, security, modularity and performance. Servo is built with Cargo, the rust package manager. It currently supports 64bit OSX, 64bit Linux, Android, and Gonk(Firefox OS).<ref>https://servo.org</ref>
What Rust does is stepping off this line. What it does is not a tradeoff between control and safety, but it provides you both the low level of control found in C and C++, and the high level of safety in JavaScript and Python.
The three concepts that distinguish rust from other programming languages are: Ownership, Borrowing and lifetime. Ownership is how Rust achieves its largest goal, memory safety.


=='''Project Description'''==
=='''Project Description'''==


The project initial steps were described as the following:
The project is to go through the following steps in a somewhat linear order:
*compile Servo and ensure that it runs on <code>tests/html/about-mozilla.html</code>
*Compiling servo on a machine and making sure it runs on a specific url. Then emailing the mozilla mailing list to introduce the group.
*email the <code>mozilla.dev.servo</code> mailing list introducing your group and your progress
*Creating a StyleSheet interface which represents an abstract, base style sheet. This creation requires a couple of steps :
*create the <code>StyleSheet</code> interface. See the documentation for how to go about this. Comment out any method in the WebIDL that cannot be trivially implemented according to the specification.
**Adding a new IDL file (which contains specifications)
*create the <code>StyleSheetList</code> interface. Leave <code>[ArrayClass]</code> commented out, since it is not yet supported by Servo. Store a <code>JS<Document></code> member to allow accessing a document's stylesheets.
**Creating the Rust file which has implementations of the things specified in IDL.
*add the <code>styleSheets</code> attribute from the Document extension to <code>Document.webidl</code>, and make it return a new instance of <code>StyleSheetList</code> referencing the current document.
**Listing the Rust file in mod.rs file.
*run <code>./mach test-wpt</code> <code>/tests/wpt/html/dom/interfaces.html</code> and update the test result expectations according to the documentation
*After that comes the creation of  a StyleSheetList interface. Which represents an ordered collection of CSS style sheets. And basically because it's another interface, again the .webidl and .rs files need to be implemented.
*update <code>tests/wpt/mozilla/tests/mozilla/interfaces.html</code> to add the new interface names and allow the test to pass
*Adding the attribute <code>stylesheets</code> to the <code>Document.webidl</code>.  
*In the three steps just mentioned some guidelines about implementation must be followed, like the return types of certain methods.
*Run the tests specified by the mozilla team.
*Finally updating the interfaces.html, and adding the new interface names.
 
The Subsequent steps are:
*making the <code>stylesheets</code> member of <code>Document</code> store the associated <code>Element</code> along with the actual implementation of <code>Stylesheet</code> and adding a member to <code>StyleSheet</code> for the associated element, and implement the <code>ownerNode</code> attribute of <code>StyleSheet</code> using this.
*create the CSSStyleSheet interface, containing an <code>Arc<Stylesheet></code> member. Add a method to <code>Document</code> that returns a new <code>CSSStyleSheet</code> instance for a particular index in the <code>stylesheets</code> member.
*creating the CSSRule interface which represents an abstract, base CSS Rule. It is done using the Webidl format.
*creating the CSSRuleList interface and implementing <code>CSSStyleSheet.cssRules</code> which is an array like object containing an ordered collection of CSS rules.
*implementing the <code>insertRule</code> and <code>deleteRule</code> methods of <code>CSSStyleSheet</code> to allow mutation of the contents of the vector in the <code>rules</code> member of the <code>Stylesheet</code>.
*creating the CSSStyleRule interface that extends the CSSRule interface and making <code>cssRules</code> return <code>CSSStyleRule</code> values for appropriate rules.


=='''Implementation'''==
=='''Implementation'''==
==='''Initial Steps'''===
The following steps were followed to meet the project requirements as per this github page.
The following steps were followed to meet the project requirements as per this github page.
==='''Step 1'''===
 
===='''Step 1'''====
 
We had to implement the StyleSheet interface for which we had to create two file: stylesheet.rs and StyleSheet.webidl and included stylesheet interface in the mod.rs file.
We had to implement the StyleSheet interface for which we had to create two file: stylesheet.rs and StyleSheet.webidl and included stylesheet interface in the mod.rs file.
In the StyleSheet interface we avoided the three methods: ownerNode, parentStyleSheet and media as they could not be trivially implemented and the instructions were clear to avoid such methods.
In the StyleSheet interface we avoided the three methods: ownerNode, parentStyleSheet and media as they could not be trivially implemented and the instructions were clear to avoid such methods.


==='''Step 2'''===
===='''Step 2'''====
 
We had to implement StyleSheetList interface for which we had to create two file: stylesheetlist.rs and StyleSheetList.webidl and included stylesheetlist interface in the mod.rs file.
We had to implement StyleSheetList interface for which we had to create two file: stylesheetlist.rs and StyleSheetList.webidl and included stylesheetlist interface in the mod.rs file.
We used JS<Document> instead of [ArrayClass] as [ArrayClass] is not yet supported by Servo.
We used JS<Document> instead of [ArrayClass] as [ArrayClass] is not yet supported by Servo.
In the StyleSheetList interface we did not implement the getter as we do not have a list of stylesheets/scripts/dom.
In the StyleSheetList interface we did not implement the getter as we do not have a list of stylesheets/scripts/dom.


==='''Step 3'''===
===='''Step 3'''====
We had to implement a partial interface Document with an attribute as styleSheets. The declaration of the interface was done in Document.webidl and its implementation was done in document.rs file. The implementation returned a new instance of StyleSheetList referencing the current docume[Snapshots of .webidl and .rs from Git]nt.
 
We had to implement a partial interface Document with an attribute as styleSheets. The declaration of the interface was done in Document.webidl and its implementation was done in document.rs file. The implementation returned a new instance of StyleSheetList referencing the current document.
 
==='''UML Diagram'''===
 
 
[[File:UMLinitial.png]]
 
==='''Subsequent Steps'''===
 
===='''Step 1'''====
 
We will be making changes in the <code>Document.rs</code> file to make the <code>stylesheets</code> member store the associated Element. We will then implement the ownerNode attribute in the StyleSheet inteface by making changes in the <code>StyleSheet.webidl</code> and <code>stylesheet.rs</code>. We would just declare <code>ownerNode</code> attribute in the .webidl and give its implementation in .rs file.
 
===='''Step 2'''====
 
We will be making a new interface named as <code>CSSStyleSheet</code>. In the <code>Document.rs</code> file, we will create a method to return the <code>CSSStyleSheet</code> from the <code>stylesheets</code> member of the Document by giving a particular index as the input.
 
===='''Step 3'''====
 
As the next step, we will be creating the <code>CSSRule</code> interface which has attributes like <code>type</code>, <code>cssText</code>, <code>parentRule</code> and <code>parentStyleSheet</code>. This interface will also have several constants like <code>STYLE_RULE</code>, <code>CHARSET_RULE</code> , <code>IMPORT_RULE, MEDIA_RULE</code>, <code>FONT_FACE_RULE</code>, <code>PAGE_RULE, MARGIN_RULE</code> and <code>NAMESPACE_RULE</code>.
This will require us to create two files: <code>CSSRule.webidl</code> and <code>cssrule.rs</code>
 
===='''Step 4'''====
 
We will be creating <code>CSSRuleList</code> interface by creating 2 files: <code>CSSRuleList.webidl</code> and <code>CSSRulelist.rs</code>. This interface will have one attribute, which is length and a getter which returns the <code>CSSRule</code> at a particular index. We will also implement <code>CSSStyleSheet.cssRules</code>
 
===='''Step 5'''====
 
We will make <code>rules</code> member of the <code>StyleSheet</code> type which contains a RefCell and implement the <code>insertRule</code> and <code>deleteRule</code> methods of <code>CSSStyleSheet</code>.
 
===='''Step 6'''====
 
We will be creating a new interface <code>CSSStyleRule</code> , by making 2 files: <code>CSSStyleRule.webidl<code> and <code>CSSStyleRule.rs</code>. The interface will contain two attributes, namely, <code>selectorText</code> and <code>CSSStyleDeclaration</code> style. The <code>cssRules</code> will be implemented to return <code>CSSStyleRule</code> values for appropriate rules.
 
=='''Design Principles'''==
In this mozilla project, we are asked to strictly follow the existing design patterns and coding conventions. The main aim of this project is to add and modify interfaces. Interfaces allow developers to follow a clean design principle wherein they do not have to program to classes. It is too easy to add a dependency on a class. In order to avoid that kind of dependency on the implementation i.e. classes, developers can program to interfaces. This type of design pattern is an architectural pattern and makes the code more modular and object oriented. Programming to an interface has a flavor of DRY principle because even if the underlying implementation changes, the handle to the implementation is an interface and that does not change. In this way, our mozilla project becomes more modular and object oriented. Servo has a utility of tests that checks the coding standards followed by Mozilla on this project. We will execute the following utility tests command.
<code>./mach test-tidy</code> Any issues encountered by executing this test can be flagged before generating the pull request. This ensures that the developers follow the coding standards.


=='''Testing'''==
=='''Testing'''==
Line 54: Line 106:
You will see that all tests pass as expected.
You will see that all tests pass as expected.


'''Note''': There were no predefined test cases mentioned for our project. So now you will see 0 testcases. We added the two new interfaces in tests/wpt/mozilla/tests/mozilla/interfaces.html
'''Note''': There were no predefined test cases mentioned for our project. So now you will see 0 testcases. As far as we know there is no way to test this from UI.
 
=='''Conclusion'''==
We have tried to implement the most commonly-used portions of the CSSOM API specification to increase Servo's compatibility with existing webpages <ref>https://github.com/servo/servo/wiki/CSSOM-student-project</ref>. We have created two interfaces, StyleSheet and StyleSheetList in our initial steps. As a part of our subsequent steps, we would be adding 4 new interfaces: CSSStyleSheet, CSSRule, CSSRuleList and the CSSStyleRule interface. We have implemented only the obvious methods for these interfaces so that more advanced work can be done to make this CSSOM API fully functional keeping our work as the basis.
 
=='''References'''==
<references/>

Latest revision as of 02:42, 9 April 2016

Introduction

Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language. Although most often used to set the visual style of web pages and user interfaces written in HTML and XHTML, the language can be applied to any XML document, including plain XML, SVG and XUL, and is applicable to rendering in speech, or on other media. Along with HTML and JavaScript, CSS is a cornerstone technology used by most websites to create visually engaging webpages, user interfaces for web applications, and user interfaces for many mobile applications

CSSOM

CSSOM which is the CSS Object Model defines APIs (including generic parsing and serialization rules) for media queries, selectors, and CSS itself<ref>https://drafts.csswg.org/cssom/</ref>. The core features of the CSSOM are oriented towards providing basic capabilities to author-defined scripts to permit access to and manipulation of style related state information and processes. CSSOM aims at proving a way to access the CSS style classes and properties. One can even modify the CSS rules using this API. These changes can be targeted to affect individual elements or the whole webpage. <ref>https://github.com/servo/servo/wiki/CSSOM-student-project</ref>. CSSOM is actually a collection of all the CSS stylesheets found on a web page which is very similar to Document Object Model (DOM) but only stores collection of CSS instead of HTML. <ref>https://varvy.com/performance/cssom.html</ref>. CSSOM identifies which elements on your webpage require styling and modifies them. It is also used to optimize the speed at which webpages get loaded. It is an integral part of the process that is used to display content on a webpage. The web browser first creates a DOM by examining the HTMLs, then creates a CSSOM by examining the CSS stylesheets. It then creates a render tree by comibining the DOM and the CSSOM and displays the webpage.<ref>https://varvy.com/performance/cssom.html</ref>.

Rust

We can organize different languages on a spectrum with control on one end and safety on the other. Languages like C and C++ fall on the end of the side of control, where as on the other side we have languages like Javascript, Ruby, Python, etc. What Rust does is stepping off this line. What it does is not a tradeoff between control and safety, but it provides you both the low level of control found in C and C++, and the high level of safety in JavaScript and Python. The three concepts that distinguish rust from other programming languages are: Ownership, Borrowing and lifetime. Ownership is how Rust achieves its largest goal, memory safety.<ref>https://en.wikipedia.org/wiki/Rust_(programming_language)</ref>

Servo

Servo is a project in Mozilla research to write a parallel layout engine in Rust. It aims to achieve better parallelism, security, modularity and performance. Servo is built with Cargo, the rust package manager. It currently supports 64bit OSX, 64bit Linux, Android, and Gonk(Firefox OS).<ref>https://servo.org</ref>

Project Description

The project is to go through the following steps in a somewhat linear order:

  • Compiling servo on a machine and making sure it runs on a specific url. Then emailing the mozilla mailing list to introduce the group.
  • Creating a StyleSheet interface which represents an abstract, base style sheet. This creation requires a couple of steps :
    • Adding a new IDL file (which contains specifications)
    • Creating the Rust file which has implementations of the things specified in IDL.
    • Listing the Rust file in mod.rs file.
  • After that comes the creation of a StyleSheetList interface. Which represents an ordered collection of CSS style sheets. And basically because it's another interface, again the .webidl and .rs files need to be implemented.
  • Adding the attribute stylesheets to the Document.webidl.
  • In the three steps just mentioned some guidelines about implementation must be followed, like the return types of certain methods.
  • Run the tests specified by the mozilla team.
  • Finally updating the interfaces.html, and adding the new interface names.

The Subsequent steps are:

  • making the stylesheets member of Document store the associated Element along with the actual implementation of Stylesheet and adding a member to StyleSheet for the associated element, and implement the ownerNode attribute of StyleSheet using this.
  • create the CSSStyleSheet interface, containing an Arc<Stylesheet> member. Add a method to Document that returns a new CSSStyleSheet instance for a particular index in the stylesheets member.
  • creating the CSSRule interface which represents an abstract, base CSS Rule. It is done using the Webidl format.
  • creating the CSSRuleList interface and implementing CSSStyleSheet.cssRules which is an array like object containing an ordered collection of CSS rules.
  • implementing the insertRule and deleteRule methods of CSSStyleSheet to allow mutation of the contents of the vector in the rules member of the Stylesheet.
  • creating the CSSStyleRule interface that extends the CSSRule interface and making cssRules return CSSStyleRule values for appropriate rules.

Implementation

Initial Steps

The following steps were followed to meet the project requirements as per this github page.

Step 1

We had to implement the StyleSheet interface for which we had to create two file: stylesheet.rs and StyleSheet.webidl and included stylesheet interface in the mod.rs file. In the StyleSheet interface we avoided the three methods: ownerNode, parentStyleSheet and media as they could not be trivially implemented and the instructions were clear to avoid such methods.

Step 2

We had to implement StyleSheetList interface for which we had to create two file: stylesheetlist.rs and StyleSheetList.webidl and included stylesheetlist interface in the mod.rs file. We used JS<Document> instead of [ArrayClass] as [ArrayClass] is not yet supported by Servo. In the StyleSheetList interface we did not implement the getter as we do not have a list of stylesheets/scripts/dom.

Step 3

We had to implement a partial interface Document with an attribute as styleSheets. The declaration of the interface was done in Document.webidl and its implementation was done in document.rs file. The implementation returned a new instance of StyleSheetList referencing the current document.

UML Diagram

Subsequent Steps

Step 1

We will be making changes in the Document.rs file to make the stylesheets member store the associated Element. We will then implement the ownerNode attribute in the StyleSheet inteface by making changes in the StyleSheet.webidl and stylesheet.rs. We would just declare ownerNode attribute in the .webidl and give its implementation in .rs file.

Step 2

We will be making a new interface named as CSSStyleSheet. In the Document.rs file, we will create a method to return the CSSStyleSheet from the stylesheets member of the Document by giving a particular index as the input.

Step 3

As the next step, we will be creating the CSSRule interface which has attributes like type, cssText, parentRule and parentStyleSheet. This interface will also have several constants like STYLE_RULE, CHARSET_RULE , IMPORT_RULE, MEDIA_RULE, FONT_FACE_RULE, PAGE_RULE, MARGIN_RULE and NAMESPACE_RULE. This will require us to create two files: CSSRule.webidl and cssrule.rs

Step 4

We will be creating CSSRuleList interface by creating 2 files: CSSRuleList.webidl and CSSRulelist.rs. This interface will have one attribute, which is length and a getter which returns the CSSRule at a particular index. We will also implement CSSStyleSheet.cssRules

Step 5

We will make rules member of the StyleSheet type which contains a RefCell and implement the insertRule and deleteRule methods of CSSStyleSheet.

Step 6

We will be creating a new interface CSSStyleRule , by making 2 files: CSSStyleRule.webidl and CSSStyleRule.rs. The interface will contain two attributes, namely, selectorText and CSSStyleDeclaration style. The cssRules will be implemented to return CSSStyleRule values for appropriate rules.

Design Principles

In this mozilla project, we are asked to strictly follow the existing design patterns and coding conventions. The main aim of this project is to add and modify interfaces. Interfaces allow developers to follow a clean design principle wherein they do not have to program to classes. It is too easy to add a dependency on a class. In order to avoid that kind of dependency on the implementation i.e. classes, developers can program to interfaces. This type of design pattern is an architectural pattern and makes the code more modular and object oriented. Programming to an interface has a flavor of DRY principle because even if the underlying implementation changes, the handle to the implementation is an interface and that does not change. In this way, our mozilla project becomes more modular and object oriented. Servo has a utility of tests that checks the coding standards followed by Mozilla on this project. We will execute the following utility tests command. ./mach test-tidy Any issues encountered by executing this test can be flagged before generating the pull request. This ensures that the developers follow the coding standards.

Testing

Following are the steps to run all the tests: 1. Install the pre-requisites required for servo as mentioned here 2. Run the following commands

   cd
   git clone https://github.com/mohammed-alfatih/servo.git
   cd servo
   ./mach build --release

Note: It may take around 30 mins to build

   ./mach test-wpt /tests/wpt/html/dom/interfaces.html

You will see that all tests pass as expected.

Note: There were no predefined test cases mentioned for our project. So now you will see 0 testcases. As far as we know there is no way to test this from UI.

Conclusion

We have tried to implement the most commonly-used portions of the CSSOM API specification to increase Servo's compatibility with existing webpages <ref>https://github.com/servo/servo/wiki/CSSOM-student-project</ref>. We have created two interfaces, StyleSheet and StyleSheetList in our initial steps. As a part of our subsequent steps, we would be adding 4 new interfaces: CSSStyleSheet, CSSRule, CSSRuleList and the CSSStyleRule interface. We have implemented only the obvious methods for these interfaces so that more advanced work can be done to make this CSSOM API fully functional keeping our work as the basis.

References

<references/>