Web page CSS structure automatic generation method and storage medium

By generating DOM trees and traversing nodes in WEB projects, and generating CSS structures according to preset rules, it solves the problem of low efficiency and difficulty in maintaining CSS code in complex projects, realizes the automatic generation and clarity of CSS structures, and improves development efficiency. .

CN112650479BActive Publication Date: 2024-09-03SHANGHAI BILIBILI TECH CO LTD
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN201910959384.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2019-10-10
Publication Date
2024-09-03
Estimated Expiration
2039-10-10

AI Technical Summary

Technical Problem

When the WEB project structure is complex and the DOM elements are multi-level, handwritten CSS code is inefficient, error-prone, and difficult to maintain. It is difficult for existing technology to achieve automatic generation and clarity of CSS structure.

Method used

By creating container elements in the web page, generating a DOM tree, and using JavaScript to traverse the nodes, the corresponding CSS structure is generated according to the preset rules to ensure the correctness of the hierarchy and order, and form clear CSS code.

Benefits of technology

It realizes the automatic generation of CSS structure in complex WEB projects, improves development efficiency, ensures the clarity and maintainability of CSS code, and reduces the complexity and error rate of handwritten code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112650479B
    Figure CN112650479B
Patent Text Reader

Abstract

The present invention relates to a method, device and storage medium for automatically generating a webpage CSS structure, and belongs to the field of Internet technology. Through the method of the present invention, firstly, a container element is created in a webpage, and an identifier of the container element is set; HTML code is assigned to the container element, and a DOM tree with the identifier of the container element as a parent node is generated; then all nodes of the DOM tree are traversed to generate a corresponding CSS structure, thereby realizing the automatic generation of the webpage CSS structure, especially when the WEB project structure is complex and there are many levels of DOM elements, the CSS structure can be made clear and easy to maintain, thereby greatly improving the work efficiency of programmers. In addition, the structure of the webpage CSS structure automatic generation device of the present invention is simple, the application cost is limited, and the application scope is also very wide.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of Internet technology, in particular to the field of WEB page technology, and specifically refers to a method for automatically generating a web page CSS structure and a storage medium. Background Art

[0002] Usually at the beginning of a WEB project, the project structure is relatively simple, with only a few pages. At this time, some basic CSS selectors such as .title, input, #app, etc. can be used to implement it, and a few lines of code can be written by hand. However, as the application grows, the front-end interaction on the page increases, and coupled with the semantic requirements of the HTML structure, the hierarchical relationship of DOM elements will increase, and the CSS code will become more and more complicated. At this time, if you still write the code line by line, the efficiency will be very low.

[0003] On the other hand, if the hierarchical relationship of the CSS structure is not handled properly, a common problem is that some elements of the page will be covered by other elements, although CSS provides some properties such as position:relative or position:absolute, and z-index values ​​to change the hierarchy of elements. However, if there are too many z-indexes or their values ​​are not fully considered, the hierarchical relationship of elements will become more complicated. In addition, many developers are accustomed to putting all new CSS codes directly at the end of the file instead of organizing them according to the corresponding DOM structure. When the CSS file exceeds 500 lines, it becomes very difficult to maintain, greatly reducing development efficiency.

[0004] Therefore, the current hand-written CSS structure method adopted by many developers in the industry has the following disadvantages: a. The hand-written method is not efficient; b. When there are many levels of DOM elements, the CSS code level will become more and more complicated and prone to errors; c. Since CSS styles do not have strong verification of code structure, the hand-written method is prone to problems such as chaotic code structure and difficulty in maintenance. How to achieve automatic generation of CSS structure when the WEB project structure is complex and there are many levels of DOM elements, so as to make the CSS structure clear, easy to maintain, and improve work efficiency has become an urgent problem to be solved in this field. Summary of the invention

[0005] The purpose of the present invention is to overcome the shortcomings of the above-mentioned prior art and provide a method, device and storage medium that can realize automatic generation of web page CSS structure when the WEB project structure is complex, thereby facilitating maintenance and improving the work efficiency of programmers.

[0006] In order to achieve the above-mentioned purpose, the method for automatically generating a webpage CSS structure of the present invention comprises the following steps:

[0007] (1) Create a container element in the web page and set the identifier of the container element;

[0008] (2) Obtain the HTML code of the web page;

[0009] (3) assigning the HTML code to the container element to generate a DOM tree with the identifier of the container element as the parent node;

[0010] (4) Traverse all nodes of the DOM tree and generate corresponding CSS structures.

[0011] In the method for automatically generating the CSS structure of a web page, the HTML code is assigned to the container element to generate a DOM tree with the identifier of the container element as the parent node. Specifically, the HTML structure of the web page is obtained according to the HTML code, and then the identifier of the container element is used as the parent node to automatically generate a DOM tree corresponding to the HTML structure.

[0012] In the method for automatically generating the CSS structure of a web page, traversing all nodes of the DOM tree to generate the corresponding CSS structure specifically comprises: utilizing JavaScript to traverse all nodes of the DOM tree to generate the corresponding CSS structure according to preset rules.

[0013] In the method for automatically generating the CSS structure of a web page, the corresponding CSS structure is generated according to preset rules, specifically: according to the hierarchical relationship and sequence of each node of the DOM tree, a CSS structure with corresponding hierarchy and sequence is generated.

[0014] In the method for automatically generating the CSS structure of a web page, the hierarchical relationship and the order of the nodes are determined according to one or all of the labels, logos, and style names of the nodes.

[0015] In the method for automatically generating a CSS structure for a web page, the style name is a pre-qualified style name, and the hierarchical relationship and order of the nodes are determined according to the pre-qualified style name of each node, and a CSS structure with a corresponding hierarchy and order is generated, which specifically includes the following steps:

[0016] (41) Determine whether the current element is given the preceding qualified style name. If so, obtain the preceding qualified style name. If not, set the preceding qualified style name to an empty string.

[0017] (42) Determine whether the container element is set with a container element style name, if so, proceed to step (43); if not, proceed to step (45);

[0018] (43) generating a new style rule according to the container element style name and the front-end qualified style name, each container element style name corresponding to a rule;

[0019] (44) Generate a new prefix-qualified style name based on the prefix-qualified style name and in combination with the first container element style name, and proceed to step (47);

[0020] (45) Determine whether the container element has child elements. If so, proceed to step (46). If not, terminate the traversal.

[0021] (46) Read the sub-element list of the container element, and read the sub-elements in order;

[0022] (47) Take the new prefix-qualified style name and the current child element as parameters and return to step (41).

[0023] The method for automatically generating a web page CSS structure also includes the following steps: (5) combining the generated CSS structures according to the generation level and order and outputting them together.

[0024] The method for automatically generating the CSS structure of a web page also includes the following steps: (6) storing the CSS structure as a .css file.

[0025] In the method for automatically generating the CSS structure of a web page, the .css file is stored in the STYLE tag under the HEAD tag of the HTML structure.

[0026] The present invention also provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the above-mentioned method for automatically generating a CSS structure of a web page is implemented.

[0027] The present invention also provides a web page CSS structure automatic generation device, which includes a processor and a memory, wherein the memory stores a computer program, and when the computer program is executed by the processor, the above-mentioned web page CSS structure automatic generation method is implemented.

[0028] The method and storage medium for automatically generating the CSS structure of a web page of the invention are adopted. Through the method, a container element is first created in the web page, and the identifier of the container element is set; the HTML code is assigned to the container element, and a DOM tree with the identifier of the container element as the parent node is generated; then all nodes of the DOM tree are traversed to generate the corresponding CSS structure, thereby realizing the automatic generation of the CSS structure of the web page, especially when the WEB project structure is complex and there are many levels of DOM elements, the CSS structure can be made clear and easy to maintain, thereby greatly improving the work efficiency of programmers. The structure of the automatic generation device of the CSS structure of the web page of the invention is simple, the application cost is limited, and the application scope is also very wide. The generation of the CSS framework structure is realized without involving built-in CSS or text collection. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] Figure 1 The present invention is a flowchart of the steps of the method for automatically generating the CSS structure of a web page.

[0030] Figure 2 It is a structural diagram of the device for automatically generating the CSS structure of a web page according to the present invention.

[0031] Figure 3 It is a schematic diagram of a flow chart of an embodiment of a method for automatically generating a webpage CSS structure according to the present invention. DETAILED DESCRIPTION

[0032] In order to more clearly understand the technical content of the present invention, the following embodiments are given in detail.

[0033] WEB pages are the basic elements of websites and the main carrier of website information display. Usually, web pages run in browsers. They can perform various operations based on user operations through programs running on the server, and then return the same or different page content based on different customers, different times and different operations.

[0034] HTML (Hyper Text Markup Language) is an application under the standard general markup language. It is both a specification and a standard. It tells the browser how to display the content (such as how to process text, how to arrange pictures, how to display pictures, etc.) by adding various tags to the text file. The browser will read the HTML file in sequence, and then interpret and display the marked content according to the tags. By combining HTML with other Web technologies (such as scripting languages, common gateway interfaces, components, etc.), developers can create powerful web pages. Therefore, HTML hypertext markup language is also the foundation of the World Wide Web (Web).

[0035] DOM, or Document Object Model, is a standard interface specification developed by W3C and a standard API for processing HTML and XML files. DOM provides an access model for the entire document, treating the document as a tree structure, with each node of the tree representing an HTML tag or a text item within a tag. The DOM tree structure accurately describes the interrelationships between tags in an HTML document. The process of converting an HTML or XML document into a DOM tree is called parsing. After the HTML document is parsed, it is converted into a DOM tree, so the processing of the HTML document can be achieved by operating the DOM tree. The DOM model not only describes the structure of the document, but also defines the behavior of the node object. Using the methods and properties of the object, you can easily access, modify, add, and delete the nodes and content of the DOM tree.

[0036] CSS (Cascading Style Sheets) is a computer language used to express the style of files such as HTML or XML. It can define style structures such as fonts, colors, positions, etc. It is often used to describe the way information on web pages is formatted and displayed. CSS can not only modify web pages statically, but also dynamically format various elements of web pages in conjunction with various scripting languages. CSS styles can be stored directly in HTML web pages or in separate style sheet files. In either case, the style sheet contains rules for applying styles to elements of a specified type. When used externally, style sheet rules are placed in an external style sheet document with the file extension .css. Style rules are formatting instructions that can be applied to elements in a web page, such as text paragraphs or links. Style rules consist of one or more style attributes and their values. Internal style sheets are placed directly in the web page, and external style sheets are saved in separate documents. The web page links to the external style file through a special tag.

[0037] JavaScript is a scripting language that is interpreted. It is a dynamically typed, weakly typed, prototype-based language with built-in support for types. Its interpreter is called the JavaScript engine, which is part of the browser and is widely used as a client-side scripting language. It was first used on HTML web pages to add dynamic functions to HTML web pages.

[0038] See also Figure 1 The figure is a flow chart of the steps of the method for automatically generating the CSS structure of a web page of the present invention.

[0039] In one embodiment, the method for automatically generating a webpage CSS structure comprises the following steps:

[0040] (1) Create a container element in the web page and set the identifier of the container element;

[0041] (2) Obtain the HTML code of the web page;

[0042] (3) assigning the HTML code to the container element to generate a DOM tree with the identifier of the container element as the parent node;

[0043] (4) Traverse all nodes of the DOM tree and generate corresponding CSS structures.

[0044] In a preferred embodiment, the HTML code is assigned to the container element to generate a DOM tree with the identifier of the container element as the parent node, specifically: the HTML structure of the web page is obtained according to the HTML code, and then the identifier of the container element is used as the parent node to automatically generate a DOM tree corresponding to the HTML structure.

[0045] The traversing of all nodes of the DOM tree to generate the corresponding CSS structure specifically includes: utilizing JavaScript to traverse all nodes of the DOM tree to generate the corresponding CSS structure according to preset rules.

[0046] In a further preferred embodiment, the generating of the corresponding CSS structure according to the preset rules is specifically: generating a CSS structure with corresponding hierarchy and order according to the hierarchy relationship and order of each node of the DOM tree. The hierarchy relationship and order of each node are determined according to one or all of the tags, logos, and style names of each node.

[0047] In a further preferred embodiment, the style name is a pre-qualified style name, and the hierarchical relationship and order of each node are determined according to the pre-qualified style name of each node, and a CSS structure with a corresponding hierarchy and order is generated, which specifically includes the following steps:

[0048] (41) Determine whether the current element is given the preceding qualified style name. If so, obtain the preceding qualified style name. If not, set the preceding qualified style name to an empty string.

[0049] (42) Determine whether the container element is set with a container element style name, if so, proceed to step (43); if not, proceed to step (45);

[0050] (43) generating a new style rule according to the container element style name and the front-end qualified style name, each container element style name corresponding to a rule;

[0051] (44) Generate a new prefix-qualified style name based on the prefix-qualified style name and in combination with the first container element style name, and proceed to step (47);

[0052] (45) Determine whether the container element has child elements. If so, proceed to step (46). If not, terminate the traversal.

[0053] (46) Read the sub-element list of the container element, and read the sub-elements in order;

[0054] (47) Take the new prefix-qualified style name and the current child element as parameters and return to step (41).

[0055] In a more preferred embodiment, the method further comprises the following steps:

[0056] (5) The generated CSS structures are combined and output according to the generation level and order.

[0057] (6) Store the CSS structure as a .css file.

[0058] The pre-qualified style name and the style name can be pre-set manually according to actual needs when creating the container element. The .css file is stored in the STYLE tag under the HEAD tag of the HTML structure.

[0059] The present invention also provides a web page CSS structure automatic generation device and a computer readable storage medium. The web page CSS structure automatic generation device is as follows: Figure 2 As shown, it includes a processor and a memory, wherein the memory is the computer-readable storage medium on which a computer program is stored. When the computer program is executed by the processor, the method for automatically generating a webpage CSS structure described in the above embodiment is implemented.

[0060] In the practical application of the present invention, by traversing the hierarchical structure of the DOM tree, the corresponding CSS structure is automatically generated according to the set rules (such as tags, #ID, .CLASS, etc.), thereby improving the development efficiency, the hierarchy of the generated CSS structure will not be wrong, and the overall CSS structure is clearer.

[0061] The following describes the method for automatically generating a web page CSS structure according to the present invention through specific embodiments.

[0062] Example 1

[0063] like Figure 3 As shown, the method for automatically generating a webpage CSS structure in this embodiment may include the following steps:

[0064] Step 1: Open a web page in the browser, create a container element in the web page, and set the container's unique identification (id), such as "main". The sample code is as follows:

[0065] <div id="main">

[0066] Step 2: Get the HTML code of the CSS organizational structure to be generated, such as a parent div element with a style name (class) of page, under which there are three div elements with style names (class) of header banner, content, and footer, and under the child div element with a style name (class) of header banner there is an h1 element with a style name (class) of page-title. The corresponding sample code is as follows:

[0067]

[0068] Step 3: Assign the HTML code obtained in step 2 to the innerHTML attribute of the container element. This step will generate a child DOM tree with the container element main in step 1 as the parent node;

[0069] Step 4: Traverse all nodes of the DOM tree and generate the CSS organizational structure, as follows:

[0070] Step 41 checks whether a "prefixed style name" is given, such as ".wrapper". The "prefixed style name" mentioned here refers to a style rule containing multiple sub-levels, such as ".aa.bb.cc{}. The part of the style name before the last level style name ".cc", i.e. ".aa.bb", is the "prefixed style name". If the "prefixed style name" is not given, the "prefixed style name" is set to an empty string, i.e. "".

[0071] Step 42: Check whether the container element has a style name set; if so, proceed to step 43; if not, jump directly to step 45;

[0072] Step 43: Generate new style rules by combining the "prefixed qualified style name" and the style name of the container element. The rule is one rule for each style name. For example, if the style name of the container element is class="book product", two rules will be generated:

[0073] .wrapper.book{}

[0074] .wrapper.product{}

[0075] Step 44: Based on the "prefixed style name", combined with the first style name of the container element, a new "prefixed style name" is generated, such as ".wrapper.book". The new "prefixed style name" will be used when processing child elements later.

[0076] Step 45: Determine whether the container element has child elements. If yes, proceed to step 46; if no, end this round of traversal.

[0077] Step 46: Read the sub-element list of the container element, and read the sub-elements in order;

[0078] Step 47: Bring the new "pre-qualified style name" and the current child element as parameters to step 41 to continue iterating through the child DOM tree;

[0079] Step 5: Combine all generated style rules in the order they were generated and output them as follows:

[0080] .page{}

[0081] .page.banner{}

[0082] .page.header{}

[0083] .page.header h1.page-title{}

[0084] .page.content{}

[0085] .page.footer{}

[0086] Example 2

[0087] Many WEB systems have permission control and require users to log in before they can use them. Therefore, the login page is essential as an entry point in many systems. In this scenario, users usually need to enter their username, password, verification code, etc. on the login page. In order to make the page more beautiful, you need to use the corresponding CSS to control the style of the page elements. In addition, you also need to write the corresponding CSS for the input box title, input prompt, error prompt, success prompt, etc.

[0088] The method for automatically generating the CSS structure of a web page of the present invention can generate a DOM tree according to a given HTML structure, and then traverse the DOM tree through JavaScript to obtain the hierarchical relationship and sequence of each node, and then generate a CSS structure of the corresponding hierarchy and sequence. The method generally includes the following steps:

[0089] Step A: Get the HTML structure of the page;

[0090] Step B: Run a piece of JavaScript code to automatically generate the corresponding CSS structure according to the HTML structure;

[0091] Step C: Save the CSS structure code as a file with a .css extension, or place it in the STYLE tag under the HEAD tag of the HTML structure.

[0092] The above method can be implemented by the following code:

[0093]

[0094]

[0095] The generated CSS structure code is as follows:

[0096] .page{}

[0097] .page.header{}

[0098] .page.header h1.page-title{}

[0099] .page.main{}

[0100] .page.main.login{}

[0101] .page.main.login.bind-only{}

[0102] .page.main.login legend.title{}

[0103] .page.main.login input.username{}

[0104] .page.main.login.password-wrap{}

[0105] .page.main.login.password-wrap input.password{}

[0106] .page.main.login.tips{}

[0107] .page.main.login input.submit{}

[0108] .page.main.login.other-select{}

[0109] .page.main.login.other-select a.signup{}

[0110] .page.main.login.other-select a.forget-password{}

[0111] .page.main.mask{}

[0112] .page.main.mask.msg-menu{}

[0113] .page.main.mask.msg-menu.text{}

[0114] .footer{}

[0115] Take a WEB project with more than 50 pages as an example. If at least 50 lines of CSS structure code are to be written for each page when the HTML code is complete, then at least 2,500 lines of CSS structure code are to be written for the entire project. According to the industry development efficiency, generally more than 500 lines of CSS code require a software engineer to spend one day, so 2,500 lines of code are the workload of a software engineer for 5 days. However, using the method of the present invention, when the HTML code is complete, it only takes a few seconds to generate the CSS structure of a page, and it only takes a few minutes to generate more than 2,500 lines of CSS structure code at most. Moreover, the generated CSS structure is more complete and has a clearer structure than manual writing.

[0116] The storage medium of the method for automatically generating the CSS structure of a web page of the invention is adopted. Through the method, a container element is first created in the web page, and the identifier of the container element is set; the HTML code is assigned to the container element, and a DOM tree with the identifier of the container element as the parent node is generated; then all nodes of the DOM tree are traversed to generate the corresponding CSS structure, thereby realizing the automatic generation of the CSS structure of the web page, especially when the WEB project structure is complex and there are many levels of DOM elements, the CSS structure can be made clear and easy to maintain, thereby greatly improving the work efficiency of programmers. In addition, the structure of the automatic generation device of the CSS structure of a web page of the invention is simple, the application cost is limited, and the application scope is also very wide.

[0117] In this specification, the present invention has been described with reference to specific embodiments thereof. However, it is apparent that various modifications and variations may be made without departing from the spirit and scope of the present invention. Therefore, the specification and drawings should be regarded as illustrative rather than restrictive.

Claims

1. A method for automatically generating a webpage CSS structure, characterized in that: The method comprises the following steps: Create a container element in the web page and set the identifier of the container element; Get the HTML code of the web page; Acquire the HTML structure of the webpage according to the HTML code, and then automatically generate a DOM tree corresponding to the HTML structure by using the identifier of the container element as a parent node; Traversing all nodes of the DOM tree, determining the hierarchical relationship and order of each node according to the pre-qualified style name of each node of the DOM tree, and generating a CSS structure with a corresponding hierarchy and order, specifically including the following steps: (41) Determine whether the current element is given the preceding qualifying style name. If so, obtain the preceding qualifying style name. If not, set the preceding qualifying style name to an empty string. (42) Determine whether the container element has a container element style name set. If so, proceed to step (43); if not, proceed to step (45); (43) generating a new style rule according to the container element style name and the preceding qualified style name, wherein each container element style name corresponds to one rule; (44) Generate a new prefix-qualified style name based on the prefix-qualified style name and the first container element style name, and proceed to step (47); (45) Determine whether the container element has child elements. If so, proceed to step (46). If not, terminate the traversal. (46) Reading a list of sub-elements of the container element, and reading the sub-elements in order; (47) Take the new prefix-qualified style name and the current child element as parameters and return to step (41).

2. The method for automatically generating a webpage CSS structure according to claim 1, characterized in that: The traversal of all nodes of the DOM tree is specifically as follows: Use JavaScript to traverse all nodes of the DOM tree.

3. The method for automatically generating a webpage CSS structure according to claim 1, characterized in that: The method further comprises the following steps: (5) Combine the generated CSS structures according to the generation level and order and output them together.

4. The method for automatically generating a webpage CSS structure according to claim 3, characterized in that: The method further comprises the following steps: (6) Store the CSS structure as a .css file.

5. The method for automatically generating a webpage CSS structure according to claim 4, characterized in that: The .css file is stored in the STYLE tag under the HEAD tag of the HTML structure.

6. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method for automatically generating a web page CSS structure according to any one of claims 1 to 5 is implemented.

Citation Information

Patent Citations

  • Method and device for extracting cascading style sheet

    CN106886398A

  • Intelligent switching method and device for browser kernel, terminal and storage medium

    CN109684584A

  • Cascading style sheet maintenance method and device, computer equipment and storage medium

    CN109725965A

  • Style and layout caching of web content

    US20120110437A1

  • Interactive web application editor

    US20160070813A1