Cascading style sheet file processing method, device, equipment and storage medium
By generating and splitting the abstract syntax tree and determining and deleting redundant style rules, the web page file size increase and loading speed problems caused by redundant rules in CSS files are solved, and the web page file size reduction and loading speed improvement is achieved.
Patent Information
- Application Number
- CN202010981021.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-09-17
- Publication Date
- 2024-11-01
- Estimated Expiration
- 2040-09-17
AI Technical Summary
With the frequent update of web page design, more and more redundant style rules appear in CSS files, resulting in the increase in the size of web page files and affecting the loading speed.
By obtaining the cascading style sheet file and the corresponding markup language file, an abstract syntax tree is generated, and the tree structure is split when dynamic elements are detected, the target style rules matching the abstract syntax tree are determined, and the target cascading style sheet file is generated to delete redundant Remaining rules.
Effectively reduces the size of web page files, improves loading speed, and avoids the error recognition of effective style rules as redundancy.
Smart Images

Figure CN112231281B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, device, equipment and storage medium for processing a cascading style sheet file. Background Art
[0002] CSS (Cascading Style Sheets) is a computer language used to express the styles of files such as HTML (HyperTextMarkup Language) and XML (eXtensible Markup Language). It can not only modify web pages statically, but also format elements in web pages with various scripting languages.
[0003] At present, with the continuous development of computer technology and the ever-changing user needs, web page design changes frequently. As web page design changes, more and more useless style rules (called redundant style rules) are likely to appear in the CSS files of the web pages, which will cause the size of the web page files to become larger and larger, thereby affecting the loading speed of the web page files. Summary of the invention
[0004] The purpose of the embodiments of the present application is to provide a method, device, equipment and storage medium for processing cascading style sheet files, so as to achieve the beneficial effect of deleting redundant style rules in cascading style sheet files, thereby reducing the size of web page files and further improving the loading speed of web page files. The specific technical solution is as follows:
[0005] The present application provides a method for processing a cascading style sheet file, the method comprising:
[0006] Obtaining a cascading style sheet file to be processed and a markup language file corresponding to the cascading style sheet file;
[0007] generating a first abstract syntax tree of the markup language file, and when it is determined that the markup language file contains dynamic elements, splitting the first abstract syntax tree to obtain at least one second abstract syntax tree;
[0008] Obtaining multiple style rules contained in the cascading style sheet file;
[0009] Determine a target style rule matching at least one of the second abstract syntax trees from the plurality of style rules;
[0010] A target cascading style sheet file is generated based on the target style rule.
[0011] The present application provides a device for processing a cascading style sheet file, the device comprising:
[0012] A file acquisition module, used to acquire a cascading style sheet file to be processed and a markup language file corresponding to the cascading style sheet file;
[0013] A tree generation module, configured to generate a first abstract syntax tree of the markup language file, and when it is determined that the markup language file contains dynamic elements, split the first abstract syntax tree to obtain at least one second abstract syntax tree;
[0014] A style acquisition module, used to acquire multiple style rules contained in the cascading style sheet file;
[0015] A style matching module, configured to determine a target style rule matching at least one of the second abstract syntax trees from the plurality of style rules;
[0016] The file generation module is used to generate a target cascading style sheet file based on the target style rule.
[0017] The present application provides a device, including a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus;
[0018] Memory, used to store computer programs;
[0019] The processor is used to implement the methods provided in the above-mentioned various optional implementations when executing the program stored in the memory.
[0020] The present application provides a storage medium, in which computer instructions are stored. When the storage medium is run on a computer, the computer is enabled to execute the methods provided in the above-mentioned various optional implementations.
[0021] The present application provides a computer program product or a computer program, which includes computer instructions stored in a computer-readable storage medium. A processor of an electronic device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the electronic device executes the methods provided in the above-mentioned various optional implementations.
[0022] The technical solution provided by the embodiment of the present application obtains the cascading style sheet file to be processed and the markup language file corresponding to the cascading style sheet file in the markup, generates a first abstract syntax tree of the markup language file, splits the first abstract syntax tree when it is determined that the markup language file contains dynamic elements, obtains multiple style rules contained in the cascading style sheet file, determines a target style rule matching the at least one second abstract syntax tree from the multiple style rules, and generates a target cascading style sheet file based on the target style rule, which can delete redundant style rules in the cascading style sheet file, thereby reducing the size of the web page file and further improving the loading speed of the web page file.
[0023] Furthermore, when a markup language file contains dynamic elements, the first abstract syntax tree of the markup language file is split to obtain at least one second abstract syntax tree that can completely and accurately represent the structure of the markup language file. Then, the target style rules in the markup language file, i.e., the valid style rules, are determined based on the at least one second abstract syntax tree. This can effectively avoid the incorrect identification of valid style rules as redundant style rules. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0025] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0026] Figure 1 A flowchart of an embodiment of a method for processing a cascading style sheet file provided in an embodiment of the present application;
[0027] Figure 2 This is a schematic diagram of the file selection interface;
[0028] Figure 3 is an example of a first abstract syntax tree;
[0029] Figure 4 is another example of the first abstract syntax tree;
[0030] Figure 5 is an example of a second abstract syntax tree;
[0031] Figure 6 is another example of the second abstract syntax tree;
[0032] Figure 7 is another example of the first abstract syntax tree;
[0033] Figure 8 is another example of the second abstract syntax tree;
[0034] Fig. 9 is another example of the first abstract syntax tree;
[0035] Fig.10 is another example of the second abstract syntax tree;
[0036] Fig.11 An implementation process of step 105 provided in an embodiment of the present application;
[0037] Fig.12 A block diagram of an embodiment of a cascading style sheet file processing device provided in an embodiment of the present application;
[0038] Fig.13 A schematic diagram of the structure of a device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0039] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0040] To facilitate understanding of the embodiments of the present application, some conceptual terms involved in the embodiments of the present application are briefly described below:
[0041] 1. Markup language:
[0042] Markup language, also known as markup language, is a computer text code that combines text and other text-related information to show the details of document structure and data processing. The widely used markup languages include but are not limited to: HTML, XML, WXML (WeiXin Markup Language, WeChat Markup Language, etc.), among which WXML is used in WeChat Mini Programs to specify the framework structure of the WeChat Mini Program interface.
[0043] 2. Cascading Style Sheets:
[0044] Cascading Style Sheets is a computer language used to express the styles of files such as HTML and XML. It can not only modify web pages statically, but also format elements in web pages in conjunction with various scripting languages.
[0045] Furthermore, WeChat CSS is a set of style languages used to describe the styles of components in WeChat markup language, i.e., WeChat CSS is used to determine how components in WeChat markup language should be displayed. Similar to WXML, WeChat CSS files are also used in WeChat mini-programs to specify the display styles of the framework and elements of the WeChat mini-program interface.
[0046] 3. Style rules, style selectors:
[0047] CSS files contain various style rules, which are composed of style selectors and declaration blocks. Among them, style selectors are used to declare the objects to be acted on, including but not limited to: class selectors, element selectors, attribute selectors, wildcard selectors, etc. Declarations are descriptions of specific style rules, consisting of attributes and values. For example, the following is an example of a style rule:
[0048] .first{ / / Style selector, the object to be acted on is the element with attribute class="first";
[0049] color: red / / declaration block, the attribute is color, the value is red;
[0050] }
[0051] 4. Abstract Syntax Tree (AST):
[0052] The abstract syntax tree is an abstract representation of the grammatical structure of the source code. It represents the grammatical structure of the programming language in a tree form, and each node in the tree represents a structure in the source code.
[0053] Some conceptual terms involved in the embodiments of the present application are explained above. The present application will be further explained below with reference to specific embodiments in conjunction with the accompanying drawings. The embodiments do not constitute a limitation on the embodiments of the present application.
[0054] See also Figure 1 , is a flowchart of an embodiment of a method for processing a cascading style sheet file provided in an embodiment of the present application. In one example, the method can be applied to an electronic device, where the electronic device can be a hardware device that supports network connection and provides various network services, including but not limited to smart phones, tablet computers, laptop computers, desktop computers, servers, etc. Figure 1 As shown, the method comprises the following steps:
[0055] Step 101: Acquire a cascading style sheet file to be processed and a markup language file corresponding to the cascading style sheet file.
[0056] Here, the markup language file corresponding to the CSS file to be processed refers to: the CSS file to be processed is used to describe the component style of the markup language file to which it corresponds. In one example, the CSS file to be processed is a WeChat CSS file, and the corresponding markup language file is a WeChat markup language file.
[0057] In the above example, as an embodiment, the electronic device can display Figure 2 The example interface. In the application, users can click Figure 2 In the interface shown, the electronic device detects that the "Select Mini Program Directory" button is triggered, and can further receive the mini program root directory selected by the user through the interface interaction, such as " / f / miniPrograms / projectA". Further, the user can click Figure 2 The “Start Scanning” button in the interface shown, when the electronic device detects that the “Start Scanning” button is triggered, can execute the cascading style sheet file processing method provided in the embodiment of the present application.
[0058] In the application, all pages of the WeChat applet are declared in the app.json file in the root directory of the applet. Therefore, in this step 101, the electronic device can find the app.json file in the received applet root directory, and then find all the pages of the WeChat applet. The file with the suffix .wxml under each page is the WeChat markup language file used to describe the frame structure of the page, and the file with the suffix .wxss is the WeChat cascading style sheet file used to describe the display style of the page. In this way, the electronic device can enter each page in turn and obtain the WeChat cascading style sheet file to be processed and the corresponding WeChat markup language file according to the suffix name.
[0059] Step 102: Generate a first abstract syntax tree of the markup language file.
[0060] In this step 102, an abstract syntax tree (hereinafter referred to as the first abstract syntax tree for the convenience of description) of the markup language file can be generated through lexical analysis and grammatical analysis. Among them, lexical analysis is also called scanning Scanner. When performing lexical analysis on the source code, it will start from the first character of the source code and read the characters one by one. When encountering a space, an operator or a special symbol, it is considered that a sentence ends, and then each sentence read is merged into identifiers one by one according to a predetermined rule to obtain a tag list (also called a one-dimensional array). Syntax analysis is also called a parser. Through syntax analysis, the array obtained by lexical analysis can be converted into a tree form, and the syntax can be verified at the same time. If a syntax error occurs, a syntax error is thrown.
[0061] For example, the following is an example of a WeChat markup language file:
[0062] <view class=“first”wx:if=“{{test}}”> First Element
[0063] <view class=“second”wx:else> Second Element
[0064] <view class=“third”> Third Element
[0065] The first abstract syntax tree generated by the above lexical analysis and grammatical analysis is as follows Figure 3 shown.
[0066] Step 103: When it is determined that the markup language file contains dynamic elements, the first abstract syntax tree is split to obtain at least one second abstract syntax tree.
[0067] In the example of step 102 above, <view> ELement< / view> Represents an element in a markup language file. <view> is the start tag of the element,< / view> It is the end tag of the element, and the start tag includes attributes set for the element, including but not limited to: class attribute, control attribute (including but not limited to if, else, elif, etc.), etc. Furthermore, the class attribute can be used to apply one or more style rules to the element to achieve the function of setting the element style; the control attribute is used to determine whether to render the element.
[0068] Specifically, in the example of step 102, when {test} is true, the first element and the third element are rendered, and when {test} is false, the second element and the third element are rendered. It can be seen that the first element and the second element in the markup language file in the example of step 102 are dynamically rendered, and in the embodiment of the present application, the dynamically rendered elements are referred to as dynamic elements.
[0069] Further, it is assumed that the cascading style sheet file corresponding to the markup language file in the above step 102 is as follows:
[0070]
[0071] The CSS file in the above example contains the following two style rules: .second+.third, .first+.third. However Figure 3 The first abstract syntax tree shown in the example does not contain a tree structure containing the style rule .first+.third. Therefore, in this case, if Figure 3The first abstract syntax tree shown in the example determines redundant style rules in the cascading style sheet file, and it is very likely that the valid style rule .first+.third is determined as a redundant style rule, resulting in incorrect recognition.
[0072] Based on this, in this step 103, when determining that the markup language file contains dynamic elements, the first abstract syntax tree is split to obtain at least one abstract syntax tree (hereinafter referred to as the second abstract syntax tree for ease of description), so that the at least one second abstract syntax tree can completely and accurately represent the structure of the markup language file containing the dynamic elements, thereby improving the accuracy of the subsequent determination of redundant style rules in the cascading style sheet file based on the abstract syntax tree of the markup language file, and avoiding the incorrect identification of valid style rules as redundant style rules. As for how to split the first abstract syntax tree to obtain at least one second abstract syntax tree, it is illustrated by examples in different embodiments below, and will not be described in detail here.
[0073] In combination with the above description, as an embodiment, in this step 103, if the markup language file contains an element with a control attribute, the element with the control attribute is determined as a dynamic element, and the control attribute includes but is not limited to: if attribute, else attribute, elif attribute, etc. Among them, in the WeChat markup language, the wx:if attribute is equivalent to the if attribute, the wx:else attribute is equivalent to the else attribute, and the wx:elif attribute is equivalent to the elif attribute.
[0074] Step 104: Acquire multiple style rules contained in the cascading style sheet file.
[0075] In the embodiment of the present application, in order to detect redundant styles in a cascading style sheet file to be processed, the style rules contained in the cascading style sheet file to be processed may be first obtained. It is understandable that, usually, a cascading style sheet file contains multiple style rules.
[0076] As an embodiment, multiple style rules contained in the cascading style sheet file can be obtained by generating an abstract syntax tree of the cascading style sheet file. As for how to generate the abstract syntax tree of the cascading style sheet file, please refer to the above description of generating the first abstract syntax tree, which will not be described in detail here.
[0077] Step 105: Determine a target style rule that matches at least one second abstract syntax tree from the plurality of style rules.
[0078] In the embodiment of the present application, the style rule matches the second abstract syntax tree means: the second abstract syntax tree contains the tree structure of the style rule. For the convenience of description, the style rule that matches at least one second abstract syntax tree is referred to as the target style rule.
[0079] Since the structure of the markup language file can be fully and accurately represented through all the second abstract syntax trees, in this step 105, for each style rule obtained in step 104, it can be checked whether each second abstract syntax tree obtained in step 103 contains the tree structure of the style rule. If any second abstract syntax tree contains the tree structure of the style rule, the style rule can be determined as a target style rule that matches at least one second abstract syntax tree.
[0080] As an embodiment, for each style rule obtained in step 104, each second abstract syntax tree can be traversed to find a tree structure (hereinafter referred to as a target tree structure) containing the style rule in the traversed second abstract syntax tree. If the target tree structure is found in any second abstract syntax tree, the style rule is determined as the target style rule. As for how to find the target tree structure containing the style rule in the traversed second abstract syntax tree, it will be described below. Fig.12 The illustrated embodiment is described below and will not be described in detail here.
[0081] Step 106: Generate a target cascading style sheet file based on the target style rule.
[0082] It can be seen from the above description that the target style rule is a valid style rule. Among the multiple style rules obtained in step 104, style rules other than the target style rule are invalid redundant style rules. Redundant style rules do not have effective display in the markup language file, so there is no need for redundant style rules to exist in the cascading style sheet file.
[0083] Based on this, in the embodiment of the present application, a new cascading style sheet file (hereinafter referred to as the target cascading style sheet file) can be generated based on the target style rule. This means that the target cascading style sheet file does not have redundant style rules, thereby deleting the redundant code in the cascading style sheet file to be processed and reducing the size of the web page file.
[0084] As an embodiment, a target cascading style sheet file is generated through an abstract syntax tree of the original cascading style sheet file and based on target style rules.
[0085] It can be seen from the above embodiments that by obtaining the cascading style sheet file to be processed and the markup language file corresponding to the cascading style sheet file in the tag, a first abstract syntax tree of the markup language file is generated, and when it is determined that the markup language file contains dynamic elements, the first abstract syntax tree is split to obtain at least one second abstract syntax tree, and multiple style rules contained in the cascading style sheet file are obtained, and a target style rule matching the at least one second abstract syntax tree is determined from the multiple style rules. A target cascading style sheet file is generated based on the target style rule, which can achieve the deletion of redundant style rules in the cascading style sheet file, thereby reducing the size of the web page file and further improving the loading speed of the web page file.
[0086] Furthermore, when a markup language file contains dynamic elements, the first abstract syntax tree of the markup language file is split to obtain at least one second abstract syntax tree that can completely and accurately represent the structure of the markup language file. Then, the target style rules in the markup language file, i.e., the valid style rules, are determined based on the at least one second abstract syntax tree. This can effectively avoid the incorrect identification of valid style rules as redundant style rules.
[0087] exist Figure 2 Based on the illustrated embodiment, as an embodiment, when a markup language file includes an element with an if attribute, there are the following two rendering results: (1) when the if condition is true, the element is rendered; (2) when the if condition is false, the element is not rendered. Therefore, when a markup language file includes an element with an if attribute, a first abstract syntax tree is split to obtain at least one second abstract syntax tree, including: deleting the tree node containing the if attribute in the first abstract syntax tree to obtain a second abstract syntax tree; and using the first abstract syntax tree as another second abstract syntax tree.
[0088] For example, if Figure 4 As shown in FIG. 1 , an example of the first abstract syntax tree is shown. Based on the above description, the tree node containing the if attribute in the first abstract syntax tree is deleted, and then Figure 5 The second abstract syntax tree shown in the example. Figure 4 The first abstract syntax tree shown in the example is split into Figure 4 and Figure 5 Two second abstract syntax trees are shown as examples.
[0089] exist Figure 2On the basis of the illustrated embodiment, as an embodiment, when a markup language file includes an element with an if attribute and an element with an else attribute, there are the following two rendering results: (1) when the if condition is true, the element with the if attribute is rendered; (2) when the if condition is false, the element with the else attribute is rendered. It can be seen that at the same time, only one of the element with the if attribute and the element with the else attribute is rendered. Therefore, when a markup language file includes an element with an if attribute and an element with an else attribute, the first abstract syntax tree is split to obtain at least one second abstract syntax tree, including: deleting the tree node containing the if attribute in the first abstract syntax tree to obtain the second abstract syntax tree; deleting the tree node containing the else attribute in the first abstract syntax tree to obtain the second abstract syntax tree.
[0090] For example, for Figure 3 Based on the above description, the tree node containing the if attribute in the first abstract syntax tree is deleted, and then the first abstract syntax tree can be obtained. Figure 6 (a) The second abstract syntax tree shown in the example; delete the tree node containing the else attribute in the first abstract syntax tree, and you can get Figure 6 (b) The second abstract syntax tree shown in the example. Figure 3 The first abstract syntax tree shown in the example is split into Figure 6 The two second abstract syntax trees shown in the example.
[0091] exist Figure 2 Based on the illustrated embodiment, as an embodiment, when a markup language file includes an element with an if attribute and an element with an elif attribute, there are the following three rendering results: (1) when the if condition is true, the element with the if attribute is rendered; (2) when the if condition is false and the elif condition is true, the element with the elif attribute is rendered; (3) when the if condition is false and the elif condition is also false, neither the element with the if attribute nor the element with the elif attribute is rendered. Therefore, when a markup language file includes an element with an if attribute and an element with an elif attribute, the first abstract syntax tree is split to obtain at least one second abstract syntax tree, including: deleting the tree node containing the if attribute in the first abstract syntax tree to obtain the second abstract syntax tree; deleting the tree node containing the elif attribute in the first abstract syntax tree to obtain the second abstract syntax tree; deleting the tree node containing the if attribute and the tree node containing the elif attribute in the first abstract syntax tree to obtain the second abstract syntax tree.
[0092] For example, if Figure 7 As shown in FIG. 1 , another example of the first abstract syntax tree is shown. Based on the above description, the tree node containing the if attribute in the first abstract syntax tree is deleted, and then Figure 8 (a) The second abstract syntax tree shown in the example; delete the tree node containing the elif attribute in the first abstract syntax tree, and you can get Figure 8 (b) The second abstract syntax tree shown in the example; delete the tree nodes containing the if attribute and the elif attribute in the first abstract syntax tree, and you can get Figure 8 (c) The second abstract syntax tree shown in the example. Figure 7 The first abstract syntax tree shown in the example is split into Figure 8 The three second abstract syntax trees shown in the example.
[0093] exist Figure 2 Based on the illustrated embodiment, as an embodiment, when a markup language file includes an element with an if attribute, an element with an elif attribute, and an element with an else attribute, there are the following three rendering results: (1) when the if condition is true, the element with the if attribute is rendered; (2) when the elif condition is true, the element with the elif attribute is rendered; (3) when the if condition is false and the elif condition is also false, the element with the else attribute is rendered. Therefore, when a markup language file contains elements with if attributes, elements with elif attributes, and elements with else attributes, the first abstract syntax tree is split to obtain at least one second abstract syntax tree, including: deleting the tree nodes containing the if attribute and the tree nodes containing the elif attribute in the first abstract syntax tree to obtain the second abstract syntax tree; deleting the tree nodes containing the elif attribute and the tree nodes containing the else attribute in the first abstract syntax tree to obtain the second abstract syntax tree; deleting the tree nodes containing the if attribute and the tree nodes containing the else attribute in the first abstract syntax tree to obtain the second abstract syntax tree.
[0094] For example, if Fig. 9 Based on the above description, the tree nodes containing the if attribute and the tree nodes containing the elif attribute in the first abstract syntax tree are deleted, and then Fig.10 (a) The second abstract syntax tree shown in the example; delete the tree nodes containing the elif attribute and the tree nodes containing the else attribute in the first abstract syntax tree, and you can get Fig.10 (b) The second abstract syntax tree shown in the example; delete the tree nodes containing the if attribute and the tree nodes containing the else attribute in the first abstract syntax tree, and you can get Fig.10(c) The second abstract syntax tree shown in the example. Fig. 9 The first abstract syntax tree shown in the example is split into Fig.10 The three second abstract syntax trees shown in the example.
[0095] The following will further explain how to search for a tree structure containing a certain style rule in the traversed second abstract syntax tree in the above step 105 with reference to the accompanying drawings. The embodiment does not constitute a limitation on the embodiments of the present application.
[0096] See also Fig.11 , is an implementation process of step 105 provided in an embodiment of the present application, and the process includes the following steps:
[0097] Step 1101: If the style rule contains adjacent sibling style selectors, the last one of the adjacent sibling style selectors is determined as the current style selector.
[0098] Step 1102 , searching for a target tree node containing the current style selector in the traversed second abstract syntax tree; if the target tree node is not found, executing step 1106 ; if the target tree node is found, executing step 1103 .
[0099] Step 1103: determine the previous style selector of the current style selector as the current style selector, and determine the previous tree node of the target tree node as the new target tree node.
[0100] Step 1104, determine whether the new target tree node contains the current style selector, if not, execute step 1106; if contained, return to execute step 1103, until the current style selector does not have a previous style selector, execute step 1105.
[0101] Step 1105: Determine whether a target tree structure containing style rules is found in the traversed second abstract syntax tree.
[0102] Step 1106: Determine whether the target tree structure containing the style rule is not found in the traversed second abstract syntax tree.
[0103] The adjacent sibling selector can select an element that is immediately after another element and both elements have the same parent element. For example, in the example of step 103 above, the style rule .first+.third includes the two adjacent sibling selectors .first and .third.
[0104] The following combination Figure 6 The above steps 1101 to 1106 are described by way of example:
[0105] Taking the style rule .first+.third as an example, according to the description in step 1101 above, .third is determined as the current style selector. Assuming that the current traversal reaches Figure 6 (a) The second abstract structure tree shown in the example, according to the description in the above step 1102, Figure 6 (a) The target tree node containing the current style selector can be found in the second abstract structure tree shown in the example, and step 1103 is continued. According to the description in the above step 1103, .first is determined as the current style selector, and Figure 6 The second-level left node in (a) is determined as the new target tree node, and step 1104 is continued. According to the description in step 1104, it is determined that the new target tree node contains the style selector .second, but does not contain the current style selector .first. At this point, step 1106 is executed to determine Figure 6 (a) The target tree structure containing the .first+.third style rule is not found in the second abstract syntax tree shown in the example.
[0106] Continue to assume that the current traversal is Figure 6 (b) The second abstract structure tree shown in the example, according to the description in the above step 1102, Figure 6 (b) The target tree node containing the current style selector can be found in the second abstract structure tree shown in the example, and step 1103 is continued. According to the description in the above step 1103, .first is determined as the current style selector, and Figure 6 (b) The second layer left node is determined as the new target tree node, and step 1104 is continued. According to the description in step 1104, it is determined that the new target tree node contains the current style selector .first. At this point, step 1105 is executed to determine Figure 6 (b) The target tree structure containing the .first+.third style rule is found in the second abstract syntax tree shown in the example.
[0107] pass Fig.11 The process shown implements searching the tree structure containing the style rule in the second abstract syntax tree corresponding to the markup language file when the style rule contains adjacent sibling style selectors, so as to determine whether the style rule is a redundant style rule according to the search result.
[0108] Corresponding to the above-mentioned embodiment of the method for processing a cascading style sheet file, the present application also provides an embodiment of an apparatus for processing a cascading style sheet file.
[0109] See also Fig.12, is a block diagram of an embodiment of a cascading style sheet file processing device provided in an embodiment of the present application, the device includes: a file acquisition module 121, a tree generation module 122, a style acquisition module 123, a style matching module 124, and a file generation module 125.
[0110] The file acquisition module 121 is used to acquire the cascading style sheet file to be processed and the markup language file corresponding to the cascading style sheet file;
[0111] A tree generation module 122, configured to generate a first abstract syntax tree of the markup language file, and when it is determined that the markup language file contains dynamic elements, split the first abstract syntax tree to obtain at least one second abstract syntax tree;
[0112] The style acquisition module 123 is used to acquire a plurality of style rules contained in the cascading style sheet file;
[0113] A style matching module 124, configured to determine a target style rule matching at least one of the second abstract syntax trees from the plurality of style rules;
[0114] The file generation module 125 is used to generate a target cascading style sheet file based on the target style rule.
[0115] In a possible implementation manner, the tree generation module 122 determines that the markup language file contains dynamic elements, including:
[0116] If the markup language file contains an element with a control attribute, the element with the control attribute is determined as a dynamic element; the control attribute includes at least: an if attribute, an else attribute, and an elif attribute.
[0117] In a possible implementation, the tree generation module 122 splits the first abstract syntax tree to obtain at least one second abstract syntax tree, including:
[0118] In a case where the markup language file includes an element with an if attribute, obtaining the second abstract syntax tree includes at least one of the following:
[0119] Deleting the tree node containing the if attribute in the first abstract syntax tree to obtain a second abstract syntax tree;
[0120] The first abstract syntax tree is used as the second abstract syntax tree.
[0121] In a possible implementation, the tree generation module 122 splits the first abstract syntax tree to obtain at least one second abstract syntax tree, including:
[0122] In a case where the markup language file includes an element with an if attribute and an element with an else attribute, obtaining the second abstract syntax tree includes at least one of the following:
[0123] Deleting the tree node containing the if attribute in the first abstract syntax tree to obtain a second abstract syntax tree;
[0124] The tree nodes including the else attribute in the first abstract syntax tree are deleted to obtain a second abstract syntax tree.
[0125] In a possible implementation, the tree generation module 122 splits the first abstract syntax tree to obtain at least one second abstract syntax tree, including:
[0126] In a case where the markup language file includes an element with an if attribute and an element with an elif attribute, obtaining the second abstract syntax tree includes at least one of the following:
[0127] Deleting the tree node containing the if attribute in the first abstract syntax tree to obtain a second abstract syntax tree;
[0128] Deleting the tree node containing the elif attribute in the first abstract syntax tree to obtain a second abstract syntax tree;
[0129] The tree nodes including the if attribute and the tree nodes including the elif attribute in the first abstract syntax tree are deleted to obtain a second abstract syntax tree.
[0130] In a possible implementation, the tree generation module 122 splits the first abstract syntax tree to obtain at least one second abstract syntax tree, including:
[0131] In a case where the markup language file includes an element with an if attribute, an element with an elif attribute, and an element with an else attribute, obtaining the second abstract syntax tree includes at least one of the following:
[0132] Deleting the tree nodes including the if attribute and the tree nodes including the elif attribute in the first abstract syntax tree to obtain a second abstract syntax tree;
[0133] Deleting the tree nodes including the elif attribute and the tree nodes including the else attribute in the first abstract syntax tree to obtain a second abstract syntax tree;
[0134] The tree nodes including the if attribute and the tree nodes including the else attribute in the first abstract syntax tree are deleted to obtain a second abstract syntax tree.
[0135] In a possible implementation, the style matching module 124 determines a target style rule that matches at least one of the second abstract syntax trees from a plurality of style rules, including:
[0136] For each of the style rules, traverse each of the second abstract syntax trees to search for a target tree structure that contains the style rule in the traversed second abstract syntax tree;
[0137] If the target tree structure is found in any of the second abstract syntax trees, the style rule is determined as a target style rule.
[0138] In a possible implementation, the style matching module 124 searches for a target tree structure containing the style rule in the traversed second abstract syntax tree, including:
[0139] If the style rule contains adjacent sibling style selectors, the last one of the adjacent sibling style selectors is determined as the current style selector;
[0140] Searching for a target tree node containing the current style selector in the traversed second abstract syntax tree;
[0141] If the target tree node is not found, determining that the target tree structure containing the style rule is not found in the traversed second abstract syntax tree;
[0142] If the target tree node is found, the previous style selector of the current style selector is determined as the current style selector, the previous tree node of the target tree node is determined as the new target tree node, and it is determined whether the new target tree node contains the current style selector;
[0143] If not, determining that the target tree structure containing the style rule is not found in the traversed second abstract syntax tree;
[0144] If included, return to the step of determining the previous style selector of the current style selector as the current style selector, until the current style selector does not have a previous style selector, and determine to find the target tree structure containing the style rule in the traversed second abstract syntax tree.
[0145] The present application also provides an electronic device, such as Fig.13 As shown, it includes a processor 1301, a communication interface 1302, a memory 1303 and a communication bus 1304, wherein the processor 1301, the communication interface 1302, and the memory 1303 communicate with each other through the communication bus 1304.
[0146] Memory 1303, used for storing computer programs;
[0147] The processor 1301 is used to implement the following steps when executing the program stored in the memory 1303:
[0148] Obtaining a cascading style sheet file to be processed and a markup language file corresponding to the cascading style sheet file;
[0149] generating a first abstract syntax tree of the markup language file, and when it is determined that the markup language file contains dynamic elements, splitting the first abstract syntax tree to obtain at least one second abstract syntax tree;
[0150] Obtaining multiple style rules contained in the cascading style sheet file;
[0151] Determine a target style rule matching at least one of the second abstract syntax trees from the plurality of style rules;
[0152] A target cascading style sheet file is generated based on the target style rule.
[0153] The communication bus mentioned in the above server may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0154] The communication interface is used for communication between the above server and other devices.
[0155] The memory may include a random access memory (RAM) or a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0156] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0157] In another embodiment provided by the present application, a storage medium is provided. The storage medium stores instructions. When the instructions are executed on a computer, the computer executes the method for processing a cascading style sheet file described in any of the above embodiments.
[0158] In another embodiment provided by the present application, a computer program product or a computer program is also provided, the computer program product or the computer program includes a computer instruction, and the computer instruction is stored in a computer-readable storage medium. A processor of an electronic device reads the computer instruction from the computer-readable storage medium, and the processor executes the computer instruction, so that the electronic device executes the method for processing a cascading style sheet file described in any of the above embodiments.
[0159] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a storage medium, or transmitted from one storage medium to another storage medium, for example, the computer instructions may be transmitted from a website site, a computer, a server or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The storage medium may be any available medium that a computer can access or a data storage device such as a server or a data center that includes one or more available media integrations. The available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive Solid State Disk (SSD)), etc.
[0160] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0161] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0162] The above description is only a preferred embodiment of the present application and is not intended to limit the protection scope of the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application are included in the protection scope of the present application.
Claims
1. A method for processing a cascading style sheet file, characterized in that: include: Obtaining a cascading style sheet file to be processed and a markup language file corresponding to the cascading style sheet file; generating a first abstract syntax tree of the markup language file, and when it is determined that the markup language file contains dynamic elements, splitting the first abstract syntax tree to obtain at least one second abstract syntax tree; Obtaining multiple style rules contained in the cascading style sheet file; Determine a target style rule matching at least one of the second abstract syntax trees from the plurality of style rules; Generate a target cascading style sheet file based on the target style rule; The step of determining that the markup language file contains dynamic elements comprises: If the markup language file contains an element with a control attribute, determining the element with the control attribute as a dynamic element; The step of determining, from a plurality of style rules, a target style rule that matches at least one of the second abstract syntax trees comprises: For each of the style rules, traverse each of the second abstract syntax trees to search for a target tree structure that contains the style rule in the traversed second abstract syntax tree; If the target tree structure is found in any of the second abstract syntax trees, the style rule is determined as a target style rule; The step of searching the traversed second abstract syntax tree for a target tree structure containing the style rule includes: If the style rule contains adjacent sibling style selectors, the last one of the adjacent sibling style selectors is determined as the current style selector; Searching for a target tree node containing the current style selector in the traversed second abstract syntax tree; If the target tree node is not found, it is determined that the target tree structure containing the style rule is not found in the traversed second abstract syntax tree; if the target tree node is found, the previous style selector of the current style selector is determined as the current style selector, the previous tree node of the target tree node is determined as the new target tree node, and it is determined whether the new target tree node contains the current style selector; If not, determine that the target tree structure containing the style rule is not found in the traversed second abstract syntax tree; if included, return to the step of determining the previous style selector of the current style selector as the current style selector, until the current style selector does not have a previous style selector, determine that the target tree structure containing the style rule is found in the traversed second abstract syntax tree.
2. The method according to claim 1, characterized in that: The control attributes include at least: if attribute, else attribute, and elif attribute.
3. The method according to claim 2, characterized in that The step of splitting the first abstract syntax tree to obtain at least one second abstract syntax tree includes: In a case where the markup language file includes an element with an if attribute, obtaining the second abstract syntax tree includes at least one of the following: Deleting the tree node containing the if attribute in the first abstract syntax tree to obtain a second abstract syntax tree; The first abstract syntax tree is used as the second abstract syntax tree.
4. The method according to claim 2, characterized in that: The step of splitting the first abstract syntax tree to obtain at least one second abstract syntax tree includes: In a case where the markup language file includes an element with an if attribute and an element with an else attribute, obtaining the second abstract syntax tree includes at least one of the following: Deleting the tree node containing the if attribute in the first abstract syntax tree to obtain a second abstract syntax tree; The tree nodes including the else attribute in the first abstract syntax tree are deleted to obtain a second abstract syntax tree.
5. The method according to claim 2, characterized in that: The step of splitting the first abstract syntax tree to obtain at least one second abstract syntax tree includes: In a case where the markup language file includes an element with an if attribute and an element with an elif attribute, obtaining the second abstract syntax tree includes at least one of the following: Deleting the tree node containing the if attribute in the first abstract syntax tree to obtain a second abstract syntax tree; Deleting the tree node containing the elif attribute in the first abstract syntax tree to obtain a second abstract syntax tree; The tree nodes including the if attribute and the tree nodes including the elif attribute in the first abstract syntax tree are deleted to obtain a second abstract syntax tree.
6. The method according to claim 2, characterized in that The step of splitting the first abstract syntax tree to obtain at least one second abstract syntax tree includes: In a case where the markup language file includes an element with an if attribute, an element with an elif attribute, and an element with an else attribute, obtaining the second abstract syntax tree includes at least one of the following: Deleting the tree nodes including the if attribute and the tree nodes including the elif attribute in the first abstract syntax tree to obtain a second abstract syntax tree; Deleting the tree nodes including the elif attribute and the tree nodes including the else attribute in the first abstract syntax tree to obtain a second abstract syntax tree; The tree nodes including the if attribute and the tree nodes including the else attribute in the first abstract syntax tree are deleted to obtain a second abstract syntax tree.
7. The method according to claim 1, characterized in that The step of generating a target cascading style sheet file based on the target style rule comprises: Generates a target CSS file based on the target style rules through the abstract syntax tree of the original CSS file.
8. A cascading style sheet file processing device, characterized in that: include: A file acquisition module, used to acquire a cascading style sheet file to be processed and a markup language file corresponding to the cascading style sheet file; A tree generation module, configured to generate a first abstract syntax tree of the markup language file, and when it is determined that the markup language file contains dynamic elements, split the first abstract syntax tree to obtain at least one second abstract syntax tree; A style acquisition module, used to acquire multiple style rules contained in the cascading style sheet file; A style matching module, configured to determine a target style rule matching at least one of the second abstract syntax trees from the plurality of style rules; A file generation module, used for generating a target cascading style sheet file based on the target style rule; The step of determining that the markup language file contains dynamic elements comprises: If the markup language file contains an element with a control attribute, determining the element with the control attribute as a dynamic element; The step of determining, from a plurality of style rules, a target style rule that matches at least one of the second abstract syntax trees comprises: For each of the style rules, traverse each of the second abstract syntax trees to search for a target tree structure that contains the style rule in the traversed second abstract syntax tree; If the target tree structure is found in any of the second abstract syntax trees, the style rule is determined as a target style rule; The step of searching the traversed second abstract syntax tree for a target tree structure containing the style rule includes: If the style rule contains adjacent sibling style selectors, the last one of the adjacent sibling style selectors is determined as the current style selector; Searching for a target tree node containing the current style selector in the traversed second abstract syntax tree; If the target tree node is not found, it is determined that the target tree structure containing the style rule is not found in the traversed second abstract syntax tree; if the target tree node is found, the previous style selector of the current style selector is determined as the current style selector, the previous tree node of the target tree node is determined as the new target tree node, and it is determined whether the new target tree node contains the current style selector; If not, determine that the target tree structure containing the style rule is not found in the traversed second abstract syntax tree; if included, return to the step of determining the previous style selector of the current style selector as the current style selector, until the current style selector does not have a previous style selector, determine that the target tree structure containing the style rule is found in the traversed second abstract syntax tree.
9. A cascading style sheet file processing device, characterized in that: include: A processor and a memory, wherein the processor is used to execute a processing program of a cascading style sheet file stored in the memory to implement the processing method of a cascading style sheet file according to any one of claims 1 to 8.
10. A storage medium, characterized in that: The storage medium stores one or more programs, and the one or more programs can be executed by one or more processors to implement the method for processing a cascading style sheet file according to any one of claims 1 to 8.
11. A computer program product, comprising computer instructions, wherein the computer instructions are stored in a computer-readable storage medium; a processor of the computer program product reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer program product implements the method for processing a cascading style sheet file according to any one of claims 1 to 8.
Citation Information
Patent Citations
Method for compiling test case of web engineering
CN105354035A
A descendant selector processing method and device
CN109558138A
Installation package generation method and device for webpage application program, storage medium and equipment
CN111124481A
