CN112416315B - Compression method of CSS code, electronic device and storage medium - Google Patents
Compression method of CSS code, electronic device and storage medium Download PDFInfo
- Publication number
- CN112416315B CN112416315B CN202010548437.4A CN202010548437A CN112416315B CN 112416315 B CN112416315 B CN 112416315B CN 202010548437 A CN202010548437 A CN 202010548437A CN 112416315 B CN112416315 B CN 112416315B
- Authority
- CN
- China
- Prior art keywords
- style
- name
- abbreviation
- compression
- benefit value
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
- 238000007906 compression Methods 0.000 title claims abstract description 109
- 230000006835 compression Effects 0.000 title claims abstract description 107
- 238000000034 method Methods 0.000 title claims abstract description 45
- 238000003860 storage Methods 0.000 title claims abstract description 7
- 230000008901 benefit Effects 0.000 claims abstract description 89
- 230000007423 decrease Effects 0.000 claims abstract description 10
- 230000014509 gene expression Effects 0.000 claims description 15
- 238000004590 computer program Methods 0.000 claims description 3
- 230000005540 biological transmission Effects 0.000 abstract description 4
- 238000004364 calculation method Methods 0.000 description 4
- 230000008569 process Effects 0.000 description 4
- 238000004458 analytical method Methods 0.000 description 3
- 239000003086 colorant Substances 0.000 description 2
- 238000011161 development Methods 0.000 description 2
- 230000000694 effects Effects 0.000 description 2
- 238000004519 manufacturing process Methods 0.000 description 2
- 238000006467 substitution reaction Methods 0.000 description 2
- 238000013459 approach Methods 0.000 description 1
- 230000009286 beneficial effect Effects 0.000 description 1
- 238000004422 calculation algorithm Methods 0.000 description 1
- 230000008859 change Effects 0.000 description 1
- 230000007812 deficiency Effects 0.000 description 1
- 238000013461 design Methods 0.000 description 1
- 238000010586 diagram Methods 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 238000000605 extraction Methods 0.000 description 1
- 239000012634 fragment Substances 0.000 description 1
- 230000006870 function Effects 0.000 description 1
- 238000009434 installation Methods 0.000 description 1
- 230000014759 maintenance of location Effects 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 238000005457 optimization Methods 0.000 description 1
- 238000012805 post-processing Methods 0.000 description 1
- 238000012545 processing Methods 0.000 description 1
- 230000009467 reduction Effects 0.000 description 1
- 238000010572 single replacement reaction Methods 0.000 description 1
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/38—Creation or generation of source code for implementing user interfaces
-
- H—ELECTRICITY
- H03—ELECTRONIC CIRCUITRY
- H03M—CODING; DECODING; CODE CONVERSION IN GENERAL
- H03M7/00—Conversion of a code where information is represented by a given sequence or number of digits to a code where the same, similar or subset of information is represented by a different sequence or number of digits
- H03M7/30—Compression; Expansion; Suppression of unnecessary data, e.g. redundancy reduction
- H03M7/3084—Compression; Expansion; Suppression of unnecessary data, e.g. redundancy reduction using adaptive string matching, e.g. the Lempel-Ziv method
-
- H—ELECTRICITY
- H03—ELECTRONIC CIRCUITRY
- H03M—CODING; DECODING; CODE CONVERSION IN GENERAL
- H03M7/00—Conversion of a code where information is represented by a given sequence or number of digits to a code where the same, similar or subset of information is represented by a different sequence or number of digits
- H03M7/30—Compression; Expansion; Suppression of unnecessary data, e.g. redundancy reduction
- H03M7/40—Conversion to or from variable length codes, e.g. Shannon-Fano code, Huffman code, Morse code
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Software Systems (AREA)
- General Engineering & Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Human Computer Interaction (AREA)
- Document Processing Apparatus (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
The invention relates to a compression method of CSS codes, electronic equipment and a storage medium, and belongs to the technical field of Internet. The compression method of the CSS code comprises the following steps: scanning CSS codes, and counting the occurrence times of each style attribute name; calculating an abbreviated compression benefit value based on the number of times, the abbreviated compression benefit value being a number of characters that generally decreases assuming the style attribute name is replaced by an abbreviation; if the abbreviation compression benefit value is positive, the abbreviation is used instead of the style attribute name. The method has the advantages that frequently-occurring style attribute names can be replaced by abbreviations with fewer characters, the size of codes is reduced, the data transmission speed is increased, the overall performance of a website can be improved, the compressed style names can be semantically maintained, and the compressed style names are easy to identify and convenient for developers to distinguish and maintain in the later period.
Description
Technical Field
The present invention relates to the field of internet technologies, and in particular, to a method for compressing a CSS code, an electronic device, and a storage medium.
Background
Along with the rapid development of the traditional Internet and the mobile Internet, the information carried by the WEB page is more and more abundant, and different webpage structures can be easily constructed by using the tags rich in HTML. But has a relatively weak ability in controlling the display effect of the web page. In order to design a webpage having an aesthetic effect, CSS is generally required. CSS makes up the deficiency of HTML, is a computer language used for representing file styles such as HTML or XML, can define style structures such as fonts, colors, positions and the like, and can provide powerful functions of page style beautification and layout for users. However, with the continuous development of services, more and more contents are on the page, the file volume of the page is also larger and larger, and the contents to be displayed are sometimes seen by users after waiting a period of time due to the limitation of physical network bandwidth and complex network environment. According to the statistical data of a certain internet company, it is found that 10% of users can be lost every second the loading time of the website is increased. It can be seen that the performance of the website is very important for the retention of the user, and the performance is a basic element for guaranteeing good user experience.
Currently, common code compression tools, such as Gulp-clean-use-CSS, CSSO, CSSNANO and the like, can delete redundant blanks, remove comments, simplify identifiers and clean unused CSS codes under the condition that the semantics of the CSS code fragments are unchanged, so that the purpose of providing the simplified CSS codes for the production environment is achieved. Wherein Gulp-clean-use-css is a middleware under the front-end build tool Gulp that needs to be used in the Gulp environment. CSSO is a CSS code compression tool running on node. Js, which can directly command after successful installation of run NPM INSTALL CSSO: CSSO style. CSS uses CSSO as a parameter for entering the CSS file name to be compressed. CSSNANO is a CSS code compression tool based on the PostCSS ecosystem, giving CSSNANO the ability to flexibly change and adapt to different scenarios through the plug-in architecture of PostCSS. However, the code compression tool only compresses the code to a certain extent to reduce the code volume, and how to further reduce the code volume to improve the performance of the website is a problem to be solved in the art.
Disclosure of Invention
In order to solve the above problems, the present invention provides a compression method of CSS codes, comprising: scanning CSS codes, and counting the occurrence times of each style attribute name; calculating an abbreviated compression benefit value based on the number of times, the abbreviated compression benefit value being a number of characters that generally decreases assuming the style attribute name is replaced by an abbreviation; if the abbreviation compression benefit value is positive, the abbreviation is used instead of the style attribute name.
Optionally, after the compressing benefit value by the abbreviation is positive, the compressing method of the CSS code further includes: counting the frequency of occurrence of the combination style names and the number of the contained abbreviations, wherein the combination style names consist of a plurality of the abbreviations; calculating a maximum value of a combined compression benefit value, which is a character reduced when the combination style name is assumed to be replaced by a specified style name, according to the frequency number and the number; and replacing the combined style name with the specified style name.
Alternatively, the abbreviation is obtained by: sorting the style attribute names resolved from the CSS codes; and generating the abbreviations of the default expression form according to the ordered style attribute names.
Optionally, after the abbreviations of the default expression forms are generated according to the ordered style attribute names, a manner of obtaining the abbreviations further includes: for the abbreviation, from style expression to class expression.
Optionally, the calculation formula of the abbreviated compression benefit value is:
Y1=N1×n-M
Wherein Y 1 is the abbreviation compression benefit value, N 1 is the number of characters reduced by replacing the style attribute name with the abbreviation, N is the number of times the replaced style attribute name appears, and M is the number of characters of the style code newly added to replace the style attribute name with the abbreviation.
Optionally, the method for obtaining the maximum value of the combined compression profit value through calculation adopts a tree exploration method.
Optionally, the expression of the combined compression benefit value is:
Y2=(L-1)×(P-1)
Where Y 2 is the combined compression benefit value, L is the length of the combination style name, and P is the number of occurrences of the combination style name.
Optionally, after said replacing the combination style name with the specified style name, the method further includes: unused abbreviations are removed, thereby reducing the volume of the CSS code during compression.
The invention also provides an electronic device, which comprises a processor and a memory, wherein the memory stores CSS codes; the processor is used for executing the following steps: scanning CSS codes, and counting the occurrence times of each style attribute name; calculating an abbreviated compression benefit value based on the number of times, the abbreviated compression benefit value being a number of characters that generally decreases assuming the style attribute name is replaced by an abbreviation; if the abbreviation compression benefit value is positive, the abbreviation is used instead of the style attribute name.
The invention also provides a computer storage medium having stored thereon a computer program which when executed by a processor implements a method as claimed in any of the preceding claims.
According to the above, the compression method of CSS code of the present invention comprises: scanning CSS codes, and counting the occurrence times of each style attribute name; calculating an abbreviated compression benefit value based on the number of times, the abbreviated compression benefit value being a number of characters that generally decreases assuming the style attribute name is replaced by an abbreviation; if the abbreviation compression benefit value is positive, the abbreviation is used instead of the style attribute name. Therefore, frequently-occurring style attribute names can be replaced by abbreviations with fewer characters, the size of codes is reduced, the data transmission speed is increased, the overall performance of a website can be improved, the compressed style names can be semantically maintained, and the compressed style attribute names are easy to identify and convenient for developers to distinguish and maintain later.
Drawings
FIG. 1 is a flow chart of a method for compressing CSS codes according to an embodiment of the present invention;
FIG. 2 is a flowchart illustrating a method for compressing CSS codes according to an embodiment of the present invention further comprising the steps of;
FIG. 3 is a flow chart of a tree exploration method according to an embodiment of the present invention;
fig. 4 is a schematic diagram of pattern assembly according to an embodiment of the present invention.
Detailed Description
While various code compression tools are known that can reduce code volume to some extent, for CSS codes, there may be a large number of codes used to define style structures such as fonts, colors, positions, etc. when representing, for example, HTML file styles, there may be a large number of repeated style property names. However, existing compression tools do not provide intelligent analysis of the code, multiplexing these repeated code extractions. Accordingly, the inventors of the present invention have proposed a compression method of CSS codes against this phenomenon, which can further reduce the CSS code volume by replacing frequently occurring style attribute names with abbreviations of a smaller number of characters. Note that, in order to replace the style attribute names with abbreviations, a new style code is required, that is, the number of characters is increased, so that not all the style attribute names that repeatedly appear are replaced with abbreviations, and only if the condition that the number of characters is reduced after replacement is satisfied, the replacing operation is performed.
Referring to fig. 1, a method for compressing CSS codes according to an embodiment of the present invention includes the following steps:
s100: scanning CSS codes, and counting the occurrence times of each style attribute name;
S200: calculating an abbreviated compression benefit value based on the number of times, the abbreviated compression benefit value being a number of characters that generally decreases assuming the style attribute name is replaced by an abbreviation;
s300: if the abbreviation compression benefit value is positive, the abbreviation is used instead of the style attribute name.
For example, one style attribute name has a number of characters of 10 and its abbreviation has a number of characters of 5, but the newly added style code has a number of characters of 12, if the style attribute name appears only 1 time, Y 1 = (10-5) ×1-12= -7, then replacing the style attribute name with the abbreviation results in a 7 character increase instead; if the style attribute name appears 4 times, Y 1 = (10-5) ×4-12=8, then replacing the style attribute name with an abbreviation results in a reduction of 8 characters. Therefore, if the style attribute name appears only 1 time, the replacement operation is not performed, and the replacement operation is performed only if the condition that the number of characters after the replacement is reduced is satisfied. The method has the advantages that frequently-occurring style attribute names can be replaced by abbreviations with fewer characters, the size of codes is reduced, the data transmission speed is increased, the overall performance of a website can be improved, the compressed style names can be semantically maintained, and the compressed style names are easy to identify and convenient for developers to distinguish and maintain in the later period.
Based on the above method, for the case where a plurality of style attribute names or abbreviations repeatedly appear in a combined form, it is also possible to further replace them with prescribed style names having a smaller number of unused characters, so that the code volume can be further reduced. A new style, in which several style property names or abbreviations are combined, is referred to herein as a combined style name. It should be noted that the compression result caused by replacing a combination style name with a prescribed style name depends not only on the number of styles or abbreviations contained in the combination style name but also on the number of times the combination style name appears.
According to the above, since the compression method adopted in the present invention depends on the occurrence frequency of the style attribute names, for style attribute names with relatively low occurrence frequency, a phenomenon that characters are increased after compression may occur, and in order to avoid such a phenomenon, a concept of compression benefit is introduced herein, which means that the number of characters of a compressed code is reduced compared with that of a code before compression. Here, the compression benefit caused by replacing frequently occurring style attribute names with abbreviations is called an abbreviation compression benefit value, and the calculation formula is:
Y1=N1×n-M
Wherein Y 1 is the abbreviation compression benefit value, N 1 is the number of characters reduced by replacing the style attribute name with the abbreviation, N is the number of times the replaced style attribute name appears, and M is the number of characters of the style code newly added to replace the style attribute name with the abbreviation. The present invention will be described with reference to a combined compression benefit value, which is a character reduced when a combination style name is replaced with a predetermined style name. As a further optimization of the present invention, it is necessary to perform an operation of replacing the combination style name with a prescribed style name after calculating the obtained combined compression benefit value as the maximum value.
In light of the foregoing, referring to fig. 2, the method for compressing CSS codes according to an embodiment of the present invention further includes the following steps after replacing the style attribute name with the abbreviation if the compression benefit value of the abbreviation is positive:
S400: counting the frequency of occurrence of the combination style names and the number of the contained abbreviations, wherein the combination style names consist of a plurality of the abbreviations;
s500: calculating a maximum value of a combined compression benefit value, which is a character reduced when the combination style name is assumed to be replaced by a specified style name, according to the frequency number and the number;
S600: and replacing the combined style name with the specified style name.
Since some style names may be repeated after being replaced with abbreviations, the repeated combinations of abbreviations may be combined and replaced with shorter specified styles, thereby further compressing the code volume. It should be noted that, the more abbreviations in the combination style name, the larger the resulting combined compression benefit value, but the fewer the number of times of repeated occurrences may be, and thus the resulting combined compression benefit value becomes smaller, so that there must be an equilibrium point where the combined compression benefit value is the maximum value. For example, the following codes are targeted:
class=“f12 c-red ml20 mb20”
class=“fr f12 c-red ml20 mb20”
class=“pos-a f12 c-red ml20 mb20”
class=“w200 f12 c-red ml20 mb20”
when the combined compression benefit value is maximized, the abbreviation "f12c-red ml20 mb20" of the above repeated part is combined and replaced with a01, the above code becomes:
class=“a01”
class=“fr a01”
class=“pos-a a01”
class=“w200 a01”
according to the above manner, the overall string length of the code is reduced by 64 characters. Therefore, the combined style name is replaced by the specified style name with fewer characters so as to achieve the aim of further compressing codes, and the speed of data transmission can be further improved, so that the overall performance of the website is further improved.
Optionally, the method for calculating and obtaining the maximum value of the combined compression profit value adopts a tree exploration method. Referring to fig. 3, since a combination style name is composed of a plurality of abbreviations, the tree search method calculates all possible cases under the condition of only a single abbreviation (e.g., a, b or c), and then selects the case in which the combined compression benefit value is large; continuing to explore the next stage, i.e., adding one more abbreviation, calculating all possible cases under the combined condition of becoming two abbreviations (e.g., aa, ab, ac, bb, bc or cc), and then selecting a combination in which the combined compression benefit value is greater; in the case of a combination of three abbreviations (aaa, aab, aac, abb, abc, acc, bbb, bbc or bcc), a combination in which the combined compression benefit value is larger is selected. And so on, the abbreviated combination that combines the compression benefit values to the maximum value will eventually be found. The basis of the method is that the more abbreviations in a combination style name are, the larger the combined compression benefit value is brought, but the repeated occurrence times are possibly reduced, so that the brought combined compression benefit value is reduced, and therefore, a balance point with the combined compression benefit value being the maximum value is necessarily present.
Alternatively, the abbreviation is obtained by: sorting the style attribute names resolved from the CSS codes; and generating abbreviations according to the ordered style attribute names. Since style attribute names resolved from the original CSS code may be unordered, more frequently occurring combined style names can be formed as much as possible after being consistently ordered, thereby increasing the compression rate.
Yet another aspect of the present invention provides an electronic device including a processor and a memory, the memory having CSS code stored therein; the processor is used for executing the following steps: scanning CSS codes, and counting the occurrence times of each style attribute name; calculating an abbreviated compression benefit value based on the number of times, the abbreviated compression benefit value being a number of characters that generally decreases assuming the style attribute name is replaced by an abbreviation; if the abbreviation compression benefit value is positive, the abbreviation is used instead of the style attribute name.
Another aspect of the present invention provides a computer storage medium having a computer program stored thereon, which when executed by a processor implements a method of compressing CSS code as provided by the present invention.
The following examples are given in detail in order to make the technical content of the present invention more clearly understood.
Embodiments of the present invention require scanning the CSS code to count the number of occurrences of each style property name, thereby replacing frequently occurring style property names with abbreviations. This process requires converting style property names parsed from the CSS code from style expressions to class expressions. The method comprises the following specific steps:
step 1: all the contents in, for example, style= ", HTML elements are parsed out one by one:
for example, the source HTML code is as follows:
<div style=“margin-left:15px;color:red;”>
<span style=“color:red;font-size:12px;”>
<b style=“margin-left:15px;color:red;margin-bottom:20px;”>a</b>
</span>
</div>
The content after analysis is as follows:
style=“margin-left:15px;color:red;”
style=“color:red;font-size:12px;”
style=“margin-left:15px;color:red;margin-bottom:20px;”
step 2: sorting according to style attribute names:
style=“color:red;margin-left:15px;”
style=“color:red;font-size:12px;”
style=“color:red;margin-bottom:20px;margin-left:15px;”
step 3: generating abbreviations (e.g., abbreviations that are default expressions) from style attribute names and values:
color:red;=>c-red
margin-left:15px;=>ml15
font-size:12px;=>f12
margin-bottom:20px;=>mb20
The abbreviations in this step may also be nonsensical characters, and the embodiment has the beneficial effect of replacing the original style attribute names with the abbreviations: the compressed style attribute names are more semantical, so that the compressed style attribute names are convenient for a developer to distinguish and later maintain, for example, the color is represented by using c-red, red is more semantical than x1, and the compressed style attribute names are easier to identify.
Step 4: replacing style code content that meets the use abbreviation instead of style attribute name conditions with class code content:
class=“c-red ml15”
class=“c-red f12”
class=“c-red mb20 ml15”
Because HTML syntax limits style expressions, multiplexing cannot be achieved even though style= "marg in-left:10px is used in many places; ", it cannot be directly replaced by style=" m10", which may cause the browser to fail to parse and report errors. But using the class = "…" approach is relatively more flexible, such as using class = "a10000" in many places, then a somewhat shorter style name that is not used, such as using a2 instead, so that all places where class = "a10000" are used can be replaced by class = "a2", which can still be correctly recognized by the browser. Then, the frequently occurring combination style names in the combination form may be replaced with a prescribed style name with a smaller number of unused characters, for example, class= "a2a3", class= "a1a2a3a4" is subjected to merging compression, and c1 is used instead of the frequently occurring a2a3 combination, the contents of which will be described in detail below. It should be noted that, here, the replacement of style= "…" with class= "…" is that the combination of < style > … </style > is required to be actually effective.
In the step 4, the condition that the abbreviation is used to replace the style attribute name is that the abbreviation compression benefit value is positive, and the abbreviation compression benefit value is described in detail by way of example below.
For example, since class= "ml10" is used instead of style= "margin-left:10px; "when new style code < style >. Ml { margin-left:10px; the }/style > can be really identified by combining the class= "ml10" browser. The specific impact of each factor on the abbreviated compression benefit value will be seen in detail from the following description.
(1) In the above process, < style >. Ml { margin-left:10px; the compression benefit value brought by the }/style > is-38 characters, namely 38 characters are added in whole, and the part belongs to the benefit brought by the newly added style (namely the character number M of the newly added style code for replacing the style attribute name by abbreviation);
(2) Replacing style attribute names with abbreviations: replacing style = "margin-left:10px with class =" ml10 "; "the compressed benefit value brought is +13 characters, i.e., 13 characters are reduced overall, which is part of the benefit of class replacement style (i.e., the reduced number of characters N 1 by abbreviation instead of style property name). It should be noted that, if the class= "…" is longer than the original style= "…", for example, class= "this_is_margin_left_10px", the compression benefit caused by the substitution is-7 characters, i.e. 7 characters are added after the substitution.
(3) For the number of times n of appearance of style attribute names or abbreviations, for example, replace style= "margin-left 10px with class=" ml10 "; ", if margin-left in the entire code is 10px; only 1 time appears, then the value of the result Y 1, where the style attribute name is replaced by an abbreviation, is 13 x 1-38 = -25, i.e. the overall content length is not reduced but increased by 25 characters in the opposite direction; in another case, if margin-left, 10px; 100 times occurs, then the compression result Y 1 value at this time is: 13 x 100-38 = 1262, i.e. the overall content length is reduced by 1262 characters.
Thus, for each style attribute name, the compression result replaced by the abbreviation needs to be measured, and if the abbreviation compression benefit value is positive, the style attribute name can be replaced by the abbreviation; otherwise, the original code is maintained.
Optionally, after the frequency of occurrence of each style attribute name is obtained through scanning, a style with a smaller number of characters can be added for the style attribute name with a higher frequency of occurrence to replace the style attribute name with a higher number of characters; for style attribute names with lower appearance frequency, a style with a longer character can be added to replace the style attribute names. Such as margin-left:50px; 100 occurrences of background-image url ('…/img/page-bg. Png'); two patterns a1{ margin-left:50px; }. a1000{ background-image: url ('…/img/page-bg. Png'); then at the occurrence of margin-left:50px; where a1 is substituted, backgroup-image: url ('…/img/page-bg. Png'); where a1000 is substituted. In this way, a1 appears 100 times plus a1000 appears 5 times, for a total of 2×100+5×5=225 characters. Conversely, if the style attribute names with high frequency of occurrence are replaced by styles with more characters, and the style attribute names with low frequency of occurrence are replaced by styles with less characters, for example, a1 is replaced by a1000, a1000 is replaced by a1, and then a total of 5×100+2×5=510 characters are obtained.
In addition, the following describes the combined compression benefit value in detail, and the expression formula is:
Y2=(L-1)×(P-1)
Where Y 2 is the combined compression benefit value, L is the length of the combination style name, and P is the number of occurrences of the combination style name.
Referring to fig. 4, for example, 12 codes are numbered 1-12 (for illustration only, the codes need not be numbered in the actual compression process), and table 1 is a combination length L and a number of occurrences P counted for patterns (abbreviations) in each code, and then the combined compression benefit value Y 2 is calculated according to the formula Y 2 = (L-1) x (P-1). For example, the combination style name "BD" has a combination length L of 2 and a number of occurrences P of 6 (which occur in codes numbered 2, 4, 6, 10, 11, and 12, respectively), and the combined compression benefit value Y 2 = (2-1) × (6-1) =5. The benefit brought by combining compression can be intuitively reflected.
So in order to further compress the code volume, after the above steps 1-4, the code compression method of the present invention may further comprise the steps of:
step 5: extracting repeated parts, and combining the repeated parts which frequently occur into new combined style names according to a compression benefit value maximization algorithm:
“c-red ml15”=>.i1{color:red;margin-left:15px;}
step 6: replacing the combined style name with the specified style name:
class=“i1”
class=“c-red f12”
class=“i1 mb20”
The combined compression benefit value due to the combination in < style > … </style > is exemplified below. For example, c1 is used instead of "a 1a 2a 3" which repeatedly appears in "a 1a 2a 3" of class= "a 1a 2a 3a 4". The specific analysis is as follows:
For example <style>.a1{color:red;}.a2{font-size:12px;}.a3{float:left;}.a4{margin-top:10px;}</style>, replaced < style >. C1{ color: red; a font-size of 12px; float, left; 10px of margin-top; the compression benefit value brought about is 15 characters, i.e., the overall content length is reduced by 15 characters.
For example, c1 may be used instead of "a1a2a3" for class= "a1a2a3", class= "c1" may be changed to be reduced by 6 characters from the total length before replacement, and similarly, the length of the style name after replacement is also related to the length of the style name after replacement, for example, instead of c1, the class = "this_is_new_a1_a2_a3" may be changed to class = "this_is_new_a1_a2_a3", and the total length is inversely increased by 12 characters. In particular, it should be noted that: the benefit from replacing a style name and the number of style name repetitions are directly related, and the aggregate benefit is equal to the single replacement benefit multiplied by the number of repetitions.
According to the above, after the code compression in steps 1-6, the overall compression benefit value y=y 1+Y2 is obtained. Specifically, the compression benefits involved in steps 1-6 include: (1) The income brought by the newly added style increases the character, and the income is negative; (2) Replacing benefits from style attribute names with abbreviations, which may increase or decrease characters as a whole, may be positive or negative; (3) The benefits of merging style names, which may increase or decrease characters as a whole, may be positive or negative. In the compression process, since the existing style names are required to be combined into new style names, new codes are introduced in the process, the code volume is increased conversely, the gain size is required to be compared for the new style names and the multiplexing style names, and if the gain brought by the multiplexing style names is smaller than the new style names, the optimal strategy is to keep the original codes; otherwise, if the benefit brought by multiplexing the style names is greater than that of the newly added style names, the compression should be selected to continue.
For further explanation of the present invention, example 1 is exemplified below.
Example 1
A website displays a commodity information page in which a user can enter related information of a commodity, such as information of a commodity name, a commodity ID, a price, a manufacturer, a production date, inventory, and the like.
HTML code implemented in the manner of inline style, style= ", is as follows:
Embodiments are described below:
Under Nodejs environment, the code is packaged into a command line tool, then a corresponding command is operated through a control console (cmd can be used under windows, shell can be used under other platforms), and the file name is taken as a parameter to be input.
The specific treatment process is as follows:
Step A: by means of class= ", replace style=", replace style attribute names that meet the replacement condition with abbreviations, such as mb15 for margin-bottom:15px; the processed code is as follows:
And (B) step (B): combining the repeated patterns (or abbreviations) into new pattern names, namely combined pattern names according to the mode of maximizing the combined compression benefit, replacing the combined pattern names with specified pattern names, and processing the new pattern names as follows:
Step C: unused patterns (abbreviations) are removed and the post-processing code is as follows:
Step D: and outputting the compressed file.
Optionally, in an embodiment of the present invention, the original data may be scanned twice, where the first scanning accurately counts the frequency of occurrence of each value in the original data, and the second scanning encodes all the values that occur, so as to finally achieve the purpose of compressing the code.
Compared with the prior art, the invention has the following two advantages: (1) The use of abbreviations instead of style attribute names and/or values can greatly reduce the code length. Such as margin-left 15px; after abbreviated as ml15, the character string is changed from 17 characters to 4 characters, and the compression benefit is 13 characters; (2) The compression benefit maximization calculation method is adopted, frequently-occurring patterns or abbreviations are further combined into new patterns, and then the new patterns are replaced by specified pattern names with fewer characters, codes are further compressed, and the code volume is reduced.
In this specification, the invention has been described with reference to specific embodiments thereof. It will be apparent that various modifications and variations can be made without departing from the spirit and scope of the invention. The specification and drawings are, accordingly, to be regarded in an illustrative rather than a restrictive sense.
Claims (9)
1. A method for compressing CSS code, comprising:
scanning CSS codes, and counting the occurrence times of each style attribute name;
Calculating an abbreviated compression benefit value based on the number of times, the abbreviated compression benefit value being a number of characters that generally decreases assuming the style attribute name is replaced by an abbreviation;
if the abbreviation compression benefit value is positive, using the abbreviation to replace the style attribute name;
After the abbreviation is used to replace the style attribute name if the abbreviation compression benefit value is positive, the method further comprises:
Counting the frequency of occurrence of the combination style names and the number of the contained abbreviations, wherein the combination style names consist of a plurality of the abbreviations;
calculating a maximum value of a combined compression benefit value, which is a character reduced when the combination style name is assumed to be replaced by a specified style name, according to the frequency number and the number;
And replacing the combined style name with the specified style name.
2. The compression method of CSS codes according to claim 1, characterized in that said abbreviations are obtained by:
Sorting the style attribute names resolved from the CSS codes;
and generating the abbreviations of the default expression form according to the ordered style attribute names.
3. The method of compressing CSS code according to claim 2, wherein after said generating said abbreviations of default expression form from the ordered style property names, the means for obtaining said abbreviations further comprises:
for the abbreviation, from style expression to class expression.
4. The compression method of CSS codes according to claim 1, wherein the abbreviated compression benefit value is calculated by the formula: Wherein Y1 is the abbreviated compression benefit value, N1 is the number of characters reduced by replacing the style attribute name with an abbreviation, N is the number of occurrences of the replaced style attribute name, and M is the number of characters of the style code newly added to replace the style attribute name with an abbreviation.
5. The method for compressing CSS code according to claim 1, wherein said method for calculating the maximum value of the combined compression benefit value uses a tree search method.
6. The compression method of CSS codes according to claim 1, wherein the expression of the combined compression benefit value is: Where Y2 is the combined compression benefit value, L is the length of the combination style name, and P is the number of occurrences of the combination style name.
7. The compression method of the CSS code according to claim 1, further comprising, after said replacing said combination style name with said prescribed style name: unused abbreviations are removed.
8. An electronic device comprising a memory and a processor,
The memory stores CSS codes;
the processor is used for executing the following steps:
scanning CSS codes, and counting the occurrence times of each style attribute name;
Calculating an abbreviated compression benefit value based on the number of times, the abbreviated compression benefit value being a number of characters that generally decreases assuming the style attribute name is replaced by an abbreviation;
if the abbreviation compression benefit value is positive, using the abbreviation to replace the style attribute name;
After the abbreviation is used to replace the style attribute name if the abbreviation compression benefit value is positive, the method further comprises:
Counting the frequency of occurrence of the combination style names and the number of the contained abbreviations, wherein the combination style names consist of a plurality of the abbreviations;
calculating a maximum value of a combined compression benefit value, which is a character reduced when the combination style name is assumed to be replaced by a specified style name, according to the frequency number and the number;
And replacing the combined style name with the specified style name.
9. A computer storage medium, characterized in that it has stored thereon a computer program which, when executed by a processor, implements the method according to any of claims 1 to 7.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010548437.4A CN112416315B (en) | 2020-06-16 | 2020-06-16 | Compression method of CSS code, electronic device and storage medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010548437.4A CN112416315B (en) | 2020-06-16 | 2020-06-16 | Compression method of CSS code, electronic device and storage medium |
Publications (2)
Publication Number | Publication Date |
---|---|
CN112416315A CN112416315A (en) | 2021-02-26 |
CN112416315B true CN112416315B (en) | 2024-05-14 |
Family
ID=74844174
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202010548437.4A Active CN112416315B (en) | 2020-06-16 | 2020-06-16 | Compression method of CSS code, electronic device and storage medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN112416315B (en) |
Families Citing this family (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN114238251A (en) * | 2021-12-21 | 2022-03-25 | 湖南快乐阳光互动娱乐传媒有限公司 | Project code packet compression method and device, storage medium and electronic equipment |
CN114840245A (en) * | 2021-12-28 | 2022-08-02 | 北京达佳互联信息技术有限公司 | Packing method and device |
Citations (12)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
JPH06195058A (en) * | 1992-12-25 | 1994-07-15 | Casio Comput Co Ltd | Character pattern data compressing device |
JPH06290021A (en) * | 1993-03-31 | 1994-10-18 | Nec Software Ltd | Method for compressing source program |
CN1367896A (en) * | 1999-08-13 | 2002-09-04 | 富士通株式会社 | File processing method, data processing device and storage medium |
WO2002082261A2 (en) * | 2001-04-05 | 2002-10-17 | Schlumberger Systèmes | Compression of codes of an application written in high-level language, for use in mobile telephony. |
CN1536768A (en) * | 2003-04-08 | 2004-10-13 | 株式会社泛泰 | Compression method for 2-byte character data |
CN101350624A (en) * | 2008-09-11 | 2009-01-21 | 中国科学院计算技术研究所 | A Compression Method of Chinese Text Supporting ANSI Code |
CN103500118A (en) * | 2013-10-24 | 2014-01-08 | 北京奇虎科技有限公司 | Method and device for optimizing cascading style sheet |
CN104063208A (en) * | 2013-03-21 | 2014-09-24 | 北京百度网讯科技有限公司 | Code file optimizing method, system and server |
CN104331269A (en) * | 2014-10-28 | 2015-02-04 | 中国科学院自动化研究所 | Executable code compression method of embedded type system and code uncompressing system |
WO2016199255A1 (en) * | 2015-06-10 | 2016-12-15 | 富士通株式会社 | Information processing device, information processing method, and information processing program |
CN109460229A (en) * | 2018-09-17 | 2019-03-12 | 深圳壹账通智能科技有限公司 | Code file generation method, device, computer equipment and storage medium |
CN110007955A (en) * | 2019-03-08 | 2019-07-12 | 浙江大学 | A kind of compression method of instruction set simulator decoding module code |
Family Cites Families (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
TWI318058B (en) * | 2005-01-26 | 2009-12-01 | Qisda Corp | Message compression method, system and machine-readable storage medium |
-
2020
- 2020-06-16 CN CN202010548437.4A patent/CN112416315B/en active Active
Patent Citations (12)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
JPH06195058A (en) * | 1992-12-25 | 1994-07-15 | Casio Comput Co Ltd | Character pattern data compressing device |
JPH06290021A (en) * | 1993-03-31 | 1994-10-18 | Nec Software Ltd | Method for compressing source program |
CN1367896A (en) * | 1999-08-13 | 2002-09-04 | 富士通株式会社 | File processing method, data processing device and storage medium |
WO2002082261A2 (en) * | 2001-04-05 | 2002-10-17 | Schlumberger Systèmes | Compression of codes of an application written in high-level language, for use in mobile telephony. |
CN1536768A (en) * | 2003-04-08 | 2004-10-13 | 株式会社泛泰 | Compression method for 2-byte character data |
CN101350624A (en) * | 2008-09-11 | 2009-01-21 | 中国科学院计算技术研究所 | A Compression Method of Chinese Text Supporting ANSI Code |
CN104063208A (en) * | 2013-03-21 | 2014-09-24 | 北京百度网讯科技有限公司 | Code file optimizing method, system and server |
CN103500118A (en) * | 2013-10-24 | 2014-01-08 | 北京奇虎科技有限公司 | Method and device for optimizing cascading style sheet |
CN104331269A (en) * | 2014-10-28 | 2015-02-04 | 中国科学院自动化研究所 | Executable code compression method of embedded type system and code uncompressing system |
WO2016199255A1 (en) * | 2015-06-10 | 2016-12-15 | 富士通株式会社 | Information processing device, information processing method, and information processing program |
CN109460229A (en) * | 2018-09-17 | 2019-03-12 | 深圳壹账通智能科技有限公司 | Code file generation method, device, computer equipment and storage medium |
CN110007955A (en) * | 2019-03-08 | 2019-07-12 | 浙江大学 | A kind of compression method of instruction set simulator decoding module code |
Also Published As
Publication number | Publication date |
---|---|
CN112416315A (en) | 2021-02-26 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US7480858B2 (en) | Analyzing webpages using function-based object models for web page display in a mobile device | |
US8122345B2 (en) | Function-based object model for use in WebSite adaptation | |
US7281205B2 (en) | Hash compact XML parser | |
US8601368B2 (en) | Processing method and device for the coding of a document of hierarchized data | |
US7995842B2 (en) | System and method for binary persistence format for a recognition result lattice | |
US20110219357A1 (en) | Compressing source code written in a scripting language | |
US8788509B2 (en) | Method and device for coding a hierarchized document | |
CN112416315B (en) | Compression method of CSS code, electronic device and storage medium | |
US8181105B2 (en) | Apparatus, method, and program that performs syntax parsing on a structured document in the form of electronic data | |
KR20040007442A (en) | Method for compressing/decompressing a structured document | |
JP5377818B2 (en) | Method and system for sequentially accessing a compiled schema | |
US8935605B2 (en) | Validator-driven architecture of an XML parsing and validating solution | |
CN110347390B (en) | Method, storage medium, equipment and system for rapidly generating WEB page | |
WO2023103914A1 (en) | Text sentiment analysis method and device, and computer-readable storage medium | |
US7509574B2 (en) | Method and system for reducing delimiters | |
US20060036943A1 (en) | Method and system for selectively enforcing presentation themes | |
US7500184B2 (en) | Determining an acceptance status during document parsing | |
US20060184547A1 (en) | Method and system for fast encoding of data documents | |
CN118193792A (en) | Design method for realizing HTML5 format lightweight conversion based on XML (extensive markup language) analysis Word file | |
US7735001B2 (en) | Method and system for decoding encoded documents | |
US8996991B2 (en) | System and method for displaying an acceptance status | |
JP4801555B2 (en) | Document processing apparatus, document processing method, and document processing program | |
CN113760247A (en) | Front-end code generation method and device, electronic equipment and storage medium | |
CN113312025A (en) | Component library generation method and device, storage medium and electronic equipment | |
US20060212799A1 (en) | Method and system for compiling schema |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |