@@ -12,102 +12,95 @@ import * as utils from './utils.js';
1212 * @param {Boolean } [options.ignoreComments=true]
1313 * @param {Boolean } [options.ignoreEndTags=false]
1414 * @param {Boolean } [options.ignoreSelfClosingSlash=false]
15- * returns {String}
15+ * @ returns {String }
1616 */
1717export default function modify ( value , options ) {
18- return new Promise ( ( resolve , reject ) => {
19- const modifiedValues = [ ] ;
20- const parser = new SAXParser ( ) ;
21-
22- /**
23- * @param {object } doctypeToken
24- * @param {String } doctypeToken.name
25- * @param {String } doctypeToken.publicId
26- * @param {String } doctypeToken.systemId
27- */
28- parser . on ( 'doctype' , function ( doctypeToken ) {
29- const name = doctypeToken . name ;
30- const publicId = doctypeToken . publicId ;
31- const systemId = doctypeToken . systemId ;
32-
33- modifiedValues . push ( serialize . doctype ( name , publicId , systemId ) ) ;
34- } ) ;
35-
36- /**
37- * @param {object } startTagToken
38- * @param {String } startTagToken.tagName
39- * @param {Array } startTagToken.attrs
40- * @param {Boolean } startTagToken.selfClosing
41- */
42- parser . on ( 'startTag' , function ( startTagToken ) {
43- let attrs = startTagToken . attrs ;
44- let selfClosing = startTagToken . selfClosing ;
45- const tagName = startTagToken . tagName ;
46-
47- if ( options . ignoreSelfClosingSlash ) {
48- selfClosing = false ;
49- }
50-
51- attrs = utils . sortAttrs ( attrs )
52- && utils . sortCssClasses ( attrs )
53- && utils . sortAttrsValues ( attrs , options . compareAttributesAsJSON )
54- && utils . removeAttrsValues ( attrs , options . ignoreAttributes ) ;
55-
56- modifiedValues . push ( serialize . startTag ( tagName , attrs , selfClosing ) ) ;
57- } ) ;
58-
59- /**
60- * @param {object } endTagToken
61- * @param {String } endTagToken.tagName
62- */
63- parser . on ( 'endTag' , function ( endTagToken ) {
64- const tagName = endTagToken . tagName ;
65-
66- if ( ! options . ignoreEndTags ) {
67- modifiedValues . push ( serialize . endTag ( tagName ) ) ;
68- }
69- } ) ;
70-
71- /**
72- * @param {object } textToken
73- * @param {String } textToken.text
74- */
75- parser . on ( 'text' , function ( textToken ) {
76- let text = textToken . text ;
77-
78- if ( options . ignoreWhitespaces ) {
79- text = utils . removeWhitespaces ( text ) ;
80- }
81-
82- modifiedValues . push ( serialize . text ( text ) ) ;
83- } ) ;
84-
85- /**
86- * @param {object } commentToken
87- * @param {String } commentToken.text
88- */
89- parser . on ( 'comment' , function ( commentToken ) {
90- modifiedValues . push ( new Promise ( resolve => {
91- const comment = commentToken . text ;
92- resolve ( utils . getConditionalComment ( comment , modify , options ) ) ;
93- } ) . then ( conditionalComment => {
94- if ( conditionalComment ) {
95- return serialize . comment ( conditionalComment ) ;
96- } else if ( ! options . ignoreComments ) {
97- return serialize . comment ( commentToken . text ) ;
98- }
99- } ) ) ;
100- } ) ;
101-
102- parser . on ( 'end' , async function ( ) {
103- const values = await Promise . all ( modifiedValues ) ;
104- resolve ( values . join ( '' ) ) ;
105- } ) ;
106-
107- parser . on ( 'error' , function ( err ) {
108- reject ( err ) ;
109- } ) ;
110-
111- parser . end ( value ) ;
18+ const modifiedValues = [ ] ;
19+ const parser = new SAXParser ( ) ;
20+
21+ /**
22+ * @param {object } doctypeToken
23+ * @param {String } doctypeToken.name
24+ * @param {String } doctypeToken.publicId
25+ * @param {String } doctypeToken.systemId
26+ */
27+ parser . on ( 'doctype' , function ( doctypeToken ) {
28+ const name = doctypeToken . name ;
29+ const publicId = doctypeToken . publicId ;
30+ const systemId = doctypeToken . systemId ;
31+
32+ modifiedValues . push ( serialize . doctype ( name , publicId , systemId ) ) ;
11233 } ) ;
34+
35+ /**
36+ * @param {object } startTagToken
37+ * @param {String } startTagToken.tagName
38+ * @param {Array } startTagToken.attrs
39+ * @param {Boolean } startTagToken.selfClosing
40+ */
41+ parser . on ( 'startTag' , function ( startTagToken ) {
42+ let attrs = startTagToken . attrs ;
43+ let selfClosing = startTagToken . selfClosing ;
44+ const tagName = startTagToken . tagName ;
45+
46+ if ( options . ignoreSelfClosingSlash ) {
47+ selfClosing = false ;
48+ }
49+
50+ attrs = utils . sortAttrs ( attrs )
51+ && utils . sortCssClasses ( attrs )
52+ && utils . sortAttrsValues ( attrs , options . compareAttributesAsJSON )
53+ && utils . removeAttrsValues ( attrs , options . ignoreAttributes ) ;
54+
55+ modifiedValues . push ( serialize . startTag ( tagName , attrs , selfClosing ) ) ;
56+ } ) ;
57+
58+ /**
59+ * @param {object } endTagToken
60+ * @param {String } endTagToken.tagName
61+ */
62+ parser . on ( 'endTag' , function ( endTagToken ) {
63+ const tagName = endTagToken . tagName ;
64+
65+ if ( ! options . ignoreEndTags ) {
66+ modifiedValues . push ( serialize . endTag ( tagName ) ) ;
67+ }
68+ } ) ;
69+
70+ /**
71+ * @param {object } textToken
72+ * @param {String } textToken.text
73+ */
74+ parser . on ( 'text' , function ( textToken ) {
75+ let text = textToken . text ;
76+
77+ if ( options . ignoreWhitespaces ) {
78+ text = utils . removeWhitespaces ( text ) ;
79+ }
80+
81+ modifiedValues . push ( serialize . text ( text ) ) ;
82+ } ) ;
83+
84+ /**
85+ * @param {object } commentToken
86+ * @param {String } commentToken.text
87+ */
88+ parser . on ( 'comment' , function ( commentToken ) {
89+ const comment = commentToken . text ;
90+ const conditionalComment = utils . getConditionalComment ( comment , modify , options ) ;
91+ if ( conditionalComment ) {
92+ modifiedValues . push ( serialize . comment ( conditionalComment ) ) ;
93+ } else if ( ! options . ignoreComments ) {
94+ modifiedValues . push ( serialize . comment ( comment ) ) ;
95+ }
96+ } ) ;
97+
98+ parser . on ( 'error' , function ( err ) {
99+ throw err ;
100+ } ) ;
101+
102+ parser . end ( value ) ;
103+
104+ // Since the parser is used synchronously, it's possible to get the final value after `end` call.
105+ return modifiedValues . join ( '' ) ;
113106}
0 commit comments