@@ -7,100 +7,100 @@ import Stylesheet from "./parser/Stylesheet.js"
77 */
88 class CSSObject {
99
10- /**
11- * CSSObject constructor
12- * @param {object } options
13- * @returns CSSObject
14- */
15- constructor ( options = { } ) {
16- this . options ( options )
17-
18- return this
19- }
20-
21- /**
22- * set options to get external stylesheets
23- * @param {object } options options
24- * @returns CSSObject
25- */
26- options ( options = { } ) {
27- this . __defaultOptions__ = {
28-
29- /**
30- * load only especified css files, if used `ignore_files` isn't used
31- * @type string[]
32- */
33- only_files : ( options . only_files || [ ] ) ,
34-
35- /**
36- * ignore css files
37- * @type string[]
38- */
39- ignore_files : ( options . ignore_files || [ ] ) ,
40-
41- /**
42- * load minified css (default `true`)
43- * @type boolean
44- */
45- load_min : ( options . load_min || true ) ,
46- }
47-
48- this . _options = { ...this . __defaultOptions__ , ...options }
49- return this
50- }
51-
52- /**
53- * get local stylesheets
54- * @param {object } callback callback function
55- * @param {boolean } all return all stylesheets in callback
56- */
57- local ( callback , all = false ) {
58- let styles = [ ]
59- Object . values ( document . styleSheets ) . forEach ( sheet => {
60- if ( sheet . ownerNode != null || sheet . ownerNode . childNodes . length > 0 )
61- if ( sheet . ownerNode . innerText != "" ) {
62- let style = new Stylesheet ( sheet . ownerNode . innerText )
63- all ? styles . push ( style ) : callback ( style )
64- }
65- } )
66-
67- if ( styles . length > 0 ) callback ( styles )
68- return this
69- }
70-
71- /**
72- * get external css (link)
73- * @param {object } callback callback
74- */
75- external ( callback ) {
76- Object . values ( document . styleSheets ) . forEach ( sheet => {
77- if ( sheet . ownerNode . nodeName == 'LINK' ) {
78- this . __external ( sheet . href , callback )
79- }
80- } )
81-
82- return this
83- }
84-
85- /**
86- * load external stylesheets
87- * @param {string } link link to external stylesheet
88- * @returns reponse (promise)
89- * @private don't use please
90- */
91- async __external ( link , callback ) {
92- let filename = link . split ( '/' ) . pop ( )
93- let is_min = filename . endsWith ( '.min.css' )
94- if ( ! this . _options . load_min_css && is_min ) return
95- if ( this . _options . only_files . length == 0 && this . _options . ignore_files . includes ( filename ) ) return
96- if ( this . _options . only_files . length > 0 && ! this . _options . only_files . includes ( filename ) ) return
97-
98- return await ( fetch ( link ) )
99- . then ( res => res . text ( ) )
100- . then ( css => callback ( new Stylesheet ( css , filename ) ) )
101- . catch ( err => console . error ( err ) )
102-
103- }
10+ /**
11+ * CSSObject constructor
12+ * @param {object } options
13+ * @returns CSSObject
14+ */
15+ constructor ( options = { } ) {
16+ this . options ( options )
17+
18+ return this
19+ }
20+
21+ /**
22+ * set options to get external stylesheets
23+ * @param {object } options options
24+ * @returns CSSObject
25+ */
26+ options ( options = { } ) {
27+ this . __defaultOptions__ = {
28+
29+ /**
30+ * load only especified css files, if used `ignore_files` isn't used
31+ * @type string[]
32+ */
33+ only_files : ( options . only_files || [ ] ) ,
34+
35+ /**
36+ * ignore css files
37+ * @type string[]
38+ */
39+ ignore_files : ( options . ignore_files || [ ] ) ,
40+
41+ /**
42+ * load minified css (default `true`)
43+ * @type boolean
44+ */
45+ load_min : ( options . load_min || true ) ,
46+ }
47+
48+ this . _options = { ...this . __defaultOptions__ , ...options }
49+ return this
50+ }
51+
52+ /**
53+ * get local stylesheets
54+ * @param {object } callback callback function
55+ * @param {boolean } all return all stylesheets in callback
56+ */
57+ local ( callback , all = false ) {
58+ let styles = [ ]
59+ Object . values ( document . styleSheets ) . forEach ( sheet => {
60+ if ( sheet . ownerNode != null || sheet . ownerNode . childNodes . length > 0 )
61+ if ( sheet . ownerNode . innerText != "" ) {
62+ let style = new Stylesheet ( sheet . ownerNode . innerText )
63+ all ? styles . push ( style ) : callback ( style )
64+ }
65+ } )
66+
67+ if ( styles . length > 0 ) callback ( styles )
68+ return this
69+ }
70+
71+ /**
72+ * get external css (link)
73+ * @param {object } callback callback
74+ */
75+ external ( callback ) {
76+ Object . values ( document . styleSheets ) . forEach ( sheet => {
77+ if ( sheet . ownerNode . nodeName == 'LINK' ) {
78+ this . __external ( sheet . href , callback )
79+ }
80+ } )
81+
82+ return this
83+ }
84+
85+ /**
86+ * load external stylesheets
87+ * @param {string } link link to external stylesheet
88+ * @returns reponse (promise)
89+ * @private don't use please
90+ */
91+ async __external ( link , callback ) {
92+ let filename = link . split ( '/' ) . pop ( )
93+ let is_min = filename . endsWith ( '.min.css' )
94+ if ( ! this . _options . load_min_css && is_min ) return
95+ if ( this . _options . only_files . length == 0 && this . _options . ignore_files . includes ( filename ) ) return
96+ if ( this . _options . only_files . length > 0 && ! this . _options . only_files . includes ( filename ) ) return
97+
98+ return await ( fetch ( link ) )
99+ . then ( res => res . text ( ) )
100+ . then ( css => callback ( new Stylesheet ( css , filename ) ) )
101+ . catch ( err => console . error ( err ) )
102+
103+ }
104104}
105105
106106
0 commit comments