MCPcopy Create free account
hub / github.com/ai/webp-in-css / transformWithClass

Function transformWithClass

plugin.js:59–129  ·  view source on GitHub ↗
(selectors, targetClassName)

Source from the content-addressed store, hash-verified

57 * @param {string} targetClassName
58 */
59 let transformWithClass = (selectors, targetClassName) => {
60 selectors.each(selector => {
61 let isBodyFound = false
62 let isHtmlFound = false
63
64 // detect body & html, add class to body
65 selector.walkTags(currentSelector => {
66 if (currentSelector.value === 'body') {
67 isBodyFound = true
68 currentSelector.parent.insertAfter(currentSelector, selectorParser.className({value: targetClassName}))
69 }
70 else if (currentSelector.value === 'html') {
71 isHtmlFound = true
72 }
73 })
74
75 let bodyCombinator = selectorParser.combinator({value: ' '})
76 let bodyTag = selectorParser.tag({value: 'body'})
77 let bodyClass = selectorParser.className({value: targetClassName})
78
79 // html found, no body tags
80 if (isHtmlFound && !isBodyFound) {
81 let isHtmlFoundHere = false
82 selector.walk(currentSelector => {
83 if (currentSelector.value === 'html') {
84 isHtmlFoundHere = true
85 }
86 else if (isHtmlFoundHere && currentSelector.type === 'combinator') {
87 let selectorParent = currentSelector.parent
88 selectorParent.insertAfter(currentSelector, bodyTag)
89 selectorParent.insertAfter(bodyTag, bodyClass)
90 selectorParent.insertAfter(bodyClass, bodyCombinator)
91 }
92 })
93 }
94 else if (!isHtmlFound && !isBodyFound) {
95 if (selector.first.spaces.before === ' ') {
96 bodyTag.spaces.before = ' '
97 }
98 selector.first.spaces.before = ' '
99 selector.prepend(bodyClass)
100 selector.prepend(bodyTag)
101 }
102 })
103
104
105 if (addNoJs && initialClassName === noWebpClass) {
106 selectors.each(selector => {
107 let selectorClone = selector.clone()
108 selectorClone.walk(subSelector => {
109 if (subSelector.type === 'class' && subSelector.value === internalNoWebpClass) {
110 subSelector.value = noJsClass
111 }
112 })
113 selectorClone.first.spaces.before = ' '
114 selectors.insertAfter(selector, selectorClone)
115 })
116 }

Callers 1

transformFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected