Skip to content

Commit

Permalink
Move CSSStyleDeclaration to css.js
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Jul 4, 2024
1 parent 6c173ce commit 84915b4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
5 changes: 4 additions & 1 deletion .github/jshint.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"quotmark": "double",
"shadow": "outer",
"undef": true,
"unused": true
"unused": true,
"globals": {
"Promise": true
}
}
32 changes: 32 additions & 0 deletions css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

/*! litejs.com/MIT-LICENSE.txt */

exports.CSSStyleDeclaration = CSSStyleDeclaration

// CSSStyleDeclaration is a single CSS declaration block,
// accessible via HTMLElement.style for inline styles, document.styleSheets[0].cssRules[0].style, and getComputedStyle()
function CSSStyleDeclaration(style) {
this.cssText = style
}

CSSStyleDeclaration.prototype = {
get cssText() {
return Object.keys(this).map(function(key) {
return (key === "cssFloat" ? "float:" : hyphenCase(key) + ":") + this[key]
}, this).join(";")
},
set cssText(style) {
for (var m, re = /(?:^|;)\s*([-a-z]+)\s*:((?:("|')(?:\\.|(?!\3)[^\\])*?\3|[^"';])+)(?=;|$)/ig; (m = re.exec(style)); ) {
this[m[1] === "float" ? "cssFloat" : camelCase(m[1])] = m[2].trim()
}
}
}

function camelCase(str) {
return str.replace(/-([a-z])/g, function(_, a) { return a.toUpperCase() })
}

function hyphenCase(str) {
return str.replace(/[A-Z]/g, "-$&").toLowerCase()
}

29 changes: 1 addition & 28 deletions dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var boolAttrs = {
, rawTextElements = { SCRIPT: /<(?=\/script)/i, STYLE: /<(?=\/style)/i }
, rawTextEscape = { SCRIPT: /<(?=\/script|!--)/ig, STYLE: /<(?=\/style|!--)/ig }
, hasOwn = voidElements.hasOwnProperty
, CSSStyleDeclaration = require("./css.js").CSSStyleDeclaration
, selector = require("./selector.js")
, Node = {
ELEMENT_NODE: 1,
Expand Down Expand Up @@ -326,25 +327,6 @@ NamedNodeMap.prototype = {
}
}

// CSSStyleDeclaration is a single CSS declaration block,
// accessible via HTMLElement.style for inline styles, document.styleSheets[0].cssRules[0].style, and getComputedStyle()
function CSSStyleDeclaration(style) {
this.cssText = style
}

CSSStyleDeclaration.prototype = {
get cssText() {
return Object.keys(this).map(function(key) {
return (key === "cssFloat" ? "float:" : hyphenCase(key) + ":") + this[key]
}, this).join(";")
},
set cssText(style) {
for (var m, re = /(?:^|;)\s*([-a-z]+)\s*:((?:("|')(?:\\.|(?!\3)[^\\])*?\3|[^"';])+)(?=;|$)/ig; (m = re.exec(style)); ) {
this[m[1] === "float" ? "cssFloat" : camelCase(m[1])] = m[2].trim()
}
}
}

function DocumentFragment() {
this.childNodes = []
}
Expand Down Expand Up @@ -508,17 +490,8 @@ function getSibling(node, step, type) {
return type > 0 ? getElement(silbings, index + step, step, type) : silbings && silbings[index + step] || null
}

function camelCase(str) {
return str.replace(/-([a-z])/g, function(_, a) { return a.toUpperCase() })
}

function hyphenCase(str) {
return str.replace(/[A-Z]/g, "-$&").toLowerCase()
}

exports.document = new Document()
exports.entities = entities
exports.CSSStyleDeclaration = CSSStyleDeclaration
exports.DOMParser = DOMParser
exports.Document = Document
exports.DocumentFragment = DocumentFragment
Expand Down

0 comments on commit 84915b4

Please sign in to comment.