mirror of
https://github.com/zoriya/react-native-svg.git
synced 2026-06-05 16:04:38 +00:00
refactor: simplify inlining css from style elements
This commit is contained in:
+15
-36
@@ -288,23 +288,6 @@ function sortSelectors(selectors) {
|
|||||||
return stable(selectors, _bySelectorSpecificity);
|
return stable(selectors, _bySelectorSpecificity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a css-tree AST style declaration to CSSStyleDeclaration property.
|
|
||||||
*
|
|
||||||
* @param {Object} declaration css-tree style declaration
|
|
||||||
* @return {Object} CSSStyleDeclaration property
|
|
||||||
*/
|
|
||||||
function csstreeToStyleDeclaration(declaration) {
|
|
||||||
const propertyName = declaration.property,
|
|
||||||
propertyValue = csstree.generate(declaration.value),
|
|
||||||
propertyPriority = declaration.important ? 'important' : '';
|
|
||||||
return {
|
|
||||||
name: propertyName,
|
|
||||||
value: propertyValue,
|
|
||||||
priority: propertyPriority,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the CSS string of a style element
|
* Gets the CSS string of a style element
|
||||||
*
|
*
|
||||||
@@ -339,8 +322,8 @@ function CSSStyleDeclaration(node) {
|
|||||||
|
|
||||||
declarations.children.each(declaration => {
|
declarations.children.each(declaration => {
|
||||||
try {
|
try {
|
||||||
const { name, value, priority } = csstreeToStyleDeclaration(declaration);
|
const { property, value, important } = declaration;
|
||||||
this.setProperty(name, value, priority);
|
this.setProperty(property, csstree.generate(value), important);
|
||||||
} catch (styleError) {
|
} catch (styleError) {
|
||||||
if (styleError.message !== 'Unknown node type: undefined') {
|
if (styleError.message !== 'Unknown node type: undefined') {
|
||||||
console.warn(
|
console.warn(
|
||||||
@@ -365,26 +348,22 @@ CSSStyleDeclaration.prototype.getProperty = function(propertyName) {
|
|||||||
/**
|
/**
|
||||||
* Modify an existing CSS property or creates a new CSS property in the declaration block.
|
* Modify an existing CSS property or creates a new CSS property in the declaration block.
|
||||||
*
|
*
|
||||||
* @param {String} propertyName representing the CSS property name to be modified.
|
* @param {String} name representing the CSS property name to be modified.
|
||||||
* @param {String} [value] containing the new property value. If not specified, treated as the empty string. value must not contain "!important" -- that should be set using the priority parameter.
|
* @param {String} [value] containing the new property value. If not specified, treated as the empty string. value must not contain "!important" -- that should be set using the priority parameter.
|
||||||
* @param {String} [priority] allowing the "important" CSS priority to be set. If not specified, treated as the empty string.
|
* @param {String} [important] allowing the "important" CSS priority to be set. If not specified, treated as the empty string.
|
||||||
* @return {undefined}
|
* @return {undefined}
|
||||||
*/
|
*/
|
||||||
CSSStyleDeclaration.prototype.setProperty = function(
|
CSSStyleDeclaration.prototype.setProperty = function(name, value, important) {
|
||||||
propertyName,
|
if (typeof name === 'undefined') {
|
||||||
value,
|
|
||||||
priority,
|
|
||||||
) {
|
|
||||||
if (typeof propertyName === 'undefined') {
|
|
||||||
throw Error('propertyName argument required, but only not present.');
|
throw Error('propertyName argument required, but only not present.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const trimmedValue = value.trim();
|
const trimmedValue = value.trim();
|
||||||
const property = {
|
const property = {
|
||||||
value: trimmedValue,
|
value: trimmedValue,
|
||||||
priority: priority.trim(),
|
important,
|
||||||
};
|
};
|
||||||
const key = propertyName.trim();
|
const key = name.trim();
|
||||||
this.properties.set(key, property);
|
this.properties.set(key, property);
|
||||||
this.style[camelCase(key)] = trimmedValue;
|
this.style[camelCase(key)] = trimmedValue;
|
||||||
|
|
||||||
@@ -520,6 +499,8 @@ export function inlineStyles(document) {
|
|||||||
if (selector.rule === null) {
|
if (selector.rule === null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
initStyle(selectedEl);
|
||||||
|
const { style } = selectedEl;
|
||||||
|
|
||||||
// merge declarations
|
// merge declarations
|
||||||
csstree.walk(selector.rule, {
|
csstree.walk(selector.rule, {
|
||||||
@@ -529,15 +510,13 @@ export function inlineStyles(document) {
|
|||||||
// no inline styles, external styles, external styles used
|
// no inline styles, external styles, external styles used
|
||||||
// inline styles, external styles same priority as inline styles, inline styles used
|
// inline styles, external styles same priority as inline styles, inline styles used
|
||||||
// inline styles, external styles higher priority than inline styles, external styles used
|
// inline styles, external styles higher priority than inline styles, external styles used
|
||||||
const { name, value, priority } = csstreeToStyleDeclaration(
|
const { property, value, important } = styleCsstreeDeclaration;
|
||||||
styleCsstreeDeclaration,
|
const styleProperty = style.getProperty(property);
|
||||||
);
|
if (styleProperty && styleProperty.important >= important) {
|
||||||
initStyle(selectedEl);
|
|
||||||
const styleProperty = selectedEl.style.getProperty(name);
|
|
||||||
if (styleProperty && styleProperty.priority >= priority) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
selectedEl.style.setProperty(name, value, priority);
|
const propertyValue = csstree.generate(value);
|
||||||
|
style.setProperty(property, propertyValue, important);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user