|
1 | 1 | import objectAssign from 'object-assign';
|
2 | 2 | import specificity from 'specificity';
|
| 3 | +import parseAttrs from 'posthtml-attrs-parser'; |
3 | 4 |
|
4 | 5 |
|
5 | 6 | export function extendStyle(htmlNode, cssNode) {
|
6 |
| - htmlNode.attrs = htmlNode.attrs || {}; |
7 |
| - htmlNode.attrs.style = htmlNode.attrs.style || ''; |
8 |
| - |
9 |
| - const htmlNodeCss = parseCssFromStyle(htmlNode.attrs.style); |
| 7 | + var attrs = parseAttrs(htmlNode.attrs); |
10 | 8 | const cssNodeCss = parseCssFromNode(cssNode);
|
11 |
| - const newCss = objectAssign({}, htmlNodeCss, cssNodeCss); |
12 |
| - htmlNode.attrs.style = stringifyCss(newCss); |
| 9 | + attrs.style = objectAssign(attrs.style || {}, cssNodeCss); |
| 10 | + htmlNode.attrs = attrs.compose(); |
13 | 11 |
|
14 | 12 | return htmlNode;
|
15 | 13 | }
|
@@ -63,30 +61,6 @@ function getSpecificity(selector) {
|
63 | 61 | }
|
64 | 62 |
|
65 | 63 |
|
66 |
| -function stringifyCss(css) { |
67 |
| - const styles = Object.keys(css).map(prop => prop + ': ' + css[prop]); |
68 |
| - return styles.join('; '); |
69 |
| -} |
70 |
| - |
71 |
| - |
72 |
| -function parseCssFromStyle(style) { |
73 |
| - var css = {}; |
74 |
| - style.split(';').forEach(cssRule => { |
75 |
| - const cssRuleParts = cssRule.split(':'); |
76 |
| - if (cssRuleParts.length < 2) { |
77 |
| - return; |
78 |
| - } |
79 |
| - |
80 |
| - const cssRuleProp = cssRuleParts[0].trim(); |
81 |
| - const cssRuleValue = cssRuleParts.slice(1).join(':').trim(); |
82 |
| - |
83 |
| - css[cssRuleProp] = cssRuleValue; |
84 |
| - }); |
85 |
| - |
86 |
| - return css; |
87 |
| -} |
88 |
| - |
89 |
| - |
90 | 64 | function parseCssFromNode(cssNode) {
|
91 | 65 | var css = {};
|
92 | 66 | cssNode.nodes.forEach(node => {
|
|
0 commit comments