|
| 1 | +import 'dart:ui'; |
| 2 | + |
| 3 | +import 'package:csslib/visitor.dart' as css; |
| 4 | +import 'package:csslib/parser.dart' as cssparser; |
| 5 | +import 'package:flutter_html/style.dart'; |
| 6 | + |
| 7 | +Map<String, Style> cssToStyles(css.StyleSheet sheet) { |
| 8 | + sheet.topLevels.forEach((treeNode) { |
| 9 | + if (treeNode is css.RuleSet) { |
| 10 | + print( |
| 11 | + treeNode.selectorGroup.selectors.first.simpleSelectorSequences.first.simpleSelector.name); |
| 12 | + } |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +Style declarationsToStyle(Map<String, List<css.Expression>> declarations) { |
| 17 | + Style style = new Style(); |
| 18 | + declarations.forEach((property, value) { |
| 19 | + switch (property) { |
| 20 | + case 'background-color': |
| 21 | + style.backgroundColor = ExpressionMapping.expressionToColor(value.first); |
| 22 | + break; |
| 23 | + case 'color': |
| 24 | + style.color = ExpressionMapping.expressionToColor(value.first); |
| 25 | + break; |
| 26 | + case 'direction': |
| 27 | + style.direction = ExpressionMapping.expressionToDirection(value.first); |
| 28 | + break; |
| 29 | + case 'display': |
| 30 | + style.display = ExpressionMapping.expressionToDisplay(value.first); |
| 31 | + break; |
| 32 | + case 'font-family': |
| 33 | + style.fontFamily = ExpressionMapping.expressionToFontFamily(value.first); |
| 34 | + break; |
| 35 | + case 'font-feature-settings': |
| 36 | + style.fontFeatureSettings = ExpressionMapping.expressionToFontFeatureSettings(value); |
| 37 | + break; |
| 38 | + case 'text-shadow': |
| 39 | + style.textShadow = ExpressionMapping.expressionToTextShadow(value); |
| 40 | + break; |
| 41 | + case 'text-align': |
| 42 | + style.textAlign = ExpressionMapping.expressionToTextAlign(value.first); |
| 43 | + break; |
| 44 | + } |
| 45 | + }); |
| 46 | + return style; |
| 47 | +} |
| 48 | + |
| 49 | +Style inlineCSSToStyle(String inlineStyle) { |
| 50 | + final sheet = cssparser.parse("*{$inlineStyle}"); |
| 51 | + final declarations = DeclarationVisitor().getDeclarations(sheet); |
| 52 | + return declarationsToStyle(declarations); |
| 53 | +} |
| 54 | + |
| 55 | +class DeclarationVisitor extends css.Visitor { |
| 56 | + Map<String, List<css.Expression>> _result; |
| 57 | + String _currentProperty; |
| 58 | + |
| 59 | + Map<String, List<css.Expression>> getDeclarations(css.StyleSheet sheet) { |
| 60 | + _result = new Map<String, List<css.Expression>>(); |
| 61 | + sheet.visit(this); |
| 62 | + return _result; |
| 63 | + } |
| 64 | + |
| 65 | + @override |
| 66 | + void visitDeclaration(css.Declaration node) { |
| 67 | + _currentProperty = node.property; |
| 68 | + _result[_currentProperty] = new List<css.Expression>(); |
| 69 | + node.expression.visit(this); |
| 70 | + } |
| 71 | + |
| 72 | + @override |
| 73 | + void visitExpressions(css.Expressions node) { |
| 74 | + node.expressions.forEach((expression) { |
| 75 | + _result[_currentProperty].add(expression); |
| 76 | + }); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +//Mapping functions |
| 81 | +class ExpressionMapping { |
| 82 | + |
| 83 | + static Color expressionToColor(css.Expression value) { |
| 84 | + if (value is css.HexColorTerm) { |
| 85 | + return stringToColor(value.text); |
| 86 | + } |
| 87 | + //TODO(Sub6Resources): Support function-term values (rgba()/rgb()) |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + static Color stringToColor(String _text) { |
| 92 | + var text = _text.replaceFirst('#', ''); |
| 93 | + if (text.length == 3) |
| 94 | + text = text.replaceAllMapped( |
| 95 | + RegExp(r"[a-f]|\d"), (match) => '${match.group(0)}${match.group(0)}'); |
| 96 | + int color = int.parse(text, radix: 16); |
| 97 | + |
| 98 | + if (color <= 0xffffff) { |
| 99 | + return new Color(color).withAlpha(255); |
| 100 | + } else { |
| 101 | + return new Color(color); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + static TextAlign expressionToTextAlign(css.Expression value) { |
| 106 | + if (value is css.LiteralTerm) { |
| 107 | + switch(value.text) { |
| 108 | + case "center": |
| 109 | + return TextAlign.center; |
| 110 | + case "left": |
| 111 | + return TextAlign.left; |
| 112 | + case "right": |
| 113 | + return TextAlign.right; |
| 114 | + case "justify": |
| 115 | + return TextAlign.justify; |
| 116 | + case "end": |
| 117 | + return TextAlign.end; |
| 118 | + case "start": |
| 119 | + return TextAlign.start; |
| 120 | + } |
| 121 | + } |
| 122 | + return TextAlign.start; |
| 123 | + } |
| 124 | + |
| 125 | + static TextDirection expressionToDirection(css.Expression value) { |
| 126 | + if (value is css.LiteralTerm) { |
| 127 | + switch(value.text) { |
| 128 | + case "ltr": |
| 129 | + return TextDirection.ltr; |
| 130 | + case "rtl": |
| 131 | + return TextDirection.rtl; |
| 132 | + } |
| 133 | + } |
| 134 | + return TextDirection.ltr; |
| 135 | + } |
| 136 | + |
| 137 | + static List<FontFeature> expressionToFontFeatureSettings(List<css.Expression> value) { |
| 138 | + //TODO |
| 139 | + return []; |
| 140 | + } |
| 141 | + |
| 142 | + static List<Shadow> expressionToTextShadow(List<css.Expression> value) { |
| 143 | + //TODO |
| 144 | + return []; |
| 145 | + } |
| 146 | + |
| 147 | + static Display expressionToDisplay(css.Expression value) { |
| 148 | + if (value is css.LiteralTerm) { |
| 149 | + switch(value.text) { |
| 150 | + case 'block': |
| 151 | + return Display.BLOCK; |
| 152 | + case 'inline-block': |
| 153 | + return Display.INLINE_BLOCK; |
| 154 | + case 'inline': |
| 155 | + return Display.INLINE; |
| 156 | + case 'list-item': |
| 157 | + return Display.LIST_ITEM; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + static String expressionToFontFamily(css.Expression value) { |
| 163 | + if(value is css.LiteralTerm) |
| 164 | + return value.text; |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | + |
0 commit comments