Skip to content

Commit 949c2b5

Browse files
OlisaevAGOlisaevAG
OlisaevAG
authored and
OlisaevAG
committed
fix: fixing issue PHPOffice#1670
1 parent b1fc4db commit 949c2b5

File tree

2 files changed

+190
-6
lines changed

2 files changed

+190
-6
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 189 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,34 @@ protected static function parseStyleDeclarations(array $selectors, array $styles
839839
case 'border-bottom':
840840
case 'border-right':
841841
case 'border-left':
842-
// must have exact order [width color style], e.g. "1px #0011CC solid" or "2pt green solid"
842+
$value = preg_replace('/ +/', ' ', trim($value));
843+
$valueArr = explode(' ', strtolower($value));
844+
845+
$size = $color = $style = null;
846+
// must have exact order of one of the options
847+
// [width color style], e.g. "1px #0011CC solid" or "2pt green solid"
848+
// [width style color], e.g. "1px solid #0011CC" or "2pt solid green"
843849
// Word does not accept shortened hex colors e.g. #CCC, only full e.g. #CCCCCC
844-
if (preg_match('/([0-9]+[^0-9]*)\s+(\#[a-fA-F0-9]+|[a-zA-Z]+)\s+([a-z]+)/', $value, $matches)) {
850+
// we determine the order of color and style by checking value of the color.
851+
$namedColors = self::mapNamedBorderColor();
852+
foreach($valueArr as $tmpValue){
853+
854+
if (strpos($tmpValue, '#') === 0 || isset($namedColors[$tmpValue])) {
855+
$color = trim($tmpValue, '#');
856+
} elseif(strpos($tmpValue, 'px') !== false ||
857+
strpos($tmpValue, 'pt') !== false ||
858+
strpos($tmpValue, 'cm') !== false ||
859+
strpos($tmpValue, 'mm') !== false ||
860+
strpos($tmpValue, 'in') !== false ||
861+
strpos($tmpValue, 'pc') !== false)
862+
{
863+
$size = Converter::cssToTwip($tmpValue);
864+
}else{
865+
$style = self::mapBorderStyle($valueArr[2]);
866+
}
867+
}
868+
869+
if ($size !== null && $color !== null && $style !== null) {
845870
if (false !== strpos($property, '-')) {
846871
$tmp = explode('-', $property);
847872
$which = $tmp[1];
@@ -855,14 +880,12 @@ protected static function parseStyleDeclarations(array $selectors, array $styles
855880
// Therefore we need to normalize converted twip value to cca 1/2 of value.
856881
// This may be adjusted, if better ratio or formula found.
857882
// BC change: up to ver. 0.17.0 was $size converted to points - Converter::cssToPoint($size)
858-
$size = Converter::cssToTwip($matches[1]);
859883
$size = (int) ($size / 2);
860884
// valid variants may be e.g. borderSize, borderTopSize, borderLeftColor, etc ..
861885
$styles["border{$which}Size"] = $size; // twips
862-
$styles["border{$which}Color"] = trim($matches[2], '#');
863-
$styles["border{$which}Style"] = self::mapBorderStyle($matches[3]);
886+
$styles["border{$which}Color"] = $color;
887+
$styles["border{$which}Style"] = $style;
864888
}
865-
866889
break;
867890
case 'vertical-align':
868891
// https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align
@@ -1009,12 +1032,172 @@ protected static function mapBorderStyle($cssBorderStyle)
10091032
case 'dashed':
10101033
case 'dotted':
10111034
case 'double':
1035+
case 'inset':
1036+
case 'outset':
10121037
return $cssBorderStyle;
10131038
default:
10141039
return 'single';
10151040
}
10161041
}
10171042

1043+
/**
1044+
* @return array
1045+
*/
1046+
protected static function mapNamedBorderColor(): array
1047+
{
1048+
$colors = [];
1049+
$colors['aliceblue'] = '#f0f8ff';
1050+
$colors['antiquewhite'] = '#faebd7';
1051+
$colors['aqua'] = '#00ffff';
1052+
$colors['aquamarine'] = '#7fffd4';
1053+
$colors['azure'] = '#f0ffff';
1054+
$colors['beige'] = '#f5f5dc';
1055+
$colors['bisque'] = '#ffe4c4';
1056+
$colors['black'] = '#000000';
1057+
$colors['blanchedalmond'] = '#ffebcd';
1058+
$colors['blue'] = '#0000ff';
1059+
$colors['blueviolet'] = '#8a2be2';
1060+
$colors['brown'] = '#a52a2a';
1061+
$colors['burlywood'] = '#deb887';
1062+
$colors['cadetblue'] = '#5f9ea0';
1063+
$colors['chartreuse'] = '#7fff00';
1064+
$colors['chocolate'] = '#d2691e';
1065+
$colors['coral'] = '#ff7f50';
1066+
$colors['cornflowerblue'] = '#6495ed';
1067+
$colors['cornsilk'] = '#fff8dc';
1068+
$colors['crimson'] = '#dc143c';
1069+
$colors['cyan'] = '#00ffff';
1070+
$colors['darkblue'] = '#00008b';
1071+
$colors['darkcyan'] = '#008b8b';
1072+
$colors['darkgoldenrod'] = '#b8860b';
1073+
$colors['darkgray'] = '#a9a9a9';
1074+
$colors['darkgrey'] = '#a9a9a9';
1075+
$colors['darkgreen'] = '#006400';
1076+
$colors['darkkhaki'] = '#bdb76b';
1077+
$colors['darkmagenta'] = '#8b008b';
1078+
$colors['darkolivegreen'] = '#556b2f';
1079+
$colors['darkorange'] = '#ff8c00';
1080+
$colors['darkorchid'] = '#9932cc';
1081+
$colors['darkred'] = '#8b0000';
1082+
$colors['darksalmon'] = '#e9967a';
1083+
$colors['darkseagreen'] = '#8fbc8f';
1084+
$colors['darkslateblue'] = '#483d8b';
1085+
$colors['darkslategray'] = '#2f4f4f';
1086+
$colors['darkslategrey'] = '#2f4f4f';
1087+
$colors['darkturquoise'] = '#00ced1';
1088+
$colors['darkviolet'] = '#9400d3';
1089+
$colors['deeppink'] = '#ff1493';
1090+
$colors['deepskyblue'] = '#00bfff';
1091+
$colors['dimgray'] = '#696969';
1092+
$colors['dimgrey'] = '#696969';
1093+
$colors['dodgerblue'] = '#1e90ff';
1094+
$colors['firebrick'] = '#b22222';
1095+
$colors['floralwhite'] = '#fffaf0';
1096+
$colors['forestgreen'] = '#228b22';
1097+
$colors['fuchsia'] = '#ff00ff';
1098+
$colors['gainsboro'] = '#dcdcdc';
1099+
$colors['ghostwhite'] = '#f8f8ff';
1100+
$colors['gold'] = '#ffd700';
1101+
$colors['goldenrod'] = '#daa520';
1102+
$colors['gray'] = '#808080';
1103+
$colors['grey'] = '#808080';
1104+
$colors['green'] = '#008000';
1105+
$colors['greenyellow'] = '#adff2f';
1106+
$colors['honeydew'] = '#f0fff0';
1107+
$colors['hotpink'] = '#ff69b4';
1108+
$colors['indianred'] = '#cd5c5c';
1109+
$colors['indigo'] = '#4b0082';
1110+
$colors['ivory'] = '#fffff0';
1111+
$colors['khaki'] = '#f0e68c';
1112+
$colors['lavender'] = '#e6e6fa';
1113+
$colors['lavenderblush'] = '#fff0f5';
1114+
$colors['lawngreen'] = '#7cfc00';
1115+
$colors['lemonchiffon'] = '#fffacd';
1116+
$colors['lightblue'] = '#add8e6';
1117+
$colors['lightcoral'] = '#f08080';
1118+
$colors['lightcyan'] = '#e0ffff';
1119+
$colors['lightgoldenrodyellow'] = '#fafad2';
1120+
$colors['lightgray'] = '#d3d3d3';
1121+
$colors['lightgrey'] = '#d3d3d3';
1122+
$colors['lightgreen'] = '#90ee90';
1123+
$colors['lightpink'] = '#ffb6c1';
1124+
$colors['lightsalmon'] = '#ffa07a';
1125+
$colors['lightseagreen'] = '#20b2aa';
1126+
$colors['lightskyblue'] = '#87cefa';
1127+
$colors['lightslategray'] = '#778899';
1128+
$colors['lightslategrey'] = '#778899';
1129+
$colors['lightsteelblue'] = '#b0c4de';
1130+
$colors['lightyellow'] = '#ffffe0';
1131+
$colors['lime'] = '#00ff00';
1132+
$colors['limegreen'] = '#32cd32';
1133+
$colors['linen'] = '#faf0e6';
1134+
$colors['magenta'] = '#ff00ff';
1135+
$colors['maroon'] = '#800000';
1136+
$colors['mediumaquamarine'] = '#66cdaa';
1137+
$colors['mediumblue'] = '#0000cd';
1138+
$colors['mediumorchid'] = '#ba55d3';
1139+
$colors['mediumpurple'] = '#9370db';
1140+
$colors['mediumseagreen'] = '#3cb371';
1141+
$colors['mediumslateblue'] = '#7b68ee';
1142+
$colors['mediumspringgreen'] = '#00fa9a';
1143+
$colors['mediumturquoise'] = '#48d1cc';
1144+
$colors['mediumvioletred'] = '#c71585';
1145+
$colors['midnightblue'] = '#191970';
1146+
$colors['mintcream'] = '#f5fffa';
1147+
$colors['mistyrose'] = '#ffe4e1';
1148+
$colors['moccasin'] = '#ffe4b5';
1149+
$colors['navajowhite'] = '#ffdead';
1150+
$colors['navy'] = '#000080';
1151+
$colors['oldlace'] = '#fdf5e6';
1152+
$colors['olive'] = '#808000';
1153+
$colors['olivedrab'] = '#6b8e23';
1154+
$colors['orange'] = '#ffa500';
1155+
$colors['orangered'] = '#ff4500';
1156+
$colors['orchid'] = '#da70d6';
1157+
$colors['palegoldenrod'] = '#eee8aa';
1158+
$colors['palegreen'] = '#98fb98';
1159+
$colors['paleturquoise'] = '#afeeee';
1160+
$colors['palevioletred'] = '#db7093';
1161+
$colors['papayawhip'] = '#ffefd5';
1162+
$colors['peachpuff'] = '#ffdab9';
1163+
$colors['peru'] = '#cd853f';
1164+
$colors['pink'] = '#ffc0cb';
1165+
$colors['plum'] = '#dda0dd';
1166+
$colors['powderblue'] = '#b0e0e6';
1167+
$colors['purple'] = '#800080';
1168+
$colors['rebeccapurple'] = '#663399';
1169+
$colors['red'] = '#ff0000';
1170+
$colors['rosybrown'] = '#bc8f8f';
1171+
$colors['royalblue'] = '#4169e1';
1172+
$colors['saddlebrown'] = '#8b4513';
1173+
$colors['salmon'] = '#fa8072';
1174+
$colors['sandybrown'] = '#f4a460';
1175+
$colors['seagreen'] = '#2e8b57';
1176+
$colors['seashell'] = '#fff5ee';
1177+
$colors['sienna'] = '#a0522d';
1178+
$colors['silver'] = '#c0c0c0';
1179+
$colors['skyblue'] = '#87ceeb';
1180+
$colors['slateblue'] = '#6a5acd';
1181+
$colors['slategray'] = '#708090';
1182+
$colors['slategrey'] = '#708090';
1183+
$colors['snow'] = '#fffafa';
1184+
$colors['springgreen'] = '#00ff7f';
1185+
$colors['steelblue'] = '#4682b4';
1186+
$colors['tan'] = '#d2b48c';
1187+
$colors['teal'] = '#008080';
1188+
$colors['thistle'] = '#d8bfd8';
1189+
$colors['tomato'] = '#ff6347';
1190+
$colors['turquoise'] = '#40e0d0';
1191+
$colors['violet'] = '#ee82ee';
1192+
$colors['wheat'] = '#f5deb3';
1193+
$colors['white'] = '#ffffff';
1194+
$colors['whitesmoke'] = '#f5f5f5';
1195+
$colors['yellow'] = '#ffff00';
1196+
$colors['yellowgreen'] = '#9acd32';
1197+
1198+
return $colors;
1199+
}
1200+
10181201
protected static function mapBorderColor(&$styles, $cssBorderColor): void
10191202
{
10201203
$numColors = substr_count($cssBorderColor, '#');

src/PhpWord/Writer/Word2007/Style/Table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ private function writeBorder(XMLWriter $xmlWriter, TableStyle $style): void
140140
$styleWriter = new MarginBorder($xmlWriter);
141141
$styleWriter->setSizes($style->getBorderSize());
142142
$styleWriter->setColors($style->getBorderColor());
143+
$styleWriter->setStyles($style->getBorderStyle());
143144
$styleWriter->write();
144145

145146
$xmlWriter->endElement(); // w:tblBorders

0 commit comments

Comments
 (0)