12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743 |
- // Copyright (c) Microsoft. All rights reserved.
- // Licensed under the MIT license. See LICENSE file in the project root for full license information.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Xml.Linq;
- using OpenXmlPowerTools;
- using OpenXmlPowerTools.HtmlToWml;
- using OpenXmlPowerTools.HtmlToWml.CSS;
- using System.Globalization;
- #if false
- Sort:
- 1. User agent declarations
- 1. Default value
- 2. Specified in default style sheet (which I am going to supply)
- 2. User normal declarations
- 1. Specified in user-supplied style sheet
- 3. Author normal declarations
- 1. Specified in style sheet (can defer this until later)
- 2. Specified in style element
- 3. Specified in style attribute
- 4. Author important declarations
- 1. Specified in style sheet
- 5. User important declarations
- 1. Specified in style sheet
- Sort Key (most significant first)
- {0} 9 if user supplied style sheet, high
- {0} 8 if author supplied style sheet, high
- {0} 7 if style attribute, high
- {0} 6 if style attribute, normal
- {0} 5 if author suplied style sheet, normal
- {0} 4 if user supplied style sheet, normal
- {0} 3 if user-agent default style sheet, high
- {0} 2 if user-agent default style sheet, normal
- {0} 1 if inherited
- {1} count of number of ID attributes in selector
- {2} count of number of attributes and pseudo classes in selector
- {3} count of number of element names and pseudo elements in selector
- {4} sequence number in order added to list
- 1. Process user-agent default style sheet
- 2. Process user-supplied style sheet
- 3. process author-supplied style sheet
- 4. process STYLE element
- 5. process style attribute on all elements
- 6. expand all shorthand properties - the new properties have the same sort key as shorthand prop
- 7. Add initial values for all properties that don't have values
- The various types of properties:
- - Set by the style sheet explicitely
- - Set at root level of hierarchy so that other elements can inherit (if no other ancestor)
- - Set as initial value for certain elements - all properties have an initial value. Some properties apply to only certain elements.
- - Some properties inherit automatically, such as font-family.
- - Some properties are set to inherit in the stylesheet.
- - border-top-color etc. are set to the color property, which then inherits.
- Properties either inherit, or are set to an initial value. Those properties that inherit are set to initial value only at the top of
- the tree. Those props that are set to an initial value throughout the tree do not inherit. It is either one or the other.
- Following is my new theory of the correct algorithm:
- - set all properties from the cascade.
- - Set initial values for properties at the top of the tree. These specifically need to be set for other properties that will
- inherit from them.
- - create a matrix of all elements, and which properties need to be set for each element.
- - iterate through the tree.
- SetAllValues
- for each element
- for each property
- call GetComputedPropertyValue // implemented in a recursive fashion to set & get its computed value.
- GetComputedPropertyValue
- if (property is already computed)
- return the computed value
- if (property is set)
- compute value
- set the computed value
- return the computed value
- if (property is inherited (either because it was set to inherit, or because it is an inherited property))
- if (parent exists)
- call GetComputedValue on parent
- set the computed value
- return the computed value
- else
- call GetInitialValue for property
- compute value
- set the computed value
- return the computed value
- else
- call GetInitialValue for property
- compute value
- set the computed value
- return the computed value
-
- ComputeValue
- this needs to be specifically coded for each property
- if value is relative (em, ex, percentage,
- if property is not on font-size
- GetComputedValue for font-size
- compute value accordingly
- return value
- if property is on font-size
- call GetComputedValue for font-size of parent
- compute value accordingly
- return value
- if value is percentage
- if margin-top, margin-bottom, margin-right, margin-left, padding-top, padding-bottom, padding-right, padding-left
- call GetComputedValue for width of containing block
- if value is absolute (in, cm, mm, pt, pc, px)
- return value
- #endif
- namespace OpenXmlPowerTools.HtmlToWml
- {
- class CssApplier
- {
- private static List<PropertyInfo> PropertyInfoList = new List<PropertyInfo>()
- {
- // color
- // Value: <color> | inherit
- // Initial: depends on UA
- // Applies to: all elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "color" },
- Inherits = true,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "black", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = (element, assignedValue, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = GetWmlColorFromExpression(assignedValue), Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- },
- // direction
- // Value: ltr | rtl | inherit
- // Initial: ltr
- // Applies to: all elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "direction" },
- Inherits = true,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "ltr", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // line-height
- // Value: normal | <number> | <length> | <percentage> | <inherit>
- // Initial: normal
- // Applies to: all elements
- // Inherited: yes
- // Percentages: refer to the font size of the element itself
- // Computed value: for <length> and <percentage> the absolute value, otherwise as specified.
- new PropertyInfo
- {
- Names = new[] { "line-height" },
- Inherits = true,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "normal", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = (element, assignedValue, settings) =>
- {
- CssExpression valueForPercentage = null;
- if (element.Parent != null)
- valueForPercentage = GetComputedPropertyValue(null, element, "font-size", settings);
- return ComputeAbsoluteLength(element, assignedValue, settings, valueForPercentage);
- },
- },
- // visibility
- // Value: visible | hidden | collapse | inherit
- // Initial: visible
- // Applies to: all elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "visibility" },
- Inherits = true,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "visible", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // list-style-type
- // Value: disc | circle | square | decimal | decimal-leading-zero |
- // lower-roman | upper-roman | lower-greek | lower-latin |
- // upper-latin | armenian | georgian | lower-alpha | upper-alpha |
- // none | inherit
- // Initial: disc
- // Applies to: elements with display: list-item
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "list-style-type" },
- Inherits = true,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "list-item")
- return true;
- return false;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "disc", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // list-style-image
- // Value: <uri> | none | inherit
- // Initial: none
- // Applies to: elements with ’display: list-item’
- // Inherited: yes
- // Percentages: N/A
- // Computed value: absolute URI or ’none’
- new PropertyInfo
- {
- Names = new[] { "list-style-image" },
- Inherits = true,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "list-item")
- return true;
- return false;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "none", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // list-style-position
- // Value: inside | outside | inherit
- // Initial: outside
- // Applies to: elements with ’display: list-item’
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "list-style-position" },
- Inherits = true,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "list-item")
- return true;
- return false;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "none", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // font-family
- // Value: [[ <family-name> | <generic-family> ] [, <family-name>|
- // <generic-family>]* ] | inherit
- // Initial: depends on user agent
- // Applies to: all elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "font-family" },
- Inherits = true,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = settings.MinorLatinFont, Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = (element, assignedValue, settings) => assignedValue,
- },
- // font-style
- // Value: normal | italic | oblique | inherit
- // Initial: normal
- // Applies to: all elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "font-style" },
- Inherits = true,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "normal", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // font-variant
- // Value: normal | small-caps | inherit
- // Initial: normal
- // Applies to: all elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "font-variant" },
- Inherits = true,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "normal", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // font-weight
- // Value: normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 |
- // 600 | 700 | 800 | 900 | inherit
- // Initial: normal
- // Applies to: all elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: see text
- new PropertyInfo
- {
- Names = new[] { "font-weight" },
- Inherits = true,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "normal", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // font-size
- // Value: <absolute-size> | <relative-size> | <length> | <percentage> |
- // inherit
- // Initial: medium
- // Applies to: all elements
- // Inherited: yes
- // Percentages: refer to inherited font size
- // Computed value: absolute length
- new PropertyInfo
- {
- Names = new[] { "font-size" },
- Inherits = true,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = ((double)settings.DefaultFontSize).ToString(CultureInfo.InvariantCulture), Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, Unit = CssUnit.PT } } },
- ComputedValue = (element, assignedValue, settings) => ComputeAbsoluteFontSize(element, assignedValue, settings),
- },
- // text-indent
- // Value: <length> | <percentage> | inherit
- // Initial: 0
- // Applies to: block containers
- // Inherited: yes
- // Percentages: refer to width of containing block
- // Computed value: the percentage as specified or the absolute length
- new PropertyInfo
- {
- Names = new[] { "text-indent" },
- Inherits = true,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "block")
- return true;
- return false;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "0", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } },
- ComputedValue = (element, assignedValue, settings) =>
- {
- CssExpression valueForPercentage = null;
- if (element.Parent != null)
- valueForPercentage = GetComputedPropertyValue(null, element.Parent, "width", settings);
- return ComputeAbsoluteLength(element, assignedValue, settings, valueForPercentage);
- },
- },
- // text-align
- // Value: left | right | center | justify | inherit
- // Initial: a nameless value that acts as ’left’ if ’direction’ is ’ltr’, ’right’ if
- // ’direction’ is ’rtl’
- // Applies to: block containers
- // Inherited: yes
- // Percentages: N/A
- // Computed value: the initial value or as spec
- new PropertyInfo
- {
- Names = new[] { "text-align" },
- Inherits = true,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "block")
- return true;
- return false;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "left", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } }, // todo should be based on the direction property
- ComputedValue = null,
- },
- // text-decoration
- // Value: none | [ underline || overline || line-through || blink ] | inherit
- // Initial: none
- // Applies to: all elements
- // Inherited: no
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "text-decoration" },
- Inherits = true, // todo need to read css 16.3.1 in full detail to understand how this is implemented.
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "none", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } },
- ComputedValue = null,
- },
- // letter-spacing
- // Value: normal | <length> | inherit
- // Initial: normal
- // Applies to: all elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: ’normal’ or absolute length
- // word-spacing
- // Value: normal | <length> | inherit
- // Initial: normal
- // Applies to: all elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: for ’normal’ the value 0; otherwise the absolute length
- new PropertyInfo
- {
- Names = new[] { "letter-spacing", "word-spacing" },
- Inherits = true,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "normal", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } },
- ComputedValue = (element, assignedValue, settings) => ComputeAbsoluteLength(element, assignedValue, settings, null),
- },
- // white-space
- // Value: normal | pre | nowrap | pre-wrap | pre-line | inherit
- // Initial: normal
- // Applies to: all elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "white-space" },
- Inherits = true,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "normal", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } },
- ComputedValue = null,
- },
- // caption-side
- // Value: top | bottom | inherit
- // Initial: top
- // Applies to: 'table-caption' elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "caption-side" },
- Inherits = true,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "table-caption")
- return true;
- return false;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "top", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } },
- ComputedValue = null,
- },
- // border-collapse
- // Value: collapse | separate | inherit
- // Initial: separate
- // Applies to: ’table’ and ’inline-table’ elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "border-collapse" },
- Inherits = true,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "table" || display.ToString() == "inline-table")
- return true;
- return false;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "separate", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } },
- ComputedValue = null,
- },
- // border-spacing
- // Value: <length> <length>? | inherit
- // Initial: 0
- // Applies to: ’table’ and ’inline-table’ elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: two absolute lengths
- new PropertyInfo
- {
- Names = new[] { "border-spacing" },
- Inherits = true,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "table" || display.ToString() == "inline-table")
- return true;
- return false;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "0", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } },
- ComputedValue = (element, assignedValue, settings) => ComputeAbsoluteLength(element, assignedValue, settings, null), // todo need to handle two lengths here
- },
- // empty-cells
- // Value: show | hide | inherit
- // Initial: show
- // Applies to: 'table-cell' elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "empty-cells" },
- Inherits = true,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "table" || display.ToString() == "table-cell")
- return true;
- return false;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "show", } } },
- ComputedValue = null,
- },
- // margin-top, margin-bottom
- // Value: <margin-width> | inherit
- // Initial: 0
- // Applies to: all elements except elements with table display types other than table-caption, table, and inline-table
- // all elements except th, td, tr
- // Inherited: no
- // Percentages: refer to width of containing block
- // Computed value: the percentage as specified or the absolute length
- new PropertyInfo
- {
- Names = new[] { "margin-top", "margin-bottom", },
- Inherits = false,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "table-caption" || display.ToString() == "table" || display.ToString() == "inline-table")
- return false;
- return true;
- },
- InitialValue = (element, settings) =>
- {
- if (settings.DefaultBlockContentMargin != null)
- {
- if (settings.DefaultBlockContentMargin == "auto")
- return new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "auto", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } };
- else if (settings.DefaultBlockContentMargin.ToLower().EndsWith("pt"))
- {
- string s1 = settings.DefaultBlockContentMargin.Substring(0, settings.DefaultBlockContentMargin.Length - 2);
- double d1;
- if (double.TryParse(s1, NumberStyles.Float, CultureInfo.InvariantCulture, out d1))
- {
- return new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = d1.ToString(CultureInfo.InvariantCulture), Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT } } };
- }
- }
- throw new OpenXmlPowerToolsException("invalid setting");
- }
- return new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "0", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT } } };
- },
- ComputedValue = (element, assignedValue, settings) =>
- {
- CssExpression valueForPercentage = null;
- if (element.Parent != null)
- valueForPercentage = GetComputedPropertyValue(null, element.Parent, "width", settings);
- return ComputeAbsoluteLength(element, assignedValue, settings, valueForPercentage);
- },
- },
- // margin-right, margin-left
- // Value: <margin-width> | inherit
- // Initial: 0
- // Applies to: all elements except elements with table display types other than table-caption, table, and inline-table
- // all elements except th, td, tr
- // Inherited: no
- // Percentages: refer to width of containing block
- // Computed value: the percentage as specified or the absolute length
- new PropertyInfo
- {
- Names = new[] { "margin-right", "margin-left", },
- Inherits = false,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "table-caption" || display.ToString() == "table" || display.ToString() == "inline-table")
- return false;
- return true;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "0", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } },
- ComputedValue = (element, assignedValue, settings) =>
- {
- CssExpression valueForPercentage = null;
- if (element.Parent != null)
- valueForPercentage = GetComputedPropertyValue(null, element.Parent, "width", settings);
- return ComputeAbsoluteLength(element, assignedValue, settings, valueForPercentage);
- },
- },
- // padding-top, padding-right, padding-bottom, padding-left
- // Value: <padding-width> | inherit
- // Initial: 0
- // Applies to: all elements except table-row-group, table-header-group,
- // table-footer-group, table-row, table-column-group and table-column
- // all elements except tr
- // Inherited: no
- // Percentages: refer to width of containing block
- // Computed value: the percentage as specified or the absolute length
- new PropertyInfo
- {
- Names = new[] { "padding-top", "padding-right", "padding-bottom", "padding-left" },
- Inherits = false,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- string dv = display.ToString();
- if (dv == "table-row-group" || dv == "table-header-group" || dv == "table-footer-group" || dv == "table-row" ||
- dv == "table-column-group" || dv == "table-column")
- return false;
- return true;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "0", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } },
- ComputedValue = (element, assignedValue, settings) =>
- {
- CssExpression valueForPercentage = null;
- if (element.Parent != null)
- valueForPercentage = GetComputedPropertyValue(null, element.Parent, "width", settings);
- return ComputeAbsoluteLength(element, assignedValue, settings, valueForPercentage);
- },
- },
- // border-top-width, border-right-width, border-bottom-width, border-left-width
- // Value: <border-width> | inherit
- // Initial: medium
- // Applies to: all elements
- // Inherited: no
- // Percentages: N/A
- // Computed value: absolute length; '0' if the border style is 'none' or 'hidden'
- new PropertyInfo
- {
- Names = new[] { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width", },
- Inherits = false,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "0", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } },
- ComputedValue = (element, assignedValue, settings) =>
- {
- string assignedValueStr = assignedValue.ToString();
- if (assignedValueStr == "thin")
- return new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "0.75", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } };
- if (assignedValueStr == "medium")
- return new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "3.0", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } };
- if (assignedValueStr == "thick")
- return new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "4.5", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } };
- return ComputeAbsoluteLength(element, assignedValue, settings, null);
- },
- },
- // border-top-style, border-right-style, border-bottom-style, border-left-style
- // Value: <border-style> | inherit
- // Initial: none
- // Applies to: all elements
- // Inherited: no
- // Percentages: N/A
- // Computed value: as specified
- new PropertyInfo
- {
- Names = new[] { "border-top-style", "border-right-style", "border-bottom-style", "border-left-style", },
- Inherits = false,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "none", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // display
- // Value: inline | block | list-item | inline-block | table | inline-table |
- // table-row-group | table-header-group | table-footer-group |
- // table-row | table-column-group | table-column | table-cell |
- // table-caption | none | inherit
- // Initial: inline
- // Applies to: all elements
- // Inherited: no
- // Percentages: N/A
- // Computed value: see text
- new PropertyInfo
- {
- Names = new[] { "display", },
- Inherits = false,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inline", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // position
- // Value: static | relative | absolute | fixed | inherit
- // Initial: static
- // Applies to: all elements
- // Inherited: no
- // Percentages: N/A
- // Computed value: as specified
- new PropertyInfo
- {
- Names = new[] { "position", },
- Inherits = false,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "static", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // float
- // Value: left | right | none | inherit
- // Initial: none
- // Applies to: all, but see 9.7 p. 153
- // Inherited: no
- // Percentages: N/A
- // Computed value: as specified
- new PropertyInfo
- {
- Names = new[] { "float", },
- Inherits = false,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "none", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // unicode-bidi
- // Value: normal | embed | bidi-override | inherit
- // Initial: normal
- // Applies to: all elements, but see prose
- // Inherited: no
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "unicode-bidi", },
- Inherits = false,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "normal", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // background-color
- // Value: <color> | transparent | inherit
- // Initial: transparent
- // Applies to: all elements
- // Inherited: no
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "background-color", },
- Inherits = false,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "transparent", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = (element, assignedValue, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = GetWmlColorFromExpression(assignedValue), Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- },
- // text-transform
- // Value: capitalize | uppercase | lowercase | none | inherit
- // Initial: none
- // Applies to: all elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "text-transform", },
- Inherits = true,
- Includes = (e, settings) => true,
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "none", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- ComputedValue = null,
- },
- // table-layout
- // Value: auto | fixed | inherit
- // Initial: auto
- // Applies to: ’table’ and ’inline-table’ elements
- // Inherited: no
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "table-layout" },
- Inherits = true,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "table" || display.ToString() == "inline-table")
- return true;
- return false;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "auto", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } },
- ComputedValue = null,
- },
- // empty-cells
- // Value: show | hide | inherit
- // Initial: show
- // Applies to: 'table-cell' elements
- // Inherited: yes
- // Percentages: N/A
- // Computed value: as spec
- new PropertyInfo
- {
- Names = new[] { "border-spacing" },
- Inherits = true,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- if (display.ToString() == "table-cell")
- return true;
- return false;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "show", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } },
- ComputedValue = null,
- },
- // border-top-color, border-right-color, border-bottom-color, border-left-color
- // Value: <color> | transparent | inherit
- // Initial: the value of the color property
- // Applies to: all elements
- // Inherited: no
- // Percentages: N/A
- // Computed value: when taken from the ’color’ property, the computed value of
- // ’color’; otherwise, as specified
- new PropertyInfo
- {
- Names = new[] { "border-top-color", "border-right-color", "border-bottom-color", "border-left-color", },
- Inherits = false,
- Includes = (e, settings) => true,
- InitialValue = (e, settings) => {
- var display = GetComputedPropertyValue(null, e, "color", settings);
- return display;
- },
- ComputedValue = (element, assignedValue, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = GetWmlColorFromExpression(assignedValue), Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } },
- },
- // width
- // Value: <length> | <percentage> | auto | inherit
- // Initial: auto
- // Applies to: all elements but non-replaced in-line elements, table rows, and row groups
- // Inherited: no
- // Percentages: refer to width of containing block
- // Computed value: the percentage or 'auto' as specified or the absolute length
- new PropertyInfo
- {
- Names = new[] { "width" },
- Inherits = false,
- Includes = (e, settings) =>
- {
- if (e.Name == XhtmlNoNamespace.img)
- return true;
- var display = GetComputedPropertyValue(null, e, "display", settings);
- var dv = display.ToString();
- if (dv == "inline" || dv == "table-row" || dv == "table-row-group")
- return false;
- return true;
- },
- InitialValue = (element, settings) =>
- {
- if (element.Parent == null)
- {
- double? pageWidth = (double?)settings.SectPr.Elements(W.pgSz).Attributes(W._w).FirstOrDefault();
- if (pageWidth == null)
- pageWidth = 12240;
- double? leftMargin = (double?)settings.SectPr.Elements(W.pgMar).Attributes(W.left).FirstOrDefault();
- if (leftMargin == null)
- leftMargin = 1440;
- double? rightMargin = (double?)settings.SectPr.Elements(W.pgMar).Attributes(W.left).FirstOrDefault();
- if (rightMargin == null)
- rightMargin = 1440;
- double width = (double)(pageWidth - leftMargin - rightMargin) / 20;
- return new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = width.ToString(CultureInfo.InvariantCulture), Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, Unit = CssUnit.PT, } } };
- }
- return new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "auto", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } };
- },
- ComputedValue = (element, assignedValue, settings) =>
- {
- if (element.Name != XhtmlNoNamespace.caption &&
- element.Name != XhtmlNoNamespace.td &&
- element.Name != XhtmlNoNamespace.th &&
- element.Name != XhtmlNoNamespace.tr &&
- element.Name != XhtmlNoNamespace.table &&
- assignedValue.IsAuto)
- {
- PropertyInfo pi = PropertyInfoList.FirstOrDefault(p => p.Names.Contains("width"));
- string display = GetComputedPropertyValue(pi, element, "display", settings).ToString();
- if (display != "inline")
- {
- CssExpression parentPropertyValue = GetComputedPropertyValue(pi, element.Parent, "width", settings);
- return parentPropertyValue;
- }
- }
- CssExpression valueForPercentage = null;
- XElement elementToQuery = element.Parent;
- while (elementToQuery != null)
- {
- valueForPercentage = GetComputedPropertyValue(null, elementToQuery, "width", settings);
- if (valueForPercentage.IsAuto)
- {
- elementToQuery = elementToQuery.Parent;
- continue;
- }
- break;
- }
- return ComputeAbsoluteLength(element, assignedValue, settings, valueForPercentage);
- },
- },
-
- // min-width
- // Value: <length> | <percentage> | inherit
- // Initial: 0
- // Applies to: all elements but non-replaced in-line elements, table rows, and row groups
- // Inherited: no
- // Percentages: refer to width of containing block
- // Computed value: the percentage as spec or the absolute length
- new PropertyInfo
- {
- Names = new[] { "min-width" },
- Inherits = false,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- var dv = display.ToString();
- if (dv == "inline" || dv == "table-row" || dv == "table-row-group")
- return false;
- return true;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "0", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } },
- ComputedValue = (element, assignedValue, settings) =>
- {
- CssExpression valueForPercentage = null;
- if (element.Parent != null)
- valueForPercentage = GetComputedPropertyValue(null, element.Parent, "width", settings);
- return ComputeAbsoluteLength(element, assignedValue, settings, valueForPercentage);
- },
- },
- // max-width
- // Value: <length> | <percentage> | none | inherit
- // Initial: none
- // Applies to: all elements but non-replaced in-line elements, table rows, and row groups
- // Inherited: no
- // Percentages: refer to width of containing block
- // Computed value: the percentage as spec or the absolute length
- new PropertyInfo
- {
- Names = new[] { "max-width" },
- Inherits = false,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- var dv = display.ToString();
- if (dv == "inline" || dv == "table-row" || dv == "table-row-group")
- return false;
- return true;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "none", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } },
- ComputedValue = (element, assignedValue, settings) =>
- {
- CssExpression valueForPercentage = null;
- if (element.Parent != null)
- valueForPercentage = GetComputedPropertyValue(null, element.Parent, "width", settings);
- return ComputeAbsoluteLength(element, assignedValue, settings, valueForPercentage);
- },
- },
- // height
- // Value: <length> | <percentage> | auto | inherit
- // Initial: auto
- // Applies to: all elements but non-replaced in-line elements, table columns, and column groups
- // Inherited: no
- // Percentages: see prose
- // Computed value: the percentage as spec or the absolute length
- new PropertyInfo
- {
- Names = new[] { "height" },
- Inherits = false,
- Includes = (e, settings) =>
- {
- if (e.Name == XhtmlNoNamespace.img)
- return true;
- var display = GetComputedPropertyValue(null, e, "display", settings);
- var dv = display.ToString();
- if (dv == "inline" || dv == "table-row" || dv == "table-row-group")
- return false;
- return true;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "auto", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } },
- ComputedValue = (element, assignedValue, settings) =>
- {
- CssExpression valueForPercentage = null;
- if (element.Parent != null)
- valueForPercentage = GetComputedPropertyValue(null, element.Parent, "height", settings);
- return ComputeAbsoluteLength(element, assignedValue, settings, valueForPercentage);
- },
- },
-
- // min-height
- // Value: <length> | <percentage> | inherit
- // Initial: 0
- // Applies to: all elements but non-replaced in-line elements, table columns, and column groups
- // Inherited: no
- // Percentages: see prose
- // Computed value: the percentage as spec or the absolute length
- new PropertyInfo
- {
- Names = new[] { "min-height" },
- Inherits = false,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- var dv = display.ToString();
- if (dv == "inline" || dv == "table-column" || dv == "table-column-group")
- return false;
- return true;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "0", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } },
- ComputedValue = (element, assignedValue, settings) =>
- {
- CssExpression valueForPercentage = null;
- if (element.Parent != null)
- valueForPercentage = GetComputedPropertyValue(null, element.Parent, "height", settings);
- return ComputeAbsoluteLength(element, assignedValue, settings, valueForPercentage);
- },
- },
- // max-height
- // Value: <length> | <percentage> | none | inherit
- // Initial: none
- // Applies to: all elements but non-replaced in-line elements, table columns, and column groups
- // Inherited: no
- // Percentages: refer to height of containing block
- // Computed value: the percentage as spec or the absolute length
- new PropertyInfo
- {
- Names = new[] { "max-height" },
- Inherits = false,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- var dv = display.ToString();
- if (dv == "inline" || dv == "table-column" || dv == "table-column-group")
- return false;
- return true;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "none", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } },
- ComputedValue = (element, assignedValue, settings) =>
- {
- CssExpression valueForPercentage = null;
- if (element.Parent != null)
- valueForPercentage = GetComputedPropertyValue(null, element.Parent, "height", settings);
- return ComputeAbsoluteLength(element, assignedValue, settings, valueForPercentage);
- },
- },
-
- // vertical-align
- // Value: baseline | sub | super | top | text-top | middle | bottom | text-bottom |
- // <percentage> | <length> | inherit
- // Initial: baseline
- // Applies to: inline-level and 'table-cell' elements
- // Inherited: no
- // Percentages: refer to the line height of the element itself
- // Computed value: for <length> and <percentage> the absolute length, otherwise as specified.
- new PropertyInfo
- {
- Names = new[] { "vertical-align" },
- Inherits = false,
- Includes = (e, settings) =>
- {
- var display = GetComputedPropertyValue(null, e, "display", settings);
- var dv = display.ToString();
- if (dv == "inline" || dv == "table-cell")
- return true;
- return false;
- },
- InitialValue = (element, settings) => new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "baseline", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String, } } },
- ComputedValue = (element, assignedValue, settings) => assignedValue, // todo fix
- },
- // positioned elements are not supported
- //
- // top
- // Value: <length> | <percentage> | auto | inherit
- // Initial: auto
- // Applies to: positioned elements
- // Inherited: no
- // Percentages: refer to height of containing block
- // Computed value: if specified as a length, the corresponding absolute length; if
- // specified as a percentage, the specified value; otherwise, ’auto’.
- //
- // right
- // Value: <length> | <percentage> | auto | inherit
- // Initial: auto
- // Applies to: positioned elements
- // Inherited: no
- // Percentages: refer to width of containing block
- // Computed value: if specified as a length, the corresponding absolute length; if
- // specified as a percentage, the specified value; otherwise, ’auto’.
- //
- // bottom
- // Value: <length> | <percentage> | auto | inherit
- // Initial: auto
- // Applies to: positioned elements
- // Inherited: no
- // Percentages: refer to height of containing block
- // Computed value: if specified as a length, the corresponding absolute length; if
- // specified as a percentage, the specified value; otherwise, ’auto’.
- //
- // left
- // Value: <length> | <percentage> | auto | inherit
- // Initial: auto
- // Applies to: positioned elements
- // Inherited: no
- // Percentages: refer to width of containing block
- // Computed value: if specified as a length, the corresponding absolute length; if
- // specified as a percentage, the specified value; otherwise, ’auto’.
- // floated elements are not supported
- //
- // clear
- // Value: none | left | right | both | inherit
- // Initial: none
- // Applies to: block-level elements
- // Inherited: no
- // Percentages: N/A
- // Computed value: as specified
- //
- // z-index
- // Value: auto | integer | inherit
- // Initial: auto
- // Applies to: positioned elements
- // Inherited: no
- // Percentages: N/A
- // Computed value: as spec
- };
- /*
- * 1. Process user-agent default style sheet
- * 2. Process user-supplied style sheet
- * 3. process author-supplied style sheet
- * 4. process STYLE element
- * 5. process style attribute on all elements
- * 6. expand all shorthand properties - the new properties have the same sort key as shorthand prop
- * 7. Add initial values for all properties that don't have values
- */
- public static void ApplyAllCss(
- string defaultCss,
- string authorCss,
- string userCss,
- XElement newXHtml,
- HtmlToWmlConverterSettings settings,
- out CssDocument defaultCssDoc,
- out CssDocument authorCssDoc,
- out CssDocument userCssDoc,
- string annotatedHtmlDumpFileName)
- {
- int propertySequence = 1;
- CssParser defaultCssParser = new CssParser();
- defaultCssDoc = defaultCssParser.ParseText(defaultCss);
- ApplyCssDocument(
- defaultCssDoc,
- newXHtml,
- Property.HighOrderPriority.UserAgentNormal,
- Property.HighOrderPriority.UserAgentHigh,
- ref propertySequence);
- //// todo dump here, see if margin is set on body.
- //if (annotatedHtmlDumpFileName != null)
- //{
- // StringBuilder sb = new StringBuilder();
- // WriteXHtmlWithAnnotations(newXHtml, sb);
- // File.WriteAllText(annotatedHtmlDumpFileName, sb.ToString());
- // Environment.Exit(0);
- //}
- //DumpCss(userAgentCssDoc);
- //Environment.Exit(0);
- CssParser userCssParser = new CssParser();
- userCssDoc = userCssParser.ParseText(userCss);
- ApplyCssDocument(
- userCssDoc,
- newXHtml,
- Property.HighOrderPriority.UserHigh,
- Property.HighOrderPriority.UserNormal,
- ref propertySequence);
- //DumpCss(userCssDoc);
- //Environment.Exit(0);
- CssParser authorCssParser = new CssParser();
- authorCssDoc = authorCssParser.ParseText(authorCss);
- ApplyCssDocument(
- authorCssDoc,
- newXHtml,
- Property.HighOrderPriority.AuthorNormal,
- Property.HighOrderPriority.AuthorHigh,
- ref propertySequence);
- //string s = DumpCss(authorCssDoc);
- //File.WriteAllText("CssTreeDump.txt", s);
- //Environment.Exit(0);
- // If processing style element, do it here.
- ApplyStyleAttributes(newXHtml, ref propertySequence);
- ExpandShorthandProperties(newXHtml, settings);
- SetAllValues(newXHtml, settings);
- if (annotatedHtmlDumpFileName != null)
- {
- StringBuilder sb = new StringBuilder();
- WriteXHtmlWithAnnotations(newXHtml, sb);
- File.WriteAllText(annotatedHtmlDumpFileName, sb.ToString());
- }
- }
- private static void SetAllValues(XElement xHtml, HtmlToWmlConverterSettings settings)
- {
- foreach (var element in xHtml.DescendantsAndSelf())
- {
- foreach (var propertyInfo in PropertyInfoList)
- {
- if (propertyInfo.Includes(element, settings))
- {
- foreach (var name in propertyInfo.Names)
- {
- GetComputedPropertyValue(propertyInfo, element, name, settings);
- }
- }
- }
- }
- }
- #if false
- GetComputedPropertyValue
- if (property is already computed)
- return the computed value
- if (property is set)
- compute value
- set the computed value
- return the computed value
- if (property is inherited (either because it was set to inherit, or because it is an inherited property))
- if (parent exists)
- call GetComputedValue on parent
- return the computed value
- else
- call GetInitialValue for property
- compute value
- set the computed value
- return the computed value
- #endif
- public static CssExpression GetComputedPropertyValue(PropertyInfo propertyInfo, XElement element, string propertyName,
- HtmlToWmlConverterSettings settings)
- {
- // if (property is already computed)
- // return the computed value
- Dictionary<string, CssExpression> computedValues = element.Annotation<Dictionary<string, CssExpression>>();
- if (computedValues == null)
- {
- computedValues = new Dictionary<string, CssExpression>();
- element.AddAnnotation(computedValues);
- }
- if (computedValues.ContainsKey(propertyName))
- {
- CssExpression r = computedValues[propertyName];
- return r;
- }
- // if property is not set or property is set to inherited value, then get inherited or initialized value.
- string pName = propertyName.ToLower();
- if (propertyInfo == null)
- {
- propertyInfo = PropertyInfoList.FirstOrDefault(pi => pi.Names.Contains(pName));
- if (propertyInfo == null)
- throw new OpenXmlPowerToolsException("all possible properties should be in the list");
- }
- Dictionary<string, Property> propList = element.Annotation<Dictionary<string, Property>>();
- if (propList == null)
- {
- CssExpression computedValue = GetInheritedOrInitializedValue(computedValues, propertyInfo, element, propertyName, false, settings);
- return computedValue;
- }
- if (!propList.ContainsKey(pName))
- {
- CssExpression computedValue = GetInheritedOrInitializedValue(computedValues, propertyInfo, element, propertyName, false, settings);
- return computedValue;
- }
- Property prop = propList[pName];
- string propStr = prop.Expression.ToString();
- if (propStr == "inherited" || propStr == "auto")
- {
- CssExpression computedValue = GetInheritedOrInitializedValue(computedValues, propertyInfo, element, propertyName, true, settings);
- return computedValue;
- }
- // if property is set, then compute the value, return the computed value
- CssExpression computedValue2;
- if (propertyInfo.ComputedValue == null)
- computedValue2 = prop.Expression;
- else
- {
- computedValue2 = propertyInfo.ComputedValue(element, prop.Expression, settings);
- }
- computedValues.Add(propertyName, computedValue2);
- return computedValue2;
- }
- //if (property is inherited (either because it was set to inherit, or because it is an inherited property))
- // if (parent exists)
- // call GetComputedValue on parent
- // return the computed value
- //else
- // call GetInitialValue for property
- // compute value
- // set the computed value
- // return the computed value
- public static CssExpression GetInheritedOrInitializedValue(Dictionary<string, CssExpression> computedValues, PropertyInfo propertyInfo, XElement element, string propertyName, bool valueIsInherit, HtmlToWmlConverterSettings settings)
- {
- if ((propertyInfo.Inherits || valueIsInherit) && element.Parent != null && propertyInfo.Includes(element.Parent, settings))
- {
- CssExpression parentPropertyValue = GetComputedPropertyValue(propertyInfo, element.Parent, propertyName, settings);
- computedValues.Add(propertyName, parentPropertyValue);
- return parentPropertyValue;
- }
- CssExpression initialPropertyValue = propertyInfo.InitialValue(element, settings);
- CssExpression computedValue;
- if (propertyInfo.ComputedValue == null)
- computedValue = initialPropertyValue;
- else
- computedValue = propertyInfo.ComputedValue(element, initialPropertyValue, settings);
- computedValues.Add(propertyName, computedValue);
- return computedValue;
- }
- private static void ApplyCssDocument(
- CssDocument cssDoc,
- XElement xHtml,
- Property.HighOrderPriority notImportantHighOrderSort,
- Property.HighOrderPriority importantHighOrderSort,
- ref int propertySequence)
- {
- foreach (var ruleSet in cssDoc.RuleSets)
- {
- foreach (var selector in ruleSet.Selectors)
- {
- ApplySelector(selector, ruleSet, xHtml, notImportantHighOrderSort,
- importantHighOrderSort, ref propertySequence);
- }
- }
- }
- private static CssExpression ComputeAbsoluteLength(XElement element, CssExpression assignedValue, HtmlToWmlConverterSettings settings,
- CssExpression lengthForPercentage)
- {
- if (assignedValue.Terms.Count != 1)
- throw new OpenXmlPowerToolsException("Should not have a unit with more than one term");
- string value = assignedValue.Terms.First().Value;
- bool negative = assignedValue.Terms.First().Sign == '-';
- if (value == "thin")
- {
- CssExpression newExpr1 = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = ".3", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } };
- return newExpr1;
- }
- if (value == "medium")
- {
- CssExpression newExpr2 = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "1.20", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } };
- return newExpr2;
- }
- if (value == "thick")
- {
- CssExpression newExpr3 = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "1.80", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } };
- return newExpr3;
- }
- if (value == "auto" || value == "normal" || value == "none")
- return assignedValue;
- CssUnit? unit = assignedValue.Terms.First().Unit;
- if (unit == CssUnit.PT || unit == null)
- return assignedValue;
- if (unit == CssUnit.Percent && lengthForPercentage == null)
- return new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "auto", Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.String } } };
- double decValue;
- if (!double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out decValue))
- throw new OpenXmlPowerToolsException("value did not parse");
- if (negative)
- decValue = -decValue;
- double? newPtSize = null;
- if (unit == CssUnit.Percent)
- {
- double ptSize;
- if (!double.TryParse(lengthForPercentage.Terms.First().Value, NumberStyles.Float, CultureInfo.InvariantCulture, out ptSize))
- throw new OpenXmlPowerToolsException("did not return a double?");
- newPtSize = ptSize * decValue / 100d;
- }
- else if (unit == CssUnit.EM || unit == CssUnit.EX)
- {
- CssExpression fontSize = GetComputedPropertyValue(null, element, "font-size", settings);
- double decFontSize;
- if (!double.TryParse(fontSize.Terms.First().Value, NumberStyles.Float, CultureInfo.InvariantCulture, out decFontSize))
- throw new OpenXmlPowerToolsException("Internal error");
- newPtSize = (unit == CssUnit.EM) ? decFontSize * decValue : decFontSize * decValue / 2;
- }
- else
- {
- if (unit == null && decValue == 0d)
- newPtSize = 0d;
- if (unit == CssUnit.IN)
- newPtSize = decValue * 72.0d;
- if (unit == CssUnit.CM)
- newPtSize = (decValue / 2.54d) * 72.0d;
- if (unit == CssUnit.MM)
- newPtSize = (decValue / 25.4d) * 72.0d;
- if (unit == CssUnit.PC)
- newPtSize = decValue * 12d;
- if (unit == CssUnit.PX)
- newPtSize = decValue * 0.75d;
- }
- if (!newPtSize.HasValue)
- throw new OpenXmlPowerToolsException("Internal error: should not have reached this exception");
- CssExpression newExpr = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = newPtSize.Value.ToString(CultureInfo.InvariantCulture), Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } };
- return newExpr;
- }
- private static CssExpression ComputeAbsoluteFontSize(XElement element, CssExpression assignedValue, HtmlToWmlConverterSettings settings)
- {
- if (assignedValue.Terms.Count != 1)
- throw new OpenXmlPowerToolsException("Should not have a unit with more than one term, I think");
- string value = assignedValue.Terms.First().Value;
- CssUnit? unit = assignedValue.Terms.First().Unit;
- if (unit == CssUnit.PT)
- return assignedValue;
- if (FontSizeMap.ContainsKey(value))
- return new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = FontSizeMap[value].ToString(CultureInfo.InvariantCulture), Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } };
- // todo what should the calculation be for computing larger / smaller?
- if (value == "larger" || value == "smaller")
- {
- CssExpression parentFontSize = GetComputedPropertyValue(null, element.Parent, "font-size", settings);
- double ptSize;
- if (!double.TryParse(parentFontSize.Terms.First().Value, NumberStyles.Float, CultureInfo.InvariantCulture, out ptSize))
- throw new OpenXmlPowerToolsException("did not return a double?");
- double newPtSize2 = 0;
- if (value == "larger")
- {
- if (ptSize < 10)
- newPtSize2 = 10d;
- if (ptSize == 10 || ptSize == 11)
- newPtSize2 = 12d;
- if (ptSize == 12)
- newPtSize2 = 13.5d;
- if (ptSize >= 13 && ptSize <= 15)
- newPtSize2 = 18d;
- if (ptSize >= 16 && ptSize <= 20)
- newPtSize2 = 24d;
- if (ptSize >= 21)
- newPtSize2 = 36d;
- }
- if (value == "smaller")
- {
- if (ptSize <= 11)
- newPtSize2 = 7.5d;
- if (ptSize == 12)
- newPtSize2 = 10d;
- if (ptSize >= 13 && ptSize <= 15)
- newPtSize2 = 12d;
- if (ptSize >= 16 && ptSize <= 20)
- newPtSize2 = 13.5d;
- if (ptSize >= 21 && ptSize <= 29)
- newPtSize2 = 18d;
- if (ptSize >= 30)
- newPtSize2 = 24d;
- }
- return new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = newPtSize2.ToString(CultureInfo.InvariantCulture), Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } };
- }
- double decValue;
- if (!double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out decValue))
- throw new OpenXmlPowerToolsException("em value did not parse");
- double? newPtSize = null;
- if (unit == CssUnit.EM || unit == CssUnit.EX || unit == CssUnit.Percent)
- {
- CssExpression parentFontSize = GetComputedPropertyValue(null, element.Parent, "font-size", settings);
- double ptSize;
- if (!double.TryParse(parentFontSize.Terms.First().Value, NumberStyles.Float, CultureInfo.InvariantCulture, out ptSize))
- throw new OpenXmlPowerToolsException("did not return a double?");
- if (unit == CssUnit.EM)
- newPtSize = ptSize * decValue;
- if (unit == CssUnit.EX)
- newPtSize = ptSize / 2 * decValue;
- if (unit == CssUnit.Percent)
- newPtSize = ptSize * decValue / 100d;
- }
- else
- {
- if (unit == CssUnit.IN)
- newPtSize = decValue * 72.0d;
- if (unit == CssUnit.CM)
- newPtSize = (decValue / 2.54d) * 72.0d;
- if (unit == CssUnit.MM)
- newPtSize = (decValue / 25.4d) * 72.0d;
- if (unit == CssUnit.PC)
- newPtSize = decValue * 12d;
- if (unit == CssUnit.PX)
- newPtSize = decValue * 0.75d;
- }
- if (!newPtSize.HasValue)
- throw new OpenXmlPowerToolsException("Internal error: should not have reached this exception");
- CssExpression newExpr = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = newPtSize.Value.ToString(CultureInfo.InvariantCulture), Type = OpenXmlPowerTools.HtmlToWml.CSS.CssTermType.Number, Unit = CssUnit.PT, } } };
- return newExpr;
- }
- private static Dictionary<string, double> FontSizeMap = new Dictionary<string, double>()
- {
- { "xx-small", 7.5d },
- { "x-small", 10d },
- { "small", 12d },
- { "medium", 13.5d },
- { "large", 18d },
- { "x-large", 24d },
- { "xx-large", 36d },
- };
- private static void ApplySelector(
- CssSelector selector,
- CssRuleSet ruleSet,
- XElement xHtml,
- Property.HighOrderPriority notImportantHighOrderSort,
- Property.HighOrderPriority importantHighOrderSort,
- ref int propertySequence)
- {
- foreach (var element in xHtml.DescendantsAndSelf())
- {
- if (DoesSelectorMatch(selector, element))
- {
- foreach (CssDeclaration declaration in ruleSet.Declarations)
- {
- Property prop = new Property()
- {
- Name = declaration.Name.ToLower(),
- Expression = declaration.Expression,
- HighOrderSort = declaration.Important ? importantHighOrderSort : notImportantHighOrderSort,
- IdAttributesInSelector = CountIdAttributesInSelector(selector),
- AttributesInSelector = CountAttributesInSelector(selector),
- ElementNamesInSelector = CountElementNamesInSelector(selector),
- SequenceNumber = propertySequence++,
- };
- AddPropertyToElement(element, prop);
- }
- }
- }
- }
- private static bool DoesSelectorMatch(
- CssSelector selector,
- XElement element)
- {
- int currentSimpleSelector = selector.SimpleSelectors.Count() - 1;
- XElement currentElement = element;
- while (true)
- {
- if (!DoesSimpleSelectorMatch(selector.SimpleSelectors[currentSimpleSelector], currentElement))
- return false;
- if (currentSimpleSelector == 0)
- return true;
- if (selector.SimpleSelectors[currentSimpleSelector].Combinator == CssCombinator.ChildOf)
- {
- currentElement = currentElement.Parent;
- if (currentElement == null)
- return false;
- currentSimpleSelector--;
- continue;
- }
- if (selector.SimpleSelectors[currentSimpleSelector].Combinator == CssCombinator.PrecededImmediatelyBy)
- {
- currentElement = currentElement.ElementsBeforeSelf().Reverse().FirstOrDefault();
- if (currentElement == null)
- return false;
- currentSimpleSelector--;
- continue;
- }
- if (selector.SimpleSelectors[currentSimpleSelector].Combinator == null)
- {
- bool continueOuter = false;
- foreach (XElement ancestor in element.Ancestors())
- {
- if (DoesSimpleSelectorMatch(selector.SimpleSelectors[currentSimpleSelector - 1], ancestor))
- {
- currentElement = ancestor;
- currentSimpleSelector--;
- continueOuter = true;
- break;
- }
- }
- if (continueOuter)
- continue;
- return false;
- }
- }
- }
- private static bool DoesSimpleSelectorMatch(
- CssSimpleSelector simpleSelector,
- XElement element)
- {
- bool elemantNameMatch = true;
- bool classNameMatch = true;
- bool childSimpleSelectorMatch = true;
- bool idMatch = true;
- bool attributeMatch = true;
- if (simpleSelector.Pseudo != null)
- return false;
- if (simpleSelector.ElementName != null && simpleSelector.ElementName != "" && simpleSelector.ElementName != "*")
- elemantNameMatch = element.Name.ToString() == simpleSelector.ElementName.ToString();
- if (elemantNameMatch)
- {
- if (simpleSelector.Class != null && simpleSelector.Class != "")
- classNameMatch = ClassesOf(element).Contains(simpleSelector.Class);
- if (classNameMatch)
- {
- if (simpleSelector.Child != null)
- childSimpleSelectorMatch = DoesSimpleSelectorMatch(simpleSelector.Child, element);
- if (childSimpleSelectorMatch)
- {
- if (simpleSelector.ID != null && simpleSelector.ID != "")
- {
- string id = (string)element.Attribute("ID");
- if (id == null)
- id = (string)element.Attribute("id");
- idMatch = simpleSelector.ID == id;
- }
- if (idMatch)
- {
- if (simpleSelector.Attribute != null)
- attributeMatch = DoesAttributeMatch(simpleSelector.Attribute, element);
- }
- }
- }
- }
- bool result =
- elemantNameMatch &&
- classNameMatch &&
- childSimpleSelectorMatch &&
- idMatch &&
- attributeMatch;
- return result;
- }
- private static bool DoesAttributeMatch(OpenXmlPowerTools.HtmlToWml.CSS.CssAttribute attribute, XElement element)
- {
- string attName = attribute.Operand.ToLower();
- string attValue = (string)element.Attribute(attName);
- if (attValue == null)
- return false;
- if (attribute.Operator == null)
- return true;
- string value = attribute.Value;
- switch (attribute.Operator)
- {
- case CssAttributeOperator.Equals:
- return attValue == value;
- case CssAttributeOperator.BeginsWith:
- return attValue.StartsWith(value);
- case CssAttributeOperator.Contains:
- return attValue.Contains(value);
- case CssAttributeOperator.EndsWith:
- return attValue.EndsWith(value);
- case CssAttributeOperator.InList:
- return attValue.Split(' ').Contains(value);
- case CssAttributeOperator.Hyphenated:
- return attValue.Split('-')[0] == value;
- default:
- return false;
- }
- }
- private static int CountIdAttributesInSimpleSelector(CssSimpleSelector simpleSelector)
- {
- int count = simpleSelector.ID != null ? 1 : 0 +
- (simpleSelector.Child != null ? CountIdAttributesInSimpleSelector(simpleSelector.Child) : 0);
- return count;
- }
- private static int CountIdAttributesInSelector(CssSelector selector)
- {
- int count = selector.SimpleSelectors.Select(ss => CountIdAttributesInSimpleSelector(ss)).Sum();
- return count;
- }
- private static int CountAttributesInSimpleSelector(CssSimpleSelector simpleSelector)
- {
- int count = (simpleSelector.Attribute != null ? 1 : 0) +
- ((simpleSelector.Class != null && simpleSelector.Class != "") ? 1 : 0) +
- (simpleSelector.Child != null ? CountAttributesInSimpleSelector(simpleSelector.Child) : 0);
- return count;
- }
- private static int CountAttributesInSelector(CssSelector selector)
- {
- int count = selector.SimpleSelectors.Select(ss => CountAttributesInSimpleSelector(ss)).Sum();
- return count;
- }
- private static int CountElementNamesInSimpleSelector(CssSimpleSelector simpleSelector)
- {
- int count = (simpleSelector.ElementName != null &&
- simpleSelector.ElementName != "" &&
- simpleSelector.ElementName != "*")
- ? 1 : 0 +
- (simpleSelector.Child != null ? CountElementNamesInSimpleSelector(simpleSelector.Child) : 0);
- return count;
- }
- private static int CountElementNamesInSelector(CssSelector selector)
- {
- int count = selector.SimpleSelectors.Select(ss => CountElementNamesInSimpleSelector(ss)).Sum();
- return count;
- }
- private static void AddPropertyToElement(
- XElement element,
- Property property)
- {
- //if (property.Name == "direction")
- // Console.WriteLine(1);
- Dictionary<string, Property> propList = element.Annotation<Dictionary<string, Property>>();
- if (propList == null)
- {
- propList = new Dictionary<string, Property>();
- element.AddAnnotation(propList);
- }
- if (!propList.ContainsKey(property.Name))
- propList.Add(property.Name, property);
- else
- {
- Property current = propList[property.Name];
- if (((System.IComparable<Property>)property).CompareTo(current) == 1)
- propList[property.Name] = property;
- }
- }
- private static void AddPropertyToDictionary(
- Dictionary<string, Property> propList,
- Property property)
- {
- if (!propList.ContainsKey(property.Name))
- propList.Add(property.Name, property);
- else
- {
- Property current = propList[property.Name];
- if (((System.IComparable<Property>)property).CompareTo(current) == 1)
- propList[property.Name] = property;
- }
- }
- private static string[] ClassesOf(XElement element)
- {
- string classesString = (string)element.Attribute("class");
- if (classesString == null)
- return new string[0];
- return classesString.Split(' ');
- }
- private static void ApplyDeclarationsToElement(
- CssRuleSet ruleSet,
- XElement element,
- Property.HighOrderPriority notImportantHighOrderSort,
- Property.HighOrderPriority importantHighOrderSort,
- ref int propertySequence)
- {
- foreach (var declaration in ruleSet.Declarations)
- {
- Property prop = new Property()
- {
- Name = declaration.Name.ToLower(),
- Expression = declaration.Expression,
- HighOrderSort = declaration.Important ? importantHighOrderSort : notImportantHighOrderSort,
- IdAttributesInSelector = 0,
- AttributesInSelector = 0,
- ElementNamesInSelector = 0,
- SequenceNumber = propertySequence++,
- };
- AddPropertyToElement(element, prop);
- }
- }
- private static void ApplyCssToElement(
- CssDocument cssDoc,
- XElement element,
- Property.HighOrderPriority notImportantHighOrderSort,
- Property.HighOrderPriority importantHighOrderSort,
- ref int propertySequence)
- {
- foreach (var ruleSet in cssDoc.RuleSets)
- {
- ApplyDeclarationsToElement(ruleSet, element, notImportantHighOrderSort, importantHighOrderSort, ref propertySequence);
- }
- }
- private static void ApplyStyleAttributes(XElement xHtml, ref int propertySequence)
- {
- foreach (var element in xHtml.DescendantsAndSelf())
- {
- XAttribute styleAtt = element.Attribute(XhtmlNoNamespace.style);
- if (styleAtt != null)
- {
- string style = (string)styleAtt;
- string cssString = element.Name + "{" + style + "}";
- cssString = cssString.Replace('\"', '\'');
- CssParser cssParser = new CssParser();
- CssDocument cssDoc = cssParser.ParseText(cssString);
- ApplyCssToElement(
- cssDoc,
- element,
- Property.HighOrderPriority.StyleAttributeNormal,
- Property.HighOrderPriority.StyleAttributeHigh,
- ref propertySequence);
- }
- XAttribute dirAtt = element.Attribute(XhtmlNoNamespace.dir);
- if (dirAtt != null)
- {
- string dir = dirAtt.Value.ToLower();
- Property prop = new Property()
- {
- Name = "direction",
- Expression = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = dir, Type = CssTermType.String } } },
- HighOrderSort = Property.HighOrderPriority.HtmlAttribute,
- IdAttributesInSelector = 0,
- AttributesInSelector = 0,
- ElementNamesInSelector = 0,
- SequenceNumber = propertySequence++,
- };
- AddPropertyToElement(element, prop);
- }
- }
- }
- private enum CssDataType
- {
- BorderWidth,
- BorderStyle,
- Color,
- ListStyleType,
- ListStylePosition,
- ListStyleImage,
- BackgroundColor,
- BackgroundImage,
- BackgroundRepeat,
- BackgroundAttachment,
- BackgroundPosition,
- FontStyle,
- FontVarient,
- FontWeight,
- FontSize,
- LineHeight,
- FontFamily,
- Length,
- };
- private class ShorthandPropertiesInfo
- {
- public string Name;
- public string Pattern;
- }
- private static ShorthandPropertiesInfo[] ShorthandProperties = new[]
- {
- new ShorthandPropertiesInfo
- {
- Name = "margin",
- Pattern = "margin-{0}",
- },
- new ShorthandPropertiesInfo
- {
- Name = "padding",
- Pattern = "padding-{0}",
- },
- new ShorthandPropertiesInfo
- {
- Name = "border-width",
- Pattern = "border-{0}-width",
- },
- new ShorthandPropertiesInfo
- {
- Name = "border-color",
- Pattern = "border-{0}-color",
- },
- new ShorthandPropertiesInfo
- {
- Name = "border-style",
- Pattern = "border-{0}-style",
- },
- };
- private static void ExpandShorthandProperties(XElement xHtml, HtmlToWmlConverterSettings settings)
- {
- foreach (var element in xHtml.DescendantsAndSelf())
- {
- ExpandShorthandPropertiesForElement(element, settings);
- }
- }
- private static void ExpandShorthandPropertiesForElement(XElement element, HtmlToWmlConverterSettings settings)
- {
- Dictionary<string, Property> propertyList = element.Annotation<Dictionary<string, Property>>();
- if (propertyList == null)
- {
- propertyList = new Dictionary<string, Property>();
- element.AddAnnotation(propertyList);
- }
- foreach (var kvp in propertyList.ToList())
- {
- Property p = kvp.Value;
- if (p.Name == "border")
- {
- CssExpression borderColor;
- CssExpression borderWidth;
- CssExpression borderStyle;
- if (p.Expression.Terms.Count == 1 && p.Expression.Terms.First().Value == "inherit")
- {
- borderColor = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- borderWidth = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- borderStyle = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- }
- else
- {
- //borderColor = GetComputedPropertyValue(null, element, "color", settings);
- //borderWidth = new Expression { Terms = new List<Term> { new Term { Value = "medium", Type = TermType.String } } };
- //borderStyle = new Expression { Terms = new List<Term> { new Term { Value = "none", Type = TermType.String } } };
- borderColor = null;
- borderWidth = null;
- borderStyle = null;
- foreach (var term in p.Expression.Terms)
- {
- CssDataType dataType = GetDatatypeFromBorderTerm(term);
- switch (dataType)
- {
- case CssDataType.Color:
- borderColor = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.BorderWidth:
- borderWidth = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.BorderStyle:
- borderStyle = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- }
- }
- }
- foreach (var side in new[] { "top", "left", "bottom", "right" })
- {
- if (borderWidth != null)
- {
- Property bwp = new Property
- {
- Name = "border-" + side + "-width",
- Expression = borderWidth,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, bwp);
- }
- if (borderStyle != null)
- {
- Property bsp = new Property
- {
- Name = "border-" + side + "-style",
- Expression = borderStyle,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, bsp);
- }
- if (borderColor != null)
- {
- Property bc = new Property
- {
- Name = "border-" + side + "-color",
- Expression = borderColor,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, bc);
- }
- }
- continue;
- }
- if (p.Name == "border-top" || p.Name == "border-right" || p.Name == "border-bottom" || p.Name == "border-left")
- {
- CssExpression borderColor;
- CssExpression borderWidth;
- CssExpression borderStyle;
- if (p.Expression.Terms.Count() == 1 && p.Expression.Terms.First().Value == "inherit")
- {
- borderColor = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- borderWidth = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- borderStyle = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- }
- else
- {
- //borderColor = GetComputedPropertyValue(null, element, "color", settings);
- //borderWidth = new Expression { Terms = new List<Term> { new Term { Value = "medium", Type = TermType.String } } };
- //borderStyle = new Expression { Terms = new List<Term> { new Term { Value = "none", Type = TermType.String } } };
- borderColor = null;
- borderWidth = null;
- borderStyle = null;
- foreach (var term in p.Expression.Terms)
- {
- CssDataType dataType = GetDatatypeFromBorderTerm(term);
- switch (dataType)
- {
- case CssDataType.Color:
- borderColor = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.BorderWidth:
- borderWidth = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.BorderStyle:
- borderStyle = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- }
- }
- }
- if (borderWidth != null)
- {
- Property bwp = new Property
- {
- Name = p.Name + "-width",
- Expression = borderWidth,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, bwp);
- }
- if (borderStyle != null)
- {
- Property bsp = new Property
- {
- Name = p.Name + "-style",
- Expression = borderStyle,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, bsp);
- }
- if (borderColor != null)
- {
- Property bc = new Property
- {
- Name = p.Name + "-color",
- Expression = borderColor,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, bc);
- }
- continue;
- }
- if (p.Name == "list-style")
- {
- CssExpression listStyleType;
- CssExpression listStylePosition;
- CssExpression listStyleImage;
- if (p.Expression.Terms.Count == 1 && p.Expression.Terms.First().Value == "inherit")
- {
- listStyleType = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- listStylePosition = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- listStyleImage = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- }
- else
- {
- listStyleType = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "disc", Type = CssTermType.String } } };
- listStylePosition = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "outside", Type = CssTermType.String } } };
- listStyleImage = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "none", Type = CssTermType.String } } };
- foreach (var term in p.Expression.Terms)
- {
- CssDataType dataType = GetDatatypeFromListStyleTerm(term);
- switch (dataType)
- {
- case CssDataType.ListStyleType:
- listStyleType = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.ListStylePosition:
- listStylePosition = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.ListStyleImage:
- listStyleImage = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- }
- }
- }
- Property lst = new Property
- {
- Name = "list-style-type",
- Expression = listStyleType,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, lst);
- Property lsp = new Property
- {
- Name = "list-style-position",
- Expression = listStylePosition,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, lsp);
- Property lsi = new Property
- {
- Name = "list-style-image",
- Expression = listStyleImage,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, lsi);
- continue;
- }
- if (p.Name == "background")
- {
- CssExpression backgroundColor;
- CssExpression backgroundImage;
- CssExpression backgroundRepeat;
- CssExpression backgroundAttachment;
- CssExpression backgroundPosition;
- if (p.Expression.Terms.Count == 1 && p.Expression.Terms.First().Value == "inherit")
- {
- backgroundColor = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- backgroundImage = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- backgroundRepeat = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- backgroundAttachment = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- backgroundPosition = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- }
- else
- {
- backgroundColor = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "transparent", Type = CssTermType.String } } };
- backgroundImage = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "none", Type = CssTermType.String } } };
- backgroundRepeat = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "repeat", Type = CssTermType.String } } };
- backgroundAttachment = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "scroll", Type = CssTermType.String } } };
- backgroundPosition = new CssExpression
- {
- Terms = new List<CssTerm> {
- new CssTerm {
- Value = "0",
- Unit = OpenXmlPowerTools.HtmlToWml.CSS.CssUnit.Percent,
- Type = CssTermType.Number },
- new CssTerm {
- Value = "0",
- Unit = OpenXmlPowerTools.HtmlToWml.CSS.CssUnit.Percent,
- Type = CssTermType.Number },
- }
- };
- List<CssTerm> backgroundPositionList = new List<CssTerm>();
- foreach (var term in p.Expression.Terms)
- {
- CssDataType dataType = GetDatatypeFromBackgroundTerm(term);
- switch (dataType)
- {
- case CssDataType.BackgroundColor:
- backgroundColor = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.BackgroundImage:
- backgroundImage = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.BackgroundRepeat:
- backgroundRepeat = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.BackgroundAttachment:
- backgroundAttachment = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.BackgroundPosition:
- backgroundPositionList.Add(term);
- break;
- }
- }
- if (backgroundPositionList.Count() == 1)
- {
- backgroundPosition = new CssExpression
- {
- Terms = new List<CssTerm> {
- backgroundPositionList.First(),
- new CssTerm {
- Value = "center",
- Type = CssTermType.String
- },
- }
- };
- }
- if (backgroundPositionList.Count() == 2)
- {
- backgroundPosition = new CssExpression
- {
- Terms = backgroundPositionList
- };
- }
- }
- Property bc = new Property
- {
- Name = "background-color",
- Expression = backgroundColor,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, bc);
- Property bgi = new Property
- {
- Name = "background-image",
- Expression = backgroundImage,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, bgi);
- Property bgr = new Property
- {
- Name = "background-repeat",
- Expression = backgroundRepeat,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, bgr);
- Property bga = new Property
- {
- Name = "background-attachment",
- Expression = backgroundAttachment,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, bga);
- Property bgp = new Property
- {
- Name = "background-position",
- Expression = backgroundPosition,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, bgp);
- continue;
- }
- if (p.Name == "font")
- {
- CssExpression fontStyle;
- CssExpression fontVarient;
- CssExpression fontWeight;
- CssExpression fontSize;
- CssExpression lineHeight;
- CssExpression fontFamily;
- if (p.Expression.Terms.Count() == 1 && p.Expression.Terms.First().Value == "inherit")
- {
- fontStyle = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- fontVarient = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- fontWeight = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- fontSize = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- lineHeight = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- fontFamily = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "inherit", Type = CssTermType.String } } };
- }
- else
- {
- fontStyle = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "normal", Type = CssTermType.String } } };
- fontVarient = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "normal", Type = CssTermType.String } } };
- fontWeight = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "normal", Type = CssTermType.String } } };
- fontSize = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "medium", Type = CssTermType.String } } };
- lineHeight = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "normal", Type = CssTermType.String } } };
- fontFamily = new CssExpression { Terms = new List<CssTerm> { new CssTerm { Value = "serif", Type = CssTermType.String } } };
- List<CssTerm> fontFamilyList = new List<CssTerm>();
- foreach (var term in p.Expression.Terms)
- {
- CssDataType dataType = GetDatatypeFromFontTerm(term);
- switch (dataType)
- {
- case CssDataType.FontStyle:
- fontStyle = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.FontVarient:
- fontVarient = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.FontWeight:
- fontWeight = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.FontSize:
- fontSize = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.Length:
- if (term.SeparatorChar == "/")
- lineHeight = new CssExpression { Terms = new List<CssTerm> { term } };
- else
- fontSize = new CssExpression { Terms = new List<CssTerm> { term } };
- break;
- case CssDataType.FontFamily:
- fontFamilyList.Add(term);
- break;
- }
- }
- if (fontFamilyList.Count > 0)
- fontFamily = new CssExpression { Terms = fontFamilyList };
- }
- Property fs = new Property
- {
- Name = "font-style",
- Expression = fontStyle,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, fs);
- Property fv = new Property
- {
- Name = "font-varient",
- Expression = fontVarient,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, fv);
- Property fw = new Property
- {
- Name = "font-weight",
- Expression = fontWeight,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, fw);
- Property fsz = new Property
- {
- Name = "font-size",
- Expression = fontSize,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, fsz);
- Property lh = new Property
- {
- Name = "line-height",
- Expression = lineHeight,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, lh);
- Property ff = new Property
- {
- Name = "font-family",
- Expression = fontFamily,
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, ff);
- continue;
- }
- foreach (var shPr in ShorthandProperties)
- {
- if (p.Name == shPr.Name)
- {
- switch (p.Expression.Terms.Count)
- {
- case 1:
- foreach (var direction in new[] { "top", "right", "bottom", "left" })
- {
- Property ep = new Property()
- {
- Name = String.Format(shPr.Pattern, direction),
- Expression = new CssExpression { Terms = new List<CssTerm> { p.Expression.Terms.First() } },
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, ep);
- }
- break;
- case 2:
- foreach (var direction in new[] { "top", "bottom" })
- {
- Property ep = new Property()
- {
- Name = String.Format(shPr.Pattern, direction),
- Expression = new CssExpression { Terms = new List<CssTerm> { p.Expression.Terms.First() } },
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, ep);
- }
- foreach (var direction in new[] { "left", "right" })
- {
- Property ep = new Property()
- {
- Name = String.Format(shPr.Pattern, direction),
- Expression = new CssExpression { Terms = new List<CssTerm> { p.Expression.Terms.Skip(1).First() } },
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, ep);
- }
- break;
- case 3:
- Property ep3 = new Property()
- {
- Name = String.Format(shPr.Pattern, "top"),
- Expression = new CssExpression { Terms = new List<CssTerm> { p.Expression.Terms.First() } },
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, ep3);
- foreach (var direction in new[] { "left", "right" })
- {
- Property ep2 = new Property()
- {
- Name = String.Format(shPr.Pattern, direction),
- Expression = new CssExpression { Terms = new List<CssTerm> { p.Expression.Terms.Skip(1).First() } },
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, ep2);
- }
- Property ep4 = new Property()
- {
- Name = String.Format(shPr.Pattern, "bottom"),
- Expression = new CssExpression { Terms = new List<CssTerm> { p.Expression.Terms.Skip(2).First() } },
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, ep4);
- break;
- case 4:
- int skip = 0;
- foreach (var direction in new[] { "top", "right", "bottom", "left" })
- {
- Property ep = new Property()
- {
- Name = String.Format(shPr.Pattern, direction),
- Expression = new CssExpression { Terms = new List<CssTerm> { p.Expression.Terms.Skip(skip++).First() } },
- HighOrderSort = p.HighOrderSort,
- IdAttributesInSelector = p.IdAttributesInSelector,
- AttributesInSelector = p.AttributesInSelector,
- ElementNamesInSelector = p.ElementNamesInSelector,
- SequenceNumber = p.SequenceNumber,
- };
- AddPropertyToDictionary(propertyList, ep);
- }
- break;
- }
- }
- }
- }
- }
- private static string[] BackgroundRepeatValues = new[]
- {
- "repeat",
- "repeat-x",
- "repeat-y",
- "no-repeat",
- };
- private static string[] BackgroundAttachmentValues = new[]
- {
- "scroll",
- "fixed",
- };
- private static string[] BackgroundPositionValues = new[]
- {
- "left",
- "right",
- "top",
- "bottom",
- "center",
- };
- private static CssDataType GetDatatypeFromBackgroundTerm(CssTerm term)
- {
- if (term.IsColor)
- return CssDataType.BackgroundColor;
- if (BackgroundRepeatValues.Contains(term.Value.ToLower()))
- return CssDataType.BackgroundRepeat;
- if (BackgroundAttachmentValues.Contains(term.Value.ToLower()))
- return CssDataType.BackgroundAttachment;
- if (term.Function != null)
- return CssDataType.BackgroundImage;
- if (term.Unit == CssUnit.CM ||
- term.Unit == CssUnit.EM ||
- term.Unit == CssUnit.IN ||
- term.Unit == CssUnit.MM ||
- term.Unit == CssUnit.PT ||
- term.Unit == CssUnit.PX ||
- term.Unit == CssUnit.Percent)
- return CssDataType.BackgroundPosition;
- if (BackgroundPositionValues.Contains(term.Value.ToLower()))
- return CssDataType.BackgroundPosition;
- return CssDataType.BackgroundPosition;
- }
- private static string[] ListStylePositionValues = new[]
- {
- "inside",
- "outside",
- };
- private static string[] BorderStyleValues = new[]
- {
- "none",
- "hidden",
- "dotted",
- "dashed",
- "solid",
- "double",
- "groove",
- "ridge",
- "inset",
- "outset",
- };
- private static CssDataType GetDatatypeFromBorderTerm(CssTerm term)
- {
- if (term.IsColor)
- {
- return CssDataType.Color;
- }
- if (BorderStyleValues.Contains(term.Value.ToLower()))
- return CssDataType.BorderStyle;
- return CssDataType.BorderWidth;
- }
- private static string[] ListStyleTypeValues = new[]
- {
- "disc",
- "circle",
- "square",
- "decimal",
- "decimal-leading-zero",
- "lower-roman",
- "upper-roman",
- "lower-greek",
- "lower-latin",
- "upper-latin",
- "armenian",
- "georgian",
- "lower-alpha",
- "upper-alpha",
- };
- private static CssDataType GetDatatypeFromListStyleTerm(CssTerm term)
- {
- if (ListStyleTypeValues.Contains(term.Value.ToLower()))
- return CssDataType.ListStyleType;
- if (ListStylePositionValues.Contains(term.Value.ToLower()))
- return CssDataType.ListStylePosition;
- return CssDataType.ListStyleImage;
- }
- private static string[] FontStyleValues = new[]
- {
- "italic",
- "oblique",
- };
- private static string[] FontVarientValues = new[]
- {
- "small-caps",
- };
- private static string[] FontWeightValues = new[]
- {
- "bold",
- "bolder",
- "lighter",
- "100",
- "200",
- "300",
- "400",
- "500",
- "600",
- "700",
- "800",
- "900",
- };
- private static CssDataType GetDatatypeFromFontTerm(CssTerm term)
- {
- if (FontStyleValues.Contains(term.Value.ToLower()))
- return CssDataType.FontStyle;
- if (FontVarientValues.Contains(term.Value.ToLower()))
- return CssDataType.FontVarient;
- if (FontWeightValues.Contains(term.Value.ToLower()))
- return CssDataType.FontWeight;
- if (FontSizeMap.ContainsKey(term.Value.ToLower()))
- return CssDataType.FontSize;
- if (term.Unit == CssUnit.CM ||
- term.Unit == CssUnit.EM ||
- term.Unit == CssUnit.IN ||
- term.Unit == CssUnit.MM ||
- term.Unit == CssUnit.PT ||
- term.Unit == CssUnit.PX ||
- term.Unit == CssUnit.Percent)
- return CssDataType.Length;
- return CssDataType.FontFamily;
- }
- public class PropertyInfo
- {
- public string[] Names;
- public bool Inherits;
- public Func<XElement, HtmlToWmlConverterSettings, bool> Includes;
- public Func<XElement, HtmlToWmlConverterSettings, CssExpression> InitialValue;
- public Func<XElement, CssExpression, HtmlToWmlConverterSettings, CssExpression> ComputedValue;
- }
- private static void WriteXHtmlWithAnnotations(XElement element, StringBuilder sb)
- {
- int depth = element.Ancestors().Count() * 2;
- XElement dummyElement = new XElement(element.Name, element.Attributes());
- sb.Append(String.Format("{0}{1}", "".PadRight(depth), dummyElement) + Environment.NewLine);
- Dictionary<string, Property> propList = element.Annotation<Dictionary<string, Property>>();
- if (propList != null)
- {
- sb.Append("".PadRight(depth + 2) + "Properties from Stylesheets" + Environment.NewLine);
- sb.Append("".PadRight(depth + 2) + "===========================" + Environment.NewLine);
- foreach (var kvp in propList.OrderBy(p => p.Key).ThenBy(p => p.Value))
- {
- Property prop = kvp.Value;
- string propString = String.Format("{0} High:{1} Id:{2} Att:{3} Ell:{4} Seq:{5}",
- (prop.Name + ":" + prop.Expression + " ").PadRight(50 - depth + 2, '.'), (int)prop.HighOrderSort, prop.IdAttributesInSelector, prop.AttributesInSelector,
- prop.ElementNamesInSelector, prop.SequenceNumber);
- sb.Append(String.Format("{0}{1}", "".PadRight(depth + 2), propString) + Environment.NewLine);
- }
- sb.Append(Environment.NewLine);
- }
- Dictionary<string, CssExpression> computedProperties = element.Annotation<Dictionary<string, CssExpression>>();
- if (computedProperties != null)
- {
- sb.Append("".PadRight(depth + 2) + "Computed Properties" + Environment.NewLine);
- sb.Append("".PadRight(depth + 2) + "===================" + Environment.NewLine);
- foreach (var prop in computedProperties.OrderBy(cp => cp.Key))
- {
- string propString = prop.Key + ":" + prop.Value;
- sb.Append(String.Format("{0}{1}", "".PadRight(depth + 2), propString) + Environment.NewLine);
- }
- sb.Append(Environment.NewLine);
- }
- foreach (var child in element.Elements())
- WriteXHtmlWithAnnotations(child, sb);
- }
- public static string DumpCss(CssDocument css)
- {
- StringBuilder sb = new StringBuilder();
- int indent = 0;
- Pr(sb, indent, "CSS Tree Dump");
- Pr(sb, indent, "=============");
- Pr(sb, indent, "Directives count: {0}", css.Directives.Count());
- Pr(sb, indent, "RuleSet count: {0}", css.RuleSets.Count());
- foreach (var rs in css.RuleSets)
- DumpRuleSet(sb, indent, rs);
- Pr(sb, indent, "");
- return sb.ToString();
- }
- private static void DumpFunction(StringBuilder sb, int indent, CssFunction f)
- {
- Pr(sb, indent, "Function: {0}", f);
- if (f != null)
- {
- indent++;
- Pr(sb, indent, "Name: {0}", f.Name);
- DumpExpression(sb, indent, f.Expression);
- indent--;
- }
- }
- private static void DumpAttribute(StringBuilder sb, int indent, OpenXmlPowerTools.HtmlToWml.CSS.CssAttribute a)
- {
- Pr(sb, indent, "Attribute: {0}", a);
- if (a != null)
- {
- indent++;
- Pr(sb, indent, "Operand: {0}", a.Operand);
- Pr(sb, indent, "Operator: {0}", a.Operator);
- Pr(sb, indent, "OperatorString: {0}", a.CssOperatorString);
- Pr(sb, indent, "Value: {0}", a.Value);
- indent--;
- }
- }
- private static void DumpSimpleSelector(StringBuilder sb, int indent, CssSimpleSelector s)
- {
- indent++;
- Pr(sb, indent, "SimpleSelector: {0}", s);
- if (s != null)
- {
- indent++;
- DumpAttribute(sb, indent, s.Attribute);
- Pr(sb, indent, "Child: {0}", s.Child);
- DumpSimpleSelector(sb, indent, s.Child);
- Pr(sb, indent, "Class: {0}", s.Class);
- Pr(sb, indent, "Combinator: {0}", s.Combinator);
- Pr(sb, indent, "CombinatorString: {0}", s.CombinatorString);
- Pr(sb, indent, "ElementName: >{0}<", s.ElementName);
- DumpFunction(sb, indent, s.Function);
- Pr(sb, indent, "ID: {0}", s.ID);
- Pr(sb, indent, "Pseudo: {0}", s.Pseudo);
- indent--;
- }
- indent--;
- }
- private static void DumpSelectors(StringBuilder sb, int indent, CssSelector s)
- {
- indent++;
- Pr(sb, indent, "SimpleSelectors count: {0}", s.SimpleSelectors.Count());
- foreach (var ss in s.SimpleSelectors)
- DumpSimpleSelector(sb, indent, ss);
- indent--;
- }
- private static void DumpTerm(StringBuilder sb, int indent, CssTerm t)
- {
- Pr(sb, indent, "Term >{0}<", t.ToString());
- indent++;
- DumpFunction(sb, indent, t.Function);
- Pr(sb, indent, "IsColor: {0}", t.IsColor);
- Pr(sb, indent, "Separator: {0}", t.Separator);
- Pr(sb, indent, "SeparatorChar: {0}", t.SeparatorChar);
- Pr(sb, indent, "Sign: {0}", t.Sign);
- Pr(sb, indent, "SignChar: {0}", t.SignChar);
- Pr(sb, indent, "Type: {0}", t.Type);
- Pr(sb, indent, "Unit: {0}", t.Unit);
- Pr(sb, indent, "UnitString: {0}", t.UnitString);
- Pr(sb, indent, "Value: {0}", t.Value);
- indent--;
- }
- private static void DumpExpression(StringBuilder sb, int indent, CssExpression e)
- {
- Pr(sb, indent, "Expression >{0}<", e.ToString());
- indent++;
- Pr(sb, indent, "Terms count: {0}", e.Terms.Count());
- foreach (var t in e.Terms)
- DumpTerm(sb, indent, t);
- indent--;
- }
- private static void DumpDeclarations(StringBuilder sb, int indent, CssDeclaration d)
- {
- indent++;
- Pr(sb, indent, "Declaration >{0}<", d.ToString());
- indent++;
- Pr(sb, indent, "Name: {0}", d.Name);
- DumpExpression(sb, indent, d.Expression);
- Pr(sb, indent, "Important: {0}", d.Important);
- indent--;
- indent--;
- }
- private static void DumpRuleSet(StringBuilder sb, int indent, CssRuleSet rs)
- {
- indent++;
- Pr(sb, indent, "RuleSet");
- indent++;
- Pr(sb, indent, "Selectors count: {0}", rs.Selectors.Count());
- foreach (var s in rs.Selectors)
- DumpSelectors(sb, indent, s);
- Pr(sb, indent, "Declarations count: {0}", rs.Declarations.Count());
- foreach (var d in rs.Declarations)
- DumpDeclarations(sb, indent, d);
- indent--;
- indent--;
- }
- private static void Pr(StringBuilder sb, int indent, string format, object o)
- {
- if (o == null)
- return;
- string text = String.Format(format, o);
- StringBuilder sb2 = new StringBuilder("".PadRight(indent * 2) + text);
- //sb2.Replace("&", "&");
- //sb2.Replace("<", "<");
- //sb2.Replace(">", ">");
- sb.Append(sb2);
- sb.Append(Environment.NewLine);
- //Console.WriteLine(sb2);
- }
- private static void Pr(StringBuilder sb, int indent, string text)
- {
- StringBuilder sb2 = new StringBuilder("".PadRight(indent * 2) + text);
- //sb2.Replace("&", "&");
- //sb2.Replace("<", "<");
- //sb2.Replace(">", ">");
- sb.Append(sb2);
- sb.Append(Environment.NewLine);
- //Console.WriteLine(sb2);
- }
- public class Property : IComparable<Property>
- {
- public string Name { get; set; }
- public CssExpression Expression { get; set; }
- public HighOrderPriority HighOrderSort { get; set; }
- public int IdAttributesInSelector { get; set; }
- public int AttributesInSelector { get; set; }
- public int ElementNamesInSelector { get; set; }
- public int SequenceNumber { get; set; }
- public enum HighOrderPriority
- {
- InitialValue = 0,
- Inherited = 1,
- UserAgentNormal = 2,
- UserAgentHigh = 3,
- UserNormal = 4,
- AuthorNormal = 5,
- HtmlAttribute = 6,
- StyleAttributeNormal = 7,
- StyleAttributeHigh = 8,
- AuthorHigh = 9,
- UserHigh = 10,
- };
- int System.IComparable<Property>.CompareTo(Property other)
- {
- // if this is less than other, return -1
- // if this is greater than other, return 1
- int gt = 1;
- int lt = -1;
- if (this.HighOrderSort < other.HighOrderSort)
- return lt;
- if (this.HighOrderSort > other.HighOrderSort)
- return gt;
- if (this.IdAttributesInSelector < other.IdAttributesInSelector)
- return lt;
- if (this.IdAttributesInSelector > other.IdAttributesInSelector)
- return gt;
- if (this.AttributesInSelector < other.AttributesInSelector)
- return lt;
- if (this.AttributesInSelector > other.AttributesInSelector)
- return gt;
- if (this.ElementNamesInSelector < other.ElementNamesInSelector)
- return lt;
- if (this.ElementNamesInSelector > other.ElementNamesInSelector)
- return gt;
- return this.SequenceNumber.CompareTo(other.SequenceNumber);
- }
- }
- private static Dictionary<string, string> ColorMap = new Dictionary<string, string>()
- {
- { "maroon", "800000" },
- { "red", "FF0000" },
- { "orange", "FFA500" },
- { "yellow", "FFFF00" },
- { "olive", "808000" },
- { "purple", "800080" },
- { "fuchsia", "FF00FF" },
- { "white", "FFFFFF" },
- { "lime", "00FF00" },
- { "green", "008000" },
- { "navy", "000080" },
- { "blue", "0000FF" },
- { "mediumblue", "0000CD" },
- { "aqua", "00FFFF" },
- { "teal", "008080" },
- { "black", "000000" },
- { "silver", "C0C0C0" },
- { "gray", "808080" },
- { "darkgray", "A9A9A9" },
- { "beige", "F5F5DC" },
- { "windowtext", "000000" },
- };
- public static string GetWmlColorFromExpression(CssExpression color)
- {
- // todo have to handle all forms of colors here
- if (color.Terms.Count() == 1)
- {
- CssTerm term = color.Terms.First();
- if (term.Type == CssTermType.Function && term.Function.Name.ToUpper() == "RGB" && term.Function.Expression.Terms.Count == 3)
- {
- List<CssTerm> lt = term.Function.Expression.Terms;
- if (lt.First().Unit == CssUnit.Percent)
- {
- string v1 = lt.First().Value;
- string v2 = lt.ElementAt(1).Value;
- string v3 = lt.ElementAt(2).Value;
- string colorInHex = String.Format("{0:x2}{1:x2}{2:x2}", (int)((float.Parse(v1) / 100.0) * 255),
- (int)((float.Parse(v2) / 100.0) * 255), (int)((float.Parse(v3) / 100.0) * 255));
- return colorInHex;
- }
- else
- {
- string v1 = lt.First().Value;
- string v2 = lt.ElementAt(1).Value;
- string v3 = lt.ElementAt(2).Value;
- string colorInHex = String.Format("{0:x2}{1:x2}{2:x2}", int.Parse(v1), int.Parse(v2), int.Parse(v3));
- return colorInHex;
- }
- }
- string value = term.Value;
- if (value.Substring(0, 1) == "#" && value.Length == 4)
- {
- string e = ConvertSingleDigit(value.Substring(1, 1)) +
- ConvertSingleDigit(value.Substring(2, 1)) +
- ConvertSingleDigit(value.Substring(3, 1));
- return e;
- }
- if (value.Substring(0, 1) == "#")
- return value.Substring(1);
- if (ColorMap.ContainsKey(value))
- return ColorMap[value];
- return value;
- }
- return "000000";
- }
- private static string ConvertSingleDigit(string singleDigit)
- {
- return singleDigit + singleDigit;
- }
- }
- }
- #if false
- color
- Value: <color> | inherit
- Initial: depends on UA
- Applies to: all elements
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- margin-top, margin-bottom
- Value: <margin-width> | inherit
- Initial: 0
- Applies to: all elements except elements with table display types other than table-caption, table, and inline-table
- all elements except th, td, tr
- Inherited: no
- Percentages: refer to width of containing block
- Computed value: the percentage as specified or the absolute length
- margin-right, margin-left
- Value: <margin-width> | inherit
- Initial: 0
- Applies to: all elements except elements with table display types other than table-caption, table, and inline-table
- all elements except th, td, tr
- Inherited: no
- Percentages: refer to width of containing block
- Computed value: the percentage as specified or the absolute length
- padding-top, padding-right, padding-bottom, padding-left
- Value: <padding-width> | inherit
- Initial: 0
- Applies to: all elements except table-row-group, table-header-group,
- table-footer-group, table-row, table-column-group and table-column
- all elements except tr
- Inherited: no
- Percentages: refer to width of containing block
- Computed value: the percentage as specified or the absolute length
- border-top-width, border-right-width, border-bottom-width, border-left-width
- Value: <border-width> | inherit
- Initial: medium
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: absolute length; '0' if the border style is 'none' or 'hidden'
- border-top-color, border-right-color, border-bottom-color, border-left-color
- Value: <color> | transparent | inherit
- Initial: the value of the color property
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: when taken from the ’color’ property, the computed value of
- ’color’; otherwise, as specified
- border-top-style, border-right-style, border-bottom-style, border-left-style
- Value: <border-style> | inherit
- Initial: none
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: as specified
- display
- Value: inline | block | list-item | inline-block | table | inline-table |
- table-row-group | table-header-group | table-footer-group |
- table-row | table-column-group | table-column | table-cell |
- table-caption | none | inherit
- Initial: inline
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: see text
- position
- Value: static | relative | absolute | fixed | inherit
- Initial: static
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: as specified
- top
- Value: <length> | <percentage> | auto | inherit
- Initial: auto
- Applies to: positioned elements
- Inherited: no
- Percentages: refer to height of containing block
- Computed value: if specified as a length, the corresponding absolute length; if
- specified as a percentage, the specified value; otherwise, ’auto’.
- right
- Value: <length> | <percentage> | auto | inherit
- Initial: auto
- Applies to: positioned elements
- Inherited: no
- Percentages: refer to width of containing block
- Computed value: if specified as a length, the corresponding absolute length; if
- specified as a percentage, the specified value; otherwise, ’auto’.
- bottom
- Value: <length> | <percentage> | auto | inherit
- Initial: auto
- Applies to: positioned elements
- Inherited: no
- Percentages: refer to height of containing block
- Computed value: if specified as a length, the corresponding absolute length; if
- specified as a percentage, the specified value; otherwise, ’auto’.
- left
- Value: <length> | <percentage> | auto | inherit
- Initial: auto
- Applies to: positioned elements
- Inherited: no
- Percentages: refer to width of containing block
- Computed value: if specified as a length, the corresponding absolute length; if
- specified as a percentage, the specified value; otherwise, ’auto’.
- float
- Value: left | right | none | inherit
- Initial: none
- Applies to: all, but see 9.7 p. 153
- Inherited: no
- Percentages: N/A
- Computed value: as specified
- clear
- Value: none | left | right | both | inherit
- Initial: none
- Applies to: block-level elements
- Inherited: no
- Percentages: N/A
- Computed value: as specified
- z-index
- Value: auto | integer | inherit
- Initial: auto
- Applies to: positioned elements
- Inherited: no
- Percentages: N/A
- Computed value: as spec
- direction
- Value: ltr | rtl | inherit
- Initial: ltr
- Applies to: all elements
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- unicode-bidi
- Value: normal | embed | bidi-override | inherit
- Initial: normal
- Applies to: all elements, but see prose
- Inherited: no
- Percentages: N/A
- Computed value: as spec
- width
- Value: <length> | <percentage> | auto | inherit
- Initial: auto
- Applies to: all elements but non-replaced in-line elements, table rows, and row groups
- Inherited: no
- Percentages: refer to width of containing block
- Computed value: the percentage or 'auto' as specified or the absolute length
- min-width
- Value: <length> | <percentage> | inherit
- Initial: 0
- Applies to: all elements but non-replaced in-line elements, table rows, and row groups
- Inherited: no
- Percentages: refer to width of containing block
- Computed value: the percentage as spec or the absolute length
- max-width
- Value: <length> | <percentage> | none | inherit
- Initial: none
- Applies to: all elements but non-replaced in-line elements, table rows, and row groups
- Inherited: no
- Percentages: refer to width of containing block
- Computed value: the percentage as spec or the absolute length
- height
- Value: <length> | <percentage> | auto | inherit
- Initial: auto
- Applies to: all elements but non-replaced in-line elements, table columns, and column groups
- Inherited: no
- Percentages: see prose
- Computed value: the percentage as spec or the absolute length
- min-height
- Value: <length> | <percentage> | inherit
- Initial: 0
- Applies to: all elements but non-replaced in-line elements, table columns, and column groups
- Inherited: no
- Percentages: see prose
- Computed value: the percentage as spec or the absolute length
- max-height
- Value: <length> | <percentage> | none | inherit
- Initial: none
- Applies to: all elements but non-replaced in-line elements, table columns, and column groups
- Inherited: no
- Percentages: refer to height of containing block
- Computed value: the percentage as spec or the absolute length
- line-height
- Value: normal | <number> | <length> | <percentage> | <inherit>
- Initial: normal
- Applies to: all elements
- Inherited: yes
- Percentages: refer to the font size of the element itself
- Computed value: for <length> and <percentage> the absolute value, otherwise as specified.
- vertical-align
- Value: baseline | sub | super | top | text-top | middle | bottom | text-bottom |
- <percentage> | <length> | inherit
- Initial: baseline
- Applies to: inline-level and 'table-cell' elements
- Inherited: no
- Percentages: refer to the line height of the element itself
- Computed value: for <length> and <percentage> the absolute length, otherwise as specified.
- visibility
- Value: visible | hidden | collapse | inherit
- Initial: visible
- Applies to: all elements
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- list-style-type
- Value: disc | circle | square | decimal | decimal-leading-zero |
- lower-roman | upper-roman | lower-greek | lower-latin |
- upper-latin | armenian | georgian | lower-alpha | upper-alpha |
- none | inherit
- Initial: disc
- Applies to: elements with display: list-item
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- list-style-image
- Value: <uri> | none | inherit
- Initial: none
- Applies to: elements with ’display: list-item’
- Inherited: yes
- Percentages: N/A
- Computed value: absolute URI or ’none’
- list-style-position
- Value: inside | outside | inherit
- Initial: outside
- Applies to: elements with ’display: list-item’
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- background-color
- Value: <color> | transparent | inherit
- Initial: transparent
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: as spec
- font-family
- Value: [[ <family-name> | <generic-family> ] [, <family-name>|
- <generic-family>]* ] | inherit
- Initial: depends on user agent
- Applies to: all elements
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- font-style
- Value: normal | italic | oblique | inherit
- Initial: normal
- Applies to: all elements
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- font-variant
- Value: normal | small-caps | inherit
- Initial: normal
- Applies to: all elements
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- font-weight
- Value: normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 |
- 600 | 700 | 800 | 900 | inherit
- Initial: normal
- Applies to: all elements
- Inherited: yes
- Percentages: N/A
- Computed value: see text
- font-size
- Value: <absolute-size> | <relative-size> | <length> | <percentage> |
- inherit
- Initial: medium
- Applies to: all elements
- Inherited: yes
- Percentages: refer to inherited font size
- Computed value: absolute length
- text-indent
- Value: <length> | <percentage> | inherit
- Initial: 0
- Applies to: block containers
- Inherited: yes
- Percentages: refer to width of containing block
- Computed value: the percentage as specified or the absolute length
- text-align
- Value: left | right | center | justify | inherit
- Initial: a nameless value that acts as ’left’ if ’direction’ is ’ltr’, ’right’ if
- ’direction’ is ’rtl’
- Applies to: block containers
- Inherited: yes
- Percentages: N/A
- Computed value: the initial value or as spec
- text-decoration
- Value: none | [ underline || overline || line-through || blink ] | inherit
- Initial: none
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: as spec
- letter-spacing
- Value: normal | <length> | inherit
- Initial: normal
- Applies to: all elements
- Inherited: yes
- Percentages: N/A
- Computed value: ’normal’ or absolute length
- word-spacing
- Value: normal | <length> | inherit
- Initial: normal
- Applies to: all elements
- Inherited: yes
- Percentages: N/A
- Computed value: for ’normal’ the value 0; otherwise the absolute length
- text-transform
- Value: capitalize | uppercase | lowercase | none | inherit
- Initial: none
- Applies to: all elements
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- white-space
- Value: normal | pre | nowrap | pre-wrap | pre-line | inherit
- Initial: normal
- Applies to: all elements
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- caption-side
- Value: top | bottom | inherit
- Initial: top
- Applies to: 'table-caption' elements
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- table-layout
- Value: auto | fixed | inherit
- Initial: auto
- Applies to: ’table’ and ’inline-table’ elements
- Inherited: no
- Percentages: N/A
- Computed value: as spec
- border-collapse
- Value: collapse | separate | inherit
- Initial: separate
- Applies to: ’table’ and ’inline-table’ elements
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- border-spacing
- Value: <length> <length>? | inherit
- Initial: 0
- Applies to: ’table’ and ’inline-table’ elements
- Inherited: yes
- Percentages: N/A
- Computed value: two absolute lengths
- empty-cells
- Value: show | hide | inherit
- Initial: show
- Applies to: 'table-cell' elements
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- Shorthand Properties
- margin
- Value: <margin-width>{1,4} | inherit
- Initial: see individual props
- Applies to: all elements except elements with table display types other than table-caption, table, and inline-table
- Inherited: no
- Percentages: refer to width of containing block
- Computed value: see individual props
- set one - sets all four values
- set two - first is top/bottom, second is right/left
- set three - first is top, second is right/left, third is bottom
- set four - top right bottom left
- padding
- Value: <padding-width>{1-4} | inherit
- Initial: see individual props
- Applies to: all elements except table-row-group, table-header-group,
- table-footer-group, table-row, table-column-group and table-column
- Inherited: no
- Percentages: refer to width of containing block
- Computed value: see individual props
- set one - sets all four values
- set two - first is top/bottom, second is right/left
- set four - top right bottom left
- border-width
- Value: <border-width>{1-4} | inherit
- Initial: see individual props
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: see individual props
- set one - sets all four values
- set two - first is top/bottom, second is right/left
- set four - top right bottom left
- border-color
- Value: [<color> | transparent]{1-4} | inherit
- Initial: see individual props
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: see individual props
- set one - sets all four values
- set two - first is top/bottom, second is right/left
- set four - top right bottom left
- border-style
- Value: <border-style>{1-4} | inherit
- Initial: see individual props
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: see individual props
- set one - sets all four values
- set two - first is top/bottom, second is right/left
- set four - top right bottom left
- border-top, border-right, border-bottom, border-left, border
- Value: [<border-width> || <border-style> || <border-top-color>] | inherit
- Initial: see individ
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: see individ
- list-style
- Value: [ <’list-style-type’> || <’list-style-position’> || <’list-style-image’>
- ] | inherit
- Initial: see individual props
- Applies to: elements with ’display: list-item’
- Inherited: yes
- Percentages: N/A
- Computed value: see individual props
- background
- Value: [<’background-color’> || <’background-image’> || <’background-
- repeat’> || <’background-attachment’> || <’background-
- position’>] | inherit
- Initial: see individual props
- Applies to: all elements
- Inherited: no
- Percentages: allowed on background-position
- Computed value: see individual props
- font
- Value: [ [ <’font-style’> || <’font-variant’> || <’font-weight’> ]?
- <’font-size’> [ / <’line-height’> ]? <’font-family’> ] | caption |
- icon | menu | message-box | small-caption | status-bar |
- inherit
- Initial: see individual props
- Applies to: all elements
- Inherited: yes
- Percentages: see individual props
- Computed value: see individual props
- probably not support
- overflow
- Value: visible | hidden | scroll | auto | inherit
- Initial: visible
- Applies to: block containers
- Inherited: no
- Percentages: N/A
- Computed value: as spec
- clip
- Value: <shape> | auto | inherit
- Initial: auto
- Applies to: absolutely positioned elements
- Inherited: no
- Percentages: N/A
- Computed value: auto if spec as 'auto', otherwise a rectangle with four values, each of which is
- 'auto' if spec as 'auto' and the computed length otherwise
- content
- Value: normal | none | [ <string> | <uri> | <counter> | attr(<identifier>)
- | open-quote | close-quote | no-open-quote | no-close-quote
- ]+ | inherit
- Initial: normal
- Applies to: :before and :after pseudo elements
- Inherited: no
- Percentages: N/A
- Computed value: On elements, always computes to ’normal’. On :before and
- :after, if ’normal’ is specified, computes to ’none’. Otherwise,
- for URI values, the absolute URI; for attr() values, the resulting
- string; for other keywords, as specified.
- quotes
- Value: [<string> <string>]+ | none | inherit
- Initial: depends on user agent
- Applies to: all elements
- Inherited: yes
- Percentages: N/A
- Computed value: as spec
- counter-reset
- Value: [ <identifier> <integer>? ]+ | none | inherit
- Initial: none
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: as spec
- counter-increment
- Value: [ <identifier> <integer>? ]+ | none | inherit
- Initial: none
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: as spec
- background-image
- Value: <uri> | none | inherit
- Initial: none
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: absolute URI or none
- background-repeat
- Value: repeat | repeat-x | repeat-y | no-repeat | inherit
- Initial: repeat
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: as spec
- background-attachment
- Value: scroll | fixed | inherit
- Initial: scroll
- Applies to: all elements
- Inherited: no
- Percentages: N/A
- Computed value: as spec
- background-position
- Value: [ [ <percentage> | <length> | left | center | right ] [ <percentage>
- | <length> | top | center | bottom ]? ] | [ [ left | center |
- right ] || [ top | center | bottom ] ] | inherit
- Initial: 0% 0%
- Applies to: all elements
- Inherited: no
- Percentages: refer to the size of the box itself
- Computed value: for <length> the absolute value, otherwise a percentage
- #endif
- #if false
- background-color
- border-bottom-color
- border-bottom-style
- border-bottom-width
- border-collapse
- border-left-color
- border-left-style
- border-left-width
- border-right-color
- border-right-style
- border-right-width
- border-spacing
- border-top-color
- border-top-style
- border-top-width
- bottom
- caption-side
- clear
- color
- direction
- display
- empty-cells
- float
- font-family
- font-size
- font-style
- font-variant
- font-weight
- height
- left
- letter-spacing
- line-height
- list-style-image
- list-style-position
- list-style-type
- margin-bottom
- margin-left
- margin-right
- margin-top
- max-height
- max-width
- min-height
- min-width
- padding-bottom
- padding-left
- padding-right
- padding-top
- position
- right
- table-layout
- text-align
- text-decoration
- text-indent
- text-transform
- top
- unicode-bidi
- vertical-align
- visibility
- white-space
- width
- word-spacing
- z-index
- attributes
- ==========
- meta
- style
- _class
- href
- don't know
- ==========
- colspan
- caption
- title
- hr
- border
- http_equiv
- content
- name
- width
- height
- src
- alt
- id
- descr
- type
- elements
- ========
- html
- head
- body
- div
- p
- h1
- h2
- h3
- h4
- h5
- h6
- h7
- h8
- h9
- a
- b
- table
- tr
- td
- br
- img
- span
- blockquote
- sub
- sup
- ol
- ul
- li
- strong
- em
- tbody
- #endif
|