StylesSidebarPane.js 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877
  1. /*
  2. * Copyright (C) 2007 Apple Inc. All rights reserved.
  3. * Copyright (C) 2009 Joseph Pecoraro
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /**
  30. * @constructor
  31. * @extends {WebInspector.SidebarPane}
  32. * @param {WebInspector.ComputedStyleSidebarPane} computedStylePane
  33. * @param {function(DOMAgent.NodeId, string, boolean)} setPseudoClassCallback
  34. */
  35. WebInspector.StylesSidebarPane = function(computedStylePane, setPseudoClassCallback)
  36. {
  37. WebInspector.SidebarPane.call(this, WebInspector.UIString("Styles"));
  38. this.settingsSelectElement = document.createElement("select");
  39. this.settingsSelectElement.className = "select-settings";
  40. var option = document.createElement("option");
  41. option.value = WebInspector.Color.Format.Original;
  42. option.label = WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "As authored" : "As Authored");
  43. this.settingsSelectElement.appendChild(option);
  44. option = document.createElement("option");
  45. option.value = WebInspector.Color.Format.HEX;
  46. option.label = WebInspector.UIString("Hex Colors");
  47. this.settingsSelectElement.appendChild(option);
  48. option = document.createElement("option");
  49. option.value = WebInspector.Color.Format.RGB;
  50. option.label = WebInspector.UIString("RGB Colors");
  51. this.settingsSelectElement.appendChild(option);
  52. option = document.createElement("option");
  53. option.value = WebInspector.Color.Format.HSL;
  54. option.label = WebInspector.UIString("HSL Colors");
  55. this.settingsSelectElement.appendChild(option);
  56. // Prevent section from collapsing.
  57. var muteEventListener = function(event) { event.consume(true); };
  58. this.settingsSelectElement.addEventListener("click", muteEventListener, true);
  59. this.settingsSelectElement.addEventListener("change", this._changeSetting.bind(this), false);
  60. this._updateColorFormatFilter();
  61. this.titleElement.appendChild(this.settingsSelectElement);
  62. this._elementStateButton = document.createElement("button");
  63. this._elementStateButton.className = "pane-title-button element-state";
  64. this._elementStateButton.title = WebInspector.UIString("Toggle Element State");
  65. this._elementStateButton.addEventListener("click", this._toggleElementStatePane.bind(this), false);
  66. this.titleElement.appendChild(this._elementStateButton);
  67. var addButton = document.createElement("button");
  68. addButton.className = "pane-title-button add";
  69. addButton.id = "add-style-button-test-id";
  70. addButton.title = WebInspector.UIString("New Style Rule");
  71. addButton.addEventListener("click", this._createNewRule.bind(this), false);
  72. this.titleElement.appendChild(addButton);
  73. this._computedStylePane = computedStylePane;
  74. computedStylePane._stylesSidebarPane = this;
  75. this._setPseudoClassCallback = setPseudoClassCallback;
  76. this.element.addEventListener("contextmenu", this._contextMenuEventFired.bind(this), true);
  77. WebInspector.settings.colorFormat.addChangeListener(this._colorFormatSettingChanged.bind(this));
  78. this._createElementStatePane();
  79. this.bodyElement.appendChild(this._elementStatePane);
  80. this._sectionsContainer = document.createElement("div");
  81. this.bodyElement.appendChild(this._sectionsContainer);
  82. this._spectrumHelper = new WebInspector.SpectrumPopupHelper();
  83. this._linkifier = new WebInspector.Linkifier(new WebInspector.Linkifier.DefaultCSSFormatter());
  84. WebInspector.cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetAdded, this._styleSheetOrMediaQueryResultChanged, this);
  85. WebInspector.cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetRemoved, this._styleSheetOrMediaQueryResultChanged, this);
  86. WebInspector.cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetChanged, this._styleSheetOrMediaQueryResultChanged, this);
  87. WebInspector.cssModel.addEventListener(WebInspector.CSSStyleModel.Events.MediaQueryResultChanged, this._styleSheetOrMediaQueryResultChanged, this);
  88. WebInspector.domAgent.addEventListener(WebInspector.DOMAgent.Events.AttrModified, this._attributeChanged, this);
  89. WebInspector.domAgent.addEventListener(WebInspector.DOMAgent.Events.AttrRemoved, this._attributeChanged, this);
  90. WebInspector.settings.showUserAgentStyles.addChangeListener(this._showUserAgentStylesSettingChanged.bind(this));
  91. this.element.addEventListener("mousemove", this._mouseMovedOverElement.bind(this), false);
  92. document.body.addEventListener("keydown", this._keyDown.bind(this), false);
  93. document.body.addEventListener("keyup", this._keyUp.bind(this), false);
  94. }
  95. // Keep in sync with RenderStyleConstants.h PseudoId enum. Array below contains pseudo id names for corresponding enum indexes.
  96. // First item is empty due to its artificial NOPSEUDO nature in the enum.
  97. // FIXME: find a way of generating this mapping or getting it from combination of RenderStyleConstants and CSSSelector.cpp at
  98. // runtime.
  99. WebInspector.StylesSidebarPane.PseudoIdNames = [
  100. "", "first-line", "first-letter", "before", "after", "selection", "", "-webkit-scrollbar", "-webkit-file-upload-button",
  101. "-webkit-input-placeholder", "-webkit-slider-thumb", "-webkit-search-cancel-button", "-webkit-search-decoration",
  102. "-webkit-search-results-decoration", "-webkit-search-results-button", "-webkit-media-controls-panel",
  103. "-webkit-media-controls-play-button", "-webkit-media-controls-mute-button", "-webkit-media-controls-timeline",
  104. "-webkit-media-controls-timeline-container", "-webkit-media-controls-volume-slider",
  105. "-webkit-media-controls-volume-slider-container", "-webkit-media-controls-current-time-display",
  106. "-webkit-media-controls-time-remaining-display", "-webkit-media-controls-seek-back-button", "-webkit-media-controls-seek-forward-button",
  107. "-webkit-media-controls-fullscreen-button", "-webkit-media-controls-rewind-button", "-webkit-media-controls-return-to-realtime-button",
  108. "-webkit-media-controls-toggle-closed-captions-button", "-webkit-media-controls-status-display", "-webkit-scrollbar-thumb",
  109. "-webkit-scrollbar-button", "-webkit-scrollbar-track", "-webkit-scrollbar-track-piece", "-webkit-scrollbar-corner",
  110. "-webkit-resizer", "-webkit-inner-spin-button", "-webkit-outer-spin-button"
  111. ];
  112. WebInspector.StylesSidebarPane._colorRegex = /((?:rgb|hsl)a?\([^)]+\)|#[0-9a-fA-F]{6}|#[0-9a-fA-F]{3}|\b\w+\b(?!-))/g;
  113. /**
  114. * @param {WebInspector.CSSProperty} property
  115. */
  116. WebInspector.StylesSidebarPane.createExclamationMark = function(property)
  117. {
  118. var exclamationElement = document.createElement("div");
  119. exclamationElement.className = "exclamation-mark" + (WebInspector.StylesSidebarPane._ignoreErrorsForProperty(property) ? "" : " warning-icon-small");
  120. exclamationElement.title = WebInspector.CSSMetadata.cssPropertiesMetainfo.keySet()[property.name.toLowerCase()] ? WebInspector.UIString("Invalid property value.") : WebInspector.UIString("Unknown property name.");
  121. return exclamationElement;
  122. }
  123. /**
  124. * @param {WebInspector.Color} color
  125. */
  126. WebInspector.StylesSidebarPane._colorFormat = function(color)
  127. {
  128. const cf = WebInspector.Color.Format;
  129. var format;
  130. var formatSetting = WebInspector.settings.colorFormat.get();
  131. if (formatSetting === cf.Original)
  132. format = cf.Original;
  133. else if (formatSetting === cf.RGB)
  134. format = (color.hasAlpha() ? cf.RGBA : cf.RGB);
  135. else if (formatSetting === cf.HSL)
  136. format = (color.hasAlpha() ? cf.HSLA : cf.HSL);
  137. else if (!color.hasAlpha())
  138. format = (color.canBeShortHex() ? cf.ShortHEX : cf.HEX);
  139. else
  140. format = cf.RGBA;
  141. return format;
  142. }
  143. /**
  144. * @param {WebInspector.CSSProperty} property
  145. */
  146. WebInspector.StylesSidebarPane._ignoreErrorsForProperty = function(property) {
  147. function hasUnknownVendorPrefix(string)
  148. {
  149. return !string.startsWith("-webkit-") && /^[-_][\w\d]+-\w/.test(string);
  150. }
  151. var name = property.name.toLowerCase();
  152. // IE hack.
  153. if (name.charAt(0) === "_")
  154. return true;
  155. // IE has a different format for this.
  156. if (name === "filter")
  157. return true;
  158. // Common IE-specific property prefix.
  159. if (name.startsWith("scrollbar-"))
  160. return true;
  161. if (hasUnknownVendorPrefix(name))
  162. return true;
  163. var value = property.value.toLowerCase();
  164. // IE hack.
  165. if (value.endsWith("\9"))
  166. return true;
  167. if (hasUnknownVendorPrefix(value))
  168. return true;
  169. return false;
  170. }
  171. WebInspector.StylesSidebarPane.prototype = {
  172. /**
  173. * @param {Event} event
  174. */
  175. _contextMenuEventFired: function(event)
  176. {
  177. // We start editing upon click -> default navigation to resources panel is not available
  178. // Hence we add a soft context menu for hrefs.
  179. var contextMenu = new WebInspector.ContextMenu(event);
  180. contextMenu.appendApplicableItems(event.target);
  181. contextMenu.show();
  182. },
  183. get _forcedPseudoClasses()
  184. {
  185. return this.node ? (this.node.getUserProperty("pseudoState") || undefined) : undefined;
  186. },
  187. _updateForcedPseudoStateInputs: function()
  188. {
  189. if (!this.node)
  190. return;
  191. var nodePseudoState = this._forcedPseudoClasses;
  192. if (!nodePseudoState)
  193. nodePseudoState = [];
  194. var inputs = this._elementStatePane.inputs;
  195. for (var i = 0; i < inputs.length; ++i)
  196. inputs[i].checked = nodePseudoState.indexOf(inputs[i].state) >= 0;
  197. },
  198. /**
  199. * @param {WebInspector.DOMNode=} node
  200. * @param {boolean=} forceUpdate
  201. */
  202. update: function(node, forceUpdate)
  203. {
  204. this._spectrumHelper.hide();
  205. this._discardElementUnderMouse();
  206. var refresh = false;
  207. if (forceUpdate)
  208. delete this.node;
  209. if (!forceUpdate && (node === this.node))
  210. refresh = true;
  211. if (node && node.nodeType() === Node.TEXT_NODE && node.parentNode)
  212. node = node.parentNode;
  213. if (node && node.nodeType() !== Node.ELEMENT_NODE)
  214. node = null;
  215. if (node)
  216. this.node = node;
  217. else
  218. node = this.node;
  219. this._updateForcedPseudoStateInputs();
  220. if (refresh)
  221. this._refreshUpdate();
  222. else
  223. this._rebuildUpdate();
  224. },
  225. /**
  226. * @param {WebInspector.StylePropertiesSection=} editedSection
  227. * @param {boolean=} forceFetchComputedStyle
  228. * @param {function()=} userCallback
  229. */
  230. _refreshUpdate: function(editedSection, forceFetchComputedStyle, userCallback)
  231. {
  232. if (this._refreshUpdateInProgress) {
  233. this._lastNodeForInnerRefresh = this.node;
  234. return;
  235. }
  236. var node = this._validateNode(userCallback);
  237. if (!node)
  238. return;
  239. function computedStyleCallback(computedStyle)
  240. {
  241. delete this._refreshUpdateInProgress;
  242. if (this._lastNodeForInnerRefresh) {
  243. delete this._lastNodeForInnerRefresh;
  244. this._refreshUpdate(editedSection, forceFetchComputedStyle, userCallback);
  245. return;
  246. }
  247. if (this.node === node && computedStyle)
  248. this._innerRefreshUpdate(node, computedStyle, editedSection);
  249. if (userCallback)
  250. userCallback();
  251. }
  252. if (this._computedStylePane.isShowing() || forceFetchComputedStyle) {
  253. this._refreshUpdateInProgress = true;
  254. WebInspector.cssModel.getComputedStyleAsync(node.id, computedStyleCallback.bind(this));
  255. } else {
  256. this._innerRefreshUpdate(node, null, editedSection);
  257. if (userCallback)
  258. userCallback();
  259. }
  260. },
  261. _rebuildUpdate: function()
  262. {
  263. if (this._rebuildUpdateInProgress) {
  264. this._lastNodeForInnerRebuild = this.node;
  265. return;
  266. }
  267. var node = this._validateNode();
  268. if (!node)
  269. return;
  270. this._rebuildUpdateInProgress = true;
  271. var resultStyles = {};
  272. function stylesCallback(matchedResult)
  273. {
  274. delete this._rebuildUpdateInProgress;
  275. var lastNodeForRebuild = this._lastNodeForInnerRebuild;
  276. if (lastNodeForRebuild) {
  277. delete this._lastNodeForInnerRebuild;
  278. if (lastNodeForRebuild !== this.node) {
  279. this._rebuildUpdate();
  280. return;
  281. }
  282. }
  283. if (matchedResult && this.node === node) {
  284. resultStyles.matchedCSSRules = matchedResult.matchedCSSRules;
  285. resultStyles.pseudoElements = matchedResult.pseudoElements;
  286. resultStyles.inherited = matchedResult.inherited;
  287. this._innerRebuildUpdate(node, resultStyles);
  288. }
  289. if (lastNodeForRebuild) {
  290. // lastNodeForRebuild is the same as this.node - another rebuild has been requested.
  291. this._rebuildUpdate();
  292. return;
  293. }
  294. }
  295. function inlineCallback(inlineStyle, attributesStyle)
  296. {
  297. resultStyles.inlineStyle = inlineStyle;
  298. resultStyles.attributesStyle = attributesStyle;
  299. }
  300. function computedCallback(computedStyle)
  301. {
  302. resultStyles.computedStyle = computedStyle;
  303. }
  304. if (this._computedStylePane.isShowing())
  305. WebInspector.cssModel.getComputedStyleAsync(node.id, computedCallback.bind(this));
  306. WebInspector.cssModel.getInlineStylesAsync(node.id, inlineCallback.bind(this));
  307. WebInspector.cssModel.getMatchedStylesAsync(node.id, true, true, stylesCallback.bind(this));
  308. },
  309. /**
  310. * @param {function()=} userCallback
  311. */
  312. _validateNode: function(userCallback)
  313. {
  314. if (!this.node) {
  315. this._sectionsContainer.removeChildren();
  316. this._computedStylePane.bodyElement.removeChildren();
  317. this.sections = {};
  318. if (userCallback)
  319. userCallback();
  320. return null;
  321. }
  322. return this.node;
  323. },
  324. _styleSheetOrMediaQueryResultChanged: function()
  325. {
  326. if (this._userOperation || this._isEditingStyle)
  327. return;
  328. this._rebuildUpdate();
  329. },
  330. _attributeChanged: function(event)
  331. {
  332. // Any attribute removal or modification can affect the styles of "related" nodes.
  333. // Do not touch the styles if they are being edited.
  334. if (this._isEditingStyle || this._userOperation)
  335. return;
  336. if (!this._canAffectCurrentStyles(event.data.node))
  337. return;
  338. this._rebuildUpdate();
  339. },
  340. _canAffectCurrentStyles: function(node)
  341. {
  342. return this.node && (this.node === node || node.parentNode === this.node.parentNode || node.isAncestor(this.node));
  343. },
  344. _innerRefreshUpdate: function(node, computedStyle, editedSection)
  345. {
  346. for (var pseudoId in this.sections) {
  347. var styleRules = this._refreshStyleRules(this.sections[pseudoId], computedStyle);
  348. var usedProperties = {};
  349. this._markUsedProperties(styleRules, usedProperties);
  350. this._refreshSectionsForStyleRules(styleRules, usedProperties, editedSection);
  351. }
  352. if (computedStyle)
  353. this.sections[0][0].rebuildComputedTrace(this.sections[0]);
  354. this._nodeStylesUpdatedForTest(node, false);
  355. },
  356. _innerRebuildUpdate: function(node, styles)
  357. {
  358. this._sectionsContainer.removeChildren();
  359. this._computedStylePane.bodyElement.removeChildren();
  360. this._linkifier.reset();
  361. var styleRules = this._rebuildStyleRules(node, styles);
  362. var usedProperties = {};
  363. this._markUsedProperties(styleRules, usedProperties);
  364. this.sections[0] = this._rebuildSectionsForStyleRules(styleRules, usedProperties, 0, null);
  365. var anchorElement = this.sections[0].inheritedPropertiesSeparatorElement;
  366. if (styles.computedStyle)
  367. this.sections[0][0].rebuildComputedTrace(this.sections[0]);
  368. for (var i = 0; i < styles.pseudoElements.length; ++i) {
  369. var pseudoElementCSSRules = styles.pseudoElements[i];
  370. styleRules = [];
  371. var pseudoId = pseudoElementCSSRules.pseudoId;
  372. var entry = { isStyleSeparator: true, pseudoId: pseudoId };
  373. styleRules.push(entry);
  374. // Add rules in reverse order to match the cascade order.
  375. for (var j = pseudoElementCSSRules.rules.length - 1; j >= 0; --j) {
  376. var rule = pseudoElementCSSRules.rules[j];
  377. styleRules.push({ style: rule.style, selectorText: rule.selectorText, media: rule.media, sourceURL: rule.resourceURL(), rule: rule, editable: !!(rule.style && rule.style.id) });
  378. }
  379. usedProperties = {};
  380. this._markUsedProperties(styleRules, usedProperties);
  381. this.sections[pseudoId] = this._rebuildSectionsForStyleRules(styleRules, usedProperties, pseudoId, anchorElement);
  382. }
  383. this._nodeStylesUpdatedForTest(node, true);
  384. },
  385. _nodeStylesUpdatedForTest: function(node, rebuild)
  386. {
  387. // Tests override this method.
  388. },
  389. _refreshStyleRules: function(sections, computedStyle)
  390. {
  391. var nodeComputedStyle = computedStyle;
  392. var styleRules = [];
  393. for (var i = 0; sections && i < sections.length; ++i) {
  394. var section = sections[i];
  395. if (section.isBlank)
  396. continue;
  397. if (section.computedStyle)
  398. section.styleRule.style = nodeComputedStyle;
  399. var styleRule = { section: section, style: section.styleRule.style, computedStyle: section.computedStyle, rule: section.rule, editable: !!(section.styleRule.style && section.styleRule.style.id),
  400. isAttribute: section.styleRule.isAttribute, isInherited: section.styleRule.isInherited, parentNode: section.styleRule.parentNode };
  401. styleRules.push(styleRule);
  402. }
  403. return styleRules;
  404. },
  405. _rebuildStyleRules: function(node, styles)
  406. {
  407. var nodeComputedStyle = styles.computedStyle;
  408. this.sections = {};
  409. var styleRules = [];
  410. function addAttributesStyle()
  411. {
  412. if (!styles.attributesStyle)
  413. return;
  414. var attrStyle = { style: styles.attributesStyle, editable: false };
  415. attrStyle.selectorText = node.nodeNameInCorrectCase() + "[" + WebInspector.UIString("Attributes Style") + "]";
  416. styleRules.push(attrStyle);
  417. }
  418. styleRules.push({ computedStyle: true, selectorText: "", style: nodeComputedStyle, editable: false });
  419. // Inline style has the greatest specificity.
  420. if (styles.inlineStyle && node.nodeType() === Node.ELEMENT_NODE) {
  421. var inlineStyle = { selectorText: "element.style", style: styles.inlineStyle, isAttribute: true };
  422. styleRules.push(inlineStyle);
  423. }
  424. // Add rules in reverse order to match the cascade order.
  425. var addedAttributesStyle;
  426. for (var i = styles.matchedCSSRules.length - 1; i >= 0; --i) {
  427. var rule = styles.matchedCSSRules[i];
  428. if (!WebInspector.settings.showUserAgentStyles.get() && (rule.isUser || rule.isUserAgent))
  429. continue;
  430. if ((rule.isUser || rule.isUserAgent) && !addedAttributesStyle) {
  431. // Show element's Style Attributes after all author rules.
  432. addedAttributesStyle = true;
  433. addAttributesStyle();
  434. }
  435. styleRules.push({ style: rule.style, selectorText: rule.selectorText, media: rule.media, sourceURL: rule.resourceURL(), rule: rule, editable: !!(rule.style && rule.style.id) });
  436. }
  437. if (!addedAttributesStyle)
  438. addAttributesStyle();
  439. // Walk the node structure and identify styles with inherited properties.
  440. var parentNode = node.parentNode;
  441. function insertInheritedNodeSeparator(node)
  442. {
  443. var entry = {};
  444. entry.isStyleSeparator = true;
  445. entry.node = node;
  446. styleRules.push(entry);
  447. }
  448. for (var parentOrdinal = 0; parentOrdinal < styles.inherited.length; ++parentOrdinal) {
  449. var parentStyles = styles.inherited[parentOrdinal];
  450. var separatorInserted = false;
  451. if (parentStyles.inlineStyle) {
  452. if (this._containsInherited(parentStyles.inlineStyle)) {
  453. var inlineStyle = { selectorText: WebInspector.UIString("Style Attribute"), style: parentStyles.inlineStyle, isAttribute: true, isInherited: true, parentNode: parentNode };
  454. if (!separatorInserted) {
  455. insertInheritedNodeSeparator(parentNode);
  456. separatorInserted = true;
  457. }
  458. styleRules.push(inlineStyle);
  459. }
  460. }
  461. for (var i = parentStyles.matchedCSSRules.length - 1; i >= 0; --i) {
  462. var rulePayload = parentStyles.matchedCSSRules[i];
  463. if (!this._containsInherited(rulePayload.style))
  464. continue;
  465. var rule = rulePayload;
  466. if (!WebInspector.settings.showUserAgentStyles.get() && (rule.isUser || rule.isUserAgent))
  467. continue;
  468. if (!separatorInserted) {
  469. insertInheritedNodeSeparator(parentNode);
  470. separatorInserted = true;
  471. }
  472. styleRules.push({ style: rule.style, selectorText: rule.selectorText, media: rule.media, sourceURL: rule.resourceURL(), rule: rule, isInherited: true, parentNode: parentNode, editable: !!(rule.style && rule.style.id) });
  473. }
  474. parentNode = parentNode.parentNode;
  475. }
  476. return styleRules;
  477. },
  478. _markUsedProperties: function(styleRules, usedProperties)
  479. {
  480. var foundImportantProperties = {};
  481. var propertyToEffectiveRule = {};
  482. var inheritedPropertyToNode = {};
  483. for (var i = 0; i < styleRules.length; ++i) {
  484. var styleRule = styleRules[i];
  485. if (styleRule.computedStyle || styleRule.isStyleSeparator)
  486. continue;
  487. if (styleRule.section && styleRule.section.noAffect)
  488. continue;
  489. styleRule.usedProperties = {};
  490. var style = styleRule.style;
  491. var allProperties = style.allProperties;
  492. for (var j = 0; j < allProperties.length; ++j) {
  493. var property = allProperties[j];
  494. if (!property.isLive || !property.parsedOk)
  495. continue;
  496. // Do not pick non-inherited properties from inherited styles.
  497. if (styleRule.isInherited && !WebInspector.CSSMetadata.isPropertyInherited(property.name))
  498. continue;
  499. var canonicalName = WebInspector.CSSMetadata.canonicalPropertyName(property.name);
  500. if (foundImportantProperties.hasOwnProperty(canonicalName))
  501. continue;
  502. var isImportant = property.priority.length;
  503. if (!isImportant && usedProperties.hasOwnProperty(canonicalName))
  504. continue;
  505. var isKnownProperty = propertyToEffectiveRule.hasOwnProperty(canonicalName);
  506. if (!isKnownProperty && styleRule.isInherited && !inheritedPropertyToNode[canonicalName])
  507. inheritedPropertyToNode[canonicalName] = styleRule.parentNode;
  508. if (isImportant) {
  509. if (styleRule.isInherited && isKnownProperty && styleRule.parentNode !== inheritedPropertyToNode[canonicalName])
  510. continue;
  511. foundImportantProperties[canonicalName] = true;
  512. if (isKnownProperty)
  513. delete propertyToEffectiveRule[canonicalName].usedProperties[canonicalName];
  514. }
  515. styleRule.usedProperties[canonicalName] = true;
  516. usedProperties[canonicalName] = true;
  517. propertyToEffectiveRule[canonicalName] = styleRule;
  518. }
  519. }
  520. },
  521. _refreshSectionsForStyleRules: function(styleRules, usedProperties, editedSection)
  522. {
  523. // Walk the style rules and update the sections with new overloaded and used properties.
  524. for (var i = 0; i < styleRules.length; ++i) {
  525. var styleRule = styleRules[i];
  526. var section = styleRule.section;
  527. if (styleRule.computedStyle) {
  528. section._usedProperties = usedProperties;
  529. section.update();
  530. } else {
  531. section._usedProperties = styleRule.usedProperties;
  532. section.update(section === editedSection);
  533. }
  534. }
  535. },
  536. _rebuildSectionsForStyleRules: function(styleRules, usedProperties, pseudoId, anchorElement)
  537. {
  538. // Make a property section for each style rule.
  539. var sections = [];
  540. var lastWasSeparator = true;
  541. for (var i = 0; i < styleRules.length; ++i) {
  542. var styleRule = styleRules[i];
  543. if (styleRule.isStyleSeparator) {
  544. var separatorElement = document.createElement("div");
  545. separatorElement.className = "sidebar-separator";
  546. if (styleRule.node) {
  547. var link = WebInspector.DOMPresentationUtils.linkifyNodeReference(styleRule.node);
  548. separatorElement.appendChild(document.createTextNode(WebInspector.UIString("Inherited from") + " "));
  549. separatorElement.appendChild(link);
  550. if (!sections.inheritedPropertiesSeparatorElement)
  551. sections.inheritedPropertiesSeparatorElement = separatorElement;
  552. } else if ("pseudoId" in styleRule) {
  553. var pseudoName = WebInspector.StylesSidebarPane.PseudoIdNames[styleRule.pseudoId];
  554. if (pseudoName)
  555. separatorElement.textContent = WebInspector.UIString("Pseudo ::%s element", pseudoName);
  556. else
  557. separatorElement.textContent = WebInspector.UIString("Pseudo element");
  558. } else
  559. separatorElement.textContent = styleRule.text;
  560. this._sectionsContainer.insertBefore(separatorElement, anchorElement);
  561. lastWasSeparator = true;
  562. continue;
  563. }
  564. var computedStyle = styleRule.computedStyle;
  565. // Default editable to true if it was omitted.
  566. var editable = styleRule.editable;
  567. if (typeof editable === "undefined")
  568. editable = true;
  569. if (computedStyle)
  570. var section = new WebInspector.ComputedStylePropertiesSection(this, styleRule, usedProperties);
  571. else {
  572. var section = new WebInspector.StylePropertiesSection(this, styleRule, editable, styleRule.isInherited, lastWasSeparator);
  573. section._markSelectorMatches();
  574. }
  575. section.expanded = true;
  576. if (computedStyle) {
  577. this._computedStylePane.bodyElement.appendChild(section.element);
  578. lastWasSeparator = true;
  579. } else {
  580. this._sectionsContainer.insertBefore(section.element, anchorElement);
  581. lastWasSeparator = false;
  582. }
  583. sections.push(section);
  584. }
  585. return sections;
  586. },
  587. _containsInherited: function(style)
  588. {
  589. var properties = style.allProperties;
  590. for (var i = 0; i < properties.length; ++i) {
  591. var property = properties[i];
  592. // Does this style contain non-overridden inherited property?
  593. if (property.isLive && WebInspector.CSSMetadata.isPropertyInherited(property.name))
  594. return true;
  595. }
  596. return false;
  597. },
  598. _colorFormatSettingChanged: function(event)
  599. {
  600. this._updateColorFormatFilter();
  601. for (var pseudoId in this.sections) {
  602. var sections = this.sections[pseudoId];
  603. for (var i = 0; i < sections.length; ++i)
  604. sections[i].update(true);
  605. }
  606. },
  607. _updateColorFormatFilter: function()
  608. {
  609. // Select the correct color format setting again, since it needs to be selected.
  610. var selectedIndex = 0;
  611. var value = WebInspector.settings.colorFormat.get();
  612. var options = this.settingsSelectElement.options;
  613. for (var i = 0; i < options.length; ++i) {
  614. if (options[i].value === value) {
  615. selectedIndex = i;
  616. break;
  617. }
  618. }
  619. this.settingsSelectElement.selectedIndex = selectedIndex;
  620. },
  621. _changeSetting: function(event)
  622. {
  623. var options = this.settingsSelectElement.options;
  624. var selectedOption = options[this.settingsSelectElement.selectedIndex];
  625. WebInspector.settings.colorFormat.set(selectedOption.value);
  626. },
  627. _createNewRule: function(event)
  628. {
  629. event.consume();
  630. this.expand();
  631. this.addBlankSection().startEditingSelector();
  632. },
  633. addBlankSection: function()
  634. {
  635. var blankSection = new WebInspector.BlankStylePropertiesSection(this, this.node ? this.node.appropriateSelectorFor(true) : "");
  636. var elementStyleSection = this.sections[0][1];
  637. this._sectionsContainer.insertBefore(blankSection.element, elementStyleSection.element.nextSibling);
  638. this.sections[0].splice(2, 0, blankSection);
  639. return blankSection;
  640. },
  641. removeSection: function(section)
  642. {
  643. for (var pseudoId in this.sections) {
  644. var sections = this.sections[pseudoId];
  645. var index = sections.indexOf(section);
  646. if (index === -1)
  647. continue;
  648. sections.splice(index, 1);
  649. section.element.remove();
  650. }
  651. },
  652. _toggleElementStatePane: function(event)
  653. {
  654. event.consume();
  655. if (!this._elementStateButton.hasStyleClass("toggled")) {
  656. this.expand();
  657. this._elementStateButton.addStyleClass("toggled");
  658. this._elementStatePane.addStyleClass("expanded");
  659. } else {
  660. this._elementStateButton.removeStyleClass("toggled");
  661. this._elementStatePane.removeStyleClass("expanded");
  662. }
  663. },
  664. _createElementStatePane: function()
  665. {
  666. this._elementStatePane = document.createElement("div");
  667. this._elementStatePane.className = "styles-element-state-pane source-code";
  668. var table = document.createElement("table");
  669. var inputs = [];
  670. this._elementStatePane.inputs = inputs;
  671. function clickListener(event)
  672. {
  673. var node = this._validateNode();
  674. if (!node)
  675. return;
  676. this._setPseudoClassCallback(node.id, event.target.state, event.target.checked);
  677. }
  678. function createCheckbox(state)
  679. {
  680. var td = document.createElement("td");
  681. var label = document.createElement("label");
  682. var input = document.createElement("input");
  683. input.type = "checkbox";
  684. input.state = state;
  685. input.addEventListener("click", clickListener.bind(this), false);
  686. inputs.push(input);
  687. label.appendChild(input);
  688. label.appendChild(document.createTextNode(":" + state));
  689. td.appendChild(label);
  690. return td;
  691. }
  692. var tr = document.createElement("tr");
  693. tr.appendChild(createCheckbox.call(this, "active"));
  694. tr.appendChild(createCheckbox.call(this, "hover"));
  695. table.appendChild(tr);
  696. tr = document.createElement("tr");
  697. tr.appendChild(createCheckbox.call(this, "focus"));
  698. tr.appendChild(createCheckbox.call(this, "visited"));
  699. table.appendChild(tr);
  700. this._elementStatePane.appendChild(table);
  701. },
  702. _showUserAgentStylesSettingChanged: function()
  703. {
  704. this._rebuildUpdate();
  705. },
  706. willHide: function()
  707. {
  708. this._spectrumHelper.hide();
  709. this._discardElementUnderMouse();
  710. },
  711. _discardElementUnderMouse: function()
  712. {
  713. if (this._elementUnderMouse)
  714. this._elementUnderMouse.removeStyleClass("styles-panel-hovered");
  715. delete this._elementUnderMouse;
  716. },
  717. _mouseMovedOverElement: function(e)
  718. {
  719. if (this._elementUnderMouse && e.target !== this._elementUnderMouse)
  720. this._discardElementUnderMouse();
  721. this._elementUnderMouse = e.target;
  722. if (WebInspector.KeyboardShortcut.eventHasCtrlOrMeta(e))
  723. this._elementUnderMouse.addStyleClass("styles-panel-hovered");
  724. },
  725. _keyDown: function(e)
  726. {
  727. if ((!WebInspector.isMac() && e.keyCode === WebInspector.KeyboardShortcut.Keys.Ctrl.code) ||
  728. (WebInspector.isMac() && e.keyCode === WebInspector.KeyboardShortcut.Keys.Meta.code)) {
  729. if (this._elementUnderMouse)
  730. this._elementUnderMouse.addStyleClass("styles-panel-hovered");
  731. }
  732. },
  733. _keyUp: function(e)
  734. {
  735. if ((!WebInspector.isMac() && e.keyCode === WebInspector.KeyboardShortcut.Keys.Ctrl.code) ||
  736. (WebInspector.isMac() && e.keyCode === WebInspector.KeyboardShortcut.Keys.Meta.code)) {
  737. this._discardElementUnderMouse();
  738. }
  739. },
  740. __proto__: WebInspector.SidebarPane.prototype
  741. }
  742. /**
  743. * @constructor
  744. * @extends {WebInspector.SidebarPane}
  745. */
  746. WebInspector.ComputedStyleSidebarPane = function()
  747. {
  748. WebInspector.SidebarPane.call(this, WebInspector.UIString("Computed Style"));
  749. var showInheritedCheckbox = new WebInspector.Checkbox(WebInspector.UIString("Show inherited"), "sidebar-pane-subtitle");
  750. this.titleElement.appendChild(showInheritedCheckbox.element);
  751. if (WebInspector.settings.showInheritedComputedStyleProperties.get()) {
  752. this.bodyElement.addStyleClass("show-inherited");
  753. showInheritedCheckbox.checked = true;
  754. }
  755. function showInheritedToggleFunction(event)
  756. {
  757. WebInspector.settings.showInheritedComputedStyleProperties.set(showInheritedCheckbox.checked);
  758. if (WebInspector.settings.showInheritedComputedStyleProperties.get())
  759. this.bodyElement.addStyleClass("show-inherited");
  760. else
  761. this.bodyElement.removeStyleClass("show-inherited");
  762. }
  763. showInheritedCheckbox.addEventListener(showInheritedToggleFunction.bind(this));
  764. }
  765. WebInspector.ComputedStyleSidebarPane.prototype = {
  766. wasShown: function()
  767. {
  768. WebInspector.SidebarPane.prototype.wasShown.call(this);
  769. if (!this._hasFreshContent)
  770. this.prepareContent();
  771. },
  772. /**
  773. * @param {function()=} callback
  774. */
  775. prepareContent: function(callback)
  776. {
  777. function wrappedCallback() {
  778. this._hasFreshContent = true;
  779. if (callback)
  780. callback();
  781. delete this._hasFreshContent;
  782. }
  783. this._stylesSidebarPane._refreshUpdate(null, true, wrappedCallback.bind(this));
  784. },
  785. __proto__: WebInspector.SidebarPane.prototype
  786. }
  787. /**
  788. * @constructor
  789. * @extends {WebInspector.PropertiesSection}
  790. */
  791. WebInspector.StylePropertiesSection = function(parentPane, styleRule, editable, isInherited, isFirstSection)
  792. {
  793. WebInspector.PropertiesSection.call(this, "");
  794. this.element.className = "styles-section matched-styles monospace" + (isFirstSection ? " first-styles-section" : "");
  795. // We don't really use properties' disclosure.
  796. this.propertiesElement.removeStyleClass("properties-tree");
  797. this._parentPane = parentPane;
  798. this.styleRule = styleRule;
  799. this.rule = this.styleRule.rule;
  800. this.editable = editable;
  801. this.isInherited = isInherited;
  802. if (styleRule.media) {
  803. for (var i = styleRule.media.length - 1; i >= 0; --i) {
  804. var media = styleRule.media[i];
  805. var mediaDataElement = this.titleElement.createChild("div", "media");
  806. var mediaText;
  807. switch (media.source) {
  808. case WebInspector.CSSMedia.Source.LINKED_SHEET:
  809. case WebInspector.CSSMedia.Source.INLINE_SHEET:
  810. mediaText = "media=\"" + media.text + "\"";
  811. break;
  812. case WebInspector.CSSMedia.Source.MEDIA_RULE:
  813. mediaText = "@media " + media.text;
  814. break;
  815. case WebInspector.CSSMedia.Source.IMPORT_RULE:
  816. mediaText = "@import " + media.text;
  817. break;
  818. }
  819. if (media.sourceURL) {
  820. var refElement = mediaDataElement.createChild("div", "subtitle");
  821. var rawLocation;
  822. var mediaHeader;
  823. if (media.range) {
  824. mediaHeader = media.header();
  825. if (mediaHeader) {
  826. var lineNumber = media.lineNumberInSource();
  827. var columnNumber = media.columnNumberInSource();
  828. console.assert(typeof lineNumber !== "undefined" && typeof columnNumber !== "undefined");
  829. rawLocation = new WebInspector.CSSLocation(media.sourceURL, lineNumber, columnNumber);
  830. }
  831. }
  832. var anchor;
  833. if (rawLocation)
  834. anchor = this._parentPane._linkifier.linkifyCSSLocation(mediaHeader.id, rawLocation);
  835. else {
  836. // The "linkedStylesheet" case.
  837. anchor = WebInspector.linkifyResourceAsNode(media.sourceURL, undefined, "subtitle", media.sourceURL);
  838. }
  839. anchor.preferredPanel = "scripts";
  840. anchor.style.float = "right";
  841. refElement.appendChild(anchor);
  842. }
  843. var mediaTextElement = mediaDataElement.createChild("span");
  844. mediaTextElement.textContent = mediaText;
  845. mediaTextElement.title = media.text;
  846. }
  847. }
  848. var selectorContainer = document.createElement("div");
  849. this._selectorElement = document.createElement("span");
  850. this._selectorElement.textContent = styleRule.selectorText;
  851. selectorContainer.appendChild(this._selectorElement);
  852. var openBrace = document.createElement("span");
  853. openBrace.textContent = " {";
  854. selectorContainer.appendChild(openBrace);
  855. selectorContainer.addEventListener("mousedown", this._handleEmptySpaceMouseDown.bind(this), false);
  856. selectorContainer.addEventListener("click", this._handleSelectorContainerClick.bind(this), false);
  857. var closeBrace = document.createElement("div");
  858. closeBrace.textContent = "}";
  859. this.element.appendChild(closeBrace);
  860. this._selectorElement.addEventListener("click", this._handleSelectorClick.bind(this), false);
  861. this.element.addEventListener("mousedown", this._handleEmptySpaceMouseDown.bind(this), false);
  862. this.element.addEventListener("click", this._handleEmptySpaceClick.bind(this), false);
  863. if (this.rule) {
  864. // Prevent editing the user agent and user rules.
  865. if (this.rule.isUserAgent || this.rule.isUser)
  866. this.editable = false;
  867. else {
  868. // Check this is a real CSSRule, not a bogus object coming from WebInspector.BlankStylePropertiesSection.
  869. if (this.rule.id)
  870. this.navigable = !!this.rule.resourceURL();
  871. }
  872. this.titleElement.addStyleClass("styles-selector");
  873. }
  874. this._usedProperties = styleRule.usedProperties;
  875. this._selectorRefElement = document.createElement("div");
  876. this._selectorRefElement.className = "subtitle";
  877. this._selectorRefElement.appendChild(this._createRuleOriginNode());
  878. selectorContainer.insertBefore(this._selectorRefElement, selectorContainer.firstChild);
  879. this.titleElement.appendChild(selectorContainer);
  880. this._selectorContainer = selectorContainer;
  881. if (isInherited)
  882. this.element.addStyleClass("show-inherited"); // This one is related to inherited rules, not computed style.
  883. if (this.navigable)
  884. this.element.addStyleClass("navigable");
  885. if (!this.editable)
  886. this.element.addStyleClass("read-only");
  887. }
  888. WebInspector.StylePropertiesSection.prototype = {
  889. get pane()
  890. {
  891. return this._parentPane;
  892. },
  893. collapse: function(dontRememberState)
  894. {
  895. // Overriding with empty body.
  896. },
  897. isPropertyInherited: function(propertyName)
  898. {
  899. if (this.isInherited) {
  900. // While rendering inherited stylesheet, reverse meaning of this property.
  901. // Render truly inherited properties with black, i.e. return them as non-inherited.
  902. return !WebInspector.CSSMetadata.isPropertyInherited(propertyName);
  903. }
  904. return false;
  905. },
  906. /**
  907. * @param {string} propertyName
  908. * @param {boolean=} isShorthand
  909. */
  910. isPropertyOverloaded: function(propertyName, isShorthand)
  911. {
  912. if (!this._usedProperties || this.noAffect)
  913. return false;
  914. if (this.isInherited && !WebInspector.CSSMetadata.isPropertyInherited(propertyName)) {
  915. // In the inherited sections, only show overrides for the potentially inherited properties.
  916. return false;
  917. }
  918. var canonicalName = WebInspector.CSSMetadata.canonicalPropertyName(propertyName);
  919. var used = (canonicalName in this._usedProperties);
  920. if (used || !isShorthand)
  921. return !used;
  922. // Find out if any of the individual longhand properties of the shorthand
  923. // are used, if none are then the shorthand is overloaded too.
  924. var longhandProperties = this.styleRule.style.longhandProperties(propertyName);
  925. for (var j = 0; j < longhandProperties.length; ++j) {
  926. var individualProperty = longhandProperties[j];
  927. if (WebInspector.CSSMetadata.canonicalPropertyName(individualProperty.name) in this._usedProperties)
  928. return false;
  929. }
  930. return true;
  931. },
  932. nextEditableSibling: function()
  933. {
  934. var curSection = this;
  935. do {
  936. curSection = curSection.nextSibling;
  937. } while (curSection && !curSection.editable);
  938. if (!curSection) {
  939. curSection = this.firstSibling;
  940. while (curSection && !curSection.editable)
  941. curSection = curSection.nextSibling;
  942. }
  943. return (curSection && curSection.editable) ? curSection : null;
  944. },
  945. previousEditableSibling: function()
  946. {
  947. var curSection = this;
  948. do {
  949. curSection = curSection.previousSibling;
  950. } while (curSection && !curSection.editable);
  951. if (!curSection) {
  952. curSection = this.lastSibling;
  953. while (curSection && !curSection.editable)
  954. curSection = curSection.previousSibling;
  955. }
  956. return (curSection && curSection.editable) ? curSection : null;
  957. },
  958. update: function(full)
  959. {
  960. if (this.styleRule.selectorText)
  961. this._selectorElement.textContent = this.styleRule.selectorText;
  962. this._markSelectorMatches();
  963. if (full) {
  964. this.propertiesTreeOutline.removeChildren();
  965. this.populated = false;
  966. } else {
  967. var child = this.propertiesTreeOutline.children[0];
  968. while (child) {
  969. child.overloaded = this.isPropertyOverloaded(child.name, child.isShorthand);
  970. child = child.traverseNextTreeElement(false, null, true);
  971. }
  972. }
  973. this.afterUpdate();
  974. },
  975. afterUpdate: function()
  976. {
  977. if (this._afterUpdate) {
  978. this._afterUpdate(this);
  979. delete this._afterUpdate;
  980. }
  981. },
  982. onpopulate: function()
  983. {
  984. var style = this.styleRule.style;
  985. var allProperties = style.allProperties;
  986. this.uniqueProperties = [];
  987. var styleHasEditableSource = this.editable && !!style.range;
  988. if (styleHasEditableSource) {
  989. for (var i = 0; i < allProperties.length; ++i) {
  990. var property = allProperties[i];
  991. this.uniqueProperties.push(property);
  992. if (property.styleBased)
  993. continue;
  994. var isShorthand = !!WebInspector.CSSMetadata.cssPropertiesMetainfo.longhands(property.name);
  995. var inherited = this.isPropertyInherited(property.name);
  996. var overloaded = property.inactive || this.isPropertyOverloaded(property.name);
  997. var item = new WebInspector.StylePropertyTreeElement(this._parentPane, this.styleRule, style, property, isShorthand, inherited, overloaded);
  998. this.propertiesTreeOutline.appendChild(item);
  999. }
  1000. return;
  1001. }
  1002. var generatedShorthands = {};
  1003. // For style-based properties, generate shorthands with values when possible.
  1004. for (var i = 0; i < allProperties.length; ++i) {
  1005. var property = allProperties[i];
  1006. this.uniqueProperties.push(property);
  1007. var isShorthand = !!WebInspector.CSSMetadata.cssPropertiesMetainfo.longhands(property.name);
  1008. // For style-based properties, try generating shorthands.
  1009. var shorthands = isShorthand ? null : WebInspector.CSSMetadata.cssPropertiesMetainfo.shorthands(property.name);
  1010. var shorthandPropertyAvailable = false;
  1011. for (var j = 0; shorthands && !shorthandPropertyAvailable && j < shorthands.length; ++j) {
  1012. var shorthand = shorthands[j];
  1013. if (shorthand in generatedShorthands) {
  1014. shorthandPropertyAvailable = true;
  1015. continue; // There already is a shorthand this longhands falls under.
  1016. }
  1017. if (style.getLiveProperty(shorthand)) {
  1018. shorthandPropertyAvailable = true;
  1019. continue; // There is an explict shorthand property this longhands falls under.
  1020. }
  1021. if (!style.shorthandValue(shorthand)) {
  1022. shorthandPropertyAvailable = false;
  1023. continue; // Never generate synthetic shorthands when no value is available.
  1024. }
  1025. // Generate synthetic shorthand we have a value for.
  1026. var shorthandProperty = new WebInspector.CSSProperty(style, style.allProperties.length, shorthand, style.shorthandValue(shorthand), "", "style", true, true);
  1027. var overloaded = property.inactive || this.isPropertyOverloaded(property.name, true);
  1028. var item = new WebInspector.StylePropertyTreeElement(this._parentPane, this.styleRule, style, shorthandProperty, /* isShorthand */ true, /* inherited */ false, overloaded);
  1029. this.propertiesTreeOutline.appendChild(item);
  1030. generatedShorthands[shorthand] = shorthandProperty;
  1031. shorthandPropertyAvailable = true;
  1032. }
  1033. if (shorthandPropertyAvailable)
  1034. continue; // Shorthand for the property found.
  1035. var inherited = this.isPropertyInherited(property.name);
  1036. var overloaded = property.inactive || this.isPropertyOverloaded(property.name, isShorthand);
  1037. var item = new WebInspector.StylePropertyTreeElement(this._parentPane, this.styleRule, style, property, isShorthand, inherited, overloaded);
  1038. this.propertiesTreeOutline.appendChild(item);
  1039. }
  1040. },
  1041. findTreeElementWithName: function(name)
  1042. {
  1043. var treeElement = this.propertiesTreeOutline.children[0];
  1044. while (treeElement) {
  1045. if (treeElement.name === name)
  1046. return treeElement;
  1047. treeElement = treeElement.traverseNextTreeElement(true, null, true);
  1048. }
  1049. return null;
  1050. },
  1051. _markSelectorMatches: function()
  1052. {
  1053. var rule = this.styleRule.rule;
  1054. if (!rule)
  1055. return;
  1056. var matchingSelectors = rule.matchingSelectors;
  1057. // .selector is rendered as non-affecting selector by default.
  1058. if (this.noAffect || matchingSelectors)
  1059. this._selectorElement.className = "selector";
  1060. if (!matchingSelectors)
  1061. return;
  1062. var selectors = rule.selectors;
  1063. var fragment = document.createDocumentFragment();
  1064. var currentMatch = 0;
  1065. for (var i = 0, lastSelectorIndex = selectors.length - 1; i <= lastSelectorIndex ; ++i) {
  1066. var selectorNode;
  1067. var textNode = document.createTextNode(selectors[i]);
  1068. if (matchingSelectors[currentMatch] === i) {
  1069. ++currentMatch;
  1070. selectorNode = document.createElement("span");
  1071. selectorNode.className = "selector-matches";
  1072. selectorNode.appendChild(textNode);
  1073. } else
  1074. selectorNode = textNode;
  1075. fragment.appendChild(selectorNode);
  1076. if (i !== lastSelectorIndex)
  1077. fragment.appendChild(document.createTextNode(", "));
  1078. }
  1079. this._selectorElement.removeChildren();
  1080. this._selectorElement.appendChild(fragment);
  1081. },
  1082. _checkWillCancelEditing: function()
  1083. {
  1084. var willCauseCancelEditing = this._willCauseCancelEditing;
  1085. delete this._willCauseCancelEditing;
  1086. return willCauseCancelEditing;
  1087. },
  1088. _handleSelectorContainerClick: function(event)
  1089. {
  1090. if (this._checkWillCancelEditing() || !this.editable)
  1091. return;
  1092. if (event.target === this._selectorContainer)
  1093. this.addNewBlankProperty(0).startEditing();
  1094. },
  1095. /**
  1096. * @param {number=} index
  1097. */
  1098. addNewBlankProperty: function(index)
  1099. {
  1100. var style = this.styleRule.style;
  1101. var property = style.newBlankProperty(index);
  1102. var item = new WebInspector.StylePropertyTreeElement(this._parentPane, this.styleRule, style, property, false, false, false);
  1103. index = property.index;
  1104. this.propertiesTreeOutline.insertChild(item, index);
  1105. item.listItemElement.textContent = "";
  1106. item._newProperty = true;
  1107. item.updateTitle();
  1108. return item;
  1109. },
  1110. _createRuleOriginNode: function()
  1111. {
  1112. /**
  1113. * @param {string} url
  1114. * @param {number} line
  1115. */
  1116. function linkifyUncopyable(url, line)
  1117. {
  1118. var link = WebInspector.linkifyResourceAsNode(url, line, "", url + ":" + (line + 1));
  1119. link.preferredPanel = "scripts";
  1120. link.classList.add("webkit-html-resource-link");
  1121. link.setAttribute("data-uncopyable", link.textContent);
  1122. link.textContent = "";
  1123. return link;
  1124. }
  1125. if (this.styleRule.sourceURL)
  1126. return this._parentPane._linkifier.linkifyCSSLocation(this.rule.id.styleSheetId, this.rule.rawLocation) || linkifyUncopyable(this.styleRule.sourceURL, this.rule.lineNumberInSource());
  1127. if (!this.rule)
  1128. return document.createTextNode("");
  1129. if (this.rule.isUserAgent)
  1130. return document.createTextNode(WebInspector.UIString("user agent stylesheet"));
  1131. if (this.rule.isUser)
  1132. return document.createTextNode(WebInspector.UIString("user stylesheet"));
  1133. if (this.rule.isViaInspector)
  1134. return document.createTextNode(WebInspector.UIString("via inspector"));
  1135. return document.createTextNode("");
  1136. },
  1137. _handleEmptySpaceMouseDown: function(event)
  1138. {
  1139. this._willCauseCancelEditing = this._parentPane._isEditingStyle;
  1140. },
  1141. _handleEmptySpaceClick: function(event)
  1142. {
  1143. if (!this.editable)
  1144. return;
  1145. if (!window.getSelection().isCollapsed)
  1146. return;
  1147. if (this._checkWillCancelEditing())
  1148. return;
  1149. if (event.target.hasStyleClass("header") || this.element.hasStyleClass("read-only") || event.target.enclosingNodeOrSelfWithClass("media")) {
  1150. event.consume();
  1151. return;
  1152. }
  1153. this.expand();
  1154. this.addNewBlankProperty().startEditing();
  1155. },
  1156. _handleSelectorClick: function(event)
  1157. {
  1158. this._startEditingOnMouseEvent();
  1159. event.consume(true);
  1160. },
  1161. _startEditingOnMouseEvent: function()
  1162. {
  1163. if (!this.editable)
  1164. return;
  1165. if (!this.rule && this.propertiesTreeOutline.children.length === 0) {
  1166. this.expand();
  1167. this.addNewBlankProperty().startEditing();
  1168. return;
  1169. }
  1170. if (!this.rule)
  1171. return;
  1172. this.startEditingSelector();
  1173. },
  1174. startEditingSelector: function()
  1175. {
  1176. var element = this._selectorElement;
  1177. if (WebInspector.isBeingEdited(element))
  1178. return;
  1179. element.scrollIntoViewIfNeeded(false);
  1180. element.textContent = element.textContent; // Reset selector marks in group.
  1181. var config = new WebInspector.EditingConfig(this.editingSelectorCommitted.bind(this), this.editingSelectorCancelled.bind(this));
  1182. WebInspector.startEditing(this._selectorElement, config);
  1183. window.getSelection().setBaseAndExtent(element, 0, element, 1);
  1184. },
  1185. _moveEditorFromSelector: function(moveDirection)
  1186. {
  1187. this._markSelectorMatches();
  1188. if (!moveDirection)
  1189. return;
  1190. if (moveDirection === "forward") {
  1191. this.expand();
  1192. var firstChild = this.propertiesTreeOutline.children[0];
  1193. while (firstChild && firstChild.inherited)
  1194. firstChild = firstChild.nextSibling;
  1195. if (!firstChild)
  1196. this.addNewBlankProperty().startEditing();
  1197. else
  1198. firstChild.startEditing(firstChild.nameElement);
  1199. } else {
  1200. var previousSection = this.previousEditableSibling();
  1201. if (!previousSection)
  1202. return;
  1203. previousSection.expand();
  1204. previousSection.addNewBlankProperty().startEditing();
  1205. }
  1206. },
  1207. editingSelectorCommitted: function(element, newContent, oldContent, context, moveDirection)
  1208. {
  1209. if (newContent)
  1210. newContent = newContent.trim();
  1211. if (newContent === oldContent) {
  1212. // Revert to a trimmed version of the selector if need be.
  1213. this._selectorElement.textContent = newContent;
  1214. return this._moveEditorFromSelector(moveDirection);
  1215. }
  1216. var selectedNode = this._parentPane.node;
  1217. function successCallback(newRule, doesAffectSelectedNode)
  1218. {
  1219. if (!doesAffectSelectedNode) {
  1220. this.noAffect = true;
  1221. this.element.addStyleClass("no-affect");
  1222. } else {
  1223. delete this.noAffect;
  1224. this.element.removeStyleClass("no-affect");
  1225. }
  1226. this.rule = newRule;
  1227. this.styleRule = { section: this, style: newRule.style, selectorText: newRule.selectorText, media: newRule.media, sourceURL: newRule.resourceURL(), rule: newRule };
  1228. this._parentPane.update(selectedNode);
  1229. finishOperationAndMoveEditor.call(this, moveDirection);
  1230. }
  1231. function finishOperationAndMoveEditor(direction)
  1232. {
  1233. delete this._parentPane._userOperation;
  1234. this._moveEditorFromSelector(direction);
  1235. }
  1236. // This gets deleted in finishOperationAndMoveEditor(), which is called both on success and failure.
  1237. this._parentPane._userOperation = true;
  1238. WebInspector.cssModel.setRuleSelector(this.rule.id, selectedNode ? selectedNode.id : 0, newContent, successCallback.bind(this), finishOperationAndMoveEditor.bind(this, moveDirection));
  1239. },
  1240. editingSelectorCancelled: function()
  1241. {
  1242. // Do nothing but mark the selectors in group if necessary.
  1243. // This is overridden by BlankStylePropertiesSection.
  1244. this._markSelectorMatches();
  1245. },
  1246. __proto__: WebInspector.PropertiesSection.prototype
  1247. }
  1248. /**
  1249. * @constructor
  1250. * @extends {WebInspector.PropertiesSection}
  1251. * @param {!WebInspector.StylesSidebarPane} stylesPane
  1252. * @param {!Object} styleRule
  1253. * @param {!Object.<string, boolean>} usedProperties
  1254. */
  1255. WebInspector.ComputedStylePropertiesSection = function(stylesPane, styleRule, usedProperties)
  1256. {
  1257. WebInspector.PropertiesSection.call(this, "");
  1258. this.headerElement.addStyleClass("hidden");
  1259. this.element.className = "styles-section monospace first-styles-section read-only computed-style";
  1260. this._stylesPane = stylesPane;
  1261. this.styleRule = styleRule;
  1262. this._usedProperties = usedProperties;
  1263. this._alwaysShowComputedProperties = { "display": true, "height": true, "width": true };
  1264. this.computedStyle = true;
  1265. this._propertyTreeElements = {};
  1266. this._expandedPropertyNames = {};
  1267. }
  1268. WebInspector.ComputedStylePropertiesSection.prototype = {
  1269. collapse: function(dontRememberState)
  1270. {
  1271. // Overriding with empty body.
  1272. },
  1273. _isPropertyInherited: function(propertyName)
  1274. {
  1275. var canonicalName = WebInspector.CSSMetadata.canonicalPropertyName(propertyName);
  1276. return !(canonicalName in this._usedProperties) && !(canonicalName in this._alwaysShowComputedProperties);
  1277. },
  1278. update: function()
  1279. {
  1280. this._expandedPropertyNames = {};
  1281. for (var name in this._propertyTreeElements) {
  1282. if (this._propertyTreeElements[name].expanded)
  1283. this._expandedPropertyNames[name] = true;
  1284. }
  1285. this._propertyTreeElements = {};
  1286. this.propertiesTreeOutline.removeChildren();
  1287. this.populated = false;
  1288. },
  1289. onpopulate: function()
  1290. {
  1291. function sorter(a, b)
  1292. {
  1293. return a.name.compareTo(b.name);
  1294. }
  1295. var style = this.styleRule.style;
  1296. if (!style)
  1297. return;
  1298. var uniqueProperties = [];
  1299. var allProperties = style.allProperties;
  1300. for (var i = 0; i < allProperties.length; ++i)
  1301. uniqueProperties.push(allProperties[i]);
  1302. uniqueProperties.sort(sorter);
  1303. this._propertyTreeElements = {};
  1304. for (var i = 0; i < uniqueProperties.length; ++i) {
  1305. var property = uniqueProperties[i];
  1306. var inherited = this._isPropertyInherited(property.name);
  1307. var item = new WebInspector.ComputedStylePropertyTreeElement(this._stylesPane, this.styleRule, style, property, inherited);
  1308. this.propertiesTreeOutline.appendChild(item);
  1309. this._propertyTreeElements[property.name] = item;
  1310. }
  1311. },
  1312. rebuildComputedTrace: function(sections)
  1313. {
  1314. for (var i = 0; i < sections.length; ++i) {
  1315. var section = sections[i];
  1316. if (section.computedStyle || section.isBlank)
  1317. continue;
  1318. for (var j = 0; j < section.uniqueProperties.length; ++j) {
  1319. var property = section.uniqueProperties[j];
  1320. if (property.disabled)
  1321. continue;
  1322. if (section.isInherited && !WebInspector.CSSMetadata.isPropertyInherited(property.name))
  1323. continue;
  1324. var treeElement = this._propertyTreeElements[property.name.toLowerCase()];
  1325. if (treeElement) {
  1326. var fragment = document.createDocumentFragment();
  1327. var selector = fragment.createChild("span");
  1328. selector.style.color = "gray";
  1329. selector.textContent = section.styleRule.selectorText;
  1330. fragment.appendChild(document.createTextNode(" - " + property.value + " "));
  1331. var subtitle = fragment.createChild("span");
  1332. subtitle.style.float = "right";
  1333. subtitle.appendChild(section._createRuleOriginNode());
  1334. var childElement = new TreeElement(fragment, null, false);
  1335. treeElement.appendChild(childElement);
  1336. if (property.inactive || section.isPropertyOverloaded(property.name))
  1337. childElement.listItemElement.addStyleClass("overloaded");
  1338. if (!property.parsedOk) {
  1339. childElement.listItemElement.addStyleClass("not-parsed-ok");
  1340. childElement.listItemElement.insertBefore(WebInspector.StylesSidebarPane.createExclamationMark(property), childElement.listItemElement.firstChild);
  1341. if (WebInspector.StylesSidebarPane._ignoreErrorsForProperty(property))
  1342. childElement.listItemElement.addStyleClass("has-ignorable-error");
  1343. }
  1344. }
  1345. }
  1346. }
  1347. // Restore expanded state after update.
  1348. for (var name in this._expandedPropertyNames) {
  1349. if (name in this._propertyTreeElements)
  1350. this._propertyTreeElements[name].expand();
  1351. }
  1352. },
  1353. __proto__: WebInspector.PropertiesSection.prototype
  1354. }
  1355. /**
  1356. * @constructor
  1357. * @extends {WebInspector.StylePropertiesSection}
  1358. * @param {WebInspector.StylesSidebarPane} stylesPane
  1359. * @param {string} defaultSelectorText
  1360. */
  1361. WebInspector.BlankStylePropertiesSection = function(stylesPane, defaultSelectorText)
  1362. {
  1363. WebInspector.StylePropertiesSection.call(this, stylesPane, {selectorText: defaultSelectorText, rule: {isViaInspector: true}}, true, false, false);
  1364. this.element.addStyleClass("blank-section");
  1365. }
  1366. WebInspector.BlankStylePropertiesSection.prototype = {
  1367. get isBlank()
  1368. {
  1369. return !this._normal;
  1370. },
  1371. expand: function()
  1372. {
  1373. if (!this.isBlank)
  1374. WebInspector.StylePropertiesSection.prototype.expand.call(this);
  1375. },
  1376. editingSelectorCommitted: function(element, newContent, oldContent, context, moveDirection)
  1377. {
  1378. if (!this.isBlank) {
  1379. WebInspector.StylePropertiesSection.prototype.editingSelectorCommitted.call(this, element, newContent, oldContent, context, moveDirection);
  1380. return;
  1381. }
  1382. function successCallback(newRule, doesSelectorAffectSelectedNode)
  1383. {
  1384. var styleRule = { section: this, style: newRule.style, selectorText: newRule.selectorText, sourceURL: newRule.resourceURL(), rule: newRule };
  1385. this.makeNormal(styleRule);
  1386. if (!doesSelectorAffectSelectedNode) {
  1387. this.noAffect = true;
  1388. this.element.addStyleClass("no-affect");
  1389. }
  1390. this._selectorRefElement.removeChildren();
  1391. this._selectorRefElement.appendChild(this._createRuleOriginNode());
  1392. this.expand();
  1393. if (this.element.parentElement) // Might have been detached already.
  1394. this._moveEditorFromSelector(moveDirection);
  1395. this._markSelectorMatches();
  1396. delete this._parentPane._userOperation;
  1397. }
  1398. if (newContent)
  1399. newContent = newContent.trim();
  1400. this._parentPane._userOperation = true;
  1401. WebInspector.cssModel.addRule(this.pane.node.id, newContent, successCallback.bind(this), this.editingSelectorCancelled.bind(this));
  1402. },
  1403. editingSelectorCancelled: function()
  1404. {
  1405. delete this._parentPane._userOperation;
  1406. if (!this.isBlank) {
  1407. WebInspector.StylePropertiesSection.prototype.editingSelectorCancelled.call(this);
  1408. return;
  1409. }
  1410. this.pane.removeSection(this);
  1411. },
  1412. makeNormal: function(styleRule)
  1413. {
  1414. this.element.removeStyleClass("blank-section");
  1415. this.styleRule = styleRule;
  1416. this.rule = styleRule.rule;
  1417. // FIXME: replace this instance by a normal WebInspector.StylePropertiesSection.
  1418. this._normal = true;
  1419. },
  1420. __proto__: WebInspector.StylePropertiesSection.prototype
  1421. }
  1422. /**
  1423. * @constructor
  1424. * @extends {TreeElement}
  1425. * @param {Object} styleRule
  1426. * @param {WebInspector.CSSStyleDeclaration} style
  1427. * @param {WebInspector.CSSProperty} property
  1428. * @param {boolean} inherited
  1429. * @param {boolean} overloaded
  1430. * @param {boolean} hasChildren
  1431. */
  1432. WebInspector.StylePropertyTreeElementBase = function(styleRule, style, property, inherited, overloaded, hasChildren)
  1433. {
  1434. this._styleRule = styleRule;
  1435. this.style = style;
  1436. this.property = property;
  1437. this._inherited = inherited;
  1438. this._overloaded = overloaded;
  1439. // Pass an empty title, the title gets made later in onattach.
  1440. TreeElement.call(this, "", null, hasChildren);
  1441. this.selectable = false;
  1442. }
  1443. WebInspector.StylePropertyTreeElementBase.prototype = {
  1444. /**
  1445. * @return {?WebInspector.DOMNode}
  1446. */
  1447. node: function()
  1448. {
  1449. return null; // Overridden by ancestors.
  1450. },
  1451. /**
  1452. * @return {?WebInspector.StylesSidebarPane}
  1453. */
  1454. editablePane: function()
  1455. {
  1456. return null; // Overridden by ancestors.
  1457. },
  1458. get inherited()
  1459. {
  1460. return this._inherited;
  1461. },
  1462. hasIgnorableError: function()
  1463. {
  1464. return !this.parsedOk && WebInspector.StylesSidebarPane._ignoreErrorsForProperty(this.property);
  1465. },
  1466. set inherited(x)
  1467. {
  1468. if (x === this._inherited)
  1469. return;
  1470. this._inherited = x;
  1471. this.updateState();
  1472. },
  1473. get overloaded()
  1474. {
  1475. return this._overloaded;
  1476. },
  1477. set overloaded(x)
  1478. {
  1479. if (x === this._overloaded)
  1480. return;
  1481. this._overloaded = x;
  1482. this.updateState();
  1483. },
  1484. get disabled()
  1485. {
  1486. return this.property.disabled;
  1487. },
  1488. get name()
  1489. {
  1490. if (!this.disabled || !this.property.text)
  1491. return this.property.name;
  1492. var text = this.property.text;
  1493. var index = text.indexOf(":");
  1494. if (index < 1)
  1495. return this.property.name;
  1496. text = text.substring(0, index).trim();
  1497. if (text.startsWith("/*"))
  1498. text = text.substring(2).trim();
  1499. return text;
  1500. },
  1501. get priority()
  1502. {
  1503. if (this.disabled)
  1504. return ""; // rely upon raw text to render it in the value field
  1505. return this.property.priority;
  1506. },
  1507. get value()
  1508. {
  1509. if (!this.disabled || !this.property.text)
  1510. return this.property.value;
  1511. var match = this.property.text.match(/(.*);\s*/);
  1512. if (!match || !match[1])
  1513. return this.property.value;
  1514. var text = match[1];
  1515. var index = text.indexOf(":");
  1516. if (index < 1)
  1517. return this.property.value;
  1518. return text.substring(index + 1).trim();
  1519. },
  1520. get parsedOk()
  1521. {
  1522. return this.property.parsedOk;
  1523. },
  1524. onattach: function()
  1525. {
  1526. this.updateTitle();
  1527. },
  1528. updateTitle: function()
  1529. {
  1530. var value = this.value;
  1531. this.updateState();
  1532. var nameElement = document.createElement("span");
  1533. nameElement.className = "webkit-css-property";
  1534. nameElement.textContent = this.name;
  1535. nameElement.title = this.property.propertyText;
  1536. this.nameElement = nameElement;
  1537. this._expandElement = document.createElement("span");
  1538. this._expandElement.className = "expand-element";
  1539. var valueElement = document.createElement("span");
  1540. valueElement.className = "value";
  1541. this.valueElement = valueElement;
  1542. var cf = WebInspector.Color.Format;
  1543. if (value) {
  1544. var self = this;
  1545. function processValue(regex, processor, nextProcessor, valueText)
  1546. {
  1547. var container = document.createDocumentFragment();
  1548. var items = valueText.replace(regex, "\0$1\0").split("\0");
  1549. for (var i = 0; i < items.length; ++i) {
  1550. if ((i % 2) === 0) {
  1551. if (nextProcessor)
  1552. container.appendChild(nextProcessor(items[i]));
  1553. else
  1554. container.appendChild(document.createTextNode(items[i]));
  1555. } else {
  1556. var processedNode = processor(items[i]);
  1557. if (processedNode)
  1558. container.appendChild(processedNode);
  1559. }
  1560. }
  1561. return container;
  1562. }
  1563. function linkifyURL(url)
  1564. {
  1565. var hrefUrl = url;
  1566. var match = hrefUrl.match(/['"]?([^'"]+)/);
  1567. if (match)
  1568. hrefUrl = match[1];
  1569. var container = document.createDocumentFragment();
  1570. container.appendChild(document.createTextNode("url("));
  1571. if (self._styleRule.sourceURL)
  1572. hrefUrl = WebInspector.ParsedURL.completeURL(self._styleRule.sourceURL, hrefUrl);
  1573. else if (self.node())
  1574. hrefUrl = self.node().resolveURL(hrefUrl);
  1575. var hasResource = !!WebInspector.resourceForURL(hrefUrl);
  1576. // FIXME: WebInspector.linkifyURLAsNode() should really use baseURI.
  1577. container.appendChild(WebInspector.linkifyURLAsNode(hrefUrl, url, undefined, !hasResource));
  1578. container.appendChild(document.createTextNode(")"));
  1579. return container;
  1580. }
  1581. function processColor(text)
  1582. {
  1583. var color = WebInspector.Color.parse(text);
  1584. // We can be called with valid non-color values of |text| (like 'none' from border style)
  1585. if (!color)
  1586. return document.createTextNode(text);
  1587. var format = WebInspector.StylesSidebarPane._colorFormat(color);
  1588. var spectrumHelper = self.editablePane() && self.editablePane()._spectrumHelper;
  1589. var spectrum = spectrumHelper ? spectrumHelper.spectrum() : null;
  1590. var colorSwatch = new WebInspector.ColorSwatch();
  1591. colorSwatch.setColorString(text);
  1592. colorSwatch.element.addEventListener("click", swatchClick, false);
  1593. var scrollerElement;
  1594. function spectrumChanged(e)
  1595. {
  1596. var colorString = /** @type {string} */ (e.data);
  1597. spectrum.displayText = colorString;
  1598. colorValueElement.textContent = colorString;
  1599. colorSwatch.setColorString(colorString);
  1600. self.applyStyleText(nameElement.textContent + ": " + valueElement.textContent, false, false, false);
  1601. }
  1602. function spectrumHidden(event)
  1603. {
  1604. if (scrollerElement)
  1605. scrollerElement.removeEventListener("scroll", repositionSpectrum, false);
  1606. var commitEdit = event.data;
  1607. var propertyText = !commitEdit && self.originalPropertyText ? self.originalPropertyText : (nameElement.textContent + ": " + valueElement.textContent);
  1608. self.applyStyleText(propertyText, true, true, false);
  1609. spectrum.removeEventListener(WebInspector.Spectrum.Events.ColorChanged, spectrumChanged);
  1610. spectrumHelper.removeEventListener(WebInspector.SpectrumPopupHelper.Events.Hidden, spectrumHidden);
  1611. delete self.editablePane()._isEditingStyle;
  1612. delete self.originalPropertyText;
  1613. }
  1614. function repositionSpectrum()
  1615. {
  1616. spectrumHelper.reposition(colorSwatch.element);
  1617. }
  1618. function swatchClick(e)
  1619. {
  1620. // Shift + click toggles color formats.
  1621. // Click opens colorpicker, only if the element is not in computed styles section.
  1622. if (!spectrumHelper || e.shiftKey)
  1623. changeColorDisplay(e);
  1624. else {
  1625. var visible = spectrumHelper.toggle(colorSwatch.element, color, format);
  1626. if (visible) {
  1627. spectrum.displayText = color.toString(format);
  1628. self.originalPropertyText = self.property.propertyText;
  1629. self.editablePane()._isEditingStyle = true;
  1630. spectrum.addEventListener(WebInspector.Spectrum.Events.ColorChanged, spectrumChanged);
  1631. spectrumHelper.addEventListener(WebInspector.SpectrumPopupHelper.Events.Hidden, spectrumHidden);
  1632. scrollerElement = colorSwatch.element.enclosingNodeOrSelfWithClass("scroll-target");
  1633. if (scrollerElement)
  1634. scrollerElement.addEventListener("scroll", repositionSpectrum, false);
  1635. else
  1636. console.error("Unable to handle color picker scrolling");
  1637. }
  1638. }
  1639. e.consume(true);
  1640. }
  1641. var colorValueElement = document.createElement("span");
  1642. colorValueElement.textContent = color.toString(format);
  1643. function nextFormat(curFormat)
  1644. {
  1645. // The format loop is as follows:
  1646. // * original
  1647. // * rgb(a)
  1648. // * hsl(a)
  1649. // * nickname (if the color has a nickname)
  1650. // * if the color is simple:
  1651. // - shorthex (if has short hex)
  1652. // - hex
  1653. switch (curFormat) {
  1654. case cf.Original:
  1655. return !color.hasAlpha() ? cf.RGB : cf.RGBA;
  1656. case cf.RGB:
  1657. case cf.RGBA:
  1658. return !color.hasAlpha() ? cf.HSL : cf.HSLA;
  1659. case cf.HSL:
  1660. case cf.HSLA:
  1661. if (color.nickname())
  1662. return cf.Nickname;
  1663. if (!color.hasAlpha())
  1664. return color.canBeShortHex() ? cf.ShortHEX : cf.HEX;
  1665. else
  1666. return cf.Original;
  1667. case cf.ShortHEX:
  1668. return cf.HEX;
  1669. case cf.HEX:
  1670. return cf.Original;
  1671. case cf.Nickname:
  1672. if (!color.hasAlpha())
  1673. return color.canBeShortHex() ? cf.ShortHEX : cf.HEX;
  1674. else
  1675. return cf.Original;
  1676. default:
  1677. return cf.RGBA;
  1678. }
  1679. }
  1680. function changeColorDisplay(event)
  1681. {
  1682. do {
  1683. format = nextFormat(format);
  1684. var currentValue = color.toString(format);
  1685. } while (currentValue === colorValueElement.textContent);
  1686. colorValueElement.textContent = currentValue;
  1687. }
  1688. var container = document.createElement("nobr");
  1689. container.appendChild(colorSwatch.element);
  1690. container.appendChild(colorValueElement);
  1691. return container;
  1692. }
  1693. var colorProcessor = processValue.bind(window, WebInspector.StylesSidebarPane._colorRegex, processColor, null);
  1694. valueElement.appendChild(processValue(/url\(\s*([^)]+)\s*\)/g, linkifyURL.bind(this), WebInspector.CSSMetadata.isColorAwareProperty(self.name) && self.parsedOk ? colorProcessor : null, value));
  1695. }
  1696. this.listItemElement.removeChildren();
  1697. nameElement.normalize();
  1698. valueElement.normalize();
  1699. if (!this.treeOutline)
  1700. return;
  1701. this.listItemElement.appendChild(nameElement);
  1702. this.listItemElement.appendChild(document.createTextNode(": "));
  1703. this.listItemElement.appendChild(this._expandElement);
  1704. this.listItemElement.appendChild(valueElement);
  1705. this.listItemElement.appendChild(document.createTextNode(";"));
  1706. if (!this.parsedOk) {
  1707. // Avoid having longhands under an invalid shorthand.
  1708. this.hasChildren = false;
  1709. this.listItemElement.addStyleClass("not-parsed-ok");
  1710. // Add a separate exclamation mark IMG element with a tooltip.
  1711. this.listItemElement.insertBefore(WebInspector.StylesSidebarPane.createExclamationMark(this.property), this.listItemElement.firstChild);
  1712. }
  1713. if (this.property.inactive)
  1714. this.listItemElement.addStyleClass("inactive");
  1715. },
  1716. updateState: function()
  1717. {
  1718. if (!this.listItemElement)
  1719. return;
  1720. if (this.style.isPropertyImplicit(this.name))
  1721. this.listItemElement.addStyleClass("implicit");
  1722. else
  1723. this.listItemElement.removeStyleClass("implicit");
  1724. if (this.hasIgnorableError())
  1725. this.listItemElement.addStyleClass("has-ignorable-error");
  1726. else
  1727. this.listItemElement.removeStyleClass("has-ignorable-error");
  1728. if (this.inherited)
  1729. this.listItemElement.addStyleClass("inherited");
  1730. else
  1731. this.listItemElement.removeStyleClass("inherited");
  1732. if (this.overloaded)
  1733. this.listItemElement.addStyleClass("overloaded");
  1734. else
  1735. this.listItemElement.removeStyleClass("overloaded");
  1736. if (this.disabled)
  1737. this.listItemElement.addStyleClass("disabled");
  1738. else
  1739. this.listItemElement.removeStyleClass("disabled");
  1740. },
  1741. __proto__: TreeElement.prototype
  1742. }
  1743. /**
  1744. * @constructor
  1745. * @extends {WebInspector.StylePropertyTreeElementBase}
  1746. * @param {WebInspector.StylesSidebarPane} stylesPane
  1747. * @param {Object} styleRule
  1748. * @param {WebInspector.CSSStyleDeclaration} style
  1749. * @param {WebInspector.CSSProperty} property
  1750. * @param {boolean} inherited
  1751. */
  1752. WebInspector.ComputedStylePropertyTreeElement = function(stylesPane, styleRule, style, property, inherited)
  1753. {
  1754. WebInspector.StylePropertyTreeElementBase.call(this, styleRule, style, property, inherited, false, false);
  1755. this._stylesPane = stylesPane;
  1756. }
  1757. WebInspector.ComputedStylePropertyTreeElement.prototype = {
  1758. /**
  1759. * @return {?WebInspector.DOMNode}
  1760. */
  1761. node: function()
  1762. {
  1763. return this._stylesPane.node;
  1764. },
  1765. /**
  1766. * @return {?WebInspector.StylesSidebarPane}
  1767. */
  1768. editablePane: function()
  1769. {
  1770. return null;
  1771. },
  1772. __proto__: WebInspector.StylePropertyTreeElementBase.prototype
  1773. }
  1774. /**
  1775. * @constructor
  1776. * @extends {WebInspector.StylePropertyTreeElementBase}
  1777. * @param {?WebInspector.StylesSidebarPane} stylesPane
  1778. * @param {Object} styleRule
  1779. * @param {WebInspector.CSSStyleDeclaration} style
  1780. * @param {WebInspector.CSSProperty} property
  1781. * @param {boolean} isShorthand
  1782. * @param {boolean} inherited
  1783. * @param {boolean} overloaded
  1784. */
  1785. WebInspector.StylePropertyTreeElement = function(stylesPane, styleRule, style, property, isShorthand, inherited, overloaded)
  1786. {
  1787. WebInspector.StylePropertyTreeElementBase.call(this, styleRule, style, property, inherited, overloaded, isShorthand);
  1788. this._parentPane = stylesPane;
  1789. this.isShorthand = isShorthand;
  1790. }
  1791. WebInspector.StylePropertyTreeElement.prototype = {
  1792. /**
  1793. * @return {?WebInspector.DOMNode}
  1794. */
  1795. node: function()
  1796. {
  1797. return this._parentPane.node;
  1798. },
  1799. /**
  1800. * @return {?WebInspector.StylesSidebarPane}
  1801. */
  1802. editablePane: function()
  1803. {
  1804. return this._parentPane;
  1805. },
  1806. /**
  1807. * @return {WebInspector.StylePropertiesSection}
  1808. */
  1809. section: function()
  1810. {
  1811. return this.treeOutline && this.treeOutline.section;
  1812. },
  1813. _updatePane: function(userCallback)
  1814. {
  1815. var section = this.section();
  1816. if (section && section.pane)
  1817. section.pane._refreshUpdate(section, false, userCallback);
  1818. else {
  1819. if (userCallback)
  1820. userCallback();
  1821. }
  1822. },
  1823. toggleEnabled: function(event)
  1824. {
  1825. var disabled = !event.target.checked;
  1826. function callback(newStyle)
  1827. {
  1828. if (!newStyle)
  1829. return;
  1830. newStyle.parentRule = this.style.parentRule;
  1831. this.style = newStyle;
  1832. this._styleRule.style = newStyle;
  1833. var section = this.section();
  1834. if (section && section.pane)
  1835. section.pane.dispatchEventToListeners("style property toggled");
  1836. this._updatePane();
  1837. delete this._parentPane._userOperation;
  1838. }
  1839. this._parentPane._userOperation = true;
  1840. this.property.setDisabled(disabled, callback.bind(this));
  1841. event.consume();
  1842. },
  1843. onpopulate: function()
  1844. {
  1845. // Only populate once and if this property is a shorthand.
  1846. if (this.children.length || !this.isShorthand)
  1847. return;
  1848. var longhandProperties = this.style.longhandProperties(this.name);
  1849. for (var i = 0; i < longhandProperties.length; ++i) {
  1850. var name = longhandProperties[i].name;
  1851. var section = this.section();
  1852. if (section) {
  1853. var inherited = section.isPropertyInherited(name);
  1854. var overloaded = section.isPropertyOverloaded(name);
  1855. }
  1856. var liveProperty = this.style.getLiveProperty(name);
  1857. if (!liveProperty)
  1858. continue;
  1859. var item = new WebInspector.StylePropertyTreeElement(this._parentPane, this._styleRule, this.style, liveProperty, false, inherited, overloaded);
  1860. this.appendChild(item);
  1861. }
  1862. },
  1863. restoreNameElement: function()
  1864. {
  1865. // Restore <span class="webkit-css-property"> if it doesn't yet exist or was accidentally deleted.
  1866. if (this.nameElement === this.listItemElement.querySelector(".webkit-css-property"))
  1867. return;
  1868. this.nameElement = document.createElement("span");
  1869. this.nameElement.className = "webkit-css-property";
  1870. this.nameElement.textContent = "";
  1871. this.listItemElement.insertBefore(this.nameElement, this.listItemElement.firstChild);
  1872. },
  1873. onattach: function()
  1874. {
  1875. WebInspector.StylePropertyTreeElementBase.prototype.onattach.call(this);
  1876. this.listItemElement.addEventListener("mousedown", this._mouseDown.bind(this));
  1877. this.listItemElement.addEventListener("mouseup", this._resetMouseDownElement.bind(this));
  1878. this.listItemElement.addEventListener("click", this._mouseClick.bind(this));
  1879. },
  1880. _mouseDown: function(event)
  1881. {
  1882. if (this._parentPane) {
  1883. this._parentPane._mouseDownTreeElement = this;
  1884. this._parentPane._mouseDownTreeElementIsName = this._isNameElement(event.target);
  1885. this._parentPane._mouseDownTreeElementIsValue = this._isValueElement(event.target);
  1886. }
  1887. },
  1888. _resetMouseDownElement: function()
  1889. {
  1890. if (this._parentPane) {
  1891. delete this._parentPane._mouseDownTreeElement;
  1892. delete this._parentPane._mouseDownTreeElementIsName;
  1893. delete this._parentPane._mouseDownTreeElementIsValue;
  1894. }
  1895. },
  1896. updateTitle: function()
  1897. {
  1898. WebInspector.StylePropertyTreeElementBase.prototype.updateTitle.call(this);
  1899. if (this.parsedOk && this.section() && this.parent.root) {
  1900. var enabledCheckboxElement = document.createElement("input");
  1901. enabledCheckboxElement.className = "enabled-button";
  1902. enabledCheckboxElement.type = "checkbox";
  1903. enabledCheckboxElement.checked = !this.disabled;
  1904. enabledCheckboxElement.addEventListener("click", this.toggleEnabled.bind(this), false);
  1905. this.listItemElement.insertBefore(enabledCheckboxElement, this.listItemElement.firstChild);
  1906. }
  1907. },
  1908. _mouseClick: function(event)
  1909. {
  1910. if (!window.getSelection().isCollapsed)
  1911. return;
  1912. event.consume(true);
  1913. if (event.target === this.listItemElement) {
  1914. var section = this.section();
  1915. if (!section || !section.editable)
  1916. return;
  1917. if (section._checkWillCancelEditing())
  1918. return;
  1919. section.addNewBlankProperty(this.property.index + 1).startEditing();
  1920. return;
  1921. }
  1922. if (WebInspector.KeyboardShortcut.eventHasCtrlOrMeta(event) && this.section().navigable) {
  1923. this._navigateToSource(event.target);
  1924. return;
  1925. }
  1926. this.startEditing(event.target);
  1927. },
  1928. /**
  1929. * @param {Element} element
  1930. */
  1931. _navigateToSource: function(element)
  1932. {
  1933. console.assert(this.section().navigable);
  1934. var propertyNameClicked = element === this.nameElement;
  1935. var uiLocation = this.property.uiLocation(propertyNameClicked);
  1936. if (!uiLocation)
  1937. return;
  1938. WebInspector.showPanel("scripts").showUILocation(uiLocation);
  1939. },
  1940. _isNameElement: function(element)
  1941. {
  1942. return element.enclosingNodeOrSelfWithClass("webkit-css-property") === this.nameElement;
  1943. },
  1944. _isValueElement: function(element)
  1945. {
  1946. return !!element.enclosingNodeOrSelfWithClass("value");
  1947. },
  1948. startEditing: function(selectElement)
  1949. {
  1950. // FIXME: we don't allow editing of longhand properties under a shorthand right now.
  1951. if (this.parent.isShorthand)
  1952. return;
  1953. if (selectElement === this._expandElement)
  1954. return;
  1955. var section = this.section();
  1956. if (section && !section.editable)
  1957. return;
  1958. if (!selectElement)
  1959. selectElement = this.nameElement; // No arguments passed in - edit the name element by default.
  1960. else
  1961. selectElement = selectElement.enclosingNodeOrSelfWithClass("webkit-css-property") || selectElement.enclosingNodeOrSelfWithClass("value");
  1962. var isEditingName = selectElement === this.nameElement;
  1963. if (!isEditingName) {
  1964. if (selectElement !== this.valueElement) {
  1965. // Click in the LI - start editing value.
  1966. selectElement = this.valueElement;
  1967. }
  1968. this.valueElement.textContent = (!this._newProperty && WebInspector.CSSMetadata.isColorAwareProperty(this.name)) ? formatColors(this.value) : this.value;
  1969. }
  1970. /**
  1971. * @param {string} value
  1972. * @return {string}
  1973. */
  1974. function formatColors(value)
  1975. {
  1976. var result = [];
  1977. var items = value.replace(WebInspector.StylesSidebarPane._colorRegex, "\0$1\0").split("\0");
  1978. for (var i = 0; i < items.length; ++i) {
  1979. var color = WebInspector.Color.parse(items[i]);
  1980. // We can be called with valid non-color values of |text| (like 'none' from border style).
  1981. result.push(color ? color.toString(WebInspector.StylesSidebarPane._colorFormat(color)) : items[i]);
  1982. }
  1983. return result.join("");
  1984. }
  1985. if (WebInspector.isBeingEdited(selectElement))
  1986. return;
  1987. var context = {
  1988. expanded: this.expanded,
  1989. hasChildren: this.hasChildren,
  1990. isEditingName: isEditingName,
  1991. previousContent: selectElement.textContent
  1992. };
  1993. // Lie about our children to prevent expanding on double click and to collapse shorthands.
  1994. this.hasChildren = false;
  1995. if (selectElement.parentElement)
  1996. selectElement.parentElement.addStyleClass("child-editing");
  1997. selectElement.textContent = selectElement.textContent; // remove color swatch and the like
  1998. function pasteHandler(context, event)
  1999. {
  2000. var data = event.clipboardData.getData("Text");
  2001. if (!data)
  2002. return;
  2003. var colonIdx = data.indexOf(":");
  2004. if (colonIdx < 0)
  2005. return;
  2006. var name = data.substring(0, colonIdx).trim();
  2007. var value = data.substring(colonIdx + 1).trim();
  2008. event.preventDefault();
  2009. if (!("originalName" in context)) {
  2010. context.originalName = this.nameElement.textContent;
  2011. context.originalValue = this.valueElement.textContent;
  2012. }
  2013. this.property.name = name;
  2014. this.property.value = value;
  2015. this.nameElement.textContent = name;
  2016. this.valueElement.textContent = value;
  2017. this.nameElement.normalize();
  2018. this.valueElement.normalize();
  2019. this.editingCommitted(null, event.target.textContent, context.previousContent, context, "forward");
  2020. }
  2021. function blurListener(context, event)
  2022. {
  2023. var treeElement = this._parentPane._mouseDownTreeElement;
  2024. var moveDirection = "";
  2025. if (treeElement === this) {
  2026. if (isEditingName && this._parentPane._mouseDownTreeElementIsValue)
  2027. moveDirection = "forward";
  2028. if (!isEditingName && this._parentPane._mouseDownTreeElementIsName)
  2029. moveDirection = "backward";
  2030. }
  2031. this.editingCommitted(null, event.target.textContent, context.previousContent, context, moveDirection);
  2032. }
  2033. delete this.originalPropertyText;
  2034. this._parentPane._isEditingStyle = true;
  2035. if (selectElement.parentElement)
  2036. selectElement.parentElement.scrollIntoViewIfNeeded(false);
  2037. var applyItemCallback = !isEditingName ? this._applyFreeFlowStyleTextEdit.bind(this, true) : undefined;
  2038. this._prompt = new WebInspector.StylesSidebarPane.CSSPropertyPrompt(isEditingName ? WebInspector.CSSMetadata.cssPropertiesMetainfo : WebInspector.CSSMetadata.keywordsForProperty(this.nameElement.textContent), this, isEditingName);
  2039. if (applyItemCallback) {
  2040. this._prompt.addEventListener(WebInspector.TextPrompt.Events.ItemApplied, applyItemCallback, this);
  2041. this._prompt.addEventListener(WebInspector.TextPrompt.Events.ItemAccepted, applyItemCallback, this);
  2042. }
  2043. var proxyElement = this._prompt.attachAndStartEditing(selectElement, blurListener.bind(this, context));
  2044. proxyElement.addEventListener("keydown", this.editingNameValueKeyDown.bind(this, context), false);
  2045. if (isEditingName)
  2046. proxyElement.addEventListener("paste", pasteHandler.bind(this, context));
  2047. window.getSelection().setBaseAndExtent(selectElement, 0, selectElement, 1);
  2048. },
  2049. editingNameValueKeyDown: function(context, event)
  2050. {
  2051. if (event.handled)
  2052. return;
  2053. var isEditingName = context.isEditingName;
  2054. var result;
  2055. function shouldCommitValueSemicolon(text, cursorPosition)
  2056. {
  2057. // FIXME: should this account for semicolons inside comments?
  2058. var openQuote = "";
  2059. for (var i = 0; i < cursorPosition; ++i) {
  2060. var ch = text[i];
  2061. if (ch === "\\" && openQuote !== "")
  2062. ++i; // skip next character inside string
  2063. else if (!openQuote && (ch === "\"" || ch === "'"))
  2064. openQuote = ch;
  2065. else if (openQuote === ch)
  2066. openQuote = "";
  2067. }
  2068. return !openQuote;
  2069. }
  2070. // FIXME: the ":"/";" detection does not work for non-US layouts due to the event being keydown rather than keypress.
  2071. var isFieldInputTerminated = (event.keyCode === WebInspector.KeyboardShortcut.Keys.Semicolon.code) &&
  2072. (isEditingName ? event.shiftKey : (!event.shiftKey && shouldCommitValueSemicolon(event.target.textContent, event.target.selectionLeftOffset())));
  2073. if (isEnterKey(event) || isFieldInputTerminated) {
  2074. // Enter or colon (for name)/semicolon outside of string (for value).
  2075. event.preventDefault();
  2076. result = "forward";
  2077. } else if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Esc.code || event.keyIdentifier === "U+001B")
  2078. result = "cancel";
  2079. else if (!isEditingName && this._newProperty && event.keyCode === WebInspector.KeyboardShortcut.Keys.Backspace.code) {
  2080. // For a new property, when Backspace is pressed at the beginning of new property value, move back to the property name.
  2081. var selection = window.getSelection();
  2082. if (selection.isCollapsed && !selection.focusOffset) {
  2083. event.preventDefault();
  2084. result = "backward";
  2085. }
  2086. } else if (event.keyIdentifier === "U+0009") { // Tab key.
  2087. result = event.shiftKey ? "backward" : "forward";
  2088. event.preventDefault();
  2089. }
  2090. if (result) {
  2091. switch (result) {
  2092. case "cancel":
  2093. this.editingCancelled(null, context);
  2094. break;
  2095. case "forward":
  2096. case "backward":
  2097. this.editingCommitted(null, event.target.textContent, context.previousContent, context, result);
  2098. break;
  2099. }
  2100. event.consume();
  2101. return;
  2102. }
  2103. if (!isEditingName)
  2104. this._applyFreeFlowStyleTextEdit(false);
  2105. },
  2106. _applyFreeFlowStyleTextEdit: function(now)
  2107. {
  2108. if (this._applyFreeFlowStyleTextEditTimer)
  2109. clearTimeout(this._applyFreeFlowStyleTextEditTimer);
  2110. function apply()
  2111. {
  2112. var valueText = this.valueElement.textContent;
  2113. if (valueText.indexOf(";") === -1)
  2114. this.applyStyleText(this.nameElement.textContent + ": " + valueText, false, false, false);
  2115. }
  2116. if (now)
  2117. apply.call(this);
  2118. else
  2119. this._applyFreeFlowStyleTextEditTimer = setTimeout(apply.bind(this), 100);
  2120. },
  2121. kickFreeFlowStyleEditForTest: function()
  2122. {
  2123. this._applyFreeFlowStyleTextEdit(true);
  2124. },
  2125. editingEnded: function(context)
  2126. {
  2127. this._resetMouseDownElement();
  2128. if (this._applyFreeFlowStyleTextEditTimer)
  2129. clearTimeout(this._applyFreeFlowStyleTextEditTimer);
  2130. this.hasChildren = context.hasChildren;
  2131. if (context.expanded)
  2132. this.expand();
  2133. var editedElement = context.isEditingName ? this.nameElement : this.valueElement;
  2134. // The proxyElement has been deleted, no need to remove listener.
  2135. if (editedElement.parentElement)
  2136. editedElement.parentElement.removeStyleClass("child-editing");
  2137. delete this._parentPane._isEditingStyle;
  2138. },
  2139. editingCancelled: function(element, context)
  2140. {
  2141. this._removePrompt();
  2142. this._revertStyleUponEditingCanceled(this.originalPropertyText);
  2143. // This should happen last, as it clears the info necessary to restore the property value after [Page]Up/Down changes.
  2144. this.editingEnded(context);
  2145. },
  2146. _revertStyleUponEditingCanceled: function(originalPropertyText)
  2147. {
  2148. if (typeof originalPropertyText === "string") {
  2149. delete this.originalPropertyText;
  2150. this.applyStyleText(originalPropertyText, true, false, true);
  2151. } else {
  2152. if (this._newProperty)
  2153. this.treeOutline.removeChild(this);
  2154. else
  2155. this.updateTitle();
  2156. }
  2157. },
  2158. _findSibling: function(moveDirection)
  2159. {
  2160. var target = this;
  2161. do {
  2162. target = (moveDirection === "forward" ? target.nextSibling : target.previousSibling);
  2163. } while(target && target.inherited);
  2164. return target;
  2165. },
  2166. editingCommitted: function(element, userInput, previousContent, context, moveDirection)
  2167. {
  2168. this._removePrompt();
  2169. this.editingEnded(context);
  2170. var isEditingName = context.isEditingName;
  2171. // Determine where to move to before making changes
  2172. var createNewProperty, moveToPropertyName, moveToSelector;
  2173. var isDataPasted = "originalName" in context;
  2174. var isDirtyViaPaste = isDataPasted && (this.nameElement.textContent !== context.originalName || this.valueElement.textContent !== context.originalValue);
  2175. var isPropertySplitPaste = isDataPasted && isEditingName && this.valueElement.textContent !== context.originalValue;
  2176. var moveTo = this;
  2177. var moveToOther = (isEditingName ^ (moveDirection === "forward"));
  2178. var abandonNewProperty = this._newProperty && !userInput && (moveToOther || isEditingName);
  2179. if (moveDirection === "forward" && (!isEditingName || isPropertySplitPaste) || moveDirection === "backward" && isEditingName) {
  2180. moveTo = moveTo._findSibling(moveDirection);
  2181. if (moveTo)
  2182. moveToPropertyName = moveTo.name;
  2183. else if (moveDirection === "forward" && (!this._newProperty || userInput))
  2184. createNewProperty = true;
  2185. else if (moveDirection === "backward")
  2186. moveToSelector = true;
  2187. }
  2188. // Make the Changes and trigger the moveToNextCallback after updating.
  2189. var moveToIndex = moveTo && this.treeOutline ? this.treeOutline.children.indexOf(moveTo) : -1;
  2190. var blankInput = /^\s*$/.test(userInput);
  2191. var shouldCommitNewProperty = this._newProperty && (isPropertySplitPaste || moveToOther || (!moveDirection && !isEditingName) || (isEditingName && blankInput));
  2192. var section = this.section();
  2193. if (((userInput !== previousContent || isDirtyViaPaste) && !this._newProperty) || shouldCommitNewProperty) {
  2194. section._afterUpdate = moveToNextCallback.bind(this, this._newProperty, !blankInput, section);
  2195. var propertyText;
  2196. if (blankInput || (this._newProperty && /^\s*$/.test(this.valueElement.textContent)))
  2197. propertyText = "";
  2198. else {
  2199. if (isEditingName)
  2200. propertyText = userInput + ": " + this.property.value;
  2201. else
  2202. propertyText = this.property.name + ": " + userInput;
  2203. }
  2204. this.applyStyleText(propertyText, true, true, false);
  2205. } else {
  2206. if (isEditingName)
  2207. this.property.name = userInput;
  2208. else
  2209. this.property.value = userInput;
  2210. if (!isDataPasted && !this._newProperty)
  2211. this.updateTitle();
  2212. moveToNextCallback.call(this, this._newProperty, false, section);
  2213. }
  2214. // The Callback to start editing the next/previous property/selector.
  2215. function moveToNextCallback(alreadyNew, valueChanged, section)
  2216. {
  2217. if (!moveDirection)
  2218. return;
  2219. // User just tabbed through without changes.
  2220. if (moveTo && moveTo.parent) {
  2221. moveTo.startEditing(!isEditingName ? moveTo.nameElement : moveTo.valueElement);
  2222. return;
  2223. }
  2224. // User has made a change then tabbed, wiping all the original treeElements.
  2225. // Recalculate the new treeElement for the same property we were going to edit next.
  2226. if (moveTo && !moveTo.parent) {
  2227. var propertyElements = section.propertiesTreeOutline.children;
  2228. if (moveDirection === "forward" && blankInput && !isEditingName)
  2229. --moveToIndex;
  2230. if (moveToIndex >= propertyElements.length && !this._newProperty)
  2231. createNewProperty = true;
  2232. else {
  2233. var treeElement = moveToIndex >= 0 ? propertyElements[moveToIndex] : null;
  2234. if (treeElement) {
  2235. var elementToEdit = !isEditingName || isPropertySplitPaste ? treeElement.nameElement : treeElement.valueElement;
  2236. if (alreadyNew && blankInput)
  2237. elementToEdit = moveDirection === "forward" ? treeElement.nameElement : treeElement.valueElement;
  2238. treeElement.startEditing(elementToEdit);
  2239. return;
  2240. } else if (!alreadyNew)
  2241. moveToSelector = true;
  2242. }
  2243. }
  2244. // Create a new attribute in this section (or move to next editable selector if possible).
  2245. if (createNewProperty) {
  2246. if (alreadyNew && !valueChanged && (isEditingName ^ (moveDirection === "backward")))
  2247. return;
  2248. section.addNewBlankProperty().startEditing();
  2249. return;
  2250. }
  2251. if (abandonNewProperty) {
  2252. moveTo = this._findSibling(moveDirection);
  2253. var sectionToEdit = (moveTo || moveDirection === "backward") ? section : section.nextEditableSibling();
  2254. if (sectionToEdit) {
  2255. if (sectionToEdit.rule)
  2256. sectionToEdit.startEditingSelector();
  2257. else
  2258. sectionToEdit._moveEditorFromSelector(moveDirection);
  2259. }
  2260. return;
  2261. }
  2262. if (moveToSelector) {
  2263. if (section.rule)
  2264. section.startEditingSelector();
  2265. else
  2266. section._moveEditorFromSelector(moveDirection);
  2267. }
  2268. }
  2269. },
  2270. _removePrompt: function()
  2271. {
  2272. // BUG 53242. This cannot go into editingEnded(), as it should always happen first for any editing outcome.
  2273. if (this._prompt) {
  2274. this._prompt.detach();
  2275. delete this._prompt;
  2276. }
  2277. },
  2278. _hasBeenModifiedIncrementally: function()
  2279. {
  2280. // New properties applied via up/down or live editing have an originalPropertyText and will be deleted later
  2281. // on, if cancelled, when the empty string gets applied as their style text.
  2282. return typeof this.originalPropertyText === "string" || (!!this.property.propertyText && this._newProperty);
  2283. },
  2284. applyStyleText: function(styleText, updateInterface, majorChange, isRevert)
  2285. {
  2286. function userOperationFinishedCallback(parentPane, updateInterface)
  2287. {
  2288. if (updateInterface)
  2289. delete parentPane._userOperation;
  2290. }
  2291. // Leave a way to cancel editing after incremental changes.
  2292. if (!isRevert && !updateInterface && !this._hasBeenModifiedIncrementally()) {
  2293. // Remember the rule's original CSS text on [Page](Up|Down), so it can be restored
  2294. // if the editing is canceled.
  2295. this.originalPropertyText = this.property.propertyText;
  2296. }
  2297. if (!this.treeOutline)
  2298. return;
  2299. var section = this.section();
  2300. styleText = styleText.replace(/\s/g, " ").trim(); // Replace &nbsp; with whitespace.
  2301. var styleTextLength = styleText.length;
  2302. if (!styleTextLength && updateInterface && !isRevert && this._newProperty && !this._hasBeenModifiedIncrementally()) {
  2303. // The user deleted everything and never applied a new property value via Up/Down scrolling/live editing, so remove the tree element and update.
  2304. this.parent.removeChild(this);
  2305. section.afterUpdate();
  2306. return;
  2307. }
  2308. var currentNode = this._parentPane.node;
  2309. if (updateInterface)
  2310. this._parentPane._userOperation = true;
  2311. function callback(userCallback, originalPropertyText, newStyle)
  2312. {
  2313. if (!newStyle) {
  2314. if (updateInterface) {
  2315. // It did not apply, cancel editing.
  2316. this._revertStyleUponEditingCanceled(originalPropertyText);
  2317. }
  2318. userCallback();
  2319. return;
  2320. }
  2321. if (this._newProperty)
  2322. this._newPropertyInStyle = true;
  2323. newStyle.parentRule = this.style.parentRule;
  2324. this.style = newStyle;
  2325. this.property = newStyle.propertyAt(this.property.index);
  2326. this._styleRule.style = this.style;
  2327. if (section && section.pane)
  2328. section.pane.dispatchEventToListeners("style edited");
  2329. if (updateInterface && currentNode === this.node()) {
  2330. this._updatePane(userCallback);
  2331. return;
  2332. }
  2333. userCallback();
  2334. }
  2335. // Append a ";" if the new text does not end in ";".
  2336. // FIXME: this does not handle trailing comments.
  2337. if (styleText.length && !/;\s*$/.test(styleText))
  2338. styleText += ";";
  2339. var overwriteProperty = !!(!this._newProperty || this._newPropertyInStyle);
  2340. this.property.setText(styleText, majorChange, overwriteProperty, callback.bind(this, userOperationFinishedCallback.bind(null, this._parentPane, updateInterface), this.originalPropertyText));
  2341. },
  2342. ondblclick: function()
  2343. {
  2344. return true; // handled
  2345. },
  2346. isEventWithinDisclosureTriangle: function(event)
  2347. {
  2348. return event.target === this._expandElement;
  2349. },
  2350. __proto__: WebInspector.StylePropertyTreeElementBase.prototype
  2351. }
  2352. /**
  2353. * @constructor
  2354. * @extends {WebInspector.TextPrompt}
  2355. * @param {!WebInspector.CSSMetadata} cssCompletions
  2356. * @param {!WebInspector.StylePropertyTreeElement} sidebarPane
  2357. * @param {boolean} isEditingName
  2358. */
  2359. WebInspector.StylesSidebarPane.CSSPropertyPrompt = function(cssCompletions, sidebarPane, isEditingName)
  2360. {
  2361. // Use the same callback both for applyItemCallback and acceptItemCallback.
  2362. WebInspector.TextPrompt.call(this, this._buildPropertyCompletions.bind(this), WebInspector.StyleValueDelimiters);
  2363. this.setSuggestBoxEnabled("generic-suggest");
  2364. this._cssCompletions = cssCompletions;
  2365. this._sidebarPane = sidebarPane;
  2366. this._isEditingName = isEditingName;
  2367. }
  2368. WebInspector.StylesSidebarPane.CSSPropertyPrompt.prototype = {
  2369. onKeyDown: function(event)
  2370. {
  2371. switch (event.keyIdentifier) {
  2372. case "Up":
  2373. case "Down":
  2374. case "PageUp":
  2375. case "PageDown":
  2376. if (this._handleNameOrValueUpDown(event)) {
  2377. event.preventDefault();
  2378. return;
  2379. }
  2380. break;
  2381. case "Enter":
  2382. if (this.autoCompleteElement && !this.autoCompleteElement.textContent.length) {
  2383. this.tabKeyPressed();
  2384. return;
  2385. }
  2386. break;
  2387. }
  2388. WebInspector.TextPrompt.prototype.onKeyDown.call(this, event);
  2389. },
  2390. onMouseWheel: function(event)
  2391. {
  2392. if (this._handleNameOrValueUpDown(event)) {
  2393. event.consume(true);
  2394. return;
  2395. }
  2396. WebInspector.TextPrompt.prototype.onMouseWheel.call(this, event);
  2397. },
  2398. /** @override */
  2399. tabKeyPressed: function()
  2400. {
  2401. this.acceptAutoComplete();
  2402. // Always tab to the next field.
  2403. return false;
  2404. },
  2405. /**
  2406. * @param {Event} event
  2407. */
  2408. _handleNameOrValueUpDown: function(event)
  2409. {
  2410. function finishHandler(originalValue, replacementString)
  2411. {
  2412. // Synthesize property text disregarding any comments, custom whitespace etc.
  2413. this._sidebarPane.applyStyleText(this._sidebarPane.nameElement.textContent + ": " + this._sidebarPane.valueElement.textContent, false, false, false);
  2414. }
  2415. // Handle numeric value increment/decrement only at this point.
  2416. if (!this._isEditingName && WebInspector.handleElementValueModifications(event, this._sidebarPane.valueElement, finishHandler.bind(this), this._isValueSuggestion.bind(this)))
  2417. return true;
  2418. return false;
  2419. },
  2420. /**
  2421. * @param {string} word
  2422. * @return {boolean}
  2423. */
  2424. _isValueSuggestion: function(word)
  2425. {
  2426. if (!word)
  2427. return false;
  2428. word = word.toLowerCase();
  2429. return this._cssCompletions.keySet().hasOwnProperty(word);
  2430. },
  2431. /**
  2432. * @param {Element} proxyElement
  2433. * @param {Range} wordRange
  2434. * @param {boolean} force
  2435. * @param {function(!Array.<string>, number=)} completionsReadyCallback
  2436. */
  2437. _buildPropertyCompletions: function(proxyElement, wordRange, force, completionsReadyCallback)
  2438. {
  2439. var prefix = wordRange.toString().toLowerCase();
  2440. if (!prefix && !force && (this._isEditingName || proxyElement.textContent.length)) {
  2441. completionsReadyCallback([]);
  2442. return;
  2443. }
  2444. var results = this._cssCompletions.startsWith(prefix);
  2445. var selectedIndex = this._cssCompletions.mostUsedOf(results);
  2446. completionsReadyCallback(results, selectedIndex);
  2447. },
  2448. __proto__: WebInspector.TextPrompt.prototype
  2449. }