CSSMedia.js 3.1 KB

123456789101112131415161718192021222324252627282930313233
  1. export class CSSMediaQuery{constructor(payload){this._active=payload.active;this._expressions=[];for(let j=0;j<payload.expressions.length;++j){this._expressions.push(CSSMediaQueryExpression.parsePayload(payload.expressions[j]));}}
  2. static parsePayload(payload){return new CSSMediaQuery(payload);}
  3. active(){return this._active;}
  4. expressions(){return this._expressions;}}
  5. export class CSSMediaQueryExpression{constructor(payload){this._value=payload.value;this._unit=payload.unit;this._feature=payload.feature;this._valueRange=payload.valueRange?TextUtils.TextRange.fromObject(payload.valueRange):null;this._computedLength=payload.computedLength||null;}
  6. static parsePayload(payload){return new CSSMediaQueryExpression(payload);}
  7. value(){return this._value;}
  8. unit(){return this._unit;}
  9. feature(){return this._feature;}
  10. valueRange(){return this._valueRange;}
  11. computedLength(){return this._computedLength;}}
  12. export default class CSSMedia{constructor(cssModel,payload){this._cssModel=cssModel;this._reinitialize(payload);}
  13. static parsePayload(cssModel,payload){return new CSSMedia(cssModel,payload);}
  14. static parseMediaArrayPayload(cssModel,payload){const result=[];for(let i=0;i<payload.length;++i){result.push(CSSMedia.parsePayload(cssModel,payload[i]));}
  15. return result;}
  16. _reinitialize(payload){this.text=payload.text;this.source=payload.source;this.sourceURL=payload.sourceURL||'';this.range=payload.range?TextUtils.TextRange.fromObject(payload.range):null;this.styleSheetId=payload.styleSheetId;this.mediaList=null;if(payload.mediaList){this.mediaList=[];for(let i=0;i<payload.mediaList.length;++i){this.mediaList.push(CSSMediaQuery.parsePayload(payload.mediaList[i]));}}}
  17. rebase(edit){if(this.styleSheetId!==edit.styleSheetId||!this.range){return;}
  18. if(edit.oldRange.equal(this.range)){this._reinitialize((edit.payload));}else{this.range=this.range.rebaseAfterTextEdit(edit.oldRange,edit.newRange);}}
  19. equal(other){if(!this.styleSheetId||!this.range||!other.range){return false;}
  20. return this.styleSheetId===other.styleSheetId&&this.range.equal(other.range);}
  21. active(){if(!this.mediaList){return true;}
  22. for(let i=0;i<this.mediaList.length;++i){if(this.mediaList[i].active()){return true;}}
  23. return false;}
  24. lineNumberInSource(){if(!this.range){return undefined;}
  25. const header=this.header();if(!header){return undefined;}
  26. return header.lineNumberInSource(this.range.startLine);}
  27. columnNumberInSource(){if(!this.range){return undefined;}
  28. const header=this.header();if(!header){return undefined;}
  29. return header.columnNumberInSource(this.range.startLine,this.range.startColumn);}
  30. header(){return this.styleSheetId?this._cssModel.styleSheetHeaderForId(this.styleSheetId):null;}
  31. rawLocation(){const header=this.header();if(!header||this.lineNumberInSource()===undefined){return null;}
  32. const lineNumber=Number(this.lineNumberInSource());return new SDK.CSSLocation(header,lineNumber,this.columnNumberInSource());}}
  33. export const Source={LINKED_SHEET:'linkedSheet',INLINE_SHEET:'inlineSheet',MEDIA_RULE:'mediaRule',IMPORT_RULE:'importRule'};self.SDK=self.SDK||{};SDK=SDK||{};SDK.CSSMediaQuery=CSSMediaQuery;SDK.CSSMediaQueryExpression=CSSMediaQueryExpression;SDK.CSSMedia=CSSMedia;SDK.CSSMedia.Source=Source;