SASSSourceMapping.js 3.9 KB

1234567891011121314151617181920
  1. export default class SASSSourceMapping{constructor(target,sourceMapManager,workspace){this._sourceMapManager=sourceMapManager;this._project=new Bindings.ContentProviderBasedProject(workspace,'cssSourceMaps:'+target.id(),Workspace.projectTypes.Network,'',false);Bindings.NetworkProject.setTargetForProject(this._project,target);this._eventListeners=[this._sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapAttached,this._sourceMapAttached,this),this._sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapDetached,this._sourceMapDetached,this),this._sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapChanged,this._sourceMapChanged,this)];}
  2. _sourceMapAttachedForTest(sourceMap){}
  3. _sourceMapAttached(event){const header=(event.data.client);const sourceMap=(event.data.sourceMap);for(const sassURL of sourceMap.sourceURLs()){let uiSourceCode=this._project.uiSourceCodeForURL(sassURL);if(uiSourceCode){Bindings.NetworkProject.addFrameAttribution(uiSourceCode,header.frameId);continue;}
  4. const contentProvider=sourceMap.sourceContentProvider(sassURL,Common.resourceTypes.SourceMapStyleSheet);const mimeType=Common.ResourceType.mimeFromURL(sassURL)||contentProvider.contentType().canonicalMimeType();const embeddedContent=sourceMap.embeddedContentByURL(sassURL);const metadata=typeof embeddedContent==='string'?new Workspace.UISourceCodeMetadata(null,embeddedContent.length):null;uiSourceCode=this._project.createUISourceCode(sassURL,contentProvider.contentType());Bindings.NetworkProject.setInitialFrameAttribution(uiSourceCode,header.frameId);uiSourceCode[_sourceMapSymbol]=sourceMap;this._project.addUISourceCodeWithProvider(uiSourceCode,contentProvider,metadata,mimeType);}
  5. Bindings.cssWorkspaceBinding.updateLocations(header);this._sourceMapAttachedForTest(sourceMap);}
  6. _sourceMapDetached(event){const header=(event.data.client);const sourceMap=(event.data.sourceMap);const headers=this._sourceMapManager.clientsForSourceMap(sourceMap);for(const sassURL of sourceMap.sourceURLs()){if(headers.length){const uiSourceCode=(this._project.uiSourceCodeForURL(sassURL));Bindings.NetworkProject.removeFrameAttribution(uiSourceCode,header.frameId);}else{this._project.removeFile(sassURL);}}
  7. Bindings.cssWorkspaceBinding.updateLocations(header);}
  8. _sourceMapChanged(event){const sourceMap=(event.data.sourceMap);const newSources=(event.data.newSources);const headers=this._sourceMapManager.clientsForSourceMap(sourceMap);for(const sourceURL of newSources.keys()){const uiSourceCode=this._project.uiSourceCodeForURL(sourceURL);if(!uiSourceCode){console.error('Failed to update source for '+sourceURL);continue;}
  9. const sassText=(newSources.get(sourceURL));uiSourceCode.setWorkingCopy(sassText);}
  10. for(const header of headers){Bindings.cssWorkspaceBinding.updateLocations(header);}}
  11. rawLocationToUILocation(rawLocation){const header=rawLocation.header();if(!header){return null;}
  12. const sourceMap=this._sourceMapManager.sourceMapForClient(header);if(!sourceMap){return null;}
  13. const entry=sourceMap.findEntry(rawLocation.lineNumber,rawLocation.columnNumber);if(!entry||!entry.sourceURL){return null;}
  14. const uiSourceCode=this._project.uiSourceCodeForURL(entry.sourceURL);if(!uiSourceCode){return null;}
  15. return uiSourceCode.uiLocation(entry.sourceLineNumber||0,entry.sourceColumnNumber);}
  16. uiLocationToRawLocations(uiLocation){const sourceMap=uiLocation.uiSourceCode[_sourceMapSymbol];if(!sourceMap){return[];}
  17. const entries=sourceMap.findReverseEntries(uiLocation.uiSourceCode.url(),uiLocation.lineNumber,uiLocation.columnNumber);const locations=[];for(const header of this._sourceMapManager.clientsForSourceMap(sourceMap)){locations.pushAll(entries.map(entry=>new SDK.CSSLocation(header,entry.lineNumber,entry.columnNumber)));}
  18. return locations;}
  19. dispose(){this._project.dispose();Common.EventTarget.removeEventListeners(this._eventListeners);}}
  20. const _sourceMapSymbol=Symbol('sourceMap');self.Bindings=self.Bindings||{};Bindings=Bindings||{};Bindings.SASSSourceMapping=SASSSourceMapping;