1/* 2 * Copyright (C) 2013 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26WebInspector.SourceCodeTextRange = function(sourceCode) /* textRange || startLocation, endLocation */ 27{ 28 WebInspector.Object.call(this); 29 30 console.assert(sourceCode instanceof WebInspector.SourceCode); 31 console.assert(arguments.length === 2 || arguments.length === 3); 32 33 this._sourceCode = sourceCode; 34 35 if (arguments.length === 2) { 36 var textRange = arguments[1]; 37 console.assert(textRange instanceof WebInspector.TextRange); 38 this._startLocation = sourceCode.createSourceCodeLocation(textRange.startLine, textRange.startColumn); 39 this._endLocation = sourceCode.createSourceCodeLocation(textRange.endLine, textRange.endColumn); 40 } else { 41 console.assert(arguments[1] instanceof WebInspector.SourceCodeLocation); 42 console.assert(arguments[2] instanceof WebInspector.SourceCodeLocation); 43 this._startLocation = arguments[1]; 44 this._endLocation = arguments[2]; 45 } 46 47 this._startLocation.addEventListener(WebInspector.SourceCodeLocation.Event.LocationChanged, this._sourceCodeLocationChanged, this); 48 this._endLocation.addEventListener(WebInspector.SourceCodeLocation.Event.LocationChanged, this._sourceCodeLocationChanged, this); 49}; 50 51WebInspector.SourceCodeTextRange.Event = { 52 RangeChanged: "source-code-text-range-range-changed" 53}; 54 55WebInspector.SourceCodeTextRange.prototype = { 56 constructor: WebInspector.SourceCodeTextRange, 57 58 // Public 59 60 get sourceCode() 61 { 62 return this._sourceCode; 63 }, 64 65 // Raw text range in the original source code. 66 67 get textRange() 68 { 69 var startLine = this._startLocation.lineNumber; 70 var startColumn = this._startLocation.columnNumber; 71 var endLine = this._endLocation.lineNumber; 72 var endColumn = this._endLocation.columnNumber; 73 return new WebInspector.TextRange(startLine, startColumn, endLine, endColumn); 74 }, 75 76 // Formatted text range in the original source code if it is pretty printed. 77 // This is the same as the raw text range if the source code has no formatter. 78 79 get formattedTextRange() 80 { 81 var startLine = this._startLocation.formattedLineNumber; 82 var startColumn = this._startLocation.formattedColumnNumber; 83 var endLine = this._endLocation.formattedLineNumber; 84 var endColumn = this._endLocation.formattedColumnNumber; 85 return new WebInspector.TextRange(startLine, startColumn, endLine, endColumn); 86 }, 87 88 // Display values: 89 // - Mapped resource and text range locations if the original source code has a 90 // source map and both start and end locations are in the same mapped resource. 91 // - Otherwise this is the formatted / raw text range. 92 93 get displaySourceCode() 94 { 95 if (!this._startAndEndLocationsInSameMappedResource()) 96 return this._sourceCode; 97 98 return this._startLocation.displaySourceCode; 99 }, 100 101 get displayTextRange() 102 { 103 if (!this._startAndEndLocationsInSameMappedResource()) 104 return this.formattedTextRange; 105 106 var startLine = this._startLocation.displayLineNumber; 107 var startColumn = this._startLocation.displayColumnNumber; 108 var endLine = this._endLocation.displayLineNumber; 109 var endColumn = this._endLocation.displayColumnNumber; 110 return new WebInspector.TextRange(startLine, startColumn, endLine, endColumn); 111 }, 112 113 // Private 114 115 _startAndEndLocationsInSameMappedResource: function() 116 { 117 return this._startLocation.hasMappedLocation() && this._endLocation.hasMappedLocation() && this._startLocation.displaySourceCode === this._endLocation.displaySourceCode; 118 }, 119 120 _sourceCodeLocationChanged: function(event) 121 { 122 this.dispatchEventToListeners(WebInspector.SourceCodeLocation.Event.RangeChanged); 123 } 124}; 125 126WebInspector.SourceCodeTextRange.prototype.__proto__ = WebInspector.Object.prototype; 127