Skip to content

Commit

Permalink
Fix for issue timdown#249: Refactored options object creation, mostly…
Browse files Browse the repository at this point in the history
… in TextRange module
  • Loading branch information
Tim committed Sep 21, 2014
1 parent 0cf6cc4 commit be8b470
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
1 change: 0 additions & 1 deletion .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
var options = {};
extend(options, defaults);
if (optionsParam) {
extend(options, optionsParam, true);
extend(options, optionsParam);
}
return options;
};
Expand Down Expand Up @@ -391,6 +391,9 @@
var errorMessage = "Module '" + name + "' failed to load: " + getErrorDesc(ex);
log.error(errorMessage, ex);
consoleLog(errorMessage);
if (ex.stack) {
consoleLog(ex.stack);
}
}
}
});
Expand Down
51 changes: 21 additions & 30 deletions src/modules/rangy-textrange.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,14 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {
}
}

function createCharacterOptions(options) {
return createOptions(options, defaultCharacterOptions);
}

function createCaretCharacterOptions(options) {
return createOptions(options, defaultCaretCharacterOptions);
}

function createFindOptions(optionsParam) {
var options = createOptions(optionsParam, defaultFindOptions);
options.characterOptions = createCharacterOptions(options.wordOptions);
function createNestedOptions(optionsParam, defaults) {
var options = createOptions(optionsParam, defaults);
if (defaults.hasOwnProperty("wordOptions")) {
options.wordOptions = createWordOptions(options.wordOptions);
}
if (defaults.hasOwnProperty("characterOptions")) {
options.characterOptions = createOptions(options.characterOptions, defaultCharacterOptions);
}
return options;
}

Expand Down Expand Up @@ -1597,24 +1594,22 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {
*/
return createEntryPointFunction(
function(session, unit, count, moveOptions) {
if (typeof count == "undefined") {
if (typeof count == UNDEF) {
count = unit;
unit = CHARACTER;
}
moveOptions = createOptions(moveOptions, defaultMoveOptions);
var characterOptions = createCharacterOptions(moveOptions.characterOptions);
var wordOptions = createWordOptions(moveOptions.wordOptions);
moveOptions = createNestedOptions(moveOptions, defaultMoveOptions);
log.debug("** moving boundary. start: " + isStart + ", unit: " + unit + ", count: " + count);

var boundaryIsStart = isStart;
if (collapse) {
boundaryIsStart = (count >= 0);
this.collapse(!boundaryIsStart);
}
var moveResult = movePositionBy(session.getRangeBoundaryPosition(this, boundaryIsStart), unit, count, characterOptions, wordOptions);
var moveResult = movePositionBy(session.getRangeBoundaryPosition(this, boundaryIsStart), unit, count, moveOptions.characterOptions, moveOptions.wordOptions);
var newPos = moveResult.position;
this[boundaryIsStart ? "setStart" : "setEnd"](newPos.node, newPos.offset);
log.debug("*** MOVED " + moveResult.unitsMoved, newPos.inspect())
log.debug("*** MOVED " + moveResult.unitsMoved, newPos.inspect());
return moveResult.unitsMoved;
}
);
Expand All @@ -1623,7 +1618,7 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {
function createRangeTrimmer(isStart) {
return createEntryPointFunction(
function(session, characterOptions) {
characterOptions = createCharacterOptions(characterOptions);
characterOptions = createOptions(characterOptions, defaultCharacterOptions);
var pos;
var it = createRangeCharacterIterator(session, this, characterOptions, !isStart);
var trimCharCount = 0;
Expand Down Expand Up @@ -1665,13 +1660,13 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {
expand: createEntryPointFunction(
function(session, unit, expandOptions) {
var moved = false;
expandOptions = createOptions(expandOptions, defaultExpandOptions);
var characterOptions = createCharacterOptions(expandOptions.characterOptions);
expandOptions = createNestedOptions(expandOptions, defaultExpandOptions);
var characterOptions = expandOptions.characterOptions;
if (!unit) {
unit = CHARACTER;
}
if (unit == WORD) {
var wordOptions = createWordOptions(expandOptions.wordOptions);
var wordOptions = expandOptions.wordOptions;
var startPos = session.getRangeBoundaryPosition(this, true);
var endPos = session.getRangeBoundaryPosition(this, false);

Expand Down Expand Up @@ -1717,7 +1712,7 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {
function(session, characterOptions) {
log.info("text. Transaction: " + session + ", characterOptions:", characterOptions);
return this.collapsed ?
"" : getRangeCharacters(session, this, createCharacterOptions(characterOptions)).join("");
"" : getRangeCharacters(session, this, createOptions(characterOptions, defaultCharacterOptions)).join("");
}
),

Expand Down Expand Up @@ -1764,12 +1759,10 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {
findText: createEntryPointFunction(
function(session, searchTermParam, findOptions) {
// Set up options
findOptions = createFindOptions(findOptions);
findOptions = createNestedOptions(findOptions, defaultFindOptions);

// Create word options if we're matching whole words only
if (findOptions.wholeWordsOnly) {
findOptions.wordOptions = createWordOptions(findOptions.wordOptions);

// We don't ever want trailing spaces for search results
findOptions.wordOptions.includeTrailingSpace = false;
}
Expand Down Expand Up @@ -1881,7 +1874,7 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {
if (!options) {
options = {};
}
options.characterOptions = createCaretCharacterOptions(options.characterOptions);
options.characterOptions = createOptions(options.characterOptions, defaultCaretCharacterOptions);
unitsMoved = range.move(unit, count, options);
log.debug("Selection move setting range " + range.inspect());
this.setSingleRange(range);
Expand Down Expand Up @@ -1961,11 +1954,9 @@ rangy.createModule("TextRange", ["WrappedSelection"], function(api, module) {

api.createWordIterator = function(startNode, startOffset, iteratorOptions) {
var session = getSession();
iteratorOptions = createOptions(iteratorOptions, defaultWordIteratorOptions);
var characterOptions = createCharacterOptions(iteratorOptions.characterOptions);
var wordOptions = createWordOptions(iteratorOptions.wordOptions);
iteratorOptions = createNestedOptions(iteratorOptions, defaultWordIteratorOptions);
var startPos = session.getPosition(startNode, startOffset);
var tokenizedTextProvider = createTokenizedTextProvider(startPos, characterOptions, wordOptions);
var tokenizedTextProvider = createTokenizedTextProvider(startPos, iteratorOptions.characterOptions, iteratorOptions.wordOptions);
var backward = isDirectionBackward(iteratorOptions.direction);

return {
Expand Down
8 changes: 8 additions & 0 deletions test/highlightertests.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ xn.test.suite("Highlighter module tests", function(s) {

});

s.test("Options test (issue 249)", function(t) {
var applier = rangy.createClassApplier("c1");
var highlighter = rangy.createHighlighter();
highlighter.addClassApplier(applier);

highlighter.highlightSelection("c1", { selection: rangy.getSelection() });
});

}, false);

0 comments on commit be8b470

Please sign in to comment.