Skip to content

Commit

Permalink
Updated highlighter to return new highlights from highlightX methods,…
Browse files Browse the repository at this point in the history
… added getHighlightElements() method to Highlight objects, using getElementsWithClassIntersectingX methods added to ClassApplier

git-svn-id: http://rangy.googlecode.com/svn/trunk@763 88e730d5-b869-a254-3594-6732aba23865
  • Loading branch information
timdown committed Feb 11, 2013
1 parent b0515fc commit 7601a51
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
4 changes: 3 additions & 1 deletion demos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ <h1>Rangy demos</h1>
<li><a href="core.html">Core</a></li>
<li><a href="saverestore.html">Selection save/restore module</a></li>
<li><a href="serializer.html">Selection serializer module</a></li>
<li><a href="cssclassapplier.html">CSS class applier module</a></li>
<li><a href="cssclassapplier.html">Class applier module</a></li>
<li><a href="textrange.html">TextRange module</a></li>
<li><a href="highlighter.html">Highlighter module</a></li>
<!--
<li><a href="bookmark.html">Selection bookmarking (core)</a></li>
-->
<li><a href="position.html">Position module (work in progress)</a></li>
<!--
<li><a href="commands.html">Commands module (work in progress)</a></li>
Expand Down
2 changes: 1 addition & 1 deletion src/js/core/wrappedselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ rangy.createModule("WrappedSelection", function(api, module) {
// The following are non-standard extensions
selProto.eachRange = function(func, returnValue) {
for (var i = 0, len = this._ranges.length; i < len; ++i) {
if (func(this.getRangeAt(i))) {
if ( func( this.getRangeAt(i) ) ) {
return returnValue;
}
}
Expand Down
54 changes: 41 additions & 13 deletions src/js/modules/rangy-cssclassapplier.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* CSS Class Applier module for Rangy.
* Adds, removes and toggles CSS classes on Ranges and Selections
* Class Applier module for Rangy.
* Adds, removes and toggles classes on Ranges and Selections
*
* Part of Rangy, a cross-browser JavaScript range and selection library
* http://code.google.com/p/rangy/
Expand All @@ -17,8 +17,9 @@ rangy.createModule("CssClassApplier", function(api, module) {

var dom = api.dom;
var DomPosition = dom.DomPosition;
var contains = dom.arrayContains;

var log = log4javascript.getLogger("rangy.cssclassapplier");
var log = log4javascript.getLogger("rangy.classapplier");

var defaultTagName = "span";

Expand Down Expand Up @@ -176,7 +177,7 @@ rangy.createModule("CssClassApplier", function(api, module) {
function elementHasNonClassAttributes(el, exceptions) {
for (var i = 0, len = el.attributes.length, attrName; i < len; ++i) {
attrName = el.attributes[i].name;
if ( !(exceptions && dom.arrayContains(exceptions, attrName)) && el.attributes[i].specified && attrName != "class") {
if ( !(exceptions && contains(exceptions, attrName)) && el.attributes[i].specified && attrName != "class") {
return true;
}
}
Expand Down Expand Up @@ -448,7 +449,7 @@ rangy.createModule("CssClassApplier", function(api, module) {
// TODO: Populate this with every attribute name that corresponds to a property with a different name
var attrNamesForProperties = {};

function CssClassApplier(cssClass, options, tagNames) {
function ClassApplier(cssClass, options, tagNames) {
this.cssClass = cssClass;
var normalize, i, len, propName;

Expand Down Expand Up @@ -503,7 +504,7 @@ rangy.createModule("CssClassApplier", function(api, module) {
}
}

CssClassApplier.prototype = {
ClassApplier.prototype = {
elementTagName: defaultTagName,
elementProperties: {},
ignoreWhiteSpace: true,
Expand Down Expand Up @@ -565,7 +566,7 @@ rangy.createModule("CssClassApplier", function(api, module) {

hasClass: function(node) {
return node.nodeType == 1 &&
dom.arrayContains(this.tagNames, node.tagName.toLowerCase()) &&
contains(this.tagNames, node.tagName.toLowerCase()) &&
hasClass(node, this.cssClass);
},

Expand Down Expand Up @@ -663,7 +664,7 @@ rangy.createModule("CssClassApplier", function(api, module) {
var parent = textNode.parentNode;
if (parent.childNodes.length == 1 &&
this.useExistingElements &&
dom.arrayContains(this.tagNames, parent.tagName.toLowerCase()) &&
contains(this.tagNames, parent.tagName.toLowerCase()) &&
elementHasProps(parent, this.elementProperties)) {

addClass(parent, this.cssClass);
Expand Down Expand Up @@ -905,15 +906,42 @@ rangy.createModule("CssClassApplier", function(api, module) {
this.applyToSelection(win);
}
},

getElementsWithClassIntersectingRange: function(range) {
var elements = [];
var applier = this;
range.getNodes([3], function(textNode) {
var el = applier.getSelfOrAncestorWithClass(textNode);
if (el && !contains(elements, el)) {
elements.push(el);
}
});
return elements;
},

getElementsWithClassIntersectingSelection: function(win) {
var sel = api.getSelection(win);
var elements = [];
var applier = this;
sel.eachRange(function(range) {
var rangeElements = applier.getElementsWithClassIntersectingRange(range);
for (var i = 0, el; el = rangeElements[i++]; ) {
if (!contains(elements, el)) {
elements.push(el);
}
}
});
return elements;
},

detach: function() {}
};

function createCssClassApplier(cssClass, options, tagNames) {
return new CssClassApplier(cssClass, options, tagNames);
function createClassApplier(cssClass, options, tagNames) {
return new ClassApplier(cssClass, options, tagNames);
}

CssClassApplier.util = {
ClassApplier.util = {
hasClass: hasClass,
addClass: addClass,
removeClass: removeClass,
Expand All @@ -927,6 +955,6 @@ rangy.createModule("CssClassApplier", function(api, module) {
isEditable: isEditable
};

api.CssClassApplier = CssClassApplier;
api.createCssClassApplier = createCssClassApplier;
api.CssClassApplier = api.ClassApplier = ClassApplier;
api.createCssClassApplier = api.createClassApplier = createClassApplier;
});
14 changes: 11 additions & 3 deletions src/js/modules/rangy-highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ rangy.createModule("Highlighter", function(api, module) {
this.classApplier.applyToRange(this.getRange());
this.applied = true;
},

getHighlightElements: function() {
return this.classApplier.getElementsWithClassIntersectingRange(this.getRange());
},

toString: function() {
return "[Highlight(ID: " + this.id + ", class: " + this.classApplier.cssClass + ", character range: " +
Expand Down Expand Up @@ -341,11 +345,15 @@ rangy.createModule("Highlighter", function(api, module) {
});

// Apply new highlights
var newHighlights = [];
forEach(highlights, function(highlight) {
if (!highlight.applied) {
highlight.apply();
newHighlights.push(highlight);
}
});

return newHighlights;
},

highlightRanges: function(className, ranges, containerElement) {
Expand All @@ -363,7 +371,7 @@ rangy.createModule("Highlighter", function(api, module) {
selCharRanges.push( converter.rangeToCharacterRange(scopedRange, containerElement || getBody(range.getDocument())) );
});

this.highlightCharacterRanges(selCharRanges, ranges, containerElementId);
return this.highlightCharacterRanges(selCharRanges, ranges, containerElementId);
},

highlightSelection: function(className, selection, containerElementId) {
Expand All @@ -387,12 +395,12 @@ rangy.createModule("Highlighter", function(api, module) {
selCharRanges.push( CharacterRange.fromCharacterRange(rangeInfo.characterRange) );
});

this.highlightCharacterRanges(className, selCharRanges, containerElementId);
var newHighlights = this.highlightCharacterRanges(className, selCharRanges, containerElementId);

// Restore selection
converter.restoreSelection(selection, serializedSelection, containerElement);

return highlights;
return newHighlights;
},

unhighlightSelection: function(selection) {
Expand Down

0 comments on commit 7601a51

Please sign in to comment.