Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
Moved Formatting.applyStyle into consuming operation
Browse files Browse the repository at this point in the history
Apply style logic is only consumed in one location. Extra wrapping
functions added in StyleHelper and Formatting are unnecessary, and
should not have been called from outside an operation. Pushing this
logic into the operation prevents accidental misuse.
  • Loading branch information
peitschie committed Dec 5, 2013
1 parent ba8d1d5 commit 9d682b9
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 70 deletions.
28 changes: 1 addition & 27 deletions webodf/lib/gui/StyleHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

/*global core, runtime, gui, odf*/

runtime.loadClass("core.DomUtils");
runtime.loadClass("odf.Namespaces");
runtime.loadClass("odf.OdfUtils");

Expand All @@ -48,8 +47,7 @@ runtime.loadClass("odf.OdfUtils");
*/
gui.StyleHelper = function StyleHelper(formatting) {
"use strict";
var domUtils = new core.DomUtils(),
odfUtils = new odf.OdfUtils(),
var odfUtils = new odf.OdfUtils(),
/**@const*/
textns = odf.Namespaces.textns;

Expand Down Expand Up @@ -79,30 +77,6 @@ gui.StyleHelper = function StyleHelper(formatting) {
*/
this.getAppliedStyles = getAppliedStyles;

/**
* Apply the specified style properties to all elements within the given range.
* Currently, only text styles are applied.
* @param {!string} memberId Identifier of the member applying the style. This is used for naming generated autostyles
* @param {!Range} range Range to apply text style to
* @param {!Object} info Style information. Only data within "style:text-properties" will be considered and applied
*/
this.applyStyle = function (memberId, range, info) {
var nextTextNodes = domUtils.splitBoundaries(range),
textNodes = odfUtils.getTextNodes(range, false),
limits;

// Avoid using the passed in range as boundaries move in strange ways as the DOM is modified
limits = {
startContainer: range.startContainer,
startOffset: range.startOffset,
endContainer: range.endContainer,
endOffset: range.endOffset
};

formatting.applyStyle(memberId, textNodes, limits, info);
nextTextNodes.forEach(domUtils.normalizeTextNodes);
};

/**
* Returns true if all the node within given range have the same value for
* the property; otherwise false.
Expand Down
20 changes: 1 addition & 19 deletions webodf/lib/odf/Formatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ runtime.loadClass("odf.TextStyleApplicator");
*/
odf.Formatting = function Formatting() {
"use strict";
var self = this,
/**@type{odf.OdfContainer}*/
var /**@type{odf.OdfContainer}*/
odfContainer,
/**@type{odf.StyleInfo}*/
styleInfo = new odf.StyleInfo(),
Expand Down Expand Up @@ -505,23 +504,6 @@ odf.Formatting = function Formatting() {
return styleChain ? calculateAppliedStyle(styleChain) : undefined;
};

/**
* Apply the specified style properties to all given text nodes
* Currently, only text styles are applied.
* @param {!string} memberId Identifier of the member applying the style. This is used for naming generated autostyles
* @param {!Array.<!CharacterData>} textNodes
* @param {!{startContainer: Node, startOffset: !number, endContainer: Node, endOffset: !number}} limits style application bounds
* @param {!Object} info Style information. Only data within "style:text-properties" will be considered and applied
*/
this.applyStyle = function (memberId, textNodes, limits, info) {
var textStyles = new odf.TextStyleApplicator(
new odf.ObjectNameGenerator(/**@type{!odf.OdfContainer}*/(odfContainer), memberId), // TODO: use the instance in SessionController
self,
odfContainer.rootElement.automaticStyles
);
textStyles.applyStyle(textNodes, limits, info);
};

/**
* Overrides the specific properties on the styleNode from the values in the supplied properties Object.
* If a newStylePrefix is supplied, this method will automatically generate a unique name for the style node
Expand Down
43 changes: 38 additions & 5 deletions webodf/lib/ops/OpApplyDirectStyling.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@

/*global ops, runtime, gui, odf, Node, core*/

runtime.loadClass("gui.StyleHelper");
runtime.loadClass("core.DomUtils");
runtime.loadClass("odf.OdfUtils");
runtime.loadClass("odf.TextStyleApplicator");

/**
* @constructor
Expand All @@ -54,7 +55,8 @@ ops.OpApplyDirectStyling = function OpApplyDirectStyling() {
/**@type {number}*/
length,
setProperties,
odfUtils = new odf.OdfUtils();
odfUtils = new odf.OdfUtils(),
domUtils = new core.DomUtils();

this.init = function (data) {
memberid = data.memberid;
Expand All @@ -78,12 +80,43 @@ ops.OpApplyDirectStyling = function OpApplyDirectStyling() {
return range;
}

/**
* Apply the specified style properties to all elements within the given range.
* Currently, only text styles are applied.
* @param {!Range} range Range to apply text style to
* @param {!Object} info Style information. Only data within "style:text-properties" will be considered and applied
*/
function applyStyle(odtDocument, range, info) {
var odfCanvas = odtDocument.getOdfCanvas(),
odfContainer = odfCanvas.odfContainer(),
nextTextNodes = domUtils.splitBoundaries(range),
textNodes = odfUtils.getTextNodes(range, false),
limits,
textStyles;

// Avoid using the passed in range as boundaries move in strange ways as the DOM is modified
limits = {
startContainer: range.startContainer,
startOffset: range.startOffset,
endContainer: range.endContainer,
endOffset: range.endOffset
};

textStyles = new odf.TextStyleApplicator(
new odf.ObjectNameGenerator(/**@type{!odf.OdfContainer}*/(odfContainer), memberid), // TODO: use the instance in SessionController
odtDocument.getFormatting(),
odfContainer.rootElement.automaticStyles
);
textStyles.applyStyle(textNodes, limits, info);
nextTextNodes.forEach(domUtils.normalizeTextNodes);
}

this.execute = function (odtDocument) {
var range = getRange(odtDocument),
impactedParagraphs = odfUtils.getImpactedParagraphs(range),
styleHelper = new gui.StyleHelper(odtDocument.getFormatting());
impactedParagraphs = odfUtils.getImpactedParagraphs(range);

applyStyle(odtDocument, range, setProperties);

styleHelper.applyStyle(memberid, range, setProperties);
range.detach();
odtDocument.getOdfCanvas().refreshCSS();
odtDocument.fixCursorPositions(); // The container splits may leave the cursor in an invalid spot
Expand Down
69 changes: 50 additions & 19 deletions webodf/tests/odf/TextStyleApplicatorTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
* @source: https://github.com/kogmbh/WebODF/
*/
/*global runtime, core, gui, odf, NodeFilter*/
runtime.loadClass("gui.StyleHelper");
runtime.loadClass("core.DomUtils");
runtime.loadClass("odf.Formatting");
runtime.loadClass("odf.OdfUtils");
runtime.loadClass("odf.TextStyleApplicator");
/**
* @constructor
* @param {core.UnitTestRunner} runner
Expand All @@ -45,6 +47,8 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
"use strict";
var t,
r = runner,
odfUtils = new odf.OdfUtils(),
domUtils = new core.DomUtils(),
namespace = {
"text": "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"office": "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
Expand All @@ -58,7 +62,6 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
testArea : core.UnitTest.provideTestAreaDiv(),
ns: namespace
};
t.styleHelper = new gui.StyleHelper(t.formatting);
};
this.tearDown = function () {
t.range.detach();
Expand Down Expand Up @@ -133,12 +136,40 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
}
return replacedNames;
}

/**
* Apply the specified style information to the supplied range
* @param {!Range} range
* @param {!Object} info
* @retur {undefined}
*/
function applyStyle(range, info) {
var limits,
newTextNodes = domUtils.splitBoundaries(t.range),
textNodes = odfUtils.getTextNodes(range, false),
textStyles = new odf.TextStyleApplicator(
new odf.ObjectNameGenerator(/**@type{!odf.OdfContainer}*/(t.container), "tStyle"), // TODO: use the instance in SessionController
t.formatting,
t.container.rootElement.automaticStyles
);

// Avoid using the passed in range as boundaries move in strange ways as the DOM is modified
limits = {
startContainer: range.startContainer,
startOffset: range.startOffset,
endContainer: range.endContainer,
endOffset: range.endOffset
};

textStyles.applyStyle(textNodes, limits, info);
newTextNodes.forEach(domUtils.normalizeTextNodes);
}
function apply_ContainerInsertion_SimpleTextRange() {
t.doc = createDocument("<text:p>ABCD</text:p>");
t.range.setStart(t.doc.childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[0], 3);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p>A<text:span text:style-name='auto0'>BC</text:span>D</text:p>");
Expand All @@ -150,7 +181,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[1], 0);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
r.shouldBe(t, "t.doc.childNodes.length", "3");
Expand All @@ -167,7 +198,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[2], 1);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p>A<text:span text:style-name='auto0'>B<text:a>C</text:a>D</text:span>E</text:p>");
Expand All @@ -178,7 +209,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0].childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[0].childNodes[0], 3);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p><text:span>A</text:span>" +
Expand All @@ -191,7 +222,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0].childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[1].childNodes[0], 1);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p><text:span>A</text:span>" +
Expand All @@ -205,7 +236,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0].childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[0].childNodes[2], 1);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p><text:span>A</text:span>" +
Expand All @@ -218,7 +249,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0].childNodes[1], 0);
t.range.setEnd(t.doc.childNodes[0].childNodes[3], 1);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p>" +
Expand All @@ -232,7 +263,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0].childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[1], 1);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p><text:span>A</text:span>" +
Expand All @@ -246,7 +277,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0].childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[2].childNodes[0], 0);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p><text:span>A</text:span>" +
Expand All @@ -260,7 +291,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0].childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[2].childNodes[0], 0);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p><text:span>A</text:span>" +
Expand All @@ -273,7 +304,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc, 1);
t.range.setEnd(t.doc, 2);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p>A<text:span text:style-name='auto0'>B<text:span/>C</text:span>D</text:p>");
Expand All @@ -285,7 +316,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[0], 3);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p text:style-name='PBold'>ABCD</text:p>");
Expand All @@ -296,7 +327,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0].childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[0].childNodes[0], 3);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p><text:span text:style-name='SBold'>ABCD</text:span></text:p>");
Expand All @@ -307,7 +338,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0].childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[0].childNodes[0], 3);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p><text:span text:style-name='ABold'>ABCD</text:span></text:p>");
Expand All @@ -318,7 +349,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[1].childNodes[0], 1);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p>A<text:span text:style-name='auto0'>B</text:span><text:span text:style-name='ABold'>CD</text:span></text:p>");
Expand All @@ -329,7 +360,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[1].childNodes[0], 1);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

t.autoStyles = simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p>A<text:span text:style-name='auto0'>B</text:span>" +
Expand All @@ -352,7 +383,7 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
t.range.setStart(t.doc.childNodes[0], 1);
t.range.setEnd(t.doc.childNodes[1].childNodes[0], 1);

t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}});
applyStyle(t.range, {"style:text-properties": {"fo:font-weight": "bold"}});

t.autoStyles = simplifyAutoStyleNames(t.doc);
t.expected = parseXML("<text:p>A<text:span text:style-name='auto0'>B</text:span>" +
Expand Down

0 comments on commit 9d682b9

Please sign in to comment.