mirror of
https://github.com/webodf/WebODF.git
synced 2026-11-30 16:47:58 +01:00
When selecting or modifying large areas of content, the multiple style information lookup becomes quite expensive. These can be greatly sped up by using a common cached value + associated query object instead.
330 lines
14 KiB
JavaScript
330 lines
14 KiB
JavaScript
/**
|
|
* Copyright (C) 2012 KO GmbH <jos.van.den.oever@kogmbh.com>
|
|
* @licstart
|
|
* The JavaScript code in this page is free software: you can redistribute it
|
|
* and/or modify it under the terms of the GNU Affero General Public License
|
|
* (GNU AGPL) as published by the Free Software Foundation, either version 3 of
|
|
* the License, or (at your option) any later version. The code is distributed
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this code. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* As additional permission under GNU AGPL version 3 section 7, you
|
|
* may distribute non-source (e.g., minimized or compacted) forms of
|
|
* that code without the copy of the GNU GPL normally required by
|
|
* section 4, provided you include this license notice and a URL
|
|
* through which recipients can access the Corresponding Source.
|
|
*
|
|
* As a special exception to the AGPL, any HTML file which merely makes function
|
|
* calls to this code, and for that purpose includes it by reference shall be
|
|
* deemed a separate work for copyright law purposes. In addition, the copyright
|
|
* holders of this code give you permission to combine this code with free
|
|
* software libraries that are released under the GNU LGPL. You may copy and
|
|
* distribute such a system following the terms of the GNU AGPL for this code
|
|
* and the LGPL for the libraries. If you modify this code, you may extend this
|
|
* exception to your version of the code, but you are not obligated to do so.
|
|
* If you do not wish to do so, delete this exception statement from your
|
|
* version.
|
|
*
|
|
* This license applies to this entire compilation.
|
|
* @licend
|
|
* @source: http://www.webodf.org/
|
|
* @source: https://github.com/kogmbh/WebODF/
|
|
*/
|
|
/*global runtime, core, gui, odf, NodeFilter*/
|
|
runtime.loadClass("gui.StyleSummary");
|
|
runtime.loadClass("odf.Formatting");
|
|
/**
|
|
* @constructor
|
|
* @param {core.UnitTestRunner} runner
|
|
* @implements {core.UnitTest}
|
|
*/
|
|
gui.StyleSummaryTests = function StyleSummaryTests(runner) {
|
|
"use strict";
|
|
var t,
|
|
r = runner,
|
|
namespace = {
|
|
"text": "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
|
|
"office": "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
|
|
"style": "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
|
|
"fo": "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
|
|
"draw": "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
|
|
"svg": "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
|
|
"dr3d": "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
|
|
};
|
|
|
|
this.setUp = function () {
|
|
t = {
|
|
formatting: new odf.Formatting(),
|
|
testArea : core.UnitTest.provideTestAreaDiv(),
|
|
ns: namespace
|
|
};
|
|
};
|
|
this.tearDown = function () {
|
|
t = {};
|
|
core.UnitTest.cleanupTestAreaDiv();
|
|
};
|
|
function createDocument(dom) {
|
|
var xml, container, fragment;
|
|
|
|
xml = "<office:styles>";
|
|
xml += " <style:style style:name='P1' style:display-name='P1 Display' style:family='paragraph'>";
|
|
xml += " <style:text-properties fo:font-name='P1 Font' />";
|
|
xml += " </style:style>";
|
|
xml += " <style:style style:name='S1' style:display-name='S1 Display' style:family='text'>";
|
|
xml += " <style:text-properties fo:font-name='S1 Font' />";
|
|
xml += " </style:style>";
|
|
xml += " <style:style style:name='SBold' style:display-name='SBold Display' style:family='text'>";
|
|
xml += " <style:text-properties fo:font-weight='bold'/>";
|
|
xml += " </style:style>";
|
|
xml += " <style:style style:name='SItalic' style:family='text'>";
|
|
xml += " <style:text-properties fo:font-style='italic' />";
|
|
xml += " </style:style>";
|
|
xml += " <style:style style:name='SUnderline' style:family='text'>";
|
|
xml += " <style:text-properties style:text-underline-style='solid' />";
|
|
xml += " </style:style>";
|
|
xml += " <style:style style:name='SStrikeThrough' style:family='text'>";
|
|
xml += " <style:text-properties style:text-line-through-style='solid' />";
|
|
xml += " </style:style>";
|
|
xml += " <style:style style:name='AlignRight' style:display-name='AlignRight' style:family='paragraph'>";
|
|
xml += " <style:paragraph-properties fo:text-align='right' />";
|
|
xml += " </style:style>";
|
|
xml += " <style:style style:name='AlignLeft' style:display-name='AlignLeft' style:family='paragraph'>";
|
|
xml += " <style:paragraph-properties fo:text-align='left' />";
|
|
xml += " </style:style>";
|
|
xml += " <style:style style:name='AlignCenter' style:display-name='AlignCenter' style:family='paragraph'>";
|
|
xml += " <style:paragraph-properties fo:text-align='center' />";
|
|
xml += " </style:style>";
|
|
xml += " <style:style style:name='AlignJustify' style:display-name='AlignJustify' style:family='paragraph'>";
|
|
xml += " <style:paragraph-properties fo:text-align='justify' />";
|
|
xml += " </style:style>";
|
|
xml += " <style:style style:name='AlignStart' style:display-name='AlignStart' style:family='paragraph'>";
|
|
xml += " <style:paragraph-properties fo:text-align='start' />";
|
|
xml += " </style:style>";
|
|
xml += " <style:style style:name='AlignEnd' style:display-name='AlignEnd' style:family='paragraph'>";
|
|
xml += " <style:paragraph-properties fo:text-align='end' />";
|
|
xml += " </style:style>";
|
|
xml += "</office:styles>";
|
|
|
|
xml += "<office:automatic-styles>";
|
|
xml += " <text:list-style style:name='L1' style:display-name='L1 Display'>";
|
|
xml += " <text:list-level-style-bullet text:level='1' text:bullet-char='*' />";
|
|
xml += " <text:list-level-style-bullet text:level='2' text:bullet-char='@' />";
|
|
xml += " </text:list-style>";
|
|
xml += " <text:list-style style:name='L2' style:display-name='L2 Display'>";
|
|
xml += " <text:list-level-style-number text:level='1' text:num-format='1' />";
|
|
xml += " </text:list-style>";
|
|
xml += "</office:automatic-styles>";
|
|
|
|
xml += "<office:text>" + dom + "</office:text>";
|
|
|
|
fragment = core.UnitTest.createOdtDocument(xml, namespace);
|
|
t.testArea.appendChild(fragment.documentElement);
|
|
container = { rootElement : {
|
|
styles : t.testArea.firstChild.childNodes[0],
|
|
automaticStyles: t.testArea.firstChild.childNodes[1]
|
|
}};
|
|
t.formatting.setOdfContainer(container);
|
|
return t.testArea.firstChild.childNodes[2].childNodes.length === 1
|
|
? t.testArea.firstChild.childNodes[2].firstChild
|
|
: t.testArea.firstChild.childNodes[2];
|
|
}
|
|
|
|
/**
|
|
* Get all text nodes in the test div
|
|
* @return {!Array.<!Node>}
|
|
*/
|
|
function getTextNodes() {
|
|
var document = t.testArea.ownerDocument,
|
|
walker = document.createTreeWalker(t.doc, NodeFilter.SHOW_TEXT, null, false),
|
|
nodes = [],
|
|
n = walker.nextNode();
|
|
|
|
while (n) {
|
|
nodes.push(n);
|
|
n = walker.nextNode();
|
|
}
|
|
return nodes;
|
|
}
|
|
|
|
/**
|
|
* Create a new style summary containing all the text nodes in the test div
|
|
* @return {!gui.StyleSummary}
|
|
*/
|
|
function createStyleSummary() {
|
|
return new gui.StyleSummary(t.formatting.getAppliedStyles(getTextNodes()));
|
|
}
|
|
|
|
function isBold_ReturnTrue() {
|
|
t.doc = createDocument("<text:p text:style-name='P1'><text:span text:style-name='SBold'>A</text:span><text:span text:style-name='SBold'>B</text:span></text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.isBold()", "true");
|
|
}
|
|
function isBold_ReturnFalse() {
|
|
t.doc = createDocument("<text:p text:style-name='P1'><text:span text:style-name='S1'>A</text:span><text:span text:style-name='SBold'>B</text:span></text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.isBold()", "false");
|
|
}
|
|
function isItalic_ReturnTrue() {
|
|
t.doc = createDocument("<text:p text:style-name='P1'><text:span text:style-name='SItalic'>A</text:span><text:span text:style-name='SItalic'>B</text:span></text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.isItalic()", "true");
|
|
}
|
|
function hasUnderline_ReturnTrue() {
|
|
t.doc = createDocument("<text:p text:style-name='P1'><text:span text:style-name='SUnderline'>A</text:span><text:span text:style-name='SUnderline'>B</text:span></text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.hasUnderline()", "true");
|
|
}
|
|
function hasStrikeThrough_ReturnTrue() {
|
|
t.doc = createDocument("<text:p text:style-name='P1'><text:span text:style-name='SStrikeThrough'>A</text:span><text:span text:style-name='SStrikeThrough'>B</text:span></text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.hasStrikeThrough()", "true");
|
|
}
|
|
|
|
function isAligned_MixedValues_ReturnsFalse() {
|
|
t.doc = createDocument("<text:p text:style-name='AlignRight'>A</text:p>"
|
|
+ "<text:p text:style-name='AlignLeft'>A</text:p>"
|
|
+ "<text:p text:style-name='AlignCenter'>A</text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.isAlignedLeft()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedRight()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedCenter()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedJustified()", "false");
|
|
}
|
|
|
|
function isAligned_Center_ReturnsTrue() {
|
|
t.doc = createDocument("<text:p text:style-name='AlignCenter'>A</text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.isAlignedLeft()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedRight()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedCenter()", "true");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedJustified()", "false");
|
|
}
|
|
|
|
function isAligned_Justify_ReturnsTrue() {
|
|
t.doc = createDocument("<text:p text:style-name='AlignJustify'>A</text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.isAlignedLeft()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedRight()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedCenter()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedJustified()", "true");
|
|
}
|
|
|
|
function isAligned_Left_ReturnsTrue() {
|
|
t.doc = createDocument("<text:p text:style-name='AlignLeft'>A</text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.isAlignedLeft()", "true");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedRight()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedCenter()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedJustified()", "false");
|
|
}
|
|
|
|
function isAligned_Start_ReturnsTrue() {
|
|
t.doc = createDocument("<text:p text:style-name='AlignStart'>A</text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.isAlignedLeft()", "true");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedRight()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedCenter()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedJustified()", "false");
|
|
}
|
|
|
|
function isAligned_LeftOrStart_ReturnsTrue() {
|
|
t.doc = createDocument("<text:p text:style-name='AlignLeft'>A</text:p>"
|
|
+ "<text:p text:style-name='AlignStart'>A</text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.isAlignedLeft()", "true");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedRight()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedCenter()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedJustified()", "false");
|
|
}
|
|
|
|
function isAligned_Right_ReturnsTrue() {
|
|
t.doc = createDocument("<text:p text:style-name='AlignRight'>A</text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.isAlignedLeft()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedRight()", "true");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedCenter()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedJustified()", "false");
|
|
}
|
|
|
|
function isAligned_End_ReturnsTrue() {
|
|
t.doc = createDocument("<text:p text:style-name='AlignEnd'>A</text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.isAlignedLeft()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedRight()", "true");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedCenter()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedJustified()", "false");
|
|
}
|
|
|
|
function isAligned_RightOrEnd_ReturnsTrue() {
|
|
t.doc = createDocument("<text:p text:style-name='AlignEnd'>A</text:p>"
|
|
+ "<text:p text:style-name='AlignRight'>A</text:p>");
|
|
|
|
t.styleSummary = createStyleSummary();
|
|
|
|
r.shouldBe(t, "t.styleSummary.isAlignedLeft()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedRight()", "true");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedCenter()", "false");
|
|
r.shouldBe(t, "t.styleSummary.isAlignedJustified()", "false");
|
|
}
|
|
|
|
this.tests = function () {
|
|
return r.name([
|
|
isBold_ReturnTrue,
|
|
isBold_ReturnFalse,
|
|
isItalic_ReturnTrue,
|
|
hasUnderline_ReturnTrue,
|
|
hasStrikeThrough_ReturnTrue,
|
|
|
|
isAligned_MixedValues_ReturnsFalse,
|
|
isAligned_Center_ReturnsTrue,
|
|
isAligned_Justify_ReturnsTrue,
|
|
isAligned_Left_ReturnsTrue,
|
|
isAligned_Start_ReturnsTrue,
|
|
isAligned_LeftOrStart_ReturnsTrue,
|
|
isAligned_Right_ReturnsTrue,
|
|
isAligned_End_ReturnsTrue,
|
|
isAligned_RightOrEnd_ReturnsTrue
|
|
]);
|
|
};
|
|
this.asyncTests = function () {
|
|
return [];
|
|
};
|
|
};
|
|
gui.StyleSummaryTests.prototype.description = function () {
|
|
"use strict";
|
|
return "Test the StyleSummary class.";
|
|
};
|
|
(function () {
|
|
"use strict";
|
|
return gui.StyleSummaryTests;
|
|
}());
|