-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVipsParser.ts
1111 lines (911 loc) · 30.8 KB
/
VipsParser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///<reference path="VipsBlock.ts"/>
/**
* Class that parses blocks on page and finds visual blocks.
* @author Tomas Popela
* @author Lars Meyer
*/
class VipsParser {
private _vipsBlocks: VipsBlock = null;
private _currentVipsBlock: VipsBlock = null;
private _tempVipsBlock: VipsBlock = null;
private _sizeThresholdWidth: number = 0;
private _sizeThresholdHeight: number = 0;
private _visualBlocksCount: number = 0;
private _pageWidth: number = 0;
private _pageHeight: number = 0;
_cnt: number = 0;
/**
* Default constructor
*/
constructor() {
this._vipsBlocks = new VipsBlock();
this._sizeThresholdHeight = 80;
this._sizeThresholdWidth = 80;
this._pageWidth = window.innerWidth;
this._pageHeight = window.innerHeight;
}
/**
* Starts visual page segmentation on given page
*/
public parse(): void {
this._vipsBlocks = new VipsBlock();
this._visualBlocksCount = 0;
this.constructVipsBlockTree(new ElementBox(document.getElementsByTagName("body").item(0)), this._vipsBlocks);
this.divideVipsBlockTree(this._vipsBlocks);
this.getVisualBlocksCount(this._vipsBlocks);
}
/**
* Counts number of visual blocks in visual structure
* @param vipsBlock Visual structure
*/
private getVisualBlocksCount(vipsBlock: VipsBlock) {
if (vipsBlock.isVisualBlock()) {
this._visualBlocksCount++;
}
for (let vipsBlockChild of vipsBlock.getChildren()) {
if (!(vipsBlockChild.getBox() instanceof TextBox)) {
this.getVisualBlocksCount(vipsBlockChild);
}
}
}
/**
* Construct VIPS block tree from viewport.
*
* Starts from <body> element.
* @param element Box that represents element
* @param node Visual structure tree node
*/
private constructVipsBlockTree(element: Box, node: VipsBlock) {
node.setBox(element);
if (!(element instanceof TextBox)) {
for (let box of (<ElementBox> element).getSubBoxList()) {
node.addChild(new VipsBlock());
this.constructVipsBlockTree(box, node.getChildren()[node.getChildren().length - 1]);
}
}
}
/**
* Tries to divide DOM elements and finds visual blocks.
* @param vipsBlock Visual structure
*/
private divideVipsBlockTree(vipsBlock: VipsBlock): void {
this._currentVipsBlock = vipsBlock;
let elementBox: ElementBox = <ElementBox> vipsBlock.getBox();
// With VIPS rules it tries to determine if element is dividable
if (this.applyVipsRules(elementBox) && vipsBlock.isDividable() && !vipsBlock.isVisualBlock()) {
// if element is dividable, let's divide it
this._currentVipsBlock.setAlreadyDivided(true);
for (let vipsBlockChild of vipsBlock.getChildren()) {
if (!(vipsBlockChild.getBox() instanceof TextBox)) {
this.divideVipsBlockTree(vipsBlockChild);
}
}
} else {
if (vipsBlock.isDividable()) {
vipsBlock.setIsVisualBlock(true);
vipsBlock.setDoC(11);
}
if (!this.verifyValidity(elementBox)) {
this._currentVipsBlock.setIsVisualBlock(false);
}
}
}
private getAllTextLength(node: ElementBox): number {
let childrenTextNodes: Array<Box> = new Array<Box>();
this.findTextChildrenNodes(node, childrenTextNodes);
let textLength: number = 0;
for (let child of childrenTextNodes) {
let childText: string = child.getText();
if (!(childText === "") && !(childText === " ") && !(childText === "\n")) {
textLength += childText.length;
}
}
return textLength;
}
private getAllChildren(node: Box, children: Array<Box>): void {
children.push(node);
if (node instanceof TextBox) {
return;
}
for (let child of (<ElementBox> node).getSubBoxList()) {
this.getAllChildren(child, children);
}
}
private verifyValidity(node: ElementBox): boolean {
if (node.getElement().getBoundingClientRect().left < 0 || node.getElement().getBoundingClientRect().top < 0) {
return false;
}
if (node.getElement().getBoundingClientRect().left + node.getElement().getBoundingClientRect().width > this._pageWidth) {
return false;
}
if (node.getElement().getBoundingClientRect().top + node.getElement().getBoundingClientRect().height > this._pageHeight) {
return false;
}
if (node.getElement().getBoundingClientRect().width <= 0 || node.getElement().getBoundingClientRect().height <= 0) {
return false;
}
if (!node.isDisplayed()) {
return false;
}
if (!node.isVisible()) {
return false;
}
if (this.getAllTextLength(node) === 0) {
let children: Array<Box> = new Array<Box>();
this.getAllChildren(node, children);
for (let child of children) {
let childNodeName: string = child.getNode().nodeName;
if (!child.isVisible()) {
continue;
}
if (childNodeName.toLowerCase() === "img") {
return true;
}
if (childNodeName.toLowerCase() === "input") {
return true;
}
}
return false;
}
return true;
}
/**
* Checks if node is a valid node.
*
* Node is valid if it's visible in the browser. This means that the node's
* width and height are not zero.
*
* @param node
* Input node
*
* @return True if node is valid, otherwise false.
*/
private isValidNode(node: ElementBox): boolean {
if (node.getElement().clientHeight > 0 && node.getElement().clientWidth > 0) {
return true;
} else {
return false;
}
}
/**
* Checks if node is a text node.
*
* @param box
* Input node
*
* @return True if node is a text node, otherwise false.
*/
private isTextNode(box: ElementBox): boolean {
return box.getNode().nodeName.toLowerCase() === "text";
}
/**
* Checks if node is a virtual text node.
*
* Inline nodes with only text node children are virtual text nodes.
*
* @param node
* Input node
*
* @return True if node is virtual text node, otherwise false.
*/
private isVirtualTextNode1(node: ElementBox): boolean {
if (node.isBlock()) {
return false;
}
for (let childNode of node.getSubBoxList()) {
if (!(childNode instanceof TextBox)) {
if (!this.isTextNode(<ElementBox> childNode)) {
return false;
}
}
}
return true;
}
/**
* Checks if node is a virtual text node.
*
* Inline nodes with only text node and virtual text node children are
* virtual text nodes.
*
* @param node
* Input node
*
* @return True if node is virtual text node, otherwise false.
*/
private isVirtualTextNode2(node: ElementBox): boolean {
if (node.isBlock()) {
return false;
}
for (let childNode of node.getSubBoxList()) {
if (!this.isTextNode(<ElementBox> childNode) ||
this.isVirtualTextNode1(<ElementBox> childNode)) {
return false;
}
}
return true;
}
/**
* Checks if node is virtual text node.
*
* @param node
* Input node
*
* @return True if node is virtual text node, otherwise false.
*/
private isVirtualTextNode(node: ElementBox): boolean {
if (this.isVirtualTextNode1(node)) {
return true;
}
if (this.isVirtualTextNode2(node)) {
return true;
}
return false;
}
private checkValidChildrenNodes(node: Box): void {
if (node instanceof TextBox) {
if (!(node.getText() === " ")) {
this._cnt++;
}
return;
} else {
if (this.isValidNode(<ElementBox> node)) {
this._cnt++;
}
}
for (let childNode of (<ElementBox> node).getSubBoxList()) {
this.checkValidChildrenNodes(childNode);
}
}
/*
* Checks if node has valid children nodes
*/
private hasValidChildrenNodes(node: ElementBox): boolean {
if (node.getNode().nodeName.toLowerCase() === "img" || node.getNode().nodeName.toLowerCase() === "input") {
if (node.getElement().clientWidth > 0 && node.getElement().clientHeight > 0) {
this._currentVipsBlock.setIsVisualBlock(true);
this._currentVipsBlock.setDoC(8);
return true;
} else {
return false;
}
}
if (node.getSubBoxList().length === 0) {
return false;
}
this._cnt = 0;
for (let child of node.getSubBoxList()) {
this.checkValidChildrenNodes(child);
}
return (this._cnt > 0) ? true : false;
}
/*
* Returns the number of node's valid children
*/
private numberOfValidChildNodes(node: ElementBox): number {
this._cnt = 0;
if (node.getSubBoxList().length === 0) {
return this._cnt;
}
for (let child of node.getSubBoxList()) {
this.checkValidChildrenNodes(child);
}
return this._cnt;
}
/**
* On different DOM nodes it applies different sets of VIPS rules.
* @param node DOM node
* @return Returns true if element is dividable, otherwise false.
*/
private applyVipsRules(node: ElementBox): boolean {
let retVal: boolean = false;
if (!node.isBlock()) {
retVal = this.applyInlineTextNodeVipsRules(node);
} else if (node.getNode().nodeName.toLowerCase() === "table") {
retVal = this.applyTableNodeVipsRules(node);
} else if (node.getNode().nodeName.toLowerCase() === "tr") {
retVal = this.applyTrNodeVipsRules(node);
} else if (node.getNode().nodeName.toLowerCase() === "td") {
retVal = this.applyTdNodeVipsRules(node);
} else if (node.getNode().nodeName.toLowerCase() === "p") {
retVal = this.applyPNodeVipsRules(node);
} else {
retVal = this.applyOtherNodeVipsRules(node);
}
return retVal;
}
/**
* Applies VIPS rules on block nodes other than <P>, <TD>, <TR>, <TABLE>.
*
* @param node Node
* @return Returns true if one of the rules succeeds and node is dividable.
*/
private applyOtherNodeVipsRules(node: ElementBox): boolean {
// 1 2 3 4 6 8 9 11
if (this.ruleOne(node))
return true;
if (this.ruleTwo(node))
return true;
if (this.ruleThree(node))
return true;
if (this.ruleFour(node))
return true;
if (this.ruleSix(node))
return true;
if (this.ruleEight(node))
return true;
if (this.ruleNine(node))
return true;
if (this.ruleEleven(node))
return true;
return false;
}
/**
* Applies VIPS rules on <P> nodes.
* @param node Node
* @return Returns true if one of the rules succeeds and node is dividable.
*/
private applyPNodeVipsRules(node: ElementBox): boolean {
// 1 2 3 4 5 6 8 9 11
if (this.ruleOne(node))
return true;
if (this.ruleTwo(node))
return true;
if (this.ruleThree(node))
return true;
if (this.ruleFour(node))
return true;
if (this.ruleFive(node))
return true;
if (this.ruleSix(node))
return true;
if (this.ruleSeven(node))
return true;
if (this.ruleEight(node))
return true;
if (this.ruleNine(node))
return true;
if (this.ruleTen(node))
return true;
if (this.ruleEleven(node))
return true;
if (this.ruleTwelve(node))
return true;
return false;
}
/**
* Applies VIPS rules on <TD> nodes.
* @param node Node
* @return Returns true if one of the rules succeeds and node is dividable.
*/
private applyTdNodeVipsRules(node: ElementBox): boolean {
// 1 2 3 4 8 9 10 12
if (this.ruleOne(node))
return true;
if (this.ruleTwo(node))
return true;
if (this.ruleThree(node))
return true;
if (this.ruleFour(node))
return true;
if (this.ruleEight(node))
return true;
if (this.ruleNine(node))
return true;
if (this.ruleTen(node))
return true;
if (this.ruleTwelve(node))
return true;
return false;
}
/**
* Applies VIPS rules on <TR> nodes.
* @param node Node
* @return Returns true if one of the rules succeeds and node is dividable.
*/
private applyTrNodeVipsRules(node: ElementBox): boolean {
// 1 2 3 7 9 12
if (this.ruleOne(node))
return true;
if (this.ruleTwo(node))
return true;
if (this.ruleThree(node))
return true;
if (this.ruleSeven(node))
return true;
if (this.ruleNine(node))
return true;
if (this.ruleTwelve(node))
return true;
return false;
}
/**
* Applies VIPS rules on <TABLE> nodes.
* @param node Node
* @return Returns true if one of the rules succeeds and node is dividable.
*/
private applyTableNodeVipsRules(node: ElementBox): boolean {
// 1 2 3 7 9 12
if (this.ruleOne(node))
return true;
if (this.ruleTwo(node))
return true;
if (this.ruleThree(node))
return true;
if (this.ruleSeven(node))
return true;
if (this.ruleNine(node))
return true;
if (this.ruleTwelve(node))
return true;
return false;
}
/**
* Applies VIPS rules on inline nodes.
* @param node Node
* @return Returns true if one of the rules succeeds and node is dividable.
*/
private applyInlineTextNodeVipsRules(node: ElementBox): boolean {
// 1 2 3 4 5 6 8 9 11
if (this.ruleOne(node))
return true;
if (this.ruleTwo(node))
return true;
if (this.ruleThree(node))
return true;
if (this.ruleFour(node))
return true;
if (this.ruleFive(node))
return true;
if (this.ruleSix(node))
return true;
if (this.ruleEight(node))
return true;
if (this.ruleNine(node))
return true;
if (this.ruleTwelve(node))
return true;
return false;
}
/**
* VIPS Rule One
*
* If the DOM node is not a text node and it has no valid children, then
* this node cannot be divided and will be cut.
*
* @param node
* Input node
*
* @return True if rule is applied, otherwise false.
*/
private ruleOne(node: ElementBox): boolean {
if (!this.isTextNode(node)) {
if (!this.hasValidChildrenNodes(node)) {
this._currentVipsBlock.setIsDividable(false);
return true;
}
} else {
return false;
}
}
/**
* VIPS Rule Two
*
* If the DOM node has only one valid child and the child is not a text
* node, then divide this node
*
* @param node
* Input node
*
* @return True if rule is applied, otherwise false.
*/
private ruleTwo(node: ElementBox): boolean {
if (this.numberOfValidChildNodes(node) === 1) {
if (node.getSubBox(0) instanceof TextBox) {
return false;
}
if (!this.isTextNode(<ElementBox> node.getSubBox(0))) {
return true;
}
}
return false;
}
/**
* VIPS Rule Three
*
* If the DOM node is the root node of the sub-DOM tree (corresponding to
* the block), and there is only one sub DOM tree corresponding to this
* block, divide this node.
*
* @param node
* Input node
*
* @return True if rule is applied, otherwise false.
*/
private ruleThree(node: ElementBox): boolean {
if (!node.isRootElement()) {
return false;
}
let result: boolean = true;
let cnt: number = 0;
for (let vipsBlock of this._vipsBlocks.getChildren()) {
if (vipsBlock.getBox().getNode().nodeName === node.getNode().nodeName) {
result = true;
this.isOnlyOneDomSubTree(node.getNode(), vipsBlock.getBox().getNode(), result);
if (result) {
cnt++;
}
}
}
return (cnt === 1) ? true : false;
}
/**
* Checks if node's subtree is unique in DOM tree.
* @param pattern Node for comparing
* @param node Node from DOM tree
* @param result True if element is unique, otherwise false
*/
private isOnlyOneDomSubTree(pattern: Node, node: Node, result: boolean): void {
if (!(pattern.nodeName === node.nodeName)) {
result = false;
}
if (pattern.childNodes.length != node.childNodes.length) {
result = false;
}
if (!result) {
return;
}
for (let i: number = 0; i < pattern.childNodes.length; i++) {
this.isOnlyOneDomSubTree(pattern.childNodes[i], node.childNodes[i], result);
}
}
/**
* VIPS Rule Four
*
* If all of the child nodes of the DOM node are text nodes or virtual text
* nodes, do not divide the node.
* If the font size and font weight of all these child nodes are the same, set
* the DoC of the extracted block to 10.
* Otherwise, set the DoC of this extracted block to 9.
*
* @param node
* Input node
*
* @return True if rule is applied, otherwise false.
*/
private ruleFour(node: ElementBox): boolean {
if (node.getSubBoxList().length === 0) {
return false;
}
for (let box of node.getSubBoxList()) {
if (box instanceof TextBox) {
continue;
}
if (!this.isTextNode(<ElementBox> box) ||
!this.isVirtualTextNode(<ElementBox> box)) {
return false;
}
}
this._currentVipsBlock.setIsVisualBlock(true);
this._currentVipsBlock.setIsDividable(false);
if (node.getSubBoxList().length === 1) {
if (node.getSubBox(0).getNode().nodeName.toLowerCase() === "em") {
this._currentVipsBlock.setDoC(11);
} else {
this._currentVipsBlock.setDoC(10);
}
return true;
}
let fontWeight: string = "";
let fontSize: number = 0;
for (let childNode of node.getSubBoxList()) {
let childFontSize: number;
if (childNode.getNode().nodeType === Node.TEXT_NODE) {
childFontSize = Number(window.getComputedStyle((<Text> childNode.getNode()).parentElement, null).getPropertyValue('font-size'));
} else if (childNode.getNode().nodeType === Node.ELEMENT_NODE) {
childFontSize = Number(window.getComputedStyle(<Element> childNode.getNode(), null).getPropertyValue('font-size'));
}
if (childNode instanceof TextBox) {
if (fontSize > 0) {
if (fontSize != childFontSize) {
this._currentVipsBlock.setDoC(9);
break;
} else {
this._currentVipsBlock.setDoC(10);
}
} else {
fontSize = childFontSize;
}
continue;
}
let child: ElementBox = <ElementBox> childNode;
if (window.getComputedStyle(child.getElement()).getPropertyValue("font-weight") === null) {
return false;
}
if (fontSize > 0) {
if (window.getComputedStyle(child.getElement()).getPropertyValue("font-weight").toString() === fontWeight
&& childFontSize === fontSize) {
this._currentVipsBlock.setDoC(10);
} else {
this._currentVipsBlock.setDoC(9);
break;
}
} else {
fontWeight = window.getComputedStyle(child.getElement()).getPropertyValue("font-weight").toString();
fontSize = childFontSize;
}
}
return true;
}
/**
* VIPS Rule Five
*
* If one of the child nodes of the DOM node is a line-break node, then
* divide this DOM node.
*
* @param node
* Input node
*
* @return True if rule is applied, otherwise false.
*/
private ruleFive(node: ElementBox): boolean {
if (node.getSubBoxList().length === 0) {
return false;
}
for (let childNode of node.getSubBoxList()) {
if (childNode.isBlock()) {
return true;
}
}
return false;
}
/**
* VIPS Rule Six
*
* If one of the child nodes of the DOM node has HTML tag <hr>, then
* divide this DOM node
*
* @param node
* Input node
*
* @return True if rule is applied, otherwise false.
*/
private ruleSix(node: ElementBox): boolean {
if (node.getSubBoxList().length === 0) {
return false;
}
let children: Array<Box> = new Array<Box>();
this.getAllChildren(node, children);
for (let child of children) {
if (child.getNode().nodeName.toLowerCase() === "hr") {
return true;
}
}
return false;
}
/**
* VIPS Rule Seven
*
* If the background color of this node is different from one of its
* children’s, divide this node and at the same time, the child nodes with
* different background color will not be divided in this round.
* Set the DoC value (6-8) for the child nodes based on the html
* tag of the child node and the size of the child node.
*
* @param node
* Input node
*
* @return True if rule is applied, otherwise false.
*/
private ruleSeven(node: ElementBox): boolean {
if (node.getSubBoxList().length === 0) {
return false;
}
if (this.isTextNode(node)) {
return false;
}
let nodeBgColor: string = this._currentVipsBlock.getBgColor();
for (let vipsStructureChild of this._currentVipsBlock.getChildren()) {
if (!(vipsStructureChild.getBgColor() === nodeBgColor)) {
vipsStructureChild.setIsDividable(false);
vipsStructureChild.setIsVisualBlock(true);
vipsStructureChild.setDoC(7);
return true;
}
}
return false;
}
private findTextChildrenNodes(node: Box, results: Array<Box>): void {
if (node instanceof TextBox) {
results.push(node);
return;
}
for (let childNode of (<ElementBox> node).getSubBoxList()) {
this.findTextChildrenNodes(childNode, results);
}
}
/**
* VIPS Rule Eight
*
* If the node has at least one text node child or at least one virtual
* text node child, and the node's relative size is smaller than
* a threshold, then the node cannot be divided.
* Set the DoC value (from 5-8) based on the html tag of the node.
* @param node
* Input node
*
* @return True if rule is applied, otherwise false.
*/
private ruleEight(node: ElementBox): boolean {
if (node.getSubBoxList().length === 0) {
return false;
}
let children: Array<Box> = new Array<Box>();
this.findTextChildrenNodes(node, children);
let cnt: number = children.length;
if (cnt === 0) {
return false;
}
if (node.getElement().clientWidth === 0 || node.getElement().clientHeight === 0) {
while (children.length > 0) {
children.pop();
}
this.getAllChildren(node, children);
for (let child of children) {
if (child.getNode().nodeType === Node.ELEMENT_NODE) {
let childNode: Element = <Element> child.getNode();
if (childNode.clientWidth != 0 && childNode.clientHeight != 0) {
return true;
}
}
}
}
if (node.getElement().clientWidth * node.getElement().clientHeight > this._sizeThresholdHeight * this._sizeThresholdWidth) {
return false;
}
if (node.getNode().nodeName.toLowerCase() === "ul") {
return true;
}
this._currentVipsBlock.setIsVisualBlock(true);
this._currentVipsBlock.setIsDividable(false);
if (node.getNode().nodeName.toLowerCase() === "Xdiv") {
this._currentVipsBlock.setDoC(7);
} else if (node.getNode().nodeName.toLowerCase() === "code") {
this._currentVipsBlock.setDoC(7);
} else if (node.getNode().nodeName.toLowerCase() === "div") {
this._currentVipsBlock.setDoC(5);
} else {
this._currentVipsBlock.setDoC(8);
}
return true;
}
/**
* VIPS Rule Nine
*
* If the children of the node with maximum size are smaller than
* a threshold (relative size), do not divide this node.
* Set the DoC based on the html tag and size of this node.
* @param node
* Input node
*
* @return True if rule is applied, otherwise false.
*/
private ruleNine(node: ElementBox): boolean {
if (node.getSubBoxList().length === 0) {
return false;
}
let maxSize: number = 0;
for (let childNode of node.getSubBoxList()) {
if (childNode.getNode().nodeType === Node.ELEMENT_NODE) {
let childElement: Element = <Element> childNode.getNode();
let childSize: number = childElement.clientWidth * childElement.clientHeight;
if (maxSize < childSize) {
maxSize = childSize;
}
}
}
if (maxSize > this._sizeThresholdWidth * this._sizeThresholdHeight) {
return true;
}
this._currentVipsBlock.setIsVisualBlock(true);
this._currentVipsBlock.setIsDividable(false);
if (node.getNode().nodeName.toLowerCase() === "Xdiv") {
this._currentVipsBlock.setDoC(7);
}
if (node.getNode().nodeName.toLowerCase() === "a") {