-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhtmlstrip_module_old.cpp
139 lines (109 loc) · 4.46 KB
/
htmlstrip_module_old.cpp
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
#include <node.h>
#include "v8.h"
#include "htmlstrip.hpp"
using namespace v8;
Persistent<String> chars_written_sym;
Persistent<String> include_script_sym;
Persistent<String> include_style_sym;
Persistent<String> compact_whitespace_sym;
Persistent<String> include_attributes_sym;
Persistent<String> taghints_sym;
Persistent<String> taghints_pos_sym;
Persistent<String> taghints_type_sym;
Persistent<String> taghints_type_script_sym;
Persistent<String> taghints_type_style_sym;
Persistent<String> taghints_type_any_sym;
Persistent<String> taghints_type_attribute_sym;
Persistent<String> taghints_type_end_sym;
void initOptions(Local<Object> optsObj, HtmlStripOptions& opts) {
if(!optsObj.IsEmpty()){
if(optsObj->Has(include_script_sym)){
opts.include_script = optsObj->Get(include_script_sym)->ToBoolean()->Value();
}
if(optsObj->Has(include_style_sym)){
opts.include_style = optsObj->Get(include_style_sym)->ToBoolean()->Value();
}
if(optsObj->Has(compact_whitespace_sym)){
opts.compact_whitespace = optsObj->Get(compact_whitespace_sym)->ToBoolean()->Value();
}
if(optsObj->Has(include_attributes_sym)){
opts.include_attributes = true;
opts.includeAttributesMap = optsObj->Get(include_attributes_sym)->ToObject();
Local< String > allAttr = String::New("*");
opts.include_all_attributes = opts.includeAttributesMap->Has(allAttr);
}
}
}
Handle<Value> HtmlStripFunc(const Arguments& args) {
HandleScope scope;
uint16_t* inBuf = NULL;
size_t inBufSize = 0;
if (args.Length() >= 2) {
inBuf = static_cast<uint16_t*>( // NULL on flush.
args[0].As<Object>()->GetIndexedPropertiesExternalArrayData());
inBufSize = args[1]->Uint32Value();
}
HtmlStripOptions opts;
// Check if we have any options passed
if(args.Length() >= 3 && !args[2].IsEmpty() && args[2]->IsObject()){
initOptions( args[2].As<Object>(), opts );
}
return scope.Close( HtmlStrip(inBuf, inBufSize, opts, NULL) );
}
Handle<Value> HtmlEntitiesDecodeFunc(const Arguments& args) {
HandleScope scope;
uint16_t* inBuf = NULL;
size_t inBufSize = 0;
if (args.Length() >= 2) {
inBuf = static_cast<uint16_t*>( // NULL on flush.
args[0].As<Object>()->GetIndexedPropertiesExternalArrayData());
inBufSize = args[1]->Uint32Value();
}
return scope.Close( HtmlEntitiesDecode(inBuf, inBufSize, NULL) );
}
Handle<Value> AccentedCharsNormalizeFunc(const Arguments& args) {
HandleScope scope;
uint16_t* inBuf = NULL;
size_t inBufSize = 0;
if (args.Length() >= 2) {
inBuf = static_cast<uint16_t*>( // NULL on flush.
args[0].As<Object>()->GetIndexedPropertiesExternalArrayData());
inBufSize = args[1]->Uint32Value();
}
return scope.Close( AccentedCharsNormalize(inBuf, inBufSize, NULL) );
}
Handle<Value> AccentedCharsStripFunc(const Arguments& args) {
HandleScope scope;
uint16_t* inBuf = NULL;
size_t inBufSize = 0;
if (args.Length() >= 2) {
inBuf = static_cast<uint16_t*>( // NULL on flush.
args[0].As<Object>()->GetIndexedPropertiesExternalArrayData());
inBufSize = args[1]->Uint32Value();
}
return scope.Close( AccentedCharsStrip(inBuf, inBufSize, NULL) );
}
void RegisterModule(Handle<Object> target) {
chars_written_sym = NODE_PSYMBOL("_charsWritten");
include_script_sym = NODE_PSYMBOL("include_script");
include_style_sym = NODE_PSYMBOL("include_style");
compact_whitespace_sym = NODE_PSYMBOL("compact_whitespace");
include_attributes_sym = NODE_PSYMBOL("include_attributes");
taghints_sym = NODE_PSYMBOL("tag_hints");
taghints_pos_sym = NODE_PSYMBOL("pos");
taghints_type_sym = NODE_PSYMBOL("type");
taghints_type_script_sym = NODE_PSYMBOL("script");
taghints_type_style_sym = NODE_PSYMBOL("style");
taghints_type_any_sym = NODE_PSYMBOL("any");
taghints_type_attribute_sym = NODE_PSYMBOL("attribute");
taghints_type_end_sym = NODE_PSYMBOL("end");
target->Set(String::NewSymbol("html_strip"),
FunctionTemplate::New(HtmlStripFunc)->GetFunction());
target->Set(String::NewSymbol("html_entities_decode"),
FunctionTemplate::New(HtmlEntitiesDecodeFunc)->GetFunction());
target->Set(String::NewSymbol("accented_chars_norm"),
FunctionTemplate::New(AccentedCharsNormalizeFunc)->GetFunction());
target->Set(String::NewSymbol("accented_chars_strip"),
FunctionTemplate::New(AccentedCharsStripFunc)->GetFunction());
}
NODE_MODULE(htmlstrip, RegisterModule);