@salve-software/react-native-nitro-jsdom 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NitroJsdom.podspec +63 -0
- package/README.md +95 -0
- package/android/CMakeLists.txt +86 -0
- package/android/build.gradle +148 -0
- package/android/fix-prefab.gradle +51 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/cpp-adapter.cpp +9 -0
- package/android/src/main/java/com/nitrojsdom/NitroJsdomPackage.kt +20 -0
- package/cpp/HybridHtmlSandbox.cpp +171 -0
- package/cpp/HybridHtmlSandbox.hpp +47 -0
- package/cpp/lexbor/LexborDocument.cpp +365 -0
- package/cpp/lexbor/LexborDocument.hpp +71 -0
- package/cpp/quickjs/DOMBindings.cpp +57 -0
- package/cpp/quickjs/DOMBindings.hpp +21 -0
- package/cpp/quickjs/DOMBindingsInternal.cpp +157 -0
- package/cpp/quickjs/DOMBindingsInternal.hpp +83 -0
- package/cpp/quickjs/MutationObservers.cpp +546 -0
- package/cpp/quickjs/MutationObservers.hpp +127 -0
- package/cpp/quickjs/QuickJSRuntime.cpp +475 -0
- package/cpp/quickjs/QuickJSRuntime.hpp +110 -0
- package/cpp/quickjs/Storage.cpp +130 -0
- package/cpp/quickjs/Storage.hpp +40 -0
- package/cpp/quickjs/bindings/ClassListBindings.cpp +241 -0
- package/cpp/quickjs/bindings/ClassListBindings.hpp +29 -0
- package/cpp/quickjs/bindings/DatasetBindings.cpp +120 -0
- package/cpp/quickjs/bindings/DatasetBindings.hpp +20 -0
- package/cpp/quickjs/bindings/DocumentBindings.cpp +155 -0
- package/cpp/quickjs/bindings/DocumentBindings.hpp +19 -0
- package/cpp/quickjs/bindings/ElementBindings.cpp +1106 -0
- package/cpp/quickjs/bindings/ElementBindings.hpp +19 -0
- package/cpp/quickjs/bindings/EventBindings.cpp +352 -0
- package/cpp/quickjs/bindings/EventBindings.hpp +21 -0
- package/cpp/quickjs/bindings/FetchBindings.cpp +347 -0
- package/cpp/quickjs/bindings/FetchBindings.hpp +16 -0
- package/cpp/quickjs/bindings/StyleBindings.cpp +245 -0
- package/cpp/quickjs/bindings/StyleBindings.hpp +22 -0
- package/cpp/quickjs/bindings/TimerBindings.cpp +94 -0
- package/cpp/quickjs/bindings/TimerBindings.hpp +16 -0
- package/cpp/quickjs/bindings/WindowBindings.cpp +211 -0
- package/cpp/quickjs/bindings/WindowBindings.hpp +16 -0
- package/ios/Bridge.h +8 -0
- package/lib/commonjs/classes/JSDOM/JSDOM.class.js +138 -0
- package/lib/commonjs/classes/JSDOM/JSDOM.class.js.map +1 -0
- package/lib/commonjs/classes/JSDOM/index.js +17 -0
- package/lib/commonjs/classes/JSDOM/index.js.map +1 -0
- package/lib/commonjs/classes/JSDOM/types/IFetchOptions.js +2 -0
- package/lib/commonjs/classes/JSDOM/types/IFetchOptions.js.map +1 -0
- package/lib/commonjs/classes/JSDOM/types/IJSDOMOptions.js +6 -0
- package/lib/commonjs/classes/JSDOM/types/IJSDOMOptions.js.map +1 -0
- package/lib/commonjs/classes/JSDOM/types/index.js +2 -0
- package/lib/commonjs/classes/JSDOM/types/index.js.map +1 -0
- package/lib/commonjs/classes/index.js +17 -0
- package/lib/commonjs/classes/index.js.map +1 -0
- package/lib/commonjs/index.js +17 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/specs/HtmlSandbox.nitro.js +6 -0
- package/lib/commonjs/specs/HtmlSandbox.nitro.js.map +1 -0
- package/lib/module/classes/JSDOM/JSDOM.class.js +133 -0
- package/lib/module/classes/JSDOM/JSDOM.class.js.map +1 -0
- package/lib/module/classes/JSDOM/index.js +4 -0
- package/lib/module/classes/JSDOM/index.js.map +1 -0
- package/lib/module/classes/JSDOM/types/IFetchOptions.js +2 -0
- package/lib/module/classes/JSDOM/types/IFetchOptions.js.map +1 -0
- package/lib/module/classes/JSDOM/types/IJSDOMOptions.js +4 -0
- package/lib/module/classes/JSDOM/types/IJSDOMOptions.js.map +1 -0
- package/lib/module/classes/JSDOM/types/index.js +2 -0
- package/lib/module/classes/JSDOM/types/index.js.map +1 -0
- package/lib/module/classes/index.js +4 -0
- package/lib/module/classes/index.js.map +1 -0
- package/lib/module/index.js +4 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/specs/HtmlSandbox.nitro.js +4 -0
- package/lib/module/specs/HtmlSandbox.nitro.js.map +1 -0
- package/lib/typescript/src/classes/JSDOM/JSDOM.class.d.ts +74 -0
- package/lib/typescript/src/classes/JSDOM/JSDOM.class.d.ts.map +1 -0
- package/lib/typescript/src/classes/JSDOM/index.d.ts +3 -0
- package/lib/typescript/src/classes/JSDOM/index.d.ts.map +1 -0
- package/lib/typescript/src/classes/JSDOM/types/IFetchOptions.d.ts +14 -0
- package/lib/typescript/src/classes/JSDOM/types/IFetchOptions.d.ts.map +1 -0
- package/lib/typescript/src/classes/JSDOM/types/IJSDOMOptions.d.ts +57 -0
- package/lib/typescript/src/classes/JSDOM/types/IJSDOMOptions.d.ts.map +1 -0
- package/lib/typescript/src/classes/JSDOM/types/index.d.ts +3 -0
- package/lib/typescript/src/classes/JSDOM/types/index.d.ts.map +1 -0
- package/lib/typescript/src/classes/index.d.ts +2 -0
- package/lib/typescript/src/classes/index.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +2 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/specs/HtmlSandbox.nitro.d.ts +13 -0
- package/lib/typescript/src/specs/HtmlSandbox.nitro.d.ts.map +1 -0
- package/nitro.json +26 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/NitroJsdom+autolinking.cmake +81 -0
- package/nitrogen/generated/android/NitroJsdom+autolinking.gradle +27 -0
- package/nitrogen/generated/android/NitroJsdomOnLoad.cpp +49 -0
- package/nitrogen/generated/android/NitroJsdomOnLoad.hpp +34 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrojsdom/NitroJsdomOnLoad.kt +35 -0
- package/nitrogen/generated/ios/NitroJsdom+autolinking.rb +62 -0
- package/nitrogen/generated/ios/NitroJsdom-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/NitroJsdom-Swift-Cxx-Bridge.hpp +27 -0
- package/nitrogen/generated/ios/NitroJsdom-Swift-Cxx-Umbrella.hpp +38 -0
- package/nitrogen/generated/ios/NitroJsdomAutolinking.mm +35 -0
- package/nitrogen/generated/ios/NitroJsdomAutolinking.swift +16 -0
- package/nitrogen/generated/shared/c++/HybridHtmlSandboxSpec.cpp +26 -0
- package/nitrogen/generated/shared/c++/HybridHtmlSandboxSpec.hpp +73 -0
- package/package.json +131 -0
- package/src/classes/JSDOM/JSDOM.class.ts +145 -0
- package/src/classes/JSDOM/index.ts +2 -0
- package/src/classes/JSDOM/types/IFetchOptions.ts +14 -0
- package/src/classes/JSDOM/types/IJSDOMOptions.ts +57 -0
- package/src/classes/JSDOM/types/index.ts +2 -0
- package/src/classes/index.ts +1 -0
- package/src/index.ts +1 -0
- package/src/specs/HtmlSandbox.nitro.ts +33 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// NitroJsdomOnLoad.kt
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
package com.margelo.nitro.nitrojsdom
|
|
9
|
+
|
|
10
|
+
import android.util.Log
|
|
11
|
+
|
|
12
|
+
internal class NitroJsdomOnLoad {
|
|
13
|
+
companion object {
|
|
14
|
+
private const val TAG = "NitroJsdomOnLoad"
|
|
15
|
+
private var didLoad = false
|
|
16
|
+
/**
|
|
17
|
+
* Initializes the native part of "NitroJsdom".
|
|
18
|
+
* This method is idempotent and can be called more than once.
|
|
19
|
+
*/
|
|
20
|
+
@JvmStatic
|
|
21
|
+
fun initializeNative() {
|
|
22
|
+
if (didLoad) return
|
|
23
|
+
try {
|
|
24
|
+
Log.i(TAG, "Loading NitroJsdom C++ library...")
|
|
25
|
+
System.loadLibrary("NitroJsdom")
|
|
26
|
+
Log.i(TAG, "Successfully loaded NitroJsdom C++ library!")
|
|
27
|
+
didLoad = true
|
|
28
|
+
} catch (e: Error) {
|
|
29
|
+
Log.e(TAG, "Failed to load NitroJsdom C++ library! Is it properly installed and linked? " +
|
|
30
|
+
"Is the name correct? (see `CMakeLists.txt`, at `add_library(...)`)", e)
|
|
31
|
+
throw e
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#
|
|
2
|
+
# NitroJsdom+autolinking.rb
|
|
3
|
+
# This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
# https://github.com/mrousavy/nitro
|
|
5
|
+
# Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
#
|
|
7
|
+
|
|
8
|
+
# This is a Ruby script that adds all files generated by Nitrogen
|
|
9
|
+
# to the given podspec.
|
|
10
|
+
#
|
|
11
|
+
# To use it, add this to your .podspec:
|
|
12
|
+
# ```ruby
|
|
13
|
+
# Pod::Spec.new do |spec|
|
|
14
|
+
# # ...
|
|
15
|
+
#
|
|
16
|
+
# # Add all files generated by Nitrogen
|
|
17
|
+
# load 'nitrogen/generated/ios/NitroJsdom+autolinking.rb'
|
|
18
|
+
# add_nitrogen_files(spec)
|
|
19
|
+
# end
|
|
20
|
+
# ```
|
|
21
|
+
|
|
22
|
+
def add_nitrogen_files(spec)
|
|
23
|
+
Pod::UI.puts "[NitroModules] 🔥 NitroJsdom is boosted by nitro!"
|
|
24
|
+
|
|
25
|
+
spec.dependency "NitroModules"
|
|
26
|
+
|
|
27
|
+
current_source_files = Array(spec.attributes_hash['source_files'])
|
|
28
|
+
spec.source_files = current_source_files + [
|
|
29
|
+
# Generated cross-platform specs
|
|
30
|
+
"nitrogen/generated/shared/**/*.{h,hpp,c,cpp,swift}",
|
|
31
|
+
# Generated bridges for the cross-platform specs
|
|
32
|
+
"nitrogen/generated/ios/**/*.{h,hpp,c,cpp,mm,swift}",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
current_public_header_files = Array(spec.attributes_hash['public_header_files'])
|
|
36
|
+
spec.public_header_files = current_public_header_files + [
|
|
37
|
+
# Generated specs
|
|
38
|
+
"nitrogen/generated/shared/**/*.{h,hpp}",
|
|
39
|
+
# Swift to C++ bridging helpers
|
|
40
|
+
"nitrogen/generated/ios/NitroJsdom-Swift-Cxx-Bridge.hpp"
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
current_private_header_files = Array(spec.attributes_hash['private_header_files'])
|
|
44
|
+
spec.private_header_files = current_private_header_files + [
|
|
45
|
+
# iOS specific specs
|
|
46
|
+
"nitrogen/generated/ios/c++/**/*.{h,hpp}",
|
|
47
|
+
# Views are framework-specific and should be private
|
|
48
|
+
"nitrogen/generated/shared/**/views/**/*"
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
current_pod_target_xcconfig = spec.attributes_hash['pod_target_xcconfig'] || {}
|
|
52
|
+
spec.pod_target_xcconfig = current_pod_target_xcconfig.merge({
|
|
53
|
+
# Use C++ 20
|
|
54
|
+
"CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
|
|
55
|
+
# Enables C++ <-> Swift interop (by default it's only ObjC)
|
|
56
|
+
"SWIFT_OBJC_INTEROP_MODE" => "objcxx",
|
|
57
|
+
# Enables stricter modular headers
|
|
58
|
+
"DEFINES_MODULE" => "YES",
|
|
59
|
+
# Disable auto-generated ObjC header for Swift (Static linkage on Xcode 26.4 breaks here)
|
|
60
|
+
"SWIFT_INSTALL_OBJC_HEADER" => "NO",
|
|
61
|
+
})
|
|
62
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// NitroJsdom-Swift-Cxx-Bridge.cpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#include "NitroJsdom-Swift-Cxx-Bridge.hpp"
|
|
9
|
+
|
|
10
|
+
// Include C++ implementation defined types
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
namespace margelo::nitro::nitrojsdom::bridge::swift {
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
} // namespace margelo::nitro::nitrojsdom::bridge::swift
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// NitroJsdom-Swift-Cxx-Bridge.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
// Forward declarations of C++ defined types
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
// Forward declarations of Swift defined types
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// Include C++ defined types
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Contains specialized versions of C++ templated types so they can be accessed from Swift,
|
|
21
|
+
* as well as helper functions to interact with those C++ types from Swift.
|
|
22
|
+
*/
|
|
23
|
+
namespace margelo::nitro::nitrojsdom::bridge::swift {
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
} // namespace margelo::nitro::nitrojsdom::bridge::swift
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// NitroJsdom-Swift-Cxx-Umbrella.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
// Forward declarations of C++ defined types
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
// Include C++ defined types
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// C++ helpers for Swift
|
|
17
|
+
#include "NitroJsdom-Swift-Cxx-Bridge.hpp"
|
|
18
|
+
|
|
19
|
+
// Common C++ types used in Swift
|
|
20
|
+
#include <NitroModules/ArrayBufferHolder.hpp>
|
|
21
|
+
#include <NitroModules/AnyMapUtils.hpp>
|
|
22
|
+
#include <NitroModules/RuntimeError.hpp>
|
|
23
|
+
#include <NitroModules/DateToChronoDate.hpp>
|
|
24
|
+
|
|
25
|
+
// Forward declarations of Swift defined types
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// Include Swift defined types
|
|
29
|
+
#if __has_include("NitroJsdom-Swift.h")
|
|
30
|
+
// This header is generated by Xcode/Swift on every app build.
|
|
31
|
+
// If it cannot be found, make sure the Swift module's name (= podspec name) is actually "NitroJsdom".
|
|
32
|
+
#include "NitroJsdom-Swift.h"
|
|
33
|
+
// Same as above, but used when building with frameworks (`use_frameworks`)
|
|
34
|
+
#elif __has_include(<NitroJsdom/NitroJsdom-Swift.h>)
|
|
35
|
+
#include <NitroJsdom/NitroJsdom-Swift.h>
|
|
36
|
+
#else
|
|
37
|
+
#error NitroJsdom's autogenerated Swift header cannot be found! Make sure the Swift module's name (= podspec name) is actually "NitroJsdom", and try building the app first.
|
|
38
|
+
#endif
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// NitroJsdomAutolinking.mm
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#import <Foundation/Foundation.h>
|
|
9
|
+
#import <NitroModules/HybridObjectRegistry.hpp>
|
|
10
|
+
|
|
11
|
+
#import <type_traits>
|
|
12
|
+
|
|
13
|
+
#include "HybridHtmlSandbox.hpp"
|
|
14
|
+
|
|
15
|
+
@interface NitroJsdomAutolinking : NSObject
|
|
16
|
+
@end
|
|
17
|
+
|
|
18
|
+
@implementation NitroJsdomAutolinking
|
|
19
|
+
|
|
20
|
+
+ (void) load {
|
|
21
|
+
using namespace margelo::nitro;
|
|
22
|
+
using namespace margelo::nitro::nitrojsdom;
|
|
23
|
+
|
|
24
|
+
HybridObjectRegistry::registerHybridObjectConstructor(
|
|
25
|
+
"HtmlSandbox",
|
|
26
|
+
[]() -> std::shared_ptr<HybridObject> {
|
|
27
|
+
static_assert(std::is_default_constructible_v<HybridHtmlSandbox>,
|
|
28
|
+
"The HybridObject \"HybridHtmlSandbox\" is not default-constructible! "
|
|
29
|
+
"Create a public constructor that takes zero arguments to be able to autolink this HybridObject.");
|
|
30
|
+
return std::make_shared<HybridHtmlSandbox>();
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// NitroJsdomAutolinking.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import NitroModules
|
|
9
|
+
|
|
10
|
+
// TODO: Use empty enums once Swift supports exporting them as namespaces
|
|
11
|
+
// See: https://github.com/swiftlang/swift/pull/83616
|
|
12
|
+
public final class NitroJsdomAutolinking {
|
|
13
|
+
public typealias bridge = margelo.nitro.nitrojsdom.bridge.swift
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridHtmlSandboxSpec.cpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#include "HybridHtmlSandboxSpec.hpp"
|
|
9
|
+
|
|
10
|
+
namespace margelo::nitro::nitrojsdom {
|
|
11
|
+
|
|
12
|
+
void HybridHtmlSandboxSpec::loadHybridMethods() {
|
|
13
|
+
// load base methods/properties
|
|
14
|
+
HybridObject::loadHybridMethods();
|
|
15
|
+
// load custom methods/properties
|
|
16
|
+
registerHybrids(this, [](Prototype& prototype) {
|
|
17
|
+
prototype.registerHybridMethod("initialize", &HybridHtmlSandboxSpec::initialize);
|
|
18
|
+
prototype.registerHybridMethod("evaluate", &HybridHtmlSandboxSpec::evaluate);
|
|
19
|
+
prototype.registerHybridMethod("serialize", &HybridHtmlSandboxSpec::serialize);
|
|
20
|
+
prototype.registerHybridMethod("setConsoleCallback", &HybridHtmlSandboxSpec::setConsoleCallback);
|
|
21
|
+
prototype.registerHybridMethod("setDialogCallbacks", &HybridHtmlSandboxSpec::setDialogCallbacks);
|
|
22
|
+
prototype.registerHybridMethod("setFetchCallback", &HybridHtmlSandboxSpec::setFetchCallback);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
} // namespace margelo::nitro::nitrojsdom
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridHtmlSandboxSpec.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/HybridObject.hpp>)
|
|
11
|
+
#include <NitroModules/HybridObject.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
#include <string>
|
|
19
|
+
#include <NitroModules/Promise.hpp>
|
|
20
|
+
#include <NitroModules/Null.hpp>
|
|
21
|
+
#include <vector>
|
|
22
|
+
#include <functional>
|
|
23
|
+
#include <variant>
|
|
24
|
+
#include <optional>
|
|
25
|
+
|
|
26
|
+
namespace margelo::nitro::nitrojsdom {
|
|
27
|
+
|
|
28
|
+
using namespace margelo::nitro;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* An abstract base class for `HtmlSandbox`
|
|
32
|
+
* Inherit this class to create instances of `HybridHtmlSandboxSpec` in C++.
|
|
33
|
+
* You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
|
|
34
|
+
* @example
|
|
35
|
+
* ```cpp
|
|
36
|
+
* class HybridHtmlSandbox: public HybridHtmlSandboxSpec {
|
|
37
|
+
* public:
|
|
38
|
+
* HybridHtmlSandbox(...): HybridObject(TAG) { ... }
|
|
39
|
+
* // ...
|
|
40
|
+
* };
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
class HybridHtmlSandboxSpec: public virtual HybridObject {
|
|
44
|
+
public:
|
|
45
|
+
// Constructor
|
|
46
|
+
explicit HybridHtmlSandboxSpec(): HybridObject(TAG) { }
|
|
47
|
+
|
|
48
|
+
// Destructor
|
|
49
|
+
~HybridHtmlSandboxSpec() override = default;
|
|
50
|
+
|
|
51
|
+
public:
|
|
52
|
+
// Properties
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
public:
|
|
56
|
+
// Methods
|
|
57
|
+
virtual void initialize(const std::string& html, bool runScripts, const std::string& url) = 0;
|
|
58
|
+
virtual std::shared_ptr<Promise<std::string>> evaluate(const std::string& script) = 0;
|
|
59
|
+
virtual std::string serialize() = 0;
|
|
60
|
+
virtual void setConsoleCallback(const std::optional<std::variant<nitro::NullType, std::function<void(const std::string& /* level */, const std::vector<std::string>& /* args */)>>>& callback) = 0;
|
|
61
|
+
virtual void setDialogCallbacks(const std::optional<std::variant<nitro::NullType, std::function<void(const std::string& /* message */)>>>& onAlert, const std::optional<std::variant<nitro::NullType, std::function<std::shared_ptr<Promise<bool>>(const std::string& /* message */)>>>& onConfirm, const std::optional<std::variant<nitro::NullType, std::function<std::shared_ptr<Promise<std::variant<nitro::NullType, std::string>>>(const std::string& /* message */, const std::optional<std::string>& /* defaultValue */)>>>& onPrompt) = 0;
|
|
62
|
+
virtual void setFetchCallback(const std::optional<std::variant<nitro::NullType, std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::string>>>>(const std::string& /* url */, const std::string& /* method */, const std::string& /* headersJson */, const std::optional<std::string>& /* body */)>>>& callback) = 0;
|
|
63
|
+
|
|
64
|
+
protected:
|
|
65
|
+
// Hybrid Setup
|
|
66
|
+
void loadHybridMethods() override;
|
|
67
|
+
|
|
68
|
+
protected:
|
|
69
|
+
// Tag for logging
|
|
70
|
+
static constexpr auto TAG = "HtmlSandbox";
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
} // namespace margelo::nitro::nitrojsdom
|
package/package.json
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@salve-software/react-native-nitro-jsdom",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"packageManager": "yarn@4.9.1",
|
|
5
|
+
"description": "A headless HTML/DOM environment for React Native.",
|
|
6
|
+
"main": "./lib/commonjs/index.js",
|
|
7
|
+
"module": "./lib/module/index.js",
|
|
8
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
9
|
+
"react-native": "src/index",
|
|
10
|
+
"source": "src/index",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"postinstall": "bash scripts/setup-clangd.sh",
|
|
13
|
+
"ai:setup": "aiworkers setup",
|
|
14
|
+
"typecheck": "tsc --noEmit",
|
|
15
|
+
"clean": "git clean -dfX",
|
|
16
|
+
"release": "semantic-release",
|
|
17
|
+
"build": "yarn run typecheck && bob build",
|
|
18
|
+
"codegen": "nitrogen --logLevel=\"debug\" && yarn run build",
|
|
19
|
+
"postcodegen": "yarn --cwd example pod",
|
|
20
|
+
"example": "yarn workspace @salve-software/react-native-nitro-jsdom-example",
|
|
21
|
+
"docs": "yarn --cwd website build && yarn --cwd website serve",
|
|
22
|
+
"docs:dev": "yarn --cwd website start",
|
|
23
|
+
"docs:build": "yarn --cwd website build"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"react-native",
|
|
27
|
+
"@salve-software/react-native-nitro-jsdom"
|
|
28
|
+
],
|
|
29
|
+
"files": [
|
|
30
|
+
"src",
|
|
31
|
+
"react-native.config.js",
|
|
32
|
+
"lib",
|
|
33
|
+
"nitrogen",
|
|
34
|
+
"cpp",
|
|
35
|
+
"nitro.json",
|
|
36
|
+
"android/build.gradle",
|
|
37
|
+
"android/fix-prefab.gradle",
|
|
38
|
+
"android/gradle.properties",
|
|
39
|
+
"android/CMakeLists.txt",
|
|
40
|
+
"android/src",
|
|
41
|
+
"ios/**/*.h",
|
|
42
|
+
"ios/**/*.m",
|
|
43
|
+
"ios/**/*.mm",
|
|
44
|
+
"ios/**/*.cpp",
|
|
45
|
+
"ios/**/*.swift",
|
|
46
|
+
"app.plugin.js",
|
|
47
|
+
"*.podspec",
|
|
48
|
+
"README.md"
|
|
49
|
+
],
|
|
50
|
+
"workspaces": [
|
|
51
|
+
"example"
|
|
52
|
+
],
|
|
53
|
+
"repository": "https://github.com/Salve-Software/react-native-nitro-jsdom.git",
|
|
54
|
+
"author": "eumaninho54",
|
|
55
|
+
"license": "MIT",
|
|
56
|
+
"bugs": "https://github.com/Salve-Software/react-native-nitro-jsdom/issues",
|
|
57
|
+
"homepage": "https://github.com/Salve-Software/react-native-nitro-jsdom#readme",
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public",
|
|
60
|
+
"registry": "https://registry.npmjs.org/"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@jamesacarr/eslint-formatter-github-actions": "^0.2.0",
|
|
64
|
+
"@salve-software/aiworkers": "^0.3.1",
|
|
65
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
66
|
+
"@semantic-release/git": "^10.0.1",
|
|
67
|
+
"@types/jest": "^29.5.12",
|
|
68
|
+
"@types/react": "19.2.0",
|
|
69
|
+
"conventional-changelog-conventionalcommits": "^9.1.0",
|
|
70
|
+
"nitrogen": "0.35.6",
|
|
71
|
+
"react": "19.2.3",
|
|
72
|
+
"react-native": "0.84.1",
|
|
73
|
+
"react-native-builder-bob": "^0.40.18",
|
|
74
|
+
"react-native-nitro-modules": "0.35.6",
|
|
75
|
+
"semantic-release": "^25.0.3",
|
|
76
|
+
"typescript": "^5.8.3"
|
|
77
|
+
},
|
|
78
|
+
"peerDependencies": {
|
|
79
|
+
"react": "*",
|
|
80
|
+
"react-native": "*",
|
|
81
|
+
"react-native-nitro-modules": "*"
|
|
82
|
+
},
|
|
83
|
+
"eslintConfig": {
|
|
84
|
+
"root": true,
|
|
85
|
+
"extends": [
|
|
86
|
+
"@react-native",
|
|
87
|
+
"prettier"
|
|
88
|
+
],
|
|
89
|
+
"plugins": [
|
|
90
|
+
"prettier"
|
|
91
|
+
],
|
|
92
|
+
"rules": {
|
|
93
|
+
"prettier/prettier": [
|
|
94
|
+
"warn",
|
|
95
|
+
{
|
|
96
|
+
"quoteProps": "consistent",
|
|
97
|
+
"singleQuote": true,
|
|
98
|
+
"tabWidth": 2,
|
|
99
|
+
"trailingComma": "es5",
|
|
100
|
+
"useTabs": false
|
|
101
|
+
}
|
|
102
|
+
]
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
"eslintIgnore": [
|
|
106
|
+
"node_modules/",
|
|
107
|
+
"lib/"
|
|
108
|
+
],
|
|
109
|
+
"prettier": {
|
|
110
|
+
"quoteProps": "consistent",
|
|
111
|
+
"singleQuote": true,
|
|
112
|
+
"tabWidth": 2,
|
|
113
|
+
"trailingComma": "es5",
|
|
114
|
+
"useTabs": false,
|
|
115
|
+
"semi": false
|
|
116
|
+
},
|
|
117
|
+
"react-native-builder-bob": {
|
|
118
|
+
"source": "src",
|
|
119
|
+
"output": "lib",
|
|
120
|
+
"targets": [
|
|
121
|
+
"commonjs",
|
|
122
|
+
"module",
|
|
123
|
+
[
|
|
124
|
+
"typescript",
|
|
125
|
+
{
|
|
126
|
+
"project": "tsconfig.json"
|
|
127
|
+
}
|
|
128
|
+
]
|
|
129
|
+
]
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { HtmlSandbox } from "../../specs/HtmlSandbox.nitro";
|
|
2
|
+
import type { IJSDOMOptions } from "./types";
|
|
3
|
+
import { NitroModules } from "react-native-nitro-modules";
|
|
4
|
+
|
|
5
|
+
function safeParseHeaders(headersJson: string): Record<string, string> {
|
|
6
|
+
try {
|
|
7
|
+
const parsed = JSON.parse(headersJson);
|
|
8
|
+
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
9
|
+
} catch {
|
|
10
|
+
return {};
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class JSDOM {
|
|
15
|
+
private sandbox: HtmlSandbox | null;
|
|
16
|
+
private _disposed: boolean = false;
|
|
17
|
+
|
|
18
|
+
private constructor(sandbox: HtmlSandbox) {
|
|
19
|
+
this.sandbox = sandbox;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new sandboxed DOM environment.
|
|
24
|
+
*
|
|
25
|
+
* @param html - The initial HTML document string.
|
|
26
|
+
* @param options.runScripts - Execute `<script>` tags found in the HTML. Default: `true`.
|
|
27
|
+
* @param options.url - Value of `window.location.href` inside the sandbox. Default: `'about:blank'`.
|
|
28
|
+
* @param options.pretendToBeVisual - Sets `document.hidden = false`, allowing scripts that
|
|
29
|
+
* gate on page visibility (e.g. `requestAnimationFrame`) to run. Default: `false`.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const dom = JSDOM.create('<html><body><div id="result">0</div></body></html>')
|
|
34
|
+
* await dom.evaluate(`document.getElementById('result').textContent = String(2 + 2)`)
|
|
35
|
+
* const value = await dom.evaluate(`document.getElementById('result').textContent`)
|
|
36
|
+
* // value → "4"
|
|
37
|
+
* dom.dispose()
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
static create(html: string, options?: IJSDOMOptions): JSDOM {
|
|
41
|
+
const sandbox = NitroModules.createHybridObject<HtmlSandbox>('HtmlSandbox');
|
|
42
|
+
|
|
43
|
+
sandbox.initialize(
|
|
44
|
+
html,
|
|
45
|
+
options?.runScripts ?? true,
|
|
46
|
+
options?.url ?? 'about:blank',
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
if (options?.onConsole) {
|
|
50
|
+
const cb = options.onConsole;
|
|
51
|
+
sandbox.setConsoleCallback((level, args) => cb(level as any, args));
|
|
52
|
+
} else {
|
|
53
|
+
sandbox.setConsoleCallback(null);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (options?.onAlert || options?.onConfirm || options?.onPrompt) {
|
|
57
|
+
sandbox.setDialogCallbacks(
|
|
58
|
+
options.onAlert ?? null,
|
|
59
|
+
options.onConfirm ?? null,
|
|
60
|
+
options.onPrompt ?? null,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (options?.onFetch) {
|
|
65
|
+
const onFetch = options.onFetch;
|
|
66
|
+
sandbox.setFetchCallback(async (url, method, headersJson, body) => {
|
|
67
|
+
const headers = safeParseHeaders(headersJson);
|
|
68
|
+
return onFetch(url, { method, headers, body })
|
|
69
|
+
.then((res) => JSON.stringify({
|
|
70
|
+
ok: (res.status ?? 200) >= 200 && (res.status ?? 200) < 300,
|
|
71
|
+
status: res.status ?? 200,
|
|
72
|
+
statusText: res.statusText ?? '',
|
|
73
|
+
headersJson: JSON.stringify(res.headers ?? {}),
|
|
74
|
+
body: res.body ?? '',
|
|
75
|
+
}))
|
|
76
|
+
.catch((err) => JSON.stringify({ error: err instanceof Error ? err.message : String(err) }));
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return new JSDOM(sandbox);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Runs arbitrary JavaScript inside the isolated QuickJS sandbox.
|
|
85
|
+
* The return value is coerced to a string (same behaviour as `String(expr)` in JS).
|
|
86
|
+
*
|
|
87
|
+
* This is the **only** door into the sandbox — all DOM queries and mutations must
|
|
88
|
+
* be expressed as JS strings passed to `evaluate()`.
|
|
89
|
+
*
|
|
90
|
+
* @param script - Any valid JS expression or block of statements.
|
|
91
|
+
* @returns The stringified result of the last evaluated expression.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* // Mutate the DOM
|
|
96
|
+
* await dom.evaluate(`document.getElementById('price').textContent = (qty * unitPrice).toFixed(2)`)
|
|
97
|
+
*
|
|
98
|
+
* // Read from the DOM
|
|
99
|
+
* const text = await dom.evaluate(`document.getElementById('price').textContent`)
|
|
100
|
+
* // text → "42.00"
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
evaluate(script: string): Promise<string> {
|
|
104
|
+
if (this._disposed) {
|
|
105
|
+
return Promise.reject(new Error("JSDOM instance has been disposed"));
|
|
106
|
+
}
|
|
107
|
+
return this.sandbox!.evaluate(script);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Serializes the current state of the document back to an HTML string.
|
|
112
|
+
* Reflects any DOM mutations made via `evaluate()`.
|
|
113
|
+
*
|
|
114
|
+
* Returns an empty string if called after `dispose()`.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* await dom.evaluate(`document.title = 'Hello'`)
|
|
119
|
+
* dom.serialize() // "<html><head><title>Hello</title></head>...</html>"
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
serialize(): string {
|
|
123
|
+
if (this._disposed) return "";
|
|
124
|
+
return this.sandbox!.serialize();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Frees all native memory held by this sandbox (Lexbor document + QuickJS runtime).
|
|
129
|
+
* After calling `dispose()`, any further `evaluate()` calls will reject with an error.
|
|
130
|
+
* Calling `dispose()` multiple times is safe (idempotent).
|
|
131
|
+
* Always call this when you are done with the sandbox to avoid memory leaks.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* const dom = JSDOM.create(html)
|
|
136
|
+
* const value = await dom.evaluate(`window.__result`)
|
|
137
|
+
* dom.dispose() // ← always pair with create()
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
dispose(): void {
|
|
141
|
+
if (this._disposed) return;
|
|
142
|
+
this._disposed = true;
|
|
143
|
+
this.sandbox = null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** The request passed to `onFetch` when `fetch(url, init)` is called inside the sandbox. */
|
|
2
|
+
export interface IFetchRequestInit {
|
|
3
|
+
method: string;
|
|
4
|
+
headers: Record<string, string>;
|
|
5
|
+
body?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** The response `onFetch` must resolve with. */
|
|
9
|
+
export interface IFetchResponseInit {
|
|
10
|
+
status: number;
|
|
11
|
+
statusText?: string;
|
|
12
|
+
headers?: Record<string, string>;
|
|
13
|
+
body?: string;
|
|
14
|
+
}
|