From 5d88c5803c20b96515624e129cc3fc864f2a9f11 Mon Sep 17 00:00:00 2001 From: Mohamed Rejeb <41842296+MohamedRejeb@users.noreply.github.com> Date: Fri, 1 Sep 2023 18:08:44 +0100 Subject: [PATCH] Fix IndexOutOfBounds when parsing a wrong Html (#22) * Fix issue with encoding some html entities * Fix issues with decoding emoji * Fix IndexOutOfBounds when parsing a wrong Html --- .../ksoup/html/parser/KsoupHtmlParser.kt | 2 +- .../ksoup/html/KsoupHtmlParserTest.kt | 13 + .../com/mohamedrejeb/ksoup/html/TestData.kt | 749 ++++++++++++++++++ 3 files changed, 763 insertions(+), 1 deletion(-) create mode 100644 ksoup-html/src/commonTest/kotlin/com/mohamedrejeb/ksoup/html/TestData.kt diff --git a/ksoup-html/src/commonMain/kotlin/com/mohamedrejeb/ksoup/html/parser/KsoupHtmlParser.kt b/ksoup-html/src/commonMain/kotlin/com/mohamedrejeb/ksoup/html/parser/KsoupHtmlParser.kt index cb838a1..a80ab35 100644 --- a/ksoup-html/src/commonMain/kotlin/com/mohamedrejeb/ksoup/html/parser/KsoupHtmlParser.kt +++ b/ksoup-html/src/commonMain/kotlin/com/mohamedrejeb/ksoup/html/parser/KsoupHtmlParser.kt @@ -184,7 +184,7 @@ public class KsoupHtmlParser( this.endOpenTag(isOpenImplied) // Self-closing tags will be on the top of the stack - if (this.stack[this.stack.size - 1] == name) { + if (this.stack.getOrNull(this.stack.size - 1) == name) { // If the opening tag isn't implied, the closing tag has to be implied. this.handler.onCloseTag(name, !isOpenImplied) this.stack.removeLast() diff --git a/ksoup-html/src/commonTest/kotlin/com/mohamedrejeb/ksoup/html/KsoupHtmlParserTest.kt b/ksoup-html/src/commonTest/kotlin/com/mohamedrejeb/ksoup/html/KsoupHtmlParserTest.kt index bdc4d08..ed5c345 100644 --- a/ksoup-html/src/commonTest/kotlin/com/mohamedrejeb/ksoup/html/KsoupHtmlParserTest.kt +++ b/ksoup-html/src/commonTest/kotlin/com/mohamedrejeb/ksoup/html/KsoupHtmlParserTest.kt @@ -270,4 +270,17 @@ class KsoupHtmlParserTest { expectedString = "First lineSecond line", ) } + + @Test + fun testBigHtml() { + val handler = KsoupHtmlHandler + .Builder() + .build() + + val ksoupHtmlParser = KsoupHtmlParser( + handler = handler, + ) + ksoupHtmlParser.write(TestData.htmlString) + ksoupHtmlParser.end() + } } \ No newline at end of file diff --git a/ksoup-html/src/commonTest/kotlin/com/mohamedrejeb/ksoup/html/TestData.kt b/ksoup-html/src/commonTest/kotlin/com/mohamedrejeb/ksoup/html/TestData.kt new file mode 100644 index 0000000..c2e2ab9 --- /dev/null +++ b/ksoup-html/src/commonTest/kotlin/com/mohamedrejeb/ksoup/html/TestData.kt @@ -0,0 +1,749 @@ +package com.mohamedrejeb.ksoup.html + +object TestData { + val htmlString = """ +
I’m writing this from a cafe here in Paris, a proper Parisian cafe with the colored chairs sitting two by each other behind small round tables. In front of me the city is buzzing, There’s a swarm of cars, motorcycles, bikes, mentally ill people yelling.
+ + + +It’s a controlled anarchy that I love. The reason I love cities. Then add to this the architecture, culture, cuisine and language of France and you have Paris. Which is without a doubt one of my favorite cities in the world. See my complete digital nomad guide for Paris here.
+ + + +I’ve been taking intensive French classes, which has me at a school for 5 hours a day speaking nothing but French and learning about pronunciation, vocabulary, and grammar (the latter is a bit of a nightmare).
+ + + +I got to Paris nearly a week ago and I’ve been wrapped up in it all. Wrapped up in the energy of the city, the buzz, the eagerness to get my French back to where it was before, and even better.
+ + + +The energy of this city is palpable to me, and I feel alive. I feel alive the same way I did hiking through the German & Austrian Alps (see my Austrian digital nomad guide here) or spending time with women I’ve been in love with (see my travel love stories here). Food tastes better. The buildings and people I’d normally let fall into the background unnoticed, I’m appreciating. I feel alive.
+ + + +It’s easy to get caught up in this high, to lose sight of my emotional, physical & financial well-being and get wrapped up in the sights, sounds and people around me.
I’ve done this a million times. As someone without any strong ties to any one place I can live my entire life chasing novelty.
+ + + +If I get tired of a woman I’m dating, just find a new one. If I get tired of a place I’m at just move to a new spot. If I get tired of the friends I’m around, get new ones.
+ + + +I’ve gone through dozens of cities, friends and women only to realize that if I can’t find stability and peace in my travels then I’ll remain unsatisfied and alone.
+ + +A photo from Thailand with my then girlfriend, click here to read my dating and traveling stories
+ + + +Instead of looking inward at my responsibility for the lack of joy, fulfillment, connection I’ll project the issue externally.
+ + + +It’s X’s fault that I’m not feeling fulfilled from this relationship. It’s the shitty weather here that’s making me depressed. It’s because of my emotionally distant friends that I’m not feeling connected.
+ + + +You can always find an external thing to blame your dissatisfaction on, and if you’re like me then you can keep changing those external things, until you’re exhausted and nowhere left to look but inside.
+ + + +I journal for a multitude of reasons, but the biggest reason is to (try to) stay calm and centered regardless of what’s going on around me.
+ + + +Journalling gives me more insight into how I’m feeling, what I’m dissatisfied with and how to improve my mood for the day.
+ + + +That keeps me from blaming my dissatisfaction on the world around me, it gives me the power to control my emotions.
+ + + +In addition to this, there are some other benefits I get from journalling
+ + + +++ + + ++
+- A daily reminder about what’s important to me (through reviewing my intentions, HW and goals)
+ + + +- Setting my mind in a positive framework (through gratitude)
+ + + +- Checking in on my emotions and bodily sensations
+ + + +- Making sure that I’m taking care of myself, and getting my major needs met
+ + + +- Making sure that I’m working toward my mental / spiritual, physical & financial goals
+ + + +- A space for me to discover why I’m feeling the way I am
+
A video from my last trip to Costa Rica. Where I started getting more serious about my journalling
+ + + +A video of me writing in my journal in a beautiful cabin in Santa Helena, Colombia
+ + + +A snapshot of a couple days in my journal
+ + + +Admittedly, I’m not great at always doing this part and the next. But this can be a great way to kickstart my gratitude for the day, and also build some positive momentum going into the day.
+ + + +I always tell my employees that making the same mistakes over and over again are how businesses fail. It’s not too different for people either, making the same mistakes over and over again is a recipe for disaster.
This process can really expedite identifying recurring issues and shortening their lifetime.
+ + + +A shot from my bike ride through Greece & Turkey, gratitude is a necessity for these rides
+ + + +A couple tips to gratitude
+ + + +A lot of journal prompts will have you outline your 1 – 5 goals for the day, I really liked this idea and I transformed it a bit.
+ + + +Everyday I journal I want to outline what I’m going to do today for my mental / spiritual health, my physical health and finally my financial / career health.
+ + + +This order is very important to me.
+ + + +My Mental health is #1, my physical health comes second, and then my career and financial health. I don’t always get this order right, but it’s a big intention of mine.
+ + + +I don’t care how much money I make, if I’m not in the physical shape I want to be in, I’m not going to be fulfilled.
I can also make a bunch of money and be in great shape, but if I’m not taking care of my mental health and I’m feeling miserable that’s not the life I want to live either.
A shot of me when I was in Vietnam. In great shape, but largely ignoring my mental health. Learn more about my journey to being a digital nomad here.
+ + + +So I start my mornings reinforcing what’s most important to me.
+ + + +++ + + +Looking back now, could I have achieved the financial / career success I have now if I had been more focused on my mental health while I was climbing the career ladder?
+ + + +I don’t know… so maybe my lack of introspection served me for building a career, but at this point I’m not interested in regularly trading happiness today for money tomorrow.
+
Here’s a breakdown of what I try to do everyday
+ + + +A shot of me journalling in Cancun, Mexico. Where I started getting more in touch with my emotions. Interested in digital nomadding in Mexico, check out my guide for Mexico City or Playa Del Carmen.
+ + + +I would recommend everyone who regularly engages in behaviors they want to change to take some time to look through this list of human needs and figure out which needs they’re getting met through those behaviors and how they can find healthier ways to get those needs met.
After struggling with overeating, drinking to feel better and relying on sex and women to feel good I’ve started outlining daily how I can get my most important needs met and it helps keep the urges (more) under control.
+ + + +Do I still slip up? Absolutely, but outlining healthier ways to get these needs met daily has really helped me in mitigating the occurrences and duration of my slip-ups.
+ + + +So everyday I try to outline 1 – 3 healthier ways I can get these 4 needs met.
+ + + +A shot of me in Portugal, not nearly as lean as my photo from Vietnam above, but not bad for someone who’s on the move. Check out my digital nomad guide for Lisbon here.
+ + +This is a huge one, especially for people who travel and want to stay fit. Having a clear plan for my diet and exercise at the beginning of each day is absolutely essential for staying fit wherever I’m at.
+ + + +For me this generally breaksdown into a couple of things
+ + + +A shot of me in Rio De Janeiro, back when I was working office jobs, and far more focused on my career & financial health, over my physical / mental health.
+ + +For this section, ideally I want to find the 1 – 2 things I can do for my businesses that are going to have the biggest impact on my long-term business health. Here’s a podcast about figuring out your ${'$'}10, ${'$'}100 & ${'$'}1,000 tasks (I’m actually working through this week) to determine what those big tasks are.
+ + + +The goal is to constantly be evaluating what I’m doing throughout the day for work. Is this something that’s in my zone of Genius ( and worth ${'$'}1,000 an hour)?
+ + + +If not, then I need to build out a process, and train someone else to do it. If it’s not important enough to train someone else to do it, then I need to really re-consider whether I should be doing it at all.
+ + + +The vision board I created when I started my digital marketing business. I used to look at that photo of the labtop and the mountains in the background on a weekly basis dreaming about where I am today. I’m incredibly lucky, and I know that journalling has helped me immensely in getting here.
+ + +This is where I put my notes in for the day, what am I thinking about? What would I like to get off my chest? Etc.
+ + + +I know people who will set a timer for 10 – 15 minutes to ensure that they get some free form writing out, I don’t do that but do think it’s a good idea for sure.
+ + + +So every 3 months I swap out my journal for a new one. I do this for a couple of reasons.
+ + + +Here’s the list of my quarterly journalling practices
+ + + +The post Daily Journalling, Goal & Intention Setting appeared first on Digital Nomad Lifestyle.
+""" +} \ No newline at end of file